@@ -908,15 +908,18 @@ router.post('/create_ad',
908908 AccessControl ( 'ads' , 'create' ) ,
909909 ( req , res , next ) => {
910910 Ads . find ( { status :true } ) . then ( ads => {
911- let check_position = ads . filter ( ad => ad . position === req . body . position && ad . sport_type === req . body . sport_type && ad . page === req . body . page )
911+ let check_start_date = moment ( req . body . start_date ) . format ( "YYYY-MM-DD" )
912+ let check_end_date = moment ( req . body . end_date ) . format ( "YYYY-MM-DD" )
913+ let check_position = ads . filter ( ad => ad . position === req . body . position && ad . sport_type === req . body . sport_type && ad . page === req . body . page && ( moment ( check_start_date ) . isBetween ( moment ( ad . start_date ) . format ( "YYYY-MM-DD" ) , moment ( ad . end_date ) . format ( "YYYY-MM-DD" ) , null , [ ] ) || moment ( check_end_date ) . isBetween ( moment ( ad . start_date ) . format ( "YYYY-MM-DD" ) , moment ( ad . end_date ) . format ( "YYYY-MM-DD" ) , null , [ ] ) ) )
912914 if ( check_position . length > 0 ) {
913915 existing_positions = [ ]
914916 check_position . map ( ad => {
915- let x = 'position: ' + ad . position . toString ( ) + "already exists in page: " + ad . page + ' in sport: '+ ad . sport_type
917+ let x = 'position: ' + ad . position . toString ( ) + " already exists in " + ad . page + ' in sport '+ ad . sport_type
916918 existing_positions . push ( x )
917919 } )
918920 res . send ( { status :"failed" , message : existing_positions [ 0 ] , existing_positions} )
919- } else {
921+ }
922+ else {
920923 Ads . create ( req . body ) . then ( ads => {
921924 Ads . findById ( { _id :ads . id } ) . lean ( ) . populate ( 'event' , '_id event type' ) . populate ( 'venue' , '_id name venue type' ) . then ( ads => {
922925 res . send ( { status :"success" , message :"ad created" , data :ads } )
@@ -934,13 +937,27 @@ router.post('/edit_ad/:id',
934937 ( req , res , next ) => {
935938 req . body . modified_at = new Date ( )
936939 req . body . modified_by = req . username
940+ Ads . find ( { status :true } ) . then ( ads => {
941+ let check_start_date = moment ( req . body . start_date ) . format ( "YYYY-MM-DD" )
942+ let check_end_date = moment ( req . body . end_date ) . format ( "YYYY-MM-DD" )
943+ let check_position = ads . filter ( ad => ad . position === req . body . position && ad . sport_type === req . body . sport_type && ad . page === req . body . page && ad . _id === req . params . id && ( moment ( check_start_date ) . isBetween ( moment ( ad . start_date ) . format ( "YYYY-MM-DD" ) , moment ( ad . end_date ) . format ( "YYYY-MM-DD" ) , null , [ ] ) || moment ( check_end_date ) . isBetween ( moment ( ad . start_date ) . format ( "YYYY-MM-DD" ) , moment ( ad . end_date ) . format ( "YYYY-MM-DD" ) , null , [ ] ) ) )
944+ if ( check_position . length > 0 ) {
945+ existing_positions = [ ]
946+ check_position . map ( ad => {
947+ let x = 'position: ' + ad . position . toString ( ) + " already exists in " + ad . page + ' in sport ' + ad . sport_type
948+ existing_positions . push ( x )
949+ } )
950+ res . send ( { status :"failed" , message : existing_positions [ 0 ] } )
951+ }
952+ else {
937953 Ads . findByIdAndUpdate ( { _id :req . params . id } , req . body ) . then ( ads => {
938954 Ads . findById ( { _id :req . params . id } ) . lean ( ) . populate ( 'event' , '_id event type' ) . populate ( 'venue' , '_id name venue type' ) . then ( ads => {
939- console . log ( ' edit ads' , ads ) ;
940955 res . send ( { status :"success" , message :"ad modified" , data :ads } )
941956 ActivityLog ( req . userId , req . role , 'ad modified' , req . name + " modified ad " )
942957 } ) . catch ( next )
943958 } ) . catch ( next )
959+ }
960+ } ) . catch ( next )
944961} )
945962
946963//// Delete ad
0 commit comments