@@ -27,9 +27,9 @@ export const getSwepBanners = asyncHandler(async (req: Request, res: Response) =
2727 if ( search && typeof search === 'string' ) {
2828 conditions . push ( {
2929 $or : [
30- { title : { $regex : search . trim ( ) , $options : 'i' } } ,
31- { shortMessage : { $regex : search . trim ( ) , $options : 'i' } } ,
32- { body : { $regex : search . trim ( ) , $options : 'i' } }
30+ { Title : { $regex : search . trim ( ) , $options : 'i' } } ,
31+ { ShortMessage : { $regex : search . trim ( ) , $options : 'i' } } ,
32+ { Body : { $regex : search . trim ( ) , $options : 'i' } }
3333 ]
3434 } ) ;
3535 }
@@ -38,15 +38,15 @@ export const getSwepBanners = asyncHandler(async (req: Request, res: Response) =
3838 if ( locations && typeof locations === 'string' ) {
3939 const locationArray = locations . split ( ',' ) . map ( loc => loc . trim ( ) ) . filter ( Boolean ) ;
4040 if ( locationArray . length > 0 ) {
41- conditions . push ( { locationSlug : { $in : locationArray } } ) ;
41+ conditions . push ( { LocationSlug : { $in : locationArray } } ) ;
4242 }
4343 } else if ( location && typeof location === 'string' ) {
44- conditions . push ( { locationSlug : location } ) ;
44+ conditions . push ( { LocationSlug : location } ) ;
4545 }
4646
4747 // Apply isActive filter
4848 if ( isActive !== undefined && isActive !== 'undefined' ) {
49- conditions . push ( { isActive : isActive === 'true' } ) ;
49+ conditions . push ( { IsActive : isActive === 'true' } ) ;
5050 }
5151
5252 // Combine all conditions with AND logic
@@ -82,7 +82,7 @@ export const getSwepBanners = asyncHandler(async (req: Request, res: Response) =
8282// @route GET /api/swep-banners/:location
8383// @access Private
8484export const getSwepBannerByLocation = asyncHandler ( async ( req : Request , res : Response ) => {
85- const swepBanner = await SwepBanner . findOne ( { locationSlug : req . params . location } ) ;
85+ const swepBanner = await SwepBanner . findOne ( { LocationSlug : req . params . location } ) ;
8686
8787 if ( ! swepBanner ) {
8888 return sendNotFound ( res , 'SWEP banner not found for this location' ) ;
@@ -98,18 +98,18 @@ export const updateSwepBanner = asyncHandler(async (req: Request, res: Response)
9898 const { location } = req . params ;
9999
100100 // Get existing SWEP banner
101- const existingSwep = await SwepBanner . findOne ( { locationSlug : location } ) . lean ( ) ;
101+ const existingSwep = await SwepBanner . findOne ( { LocationSlug : location } ) . lean ( ) ;
102102 if ( ! existingSwep ) {
103103 return sendNotFound ( res , 'SWEP banner not found for this location' ) ;
104104 }
105105
106106 // Process media fields (existing assets + new uploads) using Banner approach
107107 const processedData = processSwepMediaFields ( req ) ;
108108
109- // Preserve existing date fields and isActive (not editable in edit form)
110- processedData . swepActiveFrom = existingSwep . swepActiveFrom ;
111- processedData . swepActiveUntil = existingSwep . swepActiveUntil ;
112- processedData . isActive = existingSwep . isActive ;
109+ // Preserve existing date fields and IsActive (not editable in edit form)
110+ processedData . SwepActiveFrom = existingSwep . SwepActiveFrom ;
111+ processedData . SwepActiveUntil = existingSwep . SwepActiveUntil ;
112+ processedData . IsActive = existingSwep . IsActive ;
113113
114114 // Validate the processed data
115115 const validation = validateSwepBanner ( processedData ) ;
@@ -122,21 +122,26 @@ export const updateSwepBanner = asyncHandler(async (req: Request, res: Response)
122122 }
123123
124124 // Store old banner data for file cleanup
125- const oldImage = existingSwep . image ;
125+ const oldImage = existingSwep . Image ;
126126
127127 // Update SWEP banner
128128 const updatedSwep = await SwepBanner . findOneAndUpdate (
129- { locationSlug : location } ,
129+ { LocationSlug : location } ,
130130 {
131131 ...validation . data ,
132+ Image : validation . data ?. Image || null ,
132133 DocumentModifiedDate : new Date ( )
133134 } ,
134135 { new : true , runValidators : true }
135136 ) ;
136137
137- // Clean up old image if it was replaced
138- if ( updatedSwep && oldImage && oldImage !== updatedSwep . image ) {
139- await cleanupSwepUnusedFiles ( oldImage ) ;
138+ // Clean up old image if it was replaced or removed (empty Image property indicates removal)
139+ if ( oldImage ) {
140+ if ( ! updatedSwep ?. Image || updatedSwep . Image === null || oldImage !== updatedSwep . Image ) {
141+ // Image was removed (empty) or replaced with a new one - delete old image
142+ await cleanupSwepUnusedFiles ( oldImage ) ;
143+ console . log ( `Cleaned up old SWEP image: ${ oldImage } ` ) ;
144+ }
140145 }
141146
142147 return sendSuccess ( res , updatedSwep ) ;
@@ -147,23 +152,23 @@ export const updateSwepBanner = asyncHandler(async (req: Request, res: Response)
147152// @access Private
148153export const toggleSwepBannerActive = asyncHandler ( async ( req : Request , res : Response ) => {
149154 const { location } = req . params ;
150- const { isActive , swepActiveFrom , swepActiveUntil } = req . body ;
155+ const { IsActive , SwepActiveFrom , SwepActiveUntil } = req . body ;
151156
152157 // Get existing SWEP banner
153- const existingSwep = await SwepBanner . findOne ( { locationSlug : location } ) ;
158+ const existingSwep = await SwepBanner . findOne ( { LocationSlug : location } ) ;
154159 if ( ! existingSwep ) {
155160 return sendNotFound ( res , 'SWEP banner not found for this location' ) ;
156161 }
157162
158163 // Prepare update data
159- let shouldActivateNow = isActive !== undefined ? isActive : ! existingSwep . isActive ;
164+ let shouldActivateNow = IsActive !== undefined ? IsActive : ! existingSwep . IsActive ;
160165
161166 // Check if scheduled start date equals today - if so, activate immediately
162- if ( swepActiveFrom !== undefined && swepActiveFrom !== null ) {
167+ if ( SwepActiveFrom !== undefined && SwepActiveFrom !== null ) {
163168 const today = new Date ( ) ;
164169 today . setHours ( 0 , 0 , 0 , 0 ) ;
165170
166- const activeFromDate = new Date ( swepActiveFrom ) ;
171+ const activeFromDate = new Date ( SwepActiveFrom ) ;
167172 activeFromDate . setHours ( 0 , 0 , 0 , 0 ) ;
168173
169174 // If start date is today, activate immediately
@@ -173,28 +178,28 @@ export const toggleSwepBannerActive = asyncHandler(async (req: Request, res: Res
173178 }
174179
175180 const updateData : any = {
176- isActive : shouldActivateNow ,
181+ IsActive : shouldActivateNow ,
177182 DocumentModifiedDate : new Date ( )
178183 } ;
179184
180185 // Handle date range for scheduled activation
181- if ( swepActiveFrom !== undefined && swepActiveFrom !== null ) {
182- updateData . swepActiveFrom = new Date ( swepActiveFrom ) ;
183- } else if ( updateData . isActive && ! existingSwep . swepActiveFrom ) {
184- // If activating immediately without dates, set swepActiveFrom to now
185- updateData . swepActiveFrom = new Date ( ) ;
186+ if ( SwepActiveFrom !== undefined && SwepActiveFrom !== null ) {
187+ updateData . SwepActiveFrom = new Date ( SwepActiveFrom ) ;
188+ } else if ( updateData . IsActive && ! existingSwep . SwepActiveFrom ) {
189+ // If activating immediately without dates, set SwepActiveFrom to now
190+ updateData . SwepActiveFrom = new Date ( ) ;
186191 }
187192
188- if ( swepActiveUntil !== undefined && swepActiveUntil !== null ) {
189- updateData . swepActiveUntil = new Date ( swepActiveUntil ) ;
190- } else if ( ! updateData . isActive && ! swepActiveUntil ) {
191- // If deactivating without explicit date, set swepActiveUntil to now
192- updateData . swepActiveUntil = new Date ( ) ;
193+ if ( SwepActiveUntil !== undefined && SwepActiveUntil !== null ) {
194+ updateData . SwepActiveUntil = new Date ( SwepActiveUntil ) ;
195+ } else if ( ! updateData . IsActive && ! SwepActiveUntil ) {
196+ // If deactivating without explicit date, set SwepActiveUntil to now
197+ updateData . SwepActiveUntil = new Date ( ) ;
193198 }
194199
195200 // Update SWEP banner
196201 const updatedSwep = await SwepBanner . findOneAndUpdate (
197- { locationSlug : location } ,
202+ { LocationSlug : location } ,
198203 updateData ,
199204 { new : true , runValidators : true }
200205 ) ;
@@ -215,16 +220,20 @@ function processSwepMediaFields(req: Request): any {
215220 const existingData = processedData . existing_image
216221 ? JSON . parse ( processedData . existing_image )
217222 : null ;
223+ const explicitImageValue = processedData . Image ; // Check for explicit Image field
218224
219225 if ( newFileData ) {
220226 // New file uploaded - uploadMiddleware attaches asset with Url property
221- processedData . image = newFileData . Url || newFileData . url ;
227+ processedData . Image = newFileData . Url || newFileData . url ;
222228 } else if ( existingData ) {
223229 // No new file, preserve existing image URL
224- processedData . image = existingData . url || existingData . Url ;
230+ processedData . Image = existingData . url || existingData . Url ;
231+ } else if ( explicitImageValue === '' ) {
232+ // User explicitly removed the image by sending empty string
233+ processedData . Image = '' ;
225234 } else {
226- // Image removed by user
227- processedData . image = undefined ;
235+ // Image removed by user (no explicit value, no new file, no existing data)
236+ processedData . Image = '' ;
228237 }
229238
230239 // Clean up temporary form data keys
@@ -236,12 +245,12 @@ function processSwepMediaFields(req: Request): any {
236245
237246// Helper function to clean up uploaded files when validation fails
238247async function cleanupSwepUploadedFiles ( processedData : any ) : Promise < void > {
239- if ( processedData . image ) {
248+ if ( processedData . Image && processedData . Image !== '' ) {
240249 try {
241- await deleteFile ( processedData . image ) ;
242- console . log ( `Cleaned up uploaded SWEP image after validation failure: ${ processedData . image } ` ) ;
250+ await deleteFile ( processedData . Image ) ;
251+ console . log ( `Cleaned up uploaded SWEP image after validation failure: ${ processedData . Image } ` ) ;
243252 } catch ( error ) {
244- console . error ( `Failed to delete uploaded SWEP image ${ processedData . image } :` , error ) ;
253+ console . error ( `Failed to delete uploaded SWEP image ${ processedData . Image } :` , error ) ;
245254 // Don't throw - file cleanup failure shouldn't break the response
246255 }
247256 }
0 commit comments