@@ -3,6 +3,7 @@ import { asyncHandler } from '../utils/asyncHandler.js';
33import { sendSuccess , sendBadRequest , sendNotFound , sendPaginatedSuccess } from '../utils/apiResponses.js' ;
44import { validateSwepBanner } from '../schemas/swepBannerSchema.js' ;
55import SwepBanner from '../models/swepModel.js' ;
6+ import Cities from '../models/cityModel.js' ;
67import { deleteFile } from '../middleware/uploadMiddleware.js' ;
78
89// @desc Get all SWEP banners with optional filtering
@@ -23,6 +24,11 @@ export const getSwepBanners = asyncHandler(async (req: Request, res: Response) =
2324 const query : any = { } ;
2425 const conditions : any [ ] = [ ] ;
2526
27+ // Get public city keys to filter SWEP banners
28+ const publicCities = await Cities . find ( { IsPublic : true } ) . select ( 'Key' ) . lean ( ) ;
29+ const publicCityKeys = publicCities . map ( city => city . Key ) ;
30+ conditions . push ( { LocationSlug : { $in : publicCityKeys } } ) ;
31+
2632 // Apply search filter
2733 if ( search && typeof search === 'string' ) {
2834 conditions . push ( {
@@ -38,7 +44,9 @@ export const getSwepBanners = asyncHandler(async (req: Request, res: Response) =
3844 if ( locations && typeof locations === 'string' ) {
3945 const locationArray = locations . split ( ',' ) . map ( loc => loc . trim ( ) ) . filter ( Boolean ) ;
4046 if ( locationArray . length > 0 ) {
41- conditions . push ( { LocationSlug : { $in : locationArray } } ) ;
47+ // Intersect with public cities
48+ const filteredLocations = locationArray . filter ( loc => publicCityKeys . includes ( loc ) ) ;
49+ conditions . push ( { LocationSlug : { $in : filteredLocations } } ) ;
4250 }
4351 } else if ( location && typeof location === 'string' ) {
4452 conditions . push ( { LocationSlug : location } ) ;
0 commit comments