@@ -4,9 +4,30 @@ import { asyncHandler } from '../utils/asyncHandler.js';
44import { sendSuccess , sendCreated , sendNotFound , sendBadRequest } from '../utils/apiResponses.js' ;
55import GroupedService from '../models/groupedServiceModel.js' ;
66import Service from '../models/serviceModel.js' ;
7+ import ClientGroup from '../models/clientGroupModel.js' ;
78import { processAddressesWithCoordinates , updateLocationIfPostcodeChanged } from '../utils/postcodeValidation.js' ;
89import { validateGroupedService } from '../schemas/groupedServiceSchema.js' ;
9- import { IGroupedService } from '../types/index.js' ;
10+ import { IGroupedService , IClientGroupRef } from '../types/index.js' ;
11+
12+ /**
13+ * Helper function to denormalise client groups from keys
14+ * Looks up client group documents and returns denormalised data
15+ */
16+ async function denormaliseClientGroups ( clientGroupKeys ?: string [ ] ) : Promise < IClientGroupRef [ ] > {
17+ if ( ! clientGroupKeys || clientGroupKeys . length === 0 ) {
18+ return [ ] ;
19+ }
20+
21+ const clientGroups = await ClientGroup . find ( {
22+ Key : { $in : clientGroupKeys }
23+ } ) . lean ( ) ;
24+
25+ return clientGroups . map ( cg => ( {
26+ Key : cg . Key ,
27+ Name : cg . Name ,
28+ SortPosition : cg . SortPosition
29+ } ) ) ;
30+ }
1031
1132// @desc Get all services
1233// @route GET /api/services
@@ -152,8 +173,16 @@ export const createService = asyncHandler(async (req: Request, res: Response) =>
152173 await processAddressesWithCoordinates ( [ serviceData . Location ] ) ;
153174 }
154175
176+ // Denormalise client groups if keys provided
177+ const clientGroups = serviceData . ClientGroupKeys && serviceData . ClientGroupKeys . length > 0
178+ ? await denormaliseClientGroups ( serviceData . ClientGroupKeys )
179+ : [ ] ;
180+
155181 // Create the grouped service
156- const groupedService = await GroupedService . create ( [ serviceData ] , { session } ) ;
182+ const groupedService = await GroupedService . create ( [ {
183+ ...serviceData ,
184+ ClientGroups : clientGroups
185+ } ] , { session } ) ;
157186
158187 // Create individual ProvidedServices for each subcategory
159188 await createIndividualServices ( groupedService [ 0 ] , session ) ;
@@ -207,12 +236,12 @@ export const updateService = asyncHandler(async (req: Request, res: Response) =>
207236 // Check if location postcode has changed and update coordinates accordingly
208237 if ( updateData . Location && updateData . Location . Postcode ) {
209238 const oldLocation = existingService . Location ;
210-
239+
211240 if ( oldLocation && oldLocation . Postcode ) {
212241 // Update location if postcode changed
213242 await updateLocationIfPostcodeChanged (
214- oldLocation . Postcode ,
215- updateData . Location . Postcode ,
243+ oldLocation . Postcode ,
244+ updateData . Location . Postcode ,
216245 updateData . Location
217246 ) ;
218247 } else if ( ! updateData . Location . Location ) {
@@ -221,10 +250,18 @@ export const updateService = asyncHandler(async (req: Request, res: Response) =>
221250 }
222251 }
223252
253+ // Denormalise client groups if keys provided
254+ const clientGroups = updateData . ClientGroupKeys
255+ ? await denormaliseClientGroups ( updateData . ClientGroupKeys )
256+ : [ ] ;
257+
224258 // Update the grouped service
225259 const updatedService = await GroupedService . findByIdAndUpdate (
226- req . params . id ,
227- updateData ,
260+ req . params . id ,
261+ {
262+ ...updateData ,
263+ ClientGroups : clientGroups
264+ } ,
228265 { new : true , session }
229266 ) ;
230267
0 commit comments