|
1 | 1 | import { NIL, v4 as uuidv4 } from 'uuid'; |
2 | 2 |
|
3 | 3 | import { generateCreateStamps, generateUpdateStamps } from '../db/utils/utils'; |
4 | | -import { activityService, contactService, enquiryService, noteService, userService } from '../services'; |
| 4 | +import { |
| 5 | + activityService, |
| 6 | + activityContactService, |
| 7 | + contactService, |
| 8 | + enquiryService, |
| 9 | + noteService, |
| 10 | + userService |
| 11 | +} from '../services'; |
5 | 12 | import { Initiative } from '../utils/enums/application'; |
6 | 13 | import { ApplicationStatus, IntakeStatus, NoteType, SubmissionType } from '../utils/enums/projectCommon'; |
7 | | -import { getCurrentSubject, getCurrentUsername, isTruthy } from '../utils/utils'; |
| 14 | +import { getCurrentSubject, getCurrentUsername, partition, isTruthy } from '../utils/utils'; |
8 | 15 |
|
9 | 16 | import type { NextFunction, Request, Response } from 'express'; |
10 | | -import type { Enquiry, EnquiryIntake, EnquirySearchParameters } from '../types'; |
| 17 | +import type { Contact, Enquiry, EnquiryIntake, EnquirySearchParameters } from '../types'; |
11 | 18 |
|
12 | 19 | const controller = { |
13 | 20 | /** |
@@ -177,14 +184,31 @@ const controller = { |
177 | 184 |
|
178 | 185 | updateEnquiry: async (req: Request<never, never, Enquiry>, res: Response, next: NextFunction) => { |
179 | 186 | try { |
180 | | - // Assign contactId if not present |
181 | 187 | if (req.body.contacts) { |
182 | | - req.body.contacts = req.body.contacts.map((x) => ({ |
183 | | - ...x, |
184 | | - contactId: x.contactId ?? uuidv4() |
185 | | - })); |
| 188 | + // Predicate function to check if a contact has a contactId. |
| 189 | + // Used to partition contacts into existing (with contactId) and new (without contactId). |
| 190 | + const hasContactId = (x: Contact) => !!x.contactId; |
| 191 | + |
| 192 | + // Partition contacts into exisiting and new based on whether they have a contactId |
| 193 | + const [existingContacts, newContacts] = partition(req.body.contacts, hasContactId); |
| 194 | + |
| 195 | + // Assign a new contactId to each new contact |
| 196 | + newContacts.forEach((x) => { |
| 197 | + x.contactId = uuidv4(); |
| 198 | + }); |
| 199 | + |
| 200 | + // Combine existing contacts with new contacts |
| 201 | + const contacts = existingContacts.concat(newContacts); |
| 202 | + |
| 203 | + // Insert new contacts into the contact table |
| 204 | + await contactService.insertContacts(newContacts, req.currentContext); |
| 205 | + |
| 206 | + // Delete any activity_contact records that doesn't match the activity and contacts in the request |
| 207 | + await activityContactService.deleteUnmatchedActivityContacts(req.body.activityId, contacts); |
| 208 | + |
| 209 | + // Create or update activity_contact with the data from the request |
| 210 | + await activityContactService.upsertActivityContacts(req.body.activityId, contacts); |
186 | 211 | } |
187 | | - await contactService.upsertContacts(req.body.contacts, req.currentContext, req.body.activityId); |
188 | 212 |
|
189 | 213 | const result = await enquiryService.updateEnquiry({ |
190 | 214 | ...req.body, |
|
0 commit comments