@@ -32,10 +32,11 @@ export default class ProspectiveSponsorServices {
3232 submitter : User ,
3333 organization : Organization ,
3434 organizationName : string ,
35- lastContactDate : Date ,
36- firstContactMethod : FirstContactMethod ,
37- contactName : string ,
38- contactorUserId : string ,
35+ status : ProspectiveSponsorStatus ,
36+ lastContactDate ?: Date ,
37+ firstContactMethod ?: FirstContactMethod ,
38+ contactName ?: string ,
39+ contactorUserId ?: string ,
3940 highlightThresholdDays ?: number ,
4041 contactEmail ?: string ,
4142 contactPhone ?: string ,
@@ -47,8 +48,16 @@ export default class ProspectiveSponsorServices {
4748 throw new AccessDeniedException ( 'Only finance team members or heads can create prospective sponsors' ) ;
4849 }
4950
50- if ( ! contactEmail && ! contactPhone ) {
51- throw new HttpException ( 400 , 'At least one of contact email or contact phone is required' ) ;
51+ const isNotInContact = status === ProspectiveSponsorStatus . NOT_IN_CONTACT ;
52+
53+ if ( ! isNotInContact ) {
54+ if ( ! lastContactDate ) throw new HttpException ( 400 , 'Last contact date is required' ) ;
55+ if ( ! firstContactMethod ) throw new HttpException ( 400 , 'First contact method is required' ) ;
56+ if ( ! contactName ) throw new HttpException ( 400 , 'Contact name is required' ) ;
57+ if ( ! contactorUserId ) throw new HttpException ( 400 , 'Contactor is required' ) ;
58+ if ( ! contactEmail && ! contactPhone ) {
59+ throw new HttpException ( 400 , 'At least one of contact email or contact phone is required' ) ;
60+ }
5261 }
5362
5463 const existingProspectiveSponsor = await prisma . prospective_Sponsor . findFirst ( {
@@ -63,26 +72,27 @@ export default class ProspectiveSponsorServices {
6372 throw new HttpException ( 400 , `A prospective sponsor with the name "${ organizationName } " already exists.` ) ;
6473 }
6574
66- const contactor = await prisma . user . findUnique ( {
67- where : { userId : contactorUserId }
68- } ) ;
69-
70- if ( ! contactor ) {
71- throw new NotFoundException ( 'User' , contactorUserId ) ;
75+ if ( contactorUserId ) {
76+ const contactor = await prisma . user . findUnique ( { where : { userId : contactorUserId } } ) ;
77+ if ( ! contactor ) throw new NotFoundException ( 'User' , contactorUserId ) ;
7278 }
7379
74- const contact = await prisma . sponsor_Contact . create ( {
75- data : { name : contactName , email : contactEmail , phone : contactPhone , position : contactPosition }
76- } ) ;
80+ const contact =
81+ ! isNotInContact && contactName
82+ ? await prisma . sponsor_Contact . create ( {
83+ data : { name : contactName , email : contactEmail , phone : contactPhone , position : contactPosition }
84+ } )
85+ : null ;
7786
7887 const prospectiveSponsor = await prisma . prospective_Sponsor . create ( {
7988 data : {
8089 organizationName,
81- lastContactDate,
90+ lastContactDate : lastContactDate ?? null ,
8291 highlightThresholdDays : highlightThresholdDays ?? 10 ,
83- firstContactMethod,
84- contactorUserId,
85- contactId : contact . sponsorContactId ,
92+ status,
93+ firstContactMethod : firstContactMethod ?? null ,
94+ contactorUserId : contactorUserId ?? null ,
95+ contactId : contact ?. sponsorContactId ?? null ,
8696 notes,
8797 organizationId : organization . organizationId ,
8898 tasks : tasks ?. length
@@ -138,11 +148,11 @@ export default class ProspectiveSponsorServices {
138148 organization : Organization ,
139149 prospectiveSponsorId : string ,
140150 organizationName : string ,
141- lastContactDate : Date ,
142151 status : ProspectiveSponsorStatus ,
143- firstContactMethod : FirstContactMethod ,
144- contactName : string ,
145- contactorUserId : string ,
152+ lastContactDate ?: Date ,
153+ firstContactMethod ?: FirstContactMethod ,
154+ contactName ?: string ,
155+ contactorUserId ?: string ,
146156 highlightThresholdDays ?: number ,
147157 contactEmail ?: string ,
148158 contactPhone ?: string ,
@@ -154,8 +164,16 @@ export default class ProspectiveSponsorServices {
154164 throw new AccessDeniedException ( 'Only finance team members or heads can edit prospective sponsors' ) ;
155165 }
156166
157- if ( ! contactEmail && ! contactPhone ) {
158- throw new HttpException ( 400 , 'At least one of contact email or contact phone is required' ) ;
167+ const isNotInContact = status === ProspectiveSponsorStatus . NOT_IN_CONTACT ;
168+
169+ if ( ! isNotInContact ) {
170+ if ( ! lastContactDate ) throw new HttpException ( 400 , 'Last contact date is required' ) ;
171+ if ( ! firstContactMethod ) throw new HttpException ( 400 , 'First contact method is required' ) ;
172+ if ( ! contactName ) throw new HttpException ( 400 , 'Contact name is required' ) ;
173+ if ( ! contactorUserId ) throw new HttpException ( 400 , 'Contactor is required' ) ;
174+ if ( ! contactEmail && ! contactPhone ) {
175+ throw new HttpException ( 400 , 'At least one of contact email or contact phone is required' ) ;
176+ }
159177 }
160178
161179 const oldProspectiveSponsor = await prisma . prospective_Sponsor . findUnique ( {
@@ -180,12 +198,9 @@ export default class ProspectiveSponsorServices {
180198 }
181199 }
182200
183- const contactor = await prisma . user . findUnique ( {
184- where : { userId : contactorUserId }
185- } ) ;
186-
187- if ( ! contactor ) {
188- throw new NotFoundException ( 'User' , contactorUserId ) ;
201+ if ( contactorUserId ) {
202+ const contactor = await prisma . user . findUnique ( { where : { userId : contactorUserId } } ) ;
203+ if ( ! contactor ) throw new NotFoundException ( 'User' , contactorUserId ) ;
189204 }
190205
191206 // Upsert tasks if provided
@@ -231,20 +246,38 @@ export default class ProspectiveSponsorServices {
231246 ) ;
232247 }
233248
234- await prisma . sponsor_Contact . update ( {
235- where : { sponsorContactId : oldProspectiveSponsor . contactId } ,
236- data : { name : contactName , email : contactEmail , phone : contactPhone , position : contactPosition }
237- } ) ;
249+ // Handle contact upsert based on status
250+ const { contactId : oldContactId } = oldProspectiveSponsor ;
251+ let contactId = oldContactId ;
252+ if ( ! isNotInContact && contactName ) {
253+ if ( oldProspectiveSponsor . contactId ) {
254+ // Update existing contact
255+ await prisma . sponsor_Contact . update ( {
256+ where : { sponsorContactId : oldProspectiveSponsor . contactId } ,
257+ data : { name : contactName , email : contactEmail , phone : contactPhone , position : contactPosition }
258+ } ) ;
259+ } else {
260+ // Create new contact (transitioning from NOT_IN_CONTACT)
261+ const contact = await prisma . sponsor_Contact . create ( {
262+ data : { name : contactName , email : contactEmail , phone : contactPhone , position : contactPosition }
263+ } ) ;
264+ contactId = contact . sponsorContactId ;
265+ }
266+ } else if ( isNotInContact && oldProspectiveSponsor . contactId ) {
267+ // Clear contact when moving to NOT_IN_CONTACT
268+ contactId = null ;
269+ }
238270
239271 const updatedProspectiveSponsor = await prisma . prospective_Sponsor . update ( {
240272 where : { prospectiveSponsorId } ,
241273 data : {
242274 organizationName,
243- lastContactDate,
275+ lastContactDate : isNotInContact ? null : lastContactDate ,
244276 highlightThresholdDays : highlightThresholdDays ?? 10 ,
245277 status,
246- firstContactMethod,
247- contactorUserId,
278+ firstContactMethod : isNotInContact ? null : firstContactMethod ,
279+ contactorUserId : isNotInContact ? null : contactorUserId ,
280+ contactId,
248281 notes
249282 } ,
250283 ...getProspectiveSponsorQueryArgs ( organization . organizationId )
@@ -413,10 +446,10 @@ export default class ProspectiveSponsorServices {
413446 // Create a new contact for the sponsor, copied from the prospective sponsor's contact
414447 const sponsorContact = await prisma . sponsor_Contact . create ( {
415448 data : {
416- name : prospectiveSponsor . contact . name ,
417- email : prospectiveSponsor . contact . email ,
418- phone : prospectiveSponsor . contact . phone ,
419- position : prospectiveSponsor . contact . position
449+ name : prospectiveSponsor . contact ? .name ?? '' ,
450+ email : prospectiveSponsor . contact ? .email ,
451+ phone : prospectiveSponsor . contact ? .phone ,
452+ position : prospectiveSponsor . contact ? .position
420453 }
421454 } ) ;
422455
0 commit comments