1- import { PermitStage , PermitState } from '../db/codes/enums.ts' ;
1+ import { PermitStage , PermitState , PiesOnHold } from '../db/codes/enums.ts' ;
22import { transactionWrapper } from '../db/utils/transactionWrapper.ts' ;
33import { generateUpdateStamps } from '../db/utils/utils.ts' ;
44import { parsePeachRecords , summarizePeachRecord } from '../parsers/peach.ts' ;
@@ -10,7 +10,13 @@ import { PeachIntegratedSystem } from '../utils/enums/permit.ts';
1010
1111import type { Request , Response } from 'express' ;
1212import type { PrismaTransactionClient } from '../db/dataConnection.ts' ;
13- import type { PeachSummary , Permit , Record as PeachRecord , PermitTracking } from '../types/index.ts' ;
13+ import type {
14+ PeachSummary ,
15+ Permit ,
16+ Record as PeachRecord ,
17+ PermitTracking ,
18+ UpdatedPermitWithNote
19+ } from '../types/index.ts' ;
1420
1521const log = getLogger ( module . filename ) ;
1622
@@ -78,7 +84,7 @@ export const getPeachSummaryController = async (req: Request<never, never, Permi
7884 * Syncs PEACH data for permit tracking to PCNS, returns a list of permits used for sending update notifications
7985 * @returns Array of updated permits
8086 */
81- export const syncPeachRecords = async ( ) : Promise < Permit [ ] > => {
87+ export const syncPeachRecords = async ( ) : Promise < UpdatedPermitWithNote [ ] > => {
8288 const systemRecordPermits : { recordId : string ; systemId : string ; permit : Permit } [ ] = [ ] ;
8389 await transactionWrapper < void > ( async ( tx : PrismaTransactionClient ) => {
8490 // Only fetch permits that have a peach integrated system permit type
@@ -110,7 +116,7 @@ export const syncPeachRecords = async (): Promise<Permit[]> => {
110116 }
111117 } ) ;
112118
113- // TODO : May need rate limiting in the future
119+ // Note : May need rate limiting in the future
114120 const results = await Promise . allSettled (
115121 systemRecordPermits . map ( ( srp ) => getPeachRecord ( srp . recordId , srp . systemId ) )
116122 ) ;
@@ -137,7 +143,7 @@ export const syncPeachRecords = async (): Promise<Permit[]> => {
137143
138144 const parsedRecords : Record < string , PeachSummary > = parsePeachRecords ( records ) ;
139145
140- const updatedPermits : Permit [ ] = [ ] ;
146+ const updatedPermits : UpdatedPermitWithNote [ ] = [ ] ;
141147
142148 await transactionWrapper < void > ( async ( tx : PrismaTransactionClient ) => {
143149 for ( const systemRecordPermit of systemRecordPermits ) {
@@ -167,14 +173,21 @@ export const syncPeachRecords = async (): Promise<Permit[]> => {
167173 const lastChangedDatesEqual = compareDates ( peachStatusChangedDatetime , pcnsStatusChangedDatetime ) === 0 ;
168174 const stagesEqual = peachSummary . stage === ( pcnsPermit . stage as PermitStage ) ;
169175 const statesEqual = peachSummary . state === ( pcnsPermit . state as PermitState ) ;
176+ const onHoldCodesEqual = peachSummary . onHoldCode === pcnsPermit . onHoldCode ;
170177
178+ const stageOrStateHasDiff = ! stagesEqual || ! statesEqual ;
171179 const hasDiff =
172- ! stagesEqual || ! statesEqual || ! submittedDatesEqual || ! decisionDatesEqual || ! lastChangedDatesEqual ;
180+ stageOrStateHasDiff ||
181+ ! onHoldCodesEqual ||
182+ ! submittedDatesEqual ||
183+ ! decisionDatesEqual ||
184+ ! lastChangedDatesEqual ;
173185
174186 if ( ! hasDiff ) continue ;
175187
176188 pcnsPermit . stage = peachSummary . stage ;
177189 pcnsPermit . state = peachSummary . state ;
190+ pcnsPermit . onHoldCode = peachSummary . onHoldCode ;
178191
179192 pcnsPermit . submittedDate = peachSummary . submittedDate ;
180193 pcnsPermit . submittedTime = peachSummary . submittedTime ;
@@ -198,11 +211,16 @@ export const syncPeachRecords = async (): Promise<Permit[]> => {
198211 pcnsPermit . updatedBy = updatedBy ;
199212
200213 const cleanedPermit = omit ( pcnsPermit , [ 'permitTracking' , 'permitType' ] ) ;
201-
202214 const updatedPermit = await upsertPermit ( tx , cleanedPermit ) ;
215+ const applicantRequestedHold = ! onHoldCodesEqual && peachSummary . onHoldCode === PiesOnHold . APPLICANT_REQUEST ;
216+ let note : string | undefined = undefined ;
217+
218+ if ( applicantRequestedHold ) {
219+ note = 'This application has been placed on hold at the applicant’s request' ;
220+ }
203221
204- // Only return permits and notes that have had a status change for notifications
205- if ( ! stagesEqual || ! statesEqual ) updatedPermits . push ( updatedPermit ) ;
222+ // For notifications, only return permits that have had a status change or is now on hold by applicant's request.
223+ if ( stageOrStateHasDiff || applicantRequestedHold ) updatedPermits . push ( { permit : updatedPermit , note } ) ;
206224 }
207225 } ) ;
208226
0 commit comments