1616
1717import { ContactJobType } from '@tech-matters/types' ;
1818import { getClient } from '@tech-matters/twilio-client' ;
19- import { getSsmParameter } from '@tech-matters/ssm-cache' ;
19+ import { getSsmParameter , SsmParameterNotFound } from '@tech-matters/ssm-cache' ;
2020import {
2121 ContactJob ,
2222 deleteContactJob ,
@@ -82,17 +82,27 @@ const deleteTranscript = async (job: RetrieveContactTranscriptJob): Promise<bool
8282 }
8383
8484 await setContactJobCleanupActive ( id ) ;
85+ const client = await getClient ( { accountSid } ) ;
8586 try {
86- const client = await getClient ( { accountSid } ) ;
87- await client . chat . v2
88- . services ( job . resource . serviceSid )
89- . channels . get ( job . resource . channelSid )
90- . remove ( ) ;
87+ await client . conversations . v1 . conversations . get ( job . resource . channelSid ) . remove ( ) ;
9188 } catch ( err ) {
9289 if ( err . status === 404 ) {
93- console . log (
94- `Channel ${ channelSid } not found, assuming it has already been deleted ` ,
90+ console . info (
91+ `Conversation ${ channelSid } not found, checking legacy chat API just in case. ` ,
9592 ) ;
93+ try {
94+ await client . chat . v2
95+ . services ( job . resource . serviceSid )
96+ . channels . get ( job . resource . channelSid )
97+ . remove ( ) ;
98+ } catch ( chatError ) {
99+ if ( err . status === 404 ) {
100+ console . info (
101+ `${ channelSid } not found as a chat channel or conversation, assuming it has already been deleted` ,
102+ ) ;
103+ return true ;
104+ }
105+ }
96106 } else {
97107 console . error (
98108 new ContactJobCleanupError (
@@ -118,9 +128,11 @@ const cleanupContactJob = async (job: ContactJob): Promise<void> => {
118128 if ( ! ( await deleteTranscript ( job ) ) ) return ;
119129 }
120130
121- const { accountSid, id } = job ;
131+ const { accountSid, id, jobType , contactId } = job ;
122132 await deleteContactJob ( accountSid , id ) ;
123- console . log ( `Successfully cleaned up contact job ${ id } ` ) ;
133+ console . info (
134+ `Successfully cleaned up contact job ${ id } (${ jobType } ) for contact ${ contactId } ` ,
135+ ) ;
124136} ;
125137
126138/**
@@ -137,10 +149,20 @@ const getCleanupRetentionDays = async (accountSid): Promise<number | undefined>
137149 `/${ process . env . NODE_ENV } /hrm/${ accountSid } /transcript_retention_days` ,
138150 ) ,
139151 ) || MAX_CLEANUP_JOB_RETENTION_DAYS ;
140- } catch ( err ) {
141- console . error (
142- `Error trying to fetch /${ process . env . NODE_ENV } /hrm/${ accountSid } /transcript_retention_days ${ err } , using default` ,
152+ console . debug (
153+ `SSM parameter for transcript retention days set to ${ ssmRetentionDays } for account ${ accountSid } , so using that` ,
143154 ) ;
155+ } catch ( err ) {
156+ if ( ( err as any ) instanceof SsmParameterNotFound ) {
157+ console . debug (
158+ `SSM parameter for transcript retention days not set for account ${ accountSid } , so using default ${ MAX_CLEANUP_JOB_RETENTION_DAYS } ` ,
159+ ) ;
160+ } else {
161+ console . error (
162+ `Error trying to fetch /${ process . env . NODE_ENV } /hrm/${ accountSid } /transcript_retention_days ${ err } , using default` ,
163+ err ,
164+ ) ;
165+ }
144166 ssmRetentionDays = MAX_CLEANUP_JOB_RETENTION_DAYS ;
145167 }
146168
@@ -159,10 +181,10 @@ export const cleanupContactJobs = async (): Promise<void> => {
159181 try {
160182 const accountSids = await getPendingCleanupJobAccountSids ( ) ;
161183
162- console . log ( `Cleaning up contact jobs for accounts:` , accountSids ) ;
184+ console . info ( `Cleaning up contact jobs for accounts:` , accountSids ) ;
163185
164186 for ( const accountSid of accountSids ) {
165- console . log ( `Cleaning up contact jobs for account:` , accountSid ) ;
187+ console . info ( `Cleaning up contact jobs for account:` , accountSid ) ;
166188 const cleanupRetentionDays = await getCleanupRetentionDays ( accountSid ) ;
167189 const pendingJobs = await getPendingCleanupJobs ( accountSid , cleanupRetentionDays ) ;
168190
@@ -171,7 +193,9 @@ export const cleanupContactJobs = async (): Promise<void> => {
171193 await cleanupContactJob ( job ) ;
172194 } catch ( err ) {
173195 console . error (
174- new ContactJobCleanupError ( `Error processing job ${ job . id } : ${ err } ` ) ,
196+ new ContactJobCleanupError (
197+ `Error processing job ${ job . id } (contact: ${ job . contactId } ): ${ err } ` ,
198+ ) ,
175199 ) ;
176200 }
177201 }
0 commit comments