generated from Qlever-LLC/oada-service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(idempotent): Zendesk-sync is now idempotent
Signed-off-by: Andrew Balmos <[email protected]>
- Loading branch information
Showing
19 changed files
with
2,363 additions
and
1,662 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Qlever LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type JobSchema from '@oada/types/oada/service/job.js'; | ||
import { type Logger } from '@oada/pino-debug'; | ||
import { type OADAClient } from '@oada/client'; | ||
import { type Ticket } from '../types.js'; | ||
import { config } from '../config.js'; | ||
import { doJob } from '@oada/client/jobs'; | ||
import { setCustomField } from '../zd/zendesk.js'; | ||
|
||
interface LFSyncDocJob extends JobSchema { | ||
service: 'lf-sync'; | ||
type: 'sync-doc'; | ||
config: { | ||
doc: { _id: string }; | ||
tradingPartner: string; | ||
}; | ||
} | ||
|
||
type LFSyncDocJobResult = Record< | ||
string, | ||
{ | ||
EntryId: number; | ||
Path: string; | ||
Name: string; | ||
} | ||
>; | ||
|
||
const PATH_FIELD_ID = config.get('service.archivers.laserfiche.pathFieldId'); | ||
const LF_ID_FIELD_ID = config.get('service.archivers.laserfiche.lfIdFieldId'); | ||
|
||
export async function doLfSync( | ||
log: Logger, | ||
oada: OADAClient, | ||
ticket: Ticket, | ||
doc: { trellisId: string; tradingPartner: string }, | ||
) { | ||
const syncJob: LFSyncDocJob = { | ||
service: 'lf-sync', | ||
type: 'sync-doc', | ||
config: { | ||
doc: { _id: doc.trellisId }, | ||
tradingPartner: doc.tradingPartner, | ||
}, | ||
}; | ||
|
||
// FIXME: doJob really should take a generic for the job type response or something | ||
const { result: entities } = (await doJob(oada, syncJob)) as unknown as { | ||
result: LFSyncDocJobResult; | ||
}; | ||
|
||
// log = log.child({ entities }); | ||
|
||
const lfId = entities.email_archive?.EntryId; | ||
if (!lfId) { | ||
log.error('No Laserfiche ID for ticket PDF?'); | ||
throw new Error('Could not determine Laserfiche ID for ticket PDF?'); | ||
} | ||
|
||
const path = entities.email_archive?.Path.split('\\').slice(0, -1).join('\\'); | ||
if (!path) { | ||
log.error('No Laserfiche path for ticket PDF?'); | ||
throw new Error('Could not determine Laserfiche path for ticket folder?'); | ||
} | ||
|
||
log.debug('Ticket is in LF. Updating Zendesk state'); | ||
|
||
if (ticket.status !== 'closed') { | ||
if (PATH_FIELD_ID > 0) { | ||
log.trace('Add LF path meta data back to Zendesk'); | ||
await setCustomField(log, ticket, [{ id: PATH_FIELD_ID, value: path }]); | ||
} | ||
|
||
if (LF_ID_FIELD_ID > 0) { | ||
log.trace('Add LF ID meta data back to Zendesk'); | ||
await setCustomField(log, ticket, [{ id: LF_ID_FIELD_ID, value: lfId }]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.