Skip to content

Commit

Permalink
fix: Only create job if *newly* in processing state
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Balmos <[email protected]>
  • Loading branch information
abalmos committed Oct 17, 2024
1 parent f45b029 commit a10a74f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
7 changes: 7 additions & 0 deletions service/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export const { config } = await libConfig({
},
},
syncTicket: {
enable: {
doc: 'Enable the syncTicket service to find new work within Zendesk',
format: Boolean,
default: true,
env: 'SERVICE_SYNC_TICKET_ENABLE',
arg: 'service-sync-ticket-enable',
},
timeout: {
doc: 'Time limit for processing a ticket archive',
format: Number,
Expand Down
16 changes: 10 additions & 6 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ async function run() {
concurrency: config.get('zendesk.concurrency'),
});

log.info('Initialize `archiveTicket` service');
service.on(
'syncTicket',
config.get('service.syncTicket.timeout'),
syncTicketService,
);
if (config.get('service.syncTicket.enable')) {
log.info('Initialize `syncTicket` service');
service.on(
'syncTicket',
config.get('service.syncTicket.timeout'),
syncTicketService,
);
} else {
log.info('syncTicket service disabled.');
}

log.info('Start @oada/jobs based services');
await service.start();
Expand Down
10 changes: 7 additions & 3 deletions service/src/services/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ export function pollerService(logOrig: Logger, oada: OADAClient): CronJob {

// Update Zendesk with the new state, but only if changed. Otherwise, Zendesk tickets are flodded with "useless" updates
if (
currentState !== nextState.state ||
currentStatus !== nextState.status
(nextState.state !== undefined &&
currentState !== nextState.state) ||
(nextState.status !== undefined &&
currentStatus !== nextState.status)
) {
await setTrellisState(log, ticket, nextState);
}
Expand Down Expand Up @@ -119,13 +121,15 @@ async function computeNextState(
const currentState =
getTicketFieldValue(ticket, config.get('zendesk.fields.state')) ?? '';

log.trace({ currentState }, 'Lookup ticket currentState');

if (currentState !== '' && currentState !== STATE_PENDING) {
log.trace(
{ currentState },
`Poller found a ticket already post ${STATE_PENDING}. Keeping current state.`,
);

return { state: currentState, status: undefined };
return { state: undefined, status: undefined };
}

// **Should** always have a customer ID if ZenDesk is configured correctly (i.e., a trigger that requires it to "solve")
Expand Down
8 changes: 3 additions & 5 deletions service/src/zd/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export async function callTypedApi<T>(
key: string,
cnf?: AxiosRequestConfig,
): Promise<T> {
log.trace(`Making credentialed API request: ${url}`);

const data = await makeCredentialedGetRequest<Record<string, T>>(
log,
url,
Expand Down Expand Up @@ -110,9 +108,9 @@ export async function makeCredentialedGetRequest<T>(
return {
buffer: r.data,
} as T;
} else {
return r.data as T;
}

return r.data as T;
}

export async function makeCredentialedPutRequest(
Expand Down Expand Up @@ -152,7 +150,7 @@ export function indexById<T extends { id: number | string }>(

// TYPES
export interface TrellisState {
state: string;
state: string | undefined;
status: string | undefined;
}

Expand Down
16 changes: 11 additions & 5 deletions service/src/zd/zendesk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,14 @@ export async function setTrellisState(
return;
}

const updates = [
{
const updates: Array<{ id: string | number; value: string | number }> = [];

if (state.state) {
updates.push({
id: config.get('zendesk.fields.state'),
value: state.state,
},
];
});
}

if (state.status) {
updates.push({
Expand All @@ -382,7 +384,11 @@ export async function setTrellisState(
});
}

await setCustomField(log, ticket, updates);
if (updates.length) {
await setCustomField(log, ticket, updates);
} else {
log.debug('No state change to update');
}
}

export async function setTicketStatus(
Expand Down

0 comments on commit a10a74f

Please sign in to comment.