Skip to content

Commit

Permalink
feat(side-conv): Add list of side converstations to main ticket PDF
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Balmos <[email protected]>
  • Loading branch information
abalmos committed Jan 11, 2024
1 parent 3b82488 commit 87966f0
Show file tree
Hide file tree
Showing 7 changed files with 1,896 additions and 1,775 deletions.
4 changes: 3 additions & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
Expand All @@ -6,4 +8,4 @@ plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"

yarnPath: .yarn/releases/yarn-3.6.1.cjs
yarnPath: .yarn/releases/yarn-3.6.1.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@
"volta": {
"node": "16.19.0"
}
}
}
106 changes: 72 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ import { tpDocTypesTree } from './tree.js';
import {
EnsureResult,
getOrgs,
getTicket,
getTicketArchive,
SAP_FIELD,
searchTickets,
Ticket,
} from './zd/zendesk.js';
import { generatePdf } from './zd/pdf.js';
import { writeFileSync } from 'node:fs';
// Stuff from config
const { token, domain } = config.get('oada');
const {
Expand All @@ -50,7 +52,7 @@ const {
} = config.get('zendesk');
const concurrency = config.get('concurrency');
//const TIMEOUT = config.get('timeout');
const JOB_TIMEOUT= config.get('timeout');
const JOB_TIMEOUT = config.get('timeout');
const POLL_RATE = config.get('poll-rate');

const info = debug('zendesk-sync:info');
Expand All @@ -63,14 +65,21 @@ const workQueue = new Map<number, number>();
const work = new PQueue({ concurrency, timeout: JOB_TIMEOUT });
let cleanup: (id: number) => void | undefined;
tpDocTypesTree['resources'] = cloneDeep(
tpDocTypesTree?.bookmarks?.trellisfw?.['trading-partners'] ?? {}
tpDocTypesTree?.bookmarks?.trellisfw?.['trading-partners'] ?? {},
);

export async function run() {
const oada = await connect({ token, domain });
cleanup = watchZendesk(async (ticket: Ticket) => {
work.add(async () => handleTicket(ticket, oada));
});
////////////// const oada = await connect({ token, domain });
const oada = {};
// cleanup = watchZendesk(async (ticket: Ticket) => {
// work.add(async () => handleTicket(ticket, oada as OADAClient));
// });
info('Starting up...');
try {
handleTicket(await getTicket(72), oada as OADAClient);
} catch (e) {
error(e);
}
}

/**
Expand All @@ -85,24 +94,30 @@ export async function pollZd(): Promise<Array<Ticket>> {

// Now get the set of tickets with an organization
let tixWithOrgs = tickets.filter(
(t) => (t?.custom_fields.some((cf) => cf.id === ORG_FIELD_ID && cf.value) || t.organization_id)
)
(t) =>
t?.custom_fields.some((cf) => cf.id === ORG_FIELD_ID && cf.value) ||
t.organization_id,
);

const picked = tixWithOrgs.map(
(t) => (t?.custom_fields.find((cf) => cf.id === ORG_FIELD_ID)?.value ?? t.organization_id) as unknown as number
(t) =>
(t?.custom_fields.find((cf) => cf.id === ORG_FIELD_ID)?.value ??
t.organization_id) as unknown as number,
);

// Get all mentioned organizations
const orgs = await getOrgs(
Array.from(new Set(tixWithOrgs.map((t) => t.organization_id)))
Array.from(new Set(tixWithOrgs.map((t) => t.organization_id))),
);

tixWithOrgs = tixWithOrgs.map(
(t) => ({
...t,
organization: orgs[(t?.custom_fields?.[ORG_FIELD_ID] ?? t.organization_id) as unknown as string],
})
);
tixWithOrgs = tixWithOrgs.map((t) => ({
...t,
organization:
orgs[
(t?.custom_fields?.[ORG_FIELD_ID] ??
t.organization_id) as unknown as string
],
}));

// Return only those with an SAP_FIELD value
return tixWithOrgs.filter(
Expand All @@ -111,7 +126,10 @@ export async function pollZd(): Promise<Array<Ticket>> {
orgs[picked[i]!]?.organization_fields[SAP_FIELD] ||
// Close after 27 days without SAP ID. Otherwise, Zendesk will close it themselves and
// it won't show up under this query for solved tickets.
((Date.now() - (new Date(t.updated_at) as unknown as number))/24/3_600_000 > 27.0)
(Date.now() - (new Date(t.updated_at) as unknown as number)) /
24 /
3_600_000 >
27.0,
);
}

Expand All @@ -122,21 +140,36 @@ export async function pollZd(): Promise<Array<Ticket>> {
*/
export async function handleTicket(
ticket: Ticket,
oada: OADAClient
oada: OADAClient,
): Promise<void | string> {
try {
info(`Processing Ticket [id:${ticket.id}] [size: ${work.size}] [pending: ${work.pending}]`);
// FIXME: Remove
console.log(oada);
info(
`Processing Ticket [id:${ticket.id}] [size: ${work.size}] [pending: ${work.pending}]`,
);
workQueue.set(ticket.id, Date.now());
const archive = await getTicketArchive(ticket);
if (archive.org === null) {
error('A ticket without an organization is being archived?', archive);
throw new Error('Ticket must have an organization to archive.');
}

const pdf = await generatePdf(archive);
const sapids = archive.org?.organization_fields?.[SAP_FIELD] ?
archive.org?.organization_fields?.[SAP_FIELD].split(',').map(
(id) => `sap:${id}`
) : [];

// FIXME: Remove
info('Writing output PDF');
writeFileSync('./output.pdf', pdf);
info('Goodbye');
process.exit();

// FIXME: un-comment
/*
const sapids = archive.org?.organization_fields?.[SAP_FIELD]
? archive.org?.organization_fields?.[SAP_FIELD].split(',').map(
(id) => `sap:${id}`,
)
: [];
const zid = `zendesk:${archive.org.id}`;
const { result } = (await doJob(oada, {
service: 'trellis-data-manager',
Expand All @@ -152,7 +185,9 @@ export async function handleTicket(
if (!tp) {
tp = result.matches![0].item;
if (!tp) {
error(`Failed to find single trading-partner for ticket ${ticket.id} organization ${archive.org.name}`);
error(
`Failed to find single trading-partner for ticket ${ticket.id} organization ${archive.org.name}`,
);
}
}
if (
Expand Down Expand Up @@ -184,7 +219,7 @@ export async function handleTicket(
await oada.put({
path: `/${_id}/_meta`,
data: {
shared: 'outgoing'
shared: 'outgoing',
},
});
Expand Down Expand Up @@ -221,7 +256,7 @@ export async function handleTicket(
});
}
await oada.put({
await oada.put({
path: `/${tp.masterid}/bookmarks/trellisfw/documents/tickets`,
data: {
[trellisname]: {
Expand All @@ -246,14 +281,15 @@ export async function handleTicket(
},
},
});
*/

if (cleanup) {
trace(`Marked sync operation for ticket [${ticket.id}] as finished.`);
cleanup(ticket.id);
}
return `/${tp.masterid}/bookmarks/trellisfw/documents/tickets/${trellisname}`;
} catch(err) {
error(err)
//return `/${tp.masterid}/bookmarks/trellisfw/documents/tickets/${trellisname}`;
} catch (err) {
error(err);
}
}
/**
Expand All @@ -262,9 +298,8 @@ export async function handleTicket(
* @returns
*/
export function watchZendesk(
task: (ticket: Ticket) => void
task: (ticket: Ticket) => void,
): (id: number) => void {

const job = new CronJob(`*/${POLL_RATE} * * * * *`, async () => {
const start = new Date();

Expand All @@ -281,9 +316,12 @@ export function watchZendesk(

// Queue additional items
let tickets = await pollZd();
tickets = tickets.filter(t => !workQueue.has(t.id));
tickets = tickets.filter((t) => !workQueue.has(t.id));
info(`Current workQueue size: ${workQueue.size}`);
if (tickets.length) info(`Adding tickets to work queue: ${tickets.map(t => t.id).join(', ')}`);
if (tickets.length)
info(
`Adding tickets to work queue: ${tickets.map((t) => t.id).join(', ')}`,
);
for await (const ticket of tickets) {
trace(`Adding ticket ${ticket.id} to work queue.`);
workQueue.set(ticket.id, 0);
Expand All @@ -304,4 +342,4 @@ export function watchZendesk(
if (esMain(import.meta)) {
trace('esMain determined to run the service');
await run();
}
}
6 changes: 3 additions & 3 deletions src/zd/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const prepareFile = async (url: string) => {
const pathTraversal = !filePath.startsWith(STATIC_PATH);
const exists = await access(filePath).then(
() => true,
() => false
() => false,
);
const found = !pathTraversal && exists;
const streamPath = found ? filePath : STATIC_PATH + '/404.html';
Expand Down Expand Up @@ -98,11 +98,11 @@ export async function generatePdf(archive: TicketArchive): Promise<Buffer> {
.on('load', trace)
.on('error', error)
.on('console', (message) =>
info(`${message.type().substring(0, 3).toUpperCase()} ${message.text()}`)
info(`${message.type().substring(0, 3).toUpperCase()} ${message.text()}`),
)
.on('pageerror', error)
.on('requestfailed', (request) =>
warn(`${request.failure()?.errorText} ${request.url()}`)
warn(`${request.failure()?.errorText} ${request.url()}`),
);

await page.setRequestInterception(true);
Expand Down
Loading

0 comments on commit 87966f0

Please sign in to comment.