From ff71bca222664f653bae28dc36a49437057cce84 Mon Sep 17 00:00:00 2001 From: Sam Noel Date: Wed, 4 Dec 2024 11:45:56 -0500 Subject: [PATCH] fix: add lib-prom resolution --- package.json | 3 +- src/assessments/coi.ts | 63 ++++++++++++++++++++++++------------------ src/types.ts | 41 +++++++++++++++++++++------ yarn.lock | 50 +-------------------------------- 4 files changed, 72 insertions(+), 85 deletions(-) diff --git a/package.json b/package.json index 18fa1f5..ca0d62e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@qlever-llc/fl-sync", - "version": "1.5.3", + "version": "1.5.4", "description": "A Trellis microservice to sync Food Logiq data to a Trellis cloud", "main": "dist/index.ts", "scripts": { @@ -139,6 +139,7 @@ "typescript": "5.6.3" }, "resolutions": { + "@oada/lib-prom": "^4.0.1", "cross-spawn": "^7.0.5", "jsonpath-plus": "^10.0.0", "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz" diff --git a/src/assessments/coi.ts b/src/assessments/coi.ts index a53c8e3..9d72a31 100644 --- a/src/assessments/coi.ts +++ b/src/assessments/coi.ts @@ -170,7 +170,9 @@ async function getFlCois( * Fetch the attachments associated with a particular Food Logiq document. * For each attachment, return the OADA resource ID where the binary was stored. */ -async function fetchAndExtractAttachments(item: FlDocument | FlDocumentError): Promise { +async function fetchAndExtractAttachments( + item: FlDocument | FlDocumentError +): Promise { const attachments: AttachmentResources = {}; let zipFile: Uint8Array; @@ -392,7 +394,7 @@ async function assessCoi({ combined, part } : { - flCoi: FlDocument, + flCoi: FlDocument | FlDocumentError, combined: TrellisCOI, part: string | number }): Promise> { @@ -422,7 +424,7 @@ async function assessCoi({ expiryPassed, expiryMismatch, flExpString - } = checkExpirations(combined, flCoi); + } = checkExpirations(combined, flCoi as FlDocument); if (parsingError) { reasons.push('PDF Parsing error') @@ -441,11 +443,12 @@ async function assessCoi({ return { 'Trading Partner': { - value: flCoi?.shareSource?.sourceBusiness?.name, + // @ts-ignore + value: flCoi?.shareSource?.sourceBusiness?.name ?? 'Unknown (error retrieving FL Doc)', }, 'FoodLogiq Document Links': { - value: flCoi?.name, + value: 'name' in flCoi ? flCoi.name : flCoi._id, hyperlink: `https://connect.foodlogiq.com/businesses/${CO_ID}/documents/detail/${_id}/${COMMUNITY_ID}`, }, @@ -520,7 +523,7 @@ async function assessCoi({ ...(workersPassed ? {} : parsingError ? {} : { fill: fail }), }, - 'Comments': gatherComments(flCoi), + 'Comments': gatherComments(flCoi as FlDocument), } } @@ -711,7 +714,7 @@ export async function generateCoisReport(fname: string) { // const data = JSON.parse(json) as COI[]; // 2. Group COIs by supplier - const grouped : Record> = groupBy( + const coisBySupplier: Record> = groupBy( Object.values(flCois), (flCoi) => flCoi?.shareSource?.sourceBusiness?._id ) @@ -719,7 +722,7 @@ export async function generateCoisReport(fname: string) { const attachments : ReportDataSave["attachments"] = {}; const queryDate = new Date().setMonth(new Date().getMonth() - 18); const excelData: Array> = []; - for await (const [busId, cois] of Object.entries(grouped)) { + for await (const [busId, supplierCois] of Object.entries(coisBySupplier)) { // 3. Grab additional COIs of other statuses from that supplier // that may contribute to the assessment. @@ -737,35 +740,41 @@ export async function generateCoisReport(fname: string) { ...moreFlCois, } - cois.push(...Object.values(moreFlCois)); + // The collection of grouped flCois + supplierCois.push(...Object.values(moreFlCois)); - /* - - for await (const coi of Object.values(cois)) { - const att = await fetchAndExtractAttachments(coi); - attachments[coi._id] = att; + // Fetch the attchments and save the job result(s) which are TrellisCOIs + for await (const coi of Object.values(supplierCois)) { + attachments[coi._id] = await fetchAndExtractAttachments(coi); } // Filter the actual TrellisCOI attachments + const coisToCombine = supplierCois.flatMap( + ({_id}) => Object.values((attachments[_id] ?? {})) + // Filter errors at the coi level (failed to retrieve all attachments) + .filter((attachmentResources: AttachmentResources) => + !attachmentResources.serialized + ) + // Filter ErrObjs at the individual attachment level + .map((attachmentResources: Record) => + Object.values(attachmentResources) + .filter(value => ('results' in value)) + .map(({results}: ExtractPdfResult) => Object.values(results) as TrellisCOI[]) + ) + ) as unknown as TrellisCOI[]; - const combined = combineCois(att); - for await (const [index, coi] of cois.entries()) { - excelData.push(await assessCoi({ - _id: coi._id, - flCoi: coi.flCoi, - combined: coi.combined, + const combined = combineCois(coisToCombine); + for await (const [index, flCoi] of supplierCois.entries()) { +/* excelData.push(await assessCoi({ + flCoi, + combined, part: cois.length <= 1 ? '' : (index+1).toLocaleString(), })); + */ } } - await writeExcelFile({ - flCois, - attachments, - trellisCois - }, coiReportColumns, filename); - */ - } + await writeExcelFile(excelData, coiReportColumns, filename); } interface COI { diff --git a/src/types.ts b/src/types.ts index 2bce60c..39e0c74 100644 --- a/src/types.ts +++ b/src/types.ts @@ -384,19 +384,14 @@ export interface ErrObj { export interface FlDocumentError { _id: string, business?: string, - error: ErrObj, + error: ErrObj, } // { [coi id]: { [attachment id]: [trellis target job id] } } -// the target job captures the trellis binary resource, and trellis COI result id +// the target job captures the trellis binary resource, and the +// trellis COI result id(s) export type AttachmentResources = ErrObj | Record; -export interface ReportDataSave { - flCois: Record; - attachments: Record; - trellisCois: Record; // { [attachment id]: trellis COI } -} - export type GroupedReportData = Record } + +export interface ReportDataSave { + flCois: Record; + attachments: Record; +} + +/* Example ReportDataSave +{ + flCois: { + []: FL Doc, + ... + }, + + attachments: { + []: { + []: { + [ ]: { + job: TargetJob, + results: TrellisCOI, + }, + ... + }, + ... + }, + ... + } + +} +*/ + diff --git a/yarn.lock b/yarn.lock index 4c22efc..df487d1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -598,21 +598,6 @@ __metadata: languageName: node linkType: hard -"@oada/lib-config@npm:^3.7.0": - version: 3.9.1 - resolution: "@oada/lib-config@npm:3.9.1" - dependencies: - convict: "npm:^6.2.4" - convict-format-with-moment: "npm:^6.2.0" - convict-format-with-validator: "npm:^6.2.0" - dotenv: "npm:^16.0.3" - json5: "npm:^2.2.3" - tslib: "npm:^2.5.0" - yaml: "npm:^2.2.1" - checksum: 10/596fde472e08c1444cd241d85d927673e11b801edb205cb30194e7b85b2eef124a25c0a41c4a4e9a12b6d083569c148922e0728ed2e6464fc9b213fa26514821 - languageName: node - linkType: hard - "@oada/lib-config@npm:^4.0.0": version: 4.0.0 resolution: "@oada/lib-config@npm:4.0.0" @@ -633,21 +618,6 @@ __metadata: languageName: node linkType: hard -"@oada/lib-prom@npm:^3.8.0": - version: 3.8.0 - resolution: "@oada/lib-prom@npm:3.8.0" - dependencies: - "@oada/lib-config": "npm:^3.7.0" - nstats: "npm:^5.0.0" - prom-client: "npm:^14.2.0" - tslib: "npm:^2.5.0" - dependenciesMeta: - nstats: - optional: true - checksum: 10/2f265f4bdbae6d57165a91cf9868e56a01c7d8a89c432f9e0044045b74eff39699b77aef786646b52e9a2dc5d70d5b74d8e74d2b2b26894e682afa3863ad5e91 - languageName: node - linkType: hard - "@oada/lib-prom@npm:^4.0.1": version: 4.0.1 resolution: "@oada/lib-prom@npm:4.0.1" @@ -3152,7 +3122,7 @@ __metadata: languageName: node linkType: hard -"dotenv@npm:^16.0.3, dotenv@npm:^16.3.1, dotenv@npm:^16.4.5": +"dotenv@npm:^16.3.1, dotenv@npm:^16.4.5": version: 16.4.5 resolution: "dotenv@npm:16.4.5" checksum: 10/55a3134601115194ae0f924e54473459ed0d9fc340ae610b676e248cca45aa7c680d86365318ea964e6da4e2ea80c4514c1adab5adb43d6867fb57ff068f95c8 @@ -7049,15 +7019,6 @@ __metadata: languageName: node linkType: hard -"prom-client@npm:^14.2.0": - version: 14.2.0 - resolution: "prom-client@npm:14.2.0" - dependencies: - tdigest: "npm:^0.1.1" - checksum: 10/892eb83eb860945f3ee55bc19bb73e4a64cb63d95e28336141f49fb90a05354765b4ac4a8ba046fd895690f0bf231de1289caf180647cefdfd0d767f34725d97 - languageName: node - linkType: hard - "prom-client@npm:^15.1.3": version: 15.1.3 resolution: "prom-client@npm:15.1.3" @@ -8731,15 +8692,6 @@ __metadata: languageName: node linkType: hard -"yaml@npm:^2.2.1": - version: 2.6.1 - resolution: "yaml@npm:2.6.1" - bin: - yaml: bin.mjs - checksum: 10/cf412f03a33886db0a3aac70bb4165588f4c5b3c6f8fc91520b71491e5537800b6c2c73ed52015617f6e191eb4644c73c92973960a1999779c62a200ee4c231d - languageName: node - linkType: hard - "yaml@npm:^2.5.1": version: 2.5.1 resolution: "yaml@npm:2.5.1"