Skip to content

Commit

Permalink
fix: add lib-prom resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
sanoel committed Dec 4, 2024
1 parent 8381126 commit ff71bca
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 85 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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"
Expand Down
63 changes: 36 additions & 27 deletions src/assessments/coi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttachmentResources> {
async function fetchAndExtractAttachments(
item: FlDocument | FlDocumentError
): Promise<AttachmentResources> {
const attachments: AttachmentResources = {};

let zipFile: Uint8Array;
Expand Down Expand Up @@ -392,7 +394,7 @@ async function assessCoi({
combined,
part
} : {
flCoi: FlDocument,
flCoi: FlDocument | FlDocumentError,
combined: TrellisCOI,
part: string | number
}): Promise<Record<string, ExcelRow>> {
Expand Down Expand Up @@ -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')
Expand All @@ -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}`,
},
Expand Down Expand Up @@ -520,7 +523,7 @@ async function assessCoi({
...(workersPassed ? {} : parsingError ? {} : { fill: fail }),
},

'Comments': gatherComments(flCoi),
'Comments': gatherComments(flCoi as FlDocument),
}
}

Expand Down Expand Up @@ -711,15 +714,15 @@ export async function generateCoisReport(fname: string) {
// const data = JSON.parse(json) as COI[];

// 2. Group COIs by supplier
const grouped : Record<string, Array<FlDocument | FlDocumentError>> = groupBy(
const coisBySupplier: Record<string, Array<FlDocument | FlDocumentError>> = groupBy(
Object.values(flCois),
(flCoi) => flCoi?.shareSource?.sourceBusiness?._id
)

const attachments : ReportDataSave["attachments"] = {};
const queryDate = new Date().setMonth(new Date().getMonth() - 18);
const excelData: Array<Record<string, ExcelRow>> = [];
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.
Expand All @@ -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<string, ExtractPdfResult | ErrObj>) =>
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 {
Expand Down
41 changes: 33 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ExtractPdfResult | ErrObj>;

export interface ReportDataSave {
flCois: Record<string, FlDocument>;
attachments: Record<string, AttachmentResources>;
trellisCois: Record<string, TrellisCOI>; // { [attachment id]: trellis COI }
}

export type GroupedReportData = Record<string, {
flCoi: FlDocument;
combined: TrellisCOI;
Expand All @@ -406,3 +401,33 @@ export interface ExtractPdfResult {
job?: TargetJob,
results: ErrObj | Record<string, TrellisCOI>
}

export interface ReportDataSave {
flCois: Record<string, FlDocument>;
attachments: Record<string, AttachmentResources>;
}

/* Example ReportDataSave
{
flCois: {
[<FL Doc _id>]: FL Doc,
...
},
attachments: {
[<FL Doc _id>]: {
[<Attachment id>]: {
[<Target Result id 1> ]: {
job: TargetJob,
results: TrellisCOI,
},
...
},
...
},
...
}
}
*/

50 changes: 1 addition & 49 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ff71bca

Please sign in to comment.