From 804a2407752107e1b7be4f5d9628bf8c374dbce8 Mon Sep 17 00:00:00 2001 From: Gerald Baulig Date: Fri, 15 Nov 2024 12:21:21 +0100 Subject: [PATCH] fix(lint): solve all lint errors --- src/services/fulfillment.ts | 98 ++++++++++++++++------------- src/services/fulfillment_product.ts | 14 +++-- src/stubs/dhl_soap.ts | 13 ++-- src/utils.ts | 10 ++- 4 files changed, 80 insertions(+), 55 deletions(-) diff --git a/src/services/fulfillment.ts b/src/services/fulfillment.ts index 2fd9ced..1a3678e 100644 --- a/src/services/fulfillment.ts +++ b/src/services/fulfillment.ts @@ -94,6 +94,7 @@ import { createOperationStatusCode, ResponseMap, Courier, + unique, } from './../utils.js'; import { Stub } from './../stub.js'; @@ -596,12 +597,12 @@ export class FulfillmentService ...Object.values(address_map).map( item => item.payload?.country_id ), - ...fulfillments?.map( + ...(fulfillments?.map( item => item.packaging?.sender?.address?.country_id - ), - ...fulfillments?.map( + ) ?? []), + ...(fulfillments?.map( item => item.packaging?.recipient?.address?.country_id - ), + ) ?? []), ], this.country_service, this.tech_user ?? subject, @@ -775,15 +776,18 @@ export class FulfillmentService 'Courier' ); - couriers.every( - courier => courier.payload?.shop_ids?.includes( - item.shop_id + if (!couriers.every( + courier => courier.payload?.shop_ids?.includes( + item.shop_id + ) ) - ) || throwStatusCode( - 'Fulfillment', - item.id, - this.status_codes.SHOP_ID_NOT_IDENTICAL, - ); + ) { + throwStatusCode( + 'Fulfillment', + item.id, + this.status_codes.SHOP_ID_NOT_IDENTICAL, + ); + } const credentials = await this.getByIds( credential_map, @@ -798,8 +802,8 @@ export class FulfillmentService const status: Status[] = [ sender_country?.status, recipient_country?.status, - ...products?.map(p => p?.status), - ...couriers?.map(c => c?.status), + ...(products?.map(p => p?.status) ?? []), + ...(couriers?.map(c => c?.status) ?? []), ]; const shop_country = await this.getById( @@ -897,10 +901,12 @@ export class FulfillmentService const variant = product.variants.find( v => v.id === p.variant_id ); - variant.attributes = [ - ...(variant.attributes ?? []), - ...(product.attributes ?? []), - ]; + variant.attributes = unique( + [ + ...(product.attributes ?? []), + ...(variant.attributes ?? []), + ] + ); const courier = courier_map[product.courier_id]?.payload; const stub = Stub.getInstance(courier); const taxes = product.tax_ids.map( @@ -1169,18 +1175,21 @@ export class FulfillmentService subject: request.subject }, context); - upsert_results.items.forEach(item => { - if (this.emitters && item.payload.fulfillment_state in this.emitters) { - switch (item.payload.fulfillment_state) { - case FulfillmentState.INVALID: - case FulfillmentState.FAILED: - this.isEventsEnabled && this.topic?.emit(this.emitters[item.payload.fulfillment_state], item); - default: - this.isEventsEnabled && this.topic?.emit(this.emitters[item.payload.fulfillment_state], item.payload); - break; + if (this.isEventsEnabled) { + upsert_results.items.forEach(item => { + if (this.emitters && item.payload.fulfillment_state in this.emitters) { + switch (item.payload.fulfillment_state) { + case FulfillmentState.INVALID: + case FulfillmentState.FAILED: + this.topic?.emit(this.emitters[item.payload.fulfillment_state], item); + break; + default: + this.topic?.emit(this.emitters[item.payload.fulfillment_state], item.payload); + break; + } } - } - }); + }); + } upsert_results.items.push( ...merged.filter(item => item.payload?.labels?.length === 0) @@ -1321,17 +1330,17 @@ export class FulfillmentService context ) ).then( - updates => updates.items.forEach( + updates => this.isEventsEnabled && updates.items.forEach( item => { response_map[item.payload?.id ?? item.status?.id] = item; if (this.emitters && item.payload.fulfillment_state in this.emitters) { switch (item.payload.fulfillment_state) { case FulfillmentState.INVALID: case FulfillmentState.FAILED: - this.isEventsEnabled && this.topic?.emit(this.emitters[item.payload.fulfillment_state], item); + this.topic?.emit(this.emitters[item.payload.fulfillment_state], item); break; default: - this.isEventsEnabled && this.topic?.emit(this.emitters[item.payload.fulfillment_state], item.payload); + this.topic?.emit(this.emitters[item.payload.fulfillment_state], item.payload); break; } } @@ -1465,18 +1474,21 @@ export class FulfillmentService subject: request.subject }, context); - update_results.items.forEach(item => { - if (this.emitters && item.payload.fulfillment_state in this.emitters) { - switch (item.payload.fulfillment_state) { - case FulfillmentState.INVALID: - case FulfillmentState.FAILED: - this.isEventsEnabled && this.topic?.emit(this.emitters[item.payload.fulfillment_state], item); - default: - this.isEventsEnabled && this.topic?.emit(this.emitters[item.payload.fulfillment_state], item.payload); - break; + if (this.isEventsEnabled) { + update_results.items.forEach(item => { + if (this.emitters && item.payload.fulfillment_state in this.emitters) { + switch (item.payload.fulfillment_state) { + case FulfillmentState.INVALID: + case FulfillmentState.FAILED: + this.topic?.emit(this.emitters[item.payload.fulfillment_state], item); + break; + default: + this.topic?.emit(this.emitters[item.payload.fulfillment_state], item.payload); + break; + } } - } - }); + }); + } update_results.items.push( ...items.filter(i => i.status?.code !== 200) diff --git a/src/services/fulfillment_product.ts b/src/services/fulfillment_product.ts index 489990f..8832f17 100644 --- a/src/services/fulfillment_product.ts +++ b/src/services/fulfillment_product.ts @@ -113,7 +113,9 @@ const countItems = (goods: Item[], container: Container) => { container.getLevels().forEach((level) => level.forEach((a) => { const item = item_map.get(a.getBox().getName()); - item && (item.quantity += 1); + if (item) { + item.quantity += 1 + } }) ); return [...item_map.values()]; @@ -412,7 +414,7 @@ export class FulfillmentProductService if (response.operation_status?.code === 200) { return response.items?.reduce( (a: ResponseMap, b: Response) => { - a[b.payload?.id!] = b; + a[b.payload?.id] = b; return a; }, {} @@ -868,7 +870,7 @@ export class FulfillmentProductService ).then( response => response.items.reduce( (a: ResponseMap, b) => { - a[b.payload?.id ?? b.status?.id!] = b; + a[b.payload?.id ?? b.status?.id] = b; return a; }, {} as ResponseMap @@ -959,11 +961,11 @@ export class FulfillmentProductService shipping: null }); - const courier_ids = new Set(); const solutions: FulfillmentSolution[] = offer_lists.map( offers => packer.canFit(offers, goods) ).map( containers => { + const courier_ids = new Set(); const parcels = containers.map((container): Parcel => { const [product_id, variant_id] = container.getOffer().name.split('\t'); const product = product_map[product_id].payload; @@ -1053,9 +1055,9 @@ export class FulfillmentProductService } ).sort( (a, b) => Math.min( - ...a.amounts?.map(am => am.net) + ...(a.amounts?.map(am => am.net) ?? []) ) - Math.min( - ...b.amounts?.map(am => am.net) + ...(b.amounts?.map(am => am.net) ?? []) ) ); diff --git a/src/stubs/dhl_soap.ts b/src/stubs/dhl_soap.ts index 197228e..7888992 100644 --- a/src/stubs/dhl_soap.ts +++ b/src/stubs/dhl_soap.ts @@ -17,6 +17,7 @@ import { import { FlatAggregatedFulfillment, throwOperationStatusCode, + unique, } from '../utils.js'; import { Stub } from '../stub.js'; import { @@ -442,7 +443,7 @@ export class DHLSoap extends Stub { } protected parseService (attributes: Attribute[]) { - return attributes?.reverse().filter((att: Attribute) => + return attributes?.filter((att: Attribute) => att.id?.startsWith(this.urns.dhl_service) ).map(att=> ({ [att.value]: { @@ -641,10 +642,12 @@ export class DHLSoap extends Stub { ) ) ?? {}) } - variant.attributes = [ - ...(variant.attributes ?? []), - ...(request.product?.attributes ?? []), - ]; + variant.attributes = unique( + [ + ...(request.product.attributes ?? []), + ...(variant.attributes ?? []), + ] + ); return { sequenceNumber: i + 1, Shipment: { diff --git a/src/utils.ts b/src/utils.ts index bd856c5..6a06c4d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -55,6 +55,7 @@ import { OperationStatus, Status } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/status.js'; +import { Attribute } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/attribute.js'; export type CRUDClient = Client | Client @@ -271,4 +272,11 @@ export const mergeFulfillments = (fulfillments: FlatAggregatedFulfillment[]): Fu } }); return Object.values(merged_fulfillments); -}; \ No newline at end of file +}; + +export const unique = (objs: T[], by = 'id'): T[] => [ + ...new Map( + objs.map( + o => [o[by], o] + )).values() +]; \ No newline at end of file