Skip to content

Commit a7e7db9

Browse files
committed
feat(fulfillment): refactor fulfillment parcels view model
1 parent afb6fb9 commit a7e7db9

7 files changed

Lines changed: 95 additions & 64 deletions

packages/modules/fulfillment/src/lib/models/fulfiillment-label.model.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/modules/fulfillment/src/lib/models/fulfillment-list-item.model.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/modules/fulfillment/src/lib/models/fulfillment-parcel-item.model.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
IoRestorecommerceFulfillmentFulfillment,
3+
IoRestorecommerceFulfillmentFulfillmentState,
4+
IoRestorecommerceFulfillmentItem,
5+
IoRestorecommerceFulfillmentLabel,
6+
IoRestorecommerceFulfillmentParcel,
7+
} from '@console-core/graphql';
8+
9+
import {
10+
FulfillmentParcelItemVM,
11+
FulfillmentParcelVM,
12+
} from './fulfillment-parcel.model';
13+
14+
export function mapFulfillmentParcelsToVM(
15+
payload: IoRestorecommerceFulfillmentFulfillment
16+
): FulfillmentParcelVM[] {
17+
const parcels = payload?.packaging?.parcels ?? [];
18+
const labels = payload?.labels ?? [];
19+
20+
return parcels.map(
21+
(parcel: IoRestorecommerceFulfillmentParcel): FulfillmentParcelVM => {
22+
const label = labels.find(
23+
(l: IoRestorecommerceFulfillmentLabel) => l.parcelId === parcel.id
24+
);
25+
26+
const currency =
27+
parcel.amount?.currencyId ?? parcel.price?.currencyId ?? '';
28+
29+
return {
30+
id: `${parcel.id}`,
31+
shortId: `${parcel.id?.slice(0, 8)}`,
32+
productId: `${parcel.productId}`,
33+
variantId: `${parcel.variantId}`,
34+
35+
grossLabel: formatMoney(parcel.amount?.gross ?? 0, currency),
36+
netLabel: formatMoney(parcel.amount?.net ?? 0, currency),
37+
vatLabel: formatMoney(parcel.amount?.vats?.[0]?.vat ?? 0, currency),
38+
regularPriceLabel: formatMoney(
39+
parcel.price?.regularPrice ?? 0,
40+
currency
41+
),
42+
43+
shipmentNumber: `${label?.shipmentNumber}`,
44+
labelState:
45+
label?.state ?? IoRestorecommerceFulfillmentFulfillmentState.Pending,
46+
labelUrl: `${label?.file?.url}`,
47+
48+
items: (parcel.items ?? []).map(
49+
(item: IoRestorecommerceFulfillmentItem) =>
50+
({
51+
name: item.name,
52+
quantity: item.quantity,
53+
hsCode: item.hsCode,
54+
weightLabel: item.package?.weightInKg
55+
? `${item.package.weightInKg} kg`
56+
: '—',
57+
sizeLabel: item.package?.sizeInCm
58+
? `${item.package.sizeInCm.length} × ${item.package.sizeInCm.width} × ${item.package.sizeInCm.height} cm`
59+
: '—',
60+
}) as FulfillmentParcelItemVM
61+
),
62+
};
63+
}
64+
);
65+
}
66+
67+
function formatMoney(value?: number, currency?: string): string {
68+
if (value == null) return '—';
69+
70+
return `${value.toFixed(2)} ${currency ?? ''}`.trim();
71+
}
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
import { ParcelItem } from './fulfillment-parcel-item.model';
1+
export interface FulfillmentParcelItemVM {
2+
name: string;
3+
quantity: number;
4+
hsCode?: string;
5+
weightLabel?: string;
6+
sizeLabel?: string;
7+
}
28

3-
export interface Parcel {
9+
export interface FulfillmentParcelVM {
410
id: string;
5-
11+
shortId: string;
612
productId?: string;
713
variantId?: string;
814

9-
weightKg: number;
15+
grossLabel?: string;
16+
netLabel?: string;
17+
vatLabel?: string;
18+
regularPriceLabel?: string;
19+
20+
shipmentNumber?: string;
21+
labelState?: string;
22+
labelUrl?: string;
1023

11-
items: ParcelItem[];
24+
items: FulfillmentParcelItemVM[];
1225
}

packages/modules/fulfillment/src/lib/models/fulfillment.mapper.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,13 @@
11
import { IoRestorecommerceFulfillmentFulfillment } from '@console-core/graphql';
22

3+
import { mapFulfillmentParcelsToVM } from './fulfillment-parcel.mapper';
34
import { FulfillmentState } from './fulfillment-state.model';
45
import { Fulfillment } from './fulfillment.model';
56

67
export function mapFulfillmentDto(
78
dto: IoRestorecommerceFulfillmentFulfillment
89
): Fulfillment {
9-
const parcels =
10-
dto.packaging?.parcels?.map((p) => ({
11-
id: p.id ?? '',
12-
13-
productId: p.productId ?? undefined,
14-
variantId: p.variantId ?? undefined,
15-
16-
weightKg: p.package?.weightInKg ?? 0,
17-
18-
items:
19-
p.items?.map((i) => ({
20-
name: i.name ?? '',
21-
quantity: i.quantity ?? 0,
22-
productId: i.productId ?? '',
23-
weightKg: i.package?.weightInKg ?? 0,
24-
})) ?? [],
25-
})) ?? [];
26-
27-
const labels =
28-
dto.labels?.map((l) => ({
29-
id: l.id ?? '',
30-
parcelId: l.parcelId ?? '',
31-
shipmentNumber: l.shipmentNumber ?? '',
32-
state: l.state ?? '',
33-
url: l.file?.url ?? '',
34-
})) ?? [];
10+
const parcels = mapFulfillmentParcelsToVM(dto);
3511

3612
return {
3713
id: dto.id ?? '',
@@ -43,16 +19,15 @@ export function mapFulfillmentDto(
4319
? new Date(dto.meta.modified as string)
4420
: undefined,
4521

46-
parcels,
47-
labels,
48-
4922
shippingCost: {
5023
currency: dto.totalAmounts?.[0].currency?.code ?? 'EUR',
5124
gross: dto.totalAmounts?.[0].gross ?? 0,
5225
net: dto.totalAmounts?.[0].net ?? 0,
5326
tax: dto.totalAmounts?.[0].vats?.[0].vat ?? 0,
5427
},
5528

29+
parcels,
30+
5631
orderId:
5732
dto.references?.find((r) => r.instanceType?.includes('order'))
5833
?.instanceId ?? undefined,

packages/modules/fulfillment/src/lib/models/fulfillment.model.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Label } from './fulfiillment-label.model';
2-
import { Parcel } from './fulfillment-parcel.model';
1+
import { FulfillmentParcelVM } from './fulfillment-parcel.model';
32
import { FulfillmentState } from './fulfillment-state.model';
43
import { Money } from './money.model';
54

@@ -10,8 +9,7 @@ export interface Fulfillment {
109
createdAt: Date;
1110
modifiedAt?: Date;
1211

13-
parcels: Parcel[];
14-
labels: Label[];
12+
parcels: FulfillmentParcelVM[];
1513

1614
shippingCost?: Money;
1715

0 commit comments

Comments
 (0)