@unchainedshop/core-delivery 3.0.0-alpha7 → 3.0.0-rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/lib/db/DeliveryProvidersCollection.d.ts +30 -2
  2. package/lib/db/DeliveryProvidersCollection.d.ts.map +1 -1
  3. package/lib/db/DeliveryProvidersCollection.js +5 -0
  4. package/lib/db/DeliveryProvidersCollection.js.map +1 -1
  5. package/lib/delivery-index.d.ts +1 -8
  6. package/lib/delivery-index.d.ts.map +1 -1
  7. package/lib/delivery-index.js +1 -8
  8. package/lib/delivery-index.js.map +1 -1
  9. package/lib/delivery-settings.d.ts +7 -5
  10. package/lib/delivery-settings.d.ts.map +1 -1
  11. package/lib/delivery-settings.js.map +1 -1
  12. package/lib/director/DeliveryPricingAdapter.d.ts +2 -2
  13. package/lib/director/DeliveryPricingAdapter.d.ts.map +1 -1
  14. package/lib/director/DeliveryPricingDirector.d.ts +3 -3
  15. package/lib/director/DeliveryPricingDirector.d.ts.map +1 -1
  16. package/lib/module/configureDeliveryModule.d.ts +20 -39
  17. package/lib/module/configureDeliveryModule.d.ts.map +1 -1
  18. package/lib/module/configureDeliveryModule.js +1 -81
  19. package/lib/module/configureDeliveryModule.js.map +1 -1
  20. package/lib/types.d.ts +9 -13
  21. package/lib/types.d.ts.map +1 -1
  22. package/lib/types.js.map +1 -1
  23. package/package.json +10 -12
  24. package/src/db/DeliveryProvidersCollection.ts +32 -2
  25. package/src/delivery-index.ts +1 -10
  26. package/src/delivery-settings.ts +11 -5
  27. package/src/module/configureDeliveryModule.ts +28 -187
  28. package/tsconfig.json +5 -7
  29. package/src/director/DeliveryAdapter.ts +0 -56
  30. package/src/director/DeliveryDirector.ts +0 -76
  31. package/src/director/DeliveryError.ts +0 -6
  32. package/src/director/DeliveryPricingAdapter.ts +0 -86
  33. package/src/director/DeliveryPricingDirector.ts +0 -99
  34. package/src/director/DeliveryPricingSheet.ts +0 -87
  35. package/src/director/DeliveryProviderType.ts +0 -4
  36. package/src/types.ts +0 -115
@@ -1,78 +1,7 @@
1
- import { UnchainedCore } from '@unchainedshop/core';
2
- import { DeliveryContext, DeliveryInterface, DeliveryProvider, DeliveryProviderType } from '../types.js';
3
1
  import { emit, registerEvents } from '@unchainedshop/events';
4
2
  import { mongodb, generateDbFilterById, generateDbObjectId, ModuleInput } from '@unchainedshop/mongodb';
5
- import { DeliveryPricingSheet } from '../director/DeliveryPricingSheet.js';
6
- import { DeliveryProvidersCollection } from '../db/DeliveryProvidersCollection.js';
3
+ import { DeliveryProvidersCollection, DeliveryProvider } from '../db/DeliveryProvidersCollection.js';
7
4
  import { deliverySettings, DeliverySettingsOptions } from '../delivery-settings.js';
8
- import { DeliveryDirector } from '../director/DeliveryDirector.js';
9
- import { DeliveryPricingContext, DeliveryPricingDirector } from '../director/DeliveryPricingDirector.js';
10
-
11
- import { DeliveryError } from '../delivery-index.js';
12
- import { Order } from '@unchainedshop/core-orders';
13
- import {
14
- DeliveryPricingCalculation,
15
- IDeliveryPricingSheet,
16
- } from '../director/DeliveryPricingAdapter.js';
17
-
18
- export type DeliveryModule = {
19
- // Queries
20
- count: (query: mongodb.Filter<DeliveryProvider>) => Promise<number>;
21
- create: (doc: DeliveryProvider) => Promise<DeliveryProvider>;
22
- update: (_id: string, doc: DeliveryProvider) => Promise<DeliveryProvider>;
23
- delete: (_id: string) => Promise<DeliveryProvider>;
24
-
25
- findProvider: (
26
- query: {
27
- deliveryProviderId: string;
28
- } & mongodb.Filter<DeliveryProvider>,
29
- options?: mongodb.FindOptions,
30
- ) => Promise<DeliveryProvider>;
31
- findProviders: (
32
- query: mongodb.Filter<DeliveryProvider>,
33
- options?: mongodb.FindOptions,
34
- ) => Promise<Array<DeliveryProvider>>;
35
-
36
- providerExists: (query: { deliveryProviderId: string }) => Promise<boolean>;
37
-
38
- pricingSheet: (params: {
39
- calculation: Array<DeliveryPricingCalculation>;
40
- currency: string;
41
- }) => IDeliveryPricingSheet;
42
-
43
- // Delivery adapter
44
- findInterface: (params: DeliveryProvider) => DeliveryInterface;
45
- findInterfaces: (params: { type: DeliveryProviderType }) => Array<DeliveryInterface>;
46
- findSupported: (
47
- params: { order: Order },
48
- unchainedAPI: UnchainedCore,
49
- ) => Promise<Array<DeliveryProvider>>;
50
- determineDefault: (
51
- deliveryProviders: Array<DeliveryProvider>,
52
- params: { order: Order },
53
- unchainedAPI: UnchainedCore,
54
- ) => Promise<DeliveryProvider>;
55
-
56
- isActive: (deliveryProvider: DeliveryProvider, unchainedAPI: UnchainedCore) => Promise<boolean>;
57
- configurationError: (
58
- deliveryProvider: DeliveryProvider,
59
- unchainedAPI: UnchainedCore,
60
- ) => Promise<DeliveryError>;
61
-
62
- isAutoReleaseAllowed: (
63
- deliveryProvider: DeliveryProvider,
64
- unchainedAPI: UnchainedCore,
65
- ) => Promise<boolean>;
66
- calculate: (
67
- pricingContext: DeliveryPricingContext,
68
- unchainedAPI: UnchainedCore,
69
- ) => Promise<Array<DeliveryPricingCalculation>>;
70
- send: (
71
- deliveryProviderId: string,
72
- deliveryContext: DeliveryContext,
73
- unchainedAPI: UnchainedCore,
74
- ) => Promise<any>;
75
- };
76
5
 
77
6
  const DELIVERY_PROVIDER_EVENTS: string[] = [
78
7
  'DELIVERY_PROVIDER_CREATE',
@@ -80,11 +9,11 @@ const DELIVERY_PROVIDER_EVENTS: string[] = [
80
9
  'DELIVERY_PROVIDER_REMOVE',
81
10
  ];
82
11
 
83
- const asyncFilter = async (arr, predicate) => {
84
- const results = await Promise.all(arr.map(predicate));
85
-
86
- return arr.filter((_v, index) => results[index]);
87
- };
12
+ export interface DeliveryInterface {
13
+ _id: string;
14
+ label: string;
15
+ version: string;
16
+ }
88
17
 
89
18
  export const buildFindSelector = ({ type }: mongodb.Filter<DeliveryProvider> = {}) => {
90
19
  return { ...(type ? { type } : {}), deleted: null };
@@ -93,43 +22,44 @@ export const buildFindSelector = ({ type }: mongodb.Filter<DeliveryProvider> = {
93
22
  export const configureDeliveryModule = async ({
94
23
  db,
95
24
  options: deliveryOptions = {},
96
- }: ModuleInput<DeliverySettingsOptions>): Promise<DeliveryModule> => {
25
+ }: ModuleInput<DeliverySettingsOptions>) => {
97
26
  registerEvents(DELIVERY_PROVIDER_EVENTS);
98
27
 
99
28
  deliverySettings.configureSettings(deliveryOptions);
100
29
 
101
30
  const DeliveryProviders = await DeliveryProvidersCollection(db);
102
31
 
103
- const getDeliveryAdapter = async (
104
- deliveryProviderId: string,
105
- deliveryContext: DeliveryContext,
106
- unchainedAPI: UnchainedCore,
107
- ) => {
108
- const provider = await DeliveryProviders.findOne(generateDbFilterById(deliveryProviderId), {});
109
-
110
- return DeliveryDirector.actions(provider, deliveryContext, unchainedAPI);
111
- };
112
-
113
32
  return {
114
33
  // Queries
115
- count: async (query) => {
34
+ count: async (query: mongodb.Filter<DeliveryProvider>): Promise<number> => {
116
35
  const providerCount = await DeliveryProviders.countDocuments(buildFindSelector(query));
117
36
  return providerCount;
118
37
  },
119
38
 
120
- findProvider: async ({ deliveryProviderId, ...query }, options) => {
39
+ findProvider: async (
40
+ {
41
+ deliveryProviderId,
42
+ ...query
43
+ }: {
44
+ deliveryProviderId: string;
45
+ } & mongodb.Filter<DeliveryProvider>,
46
+ options?: mongodb.FindOptions<DeliveryProvider>,
47
+ ): Promise<DeliveryProvider> => {
121
48
  return DeliveryProviders.findOne(
122
49
  deliveryProviderId ? generateDbFilterById(deliveryProviderId) : query,
123
50
  options,
124
51
  );
125
52
  },
126
53
 
127
- findProviders: async (query, options = { sort: { created: 1 } }) => {
54
+ findProviders: async (
55
+ query: mongodb.Filter<DeliveryProvider>,
56
+ options: mongodb.FindOptions<DeliveryProvider> = { sort: { created: 1 } },
57
+ ): Promise<Array<DeliveryProvider>> => {
128
58
  const providers = DeliveryProviders.find(buildFindSelector(query), options);
129
59
  return providers.toArray();
130
60
  },
131
61
 
132
- providerExists: async ({ deliveryProviderId }) => {
62
+ providerExists: async ({ deliveryProviderId }: { deliveryProviderId: string }): Promise<boolean> => {
133
63
  const providerCount = await DeliveryProviders.countDocuments(
134
64
  generateDbFilterById(deliveryProviderId, { deleted: null }),
135
65
  { limit: 1 },
@@ -137,110 +67,19 @@ export const configureDeliveryModule = async ({
137
67
  return !!providerCount;
138
68
  },
139
69
 
140
- // Delivery Adapter
141
-
142
- findInterface: (paymentProvider) => {
143
- const Adapter = DeliveryDirector.getAdapter(paymentProvider.adapterKey);
144
- if (!Adapter) return null;
145
- return {
146
- _id: Adapter.key,
147
- label: Adapter.label,
148
- version: Adapter.version,
149
- };
150
- },
151
-
152
- findInterfaces: ({ type }) => {
153
- return DeliveryDirector.getAdapters({
154
- adapterFilter: (Adapter) => Adapter.typeSupported(type),
155
- }).map((Adapter) => ({
156
- _id: Adapter.key,
157
- label: Adapter.label,
158
- version: Adapter.version,
159
- }));
160
- },
161
-
162
- findSupported: async (deliveryContext, unchainedAPI) => {
163
- const foundProviders = await DeliveryProviders.find(buildFindSelector({})).toArray();
164
- const providers: DeliveryProvider[] = await asyncFilter(
165
- foundProviders,
166
- async (provider: DeliveryProvider) => {
167
- try {
168
- const director = await DeliveryDirector.actions(provider, deliveryContext, unchainedAPI);
169
- return director.isActive();
170
- } catch {
171
- return false;
172
- }
173
- },
174
- );
175
-
176
- return deliverySettings.filterSupportedProviders(
177
- {
178
- providers,
179
- order: deliveryContext.order,
180
- },
181
- unchainedAPI,
182
- );
183
- },
184
-
185
- determineDefault: async (deliveryProviders, deliveryContext, unchainedAPI) => {
186
- return deliverySettings.determineDefaultProvider(
187
- {
188
- providers: deliveryProviders,
189
- ...deliveryContext,
190
- },
191
- unchainedAPI,
192
- );
193
- },
194
-
195
- configurationError: async (deliveryProvider, unchainedAPI) => {
196
- const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI);
197
- return director.configurationError();
198
- },
199
-
200
- isActive: async (deliveryProvider, unchainedAPI) => {
201
- const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI);
202
- return Boolean(director.isActive());
203
- },
204
-
205
- isAutoReleaseAllowed: async (deliveryProvider, unchainedAPI) => {
206
- const director = await DeliveryDirector.actions(deliveryProvider, {}, unchainedAPI);
207
- return Boolean(director.isAutoReleaseAllowed());
208
- },
209
-
210
- calculate: async (pricingContext, unchainedAPI) => {
211
- const pricing = await DeliveryPricingDirector.actions(pricingContext, unchainedAPI);
212
- return pricing.calculate();
213
- },
214
-
215
- send: async (deliveryProviderId, deliveryContext, unchainedAPI) => {
216
- const adapter = await getDeliveryAdapter(deliveryProviderId, deliveryContext, unchainedAPI);
217
- return adapter.send();
218
- },
219
-
220
- pricingSheet: (params) => {
221
- return DeliveryPricingSheet(params);
222
- },
223
-
224
70
  // Mutations
225
- create: async (doc) => {
226
- const Adapter = DeliveryDirector.getAdapter(doc.adapterKey);
227
- if (!Adapter) return null;
228
-
71
+ create: async (doc: DeliveryProvider): Promise<DeliveryProvider> => {
229
72
  const { insertedId: deliveryProviderId } = await DeliveryProviders.insertOne({
230
73
  _id: generateDbObjectId(),
231
74
  created: new Date(),
232
- configuration: Adapter.initialConfiguration,
233
75
  ...doc,
234
76
  });
235
- const deliveryProvider = await DeliveryProviders.findOne(
236
- generateDbFilterById(deliveryProviderId),
237
- {},
238
- );
77
+ const deliveryProvider = await DeliveryProviders.findOne({ _id: deliveryProviderId }, {});
239
78
  await emit('DELIVERY_PROVIDER_CREATE', { deliveryProvider });
240
79
  return deliveryProvider;
241
80
  },
242
81
 
243
- update: async (_id: string, doc: DeliveryProvider) => {
82
+ update: async (_id: string, doc: DeliveryProvider): Promise<DeliveryProvider> => {
244
83
  const deliveryProvider = await DeliveryProviders.findOneAndUpdate(
245
84
  generateDbFilterById(_id),
246
85
  {
@@ -255,7 +94,7 @@ export const configureDeliveryModule = async ({
255
94
  return deliveryProvider;
256
95
  },
257
96
 
258
- delete: async (_id) => {
97
+ delete: async (_id: string): Promise<DeliveryProvider> => {
259
98
  const deliveryProvider = await DeliveryProviders.findOneAndUpdate(
260
99
  generateDbFilterById(_id),
261
100
  {
@@ -270,3 +109,5 @@ export const configureDeliveryModule = async ({
270
109
  },
271
110
  };
272
111
  };
112
+
113
+ export type DeliveryModule = Awaited<ReturnType<typeof configureDeliveryModule>>;
package/tsconfig.json CHANGED
@@ -1,11 +1,9 @@
1
1
  {
2
2
  "extends": "../shared/base.tsconfig.json",
3
3
  "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "lib",
4
+ "declarationDir": "./lib",
5
+ "rootDir": "./src",
6
+ "outDir": "./lib"
6
7
  },
7
- "exclude": ["**/*.test.ts", "**/*.test.js", "tests"],
8
- "include": [
9
- "./src"
10
- ]
11
- }
8
+ "exclude": ["**/*.test.ts", "**/*.test.js", "tests", "lib"]
9
+ }
@@ -1,56 +0,0 @@
1
- import { log, LogLevel } from '@unchainedshop/logger';
2
- import { DeliveryError } from './DeliveryError.js';
3
- import { IDeliveryAdapter } from '../types.js';
4
-
5
- export const DeliveryAdapter: Omit<IDeliveryAdapter, 'key' | 'label' | 'version'> = {
6
- initialConfiguration: [],
7
-
8
- typeSupported: () => {
9
- return false;
10
- },
11
-
12
- actions: () => {
13
- return {
14
- configurationError: () => {
15
- return DeliveryError.NOT_IMPLEMENTED;
16
- },
17
-
18
- estimatedDeliveryThroughput: async () => {
19
- return 0;
20
- },
21
-
22
- isActive: () => {
23
- return false;
24
- },
25
-
26
- isAutoReleaseAllowed: () => {
27
- // if you return false here,
28
- // the order will need manual confirmation before
29
- // unchained will try to invoke send()
30
- return true;
31
- },
32
-
33
- send: async () => {
34
- // if you return true, the status will be changed to DELIVERED
35
-
36
- // if you return false, the order delivery status stays the
37
- // same but the order status might change
38
-
39
- // if you throw an error, you cancel the whole checkout process
40
- return false;
41
- },
42
-
43
- pickUpLocationById: async () => {
44
- return null;
45
- },
46
-
47
- pickUpLocations: async () => {
48
- return [];
49
- },
50
- };
51
- },
52
-
53
- log: (message: string, { level = LogLevel.Debug, ...options } = {}) => {
54
- return log(message, { level, ...options });
55
- },
56
- };
@@ -1,76 +0,0 @@
1
- import { log, LogLevel } from '@unchainedshop/logger';
2
- import { BaseDirector } from '@unchainedshop/utils';
3
- import { DeliveryError } from './DeliveryError.js';
4
- import { IDeliveryAdapter, IDeliveryDirector } from '../types.js';
5
-
6
- const baseDirector = BaseDirector<IDeliveryAdapter>('DeliveryDirector');
7
-
8
- export const DeliveryDirector: IDeliveryDirector = {
9
- ...baseDirector,
10
-
11
- actions: async (deliveryProvider, deliveryContext, unchainedAPI) => {
12
- const Adapter = baseDirector.getAdapter(deliveryProvider.adapterKey);
13
-
14
- const context = { ...deliveryContext, ...unchainedAPI };
15
- const adapter = Adapter.actions(deliveryProvider.configuration, context);
16
-
17
- return {
18
- configurationError: () => {
19
- try {
20
- return adapter.configurationError();
21
- } catch {
22
- return DeliveryError.ADAPTER_NOT_FOUND;
23
- }
24
- },
25
-
26
- estimatedDeliveryThroughput: async (warehousingThroughputTime) => {
27
- try {
28
- const throughput = await adapter.estimatedDeliveryThroughput(warehousingThroughputTime);
29
- return throughput;
30
- } catch (error) {
31
- log('Delivery Director -> Error while estimating delivery throughput', {
32
- level: LogLevel.Warning,
33
- ...error,
34
- });
35
- return null;
36
- }
37
- },
38
-
39
- isActive: () => {
40
- try {
41
- return adapter.isActive();
42
- } catch (error) {
43
- log('Delivery Director -> Error while checking if is active', {
44
- level: LogLevel.Warning,
45
- ...error,
46
- });
47
- return false;
48
- }
49
- },
50
-
51
- isAutoReleaseAllowed: () => {
52
- try {
53
- return adapter.isAutoReleaseAllowed();
54
- } catch (error) {
55
- log('Delivery Director -> Error while checking if auto release is allowed', {
56
- level: LogLevel.Warning,
57
- ...error,
58
- });
59
- return false;
60
- }
61
- },
62
-
63
- send: async () => {
64
- return adapter.send();
65
- },
66
-
67
- pickUpLocationById: async (locationId) => {
68
- return adapter.pickUpLocationById(locationId);
69
- },
70
-
71
- pickUpLocations: async () => {
72
- return adapter.pickUpLocations();
73
- },
74
- };
75
- },
76
- };
@@ -1,6 +0,0 @@
1
- export enum DeliveryError {
2
- ADAPTER_NOT_FOUND = 'ADAPTER_NOT_FOUND',
3
- NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',
4
- INCOMPLETE_CONFIGURATION = 'INCOMPLETE_CONFIGURATION',
5
- WRONG_CREDENTIALS = 'WRONG_CREDENTIALS',
6
- }
@@ -1,86 +0,0 @@
1
- import { BasePricingAdapter } from '@unchainedshop/utils';
2
- import { DeliveryPricingSheet } from './DeliveryPricingSheet.js';
3
- import {
4
- BasePricingAdapterContext,
5
- IPricingAdapter,
6
- IPricingSheet,
7
- PricingCalculation,
8
- } from '@unchainedshop/utils';
9
- import { DeliveryProvider } from '../types.js';
10
- import { OrderDelivery, OrderDiscount, Order } from '@unchainedshop/core-orders';
11
- import { User } from '@unchainedshop/core-users';
12
-
13
- export interface DeliveryPricingCalculation extends PricingCalculation {
14
- discountId?: string;
15
- isNetPrice: boolean;
16
- isTaxable: boolean;
17
- rate?: number;
18
- }
19
-
20
- export type IDeliveryPricingSheet = IPricingSheet<DeliveryPricingCalculation> & {
21
- addFee: (params: { amount: number; isTaxable: boolean; isNetPrice: boolean; meta?: any }) => void;
22
- addTax: (params: {
23
- amount: number;
24
- rate: number;
25
- baseCategory?: string;
26
- discountId?: string;
27
- meta?: any;
28
- }) => void;
29
- addDiscount: (params: {
30
- amount: number;
31
- isTaxable: boolean;
32
- isNetPrice: boolean;
33
- discountId: string;
34
- meta?: any;
35
- }) => void;
36
- };
37
-
38
- export interface DeliveryPricingAdapterContext extends BasePricingAdapterContext {
39
- country?: string;
40
- currency?: string;
41
- provider: DeliveryProvider;
42
- providerContext?: any;
43
- order: Order;
44
- orderDelivery: OrderDelivery;
45
- user: User;
46
- discounts: Array<OrderDiscount>;
47
- }
48
-
49
- export type IDeliveryPricingAdapter<DiscountConfiguration = unknown> = IPricingAdapter<
50
- DeliveryPricingAdapterContext,
51
- DeliveryPricingCalculation,
52
- IDeliveryPricingSheet,
53
- DiscountConfiguration
54
- >;
55
-
56
- const basePricingAdapter = BasePricingAdapter<
57
- DeliveryPricingAdapterContext,
58
- DeliveryPricingCalculation
59
- >();
60
-
61
- export const DeliveryPricingAdapter: IDeliveryPricingAdapter = {
62
- ...basePricingAdapter,
63
-
64
- isActivatedFor: () => {
65
- return false;
66
- },
67
-
68
- actions: (params) => {
69
- const { context } = params;
70
- const { currency } = context;
71
- const baseActions = basePricingAdapter.actions(params);
72
- const resultSheet = DeliveryPricingSheet({ currency });
73
-
74
- return {
75
- ...baseActions,
76
- calculate: async () => {
77
- const resultRaw = resultSheet.getRawPricingSheet();
78
- resultRaw.forEach(({ amount, category }) =>
79
- basePricingAdapter.log(`Delivery Calculation -> ${category} ${amount}`),
80
- );
81
- return resultRaw;
82
- },
83
- resultSheet: () => resultSheet,
84
- };
85
- },
86
- };
@@ -1,99 +0,0 @@
1
- import { BasePricingDirector } from '@unchainedshop/utils';
2
- import { DeliveryPricingSheet } from './DeliveryPricingSheet.js';
3
- import { DeliveryProvider } from '../types.js';
4
- import { Order } from '@unchainedshop/core-orders';
5
- import { User } from '@unchainedshop/core-users';
6
- import { OrderDelivery } from '@unchainedshop/core-orders';
7
- import { IPricingDirector } from '@unchainedshop/utils';
8
- import {
9
- DeliveryPricingAdapterContext,
10
- DeliveryPricingCalculation,
11
- IDeliveryPricingAdapter,
12
- IDeliveryPricingSheet,
13
- } from './DeliveryPricingAdapter.js';
14
-
15
- export type DeliveryPricingContext =
16
- | {
17
- country?: string;
18
- currency?: string;
19
- provider: DeliveryProvider;
20
- providerContext?: any;
21
- order: Order;
22
- user: User;
23
- }
24
- | { item: OrderDelivery };
25
-
26
- export type IDeliveryPricingDirector<DiscountConfiguration = unknown> = IPricingDirector<
27
- DeliveryPricingContext,
28
- DeliveryPricingCalculation,
29
- DeliveryPricingAdapterContext,
30
- IDeliveryPricingSheet,
31
- IDeliveryPricingAdapter<DiscountConfiguration>
32
- >;
33
-
34
- const baseDirector = BasePricingDirector<
35
- DeliveryPricingContext,
36
- DeliveryPricingAdapterContext,
37
- DeliveryPricingCalculation,
38
- IDeliveryPricingAdapter
39
- >('DeliveryPricingDirector');
40
-
41
- export const DeliveryPricingDirector: IDeliveryPricingDirector = {
42
- ...baseDirector,
43
-
44
- async buildPricingContext(context, unchainedAPI) {
45
- const { modules } = unchainedAPI;
46
-
47
- if ('item' in context) {
48
- const { item } = context;
49
- const order = await modules.orders.findOrder({
50
- orderId: item.orderId,
51
- });
52
- const provider = await modules.delivery.findProvider({
53
- deliveryProviderId: item.deliveryProviderId,
54
- });
55
- const user = await modules.users.findUserById(order.userId);
56
- const discounts = await modules.orders.discounts.findOrderDiscounts({
57
- orderId: item.orderId,
58
- });
59
-
60
- return {
61
- ...unchainedAPI,
62
- country: order.countryCode,
63
- currency: order.currency,
64
- order,
65
- provider,
66
- user,
67
- discounts,
68
- orderDelivery: item,
69
- providerContext: null,
70
- };
71
- }
72
-
73
- return {
74
- ...unchainedAPI,
75
- country: context.country,
76
- currency: context.currency,
77
- order: context.order,
78
- provider: context.provider,
79
- user: context.user,
80
- discounts: [],
81
- orderDelivery: null,
82
- providerContext: context.providerContext,
83
- };
84
- },
85
-
86
- async actions(pricingContext, unchainedAPI) {
87
- const actions = await baseDirector.actions(pricingContext, unchainedAPI, this.buildPricingContext);
88
- return {
89
- ...actions,
90
- calculationSheet() {
91
- const context = actions.getContext();
92
- return DeliveryPricingSheet({
93
- calculation: actions.getCalculation(),
94
- currency: context.currency,
95
- });
96
- },
97
- };
98
- },
99
- };