@unchainedshop/core-warehousing 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 (33) hide show
  1. package/lib/db/TokenSurrogateCollection.d.ts +19 -1
  2. package/lib/db/TokenSurrogateCollection.d.ts.map +1 -1
  3. package/lib/db/TokenSurrogateCollection.js +6 -0
  4. package/lib/db/TokenSurrogateCollection.js.map +1 -1
  5. package/lib/db/WarehousingProvidersCollection.d.ts +15 -2
  6. package/lib/db/WarehousingProvidersCollection.d.ts.map +1 -1
  7. package/lib/db/WarehousingProvidersCollection.js +5 -0
  8. package/lib/db/WarehousingProvidersCollection.js.map +1 -1
  9. package/lib/director/WarehousingDirector.js +1 -1
  10. package/lib/director/WarehousingDirector.js.map +1 -1
  11. package/lib/module/configureWarehousingModule.d.ts +32 -53
  12. package/lib/module/configureWarehousingModule.d.ts.map +1 -1
  13. package/lib/module/configureWarehousingModule.js +23 -139
  14. package/lib/module/configureWarehousingModule.js.map +1 -1
  15. package/lib/types.d.ts +6 -8
  16. package/lib/types.d.ts.map +1 -1
  17. package/lib/types.js.map +1 -1
  18. package/lib/warehousing-index.d.ts +2 -5
  19. package/lib/warehousing-index.d.ts.map +1 -1
  20. package/lib/warehousing-index.js +2 -5
  21. package/lib/warehousing-index.js.map +1 -1
  22. package/package.json +9 -12
  23. package/src/db/TokenSurrogateCollection.ts +21 -1
  24. package/src/db/WarehousingProvidersCollection.ts +15 -2
  25. package/src/module/buildFindSelector.test.ts +1 -1
  26. package/src/module/configureWarehousingModule.ts +65 -289
  27. package/src/warehousing-index.ts +2 -6
  28. package/tsconfig.json +5 -7
  29. package/src/director/WarehousingAdapter.ts +0 -39
  30. package/src/director/WarehousingDirector.ts +0 -153
  31. package/src/director/WarehousingError.ts +0 -6
  32. package/src/director/WarehousingProviderType.ts +0 -4
  33. package/src/types.ts +0 -116
@@ -1,153 +0,0 @@
1
- import { IWarehousingAdapter, IWarehousingDirector, WarehousingContext } from '../types.js';
2
- import { DeliveryDirector } from '@unchainedshop/core-delivery';
3
- import { log, LogLevel } from '@unchainedshop/logger';
4
- import { BaseDirector } from '@unchainedshop/utils';
5
- import { WarehousingError } from './WarehousingError.js';
6
-
7
- const getReferenceDate = (context: WarehousingContext) => {
8
- return context && context.referenceDate ? context.referenceDate : new Date();
9
- };
10
-
11
- const baseDirector = BaseDirector<IWarehousingAdapter>('WarehousingDirector', {
12
- adapterSortKey: 'orderIndex',
13
- });
14
-
15
- export const WarehousingDirector: IWarehousingDirector = {
16
- ...baseDirector,
17
-
18
- actions: async (warehousingProvider, warehousingContext, unchainedAPI) => {
19
- const Adapter = baseDirector.getAdapter(warehousingProvider.adapterKey);
20
-
21
- if (!Adapter) {
22
- throw new Error(`Warehousing Plugin ${warehousingProvider.adapterKey} not available`);
23
- }
24
- const context = {
25
- warehousingProviderId: warehousingProvider._id,
26
- ...warehousingContext,
27
- ...unchainedAPI,
28
- };
29
- const adapter = Adapter.actions(warehousingProvider.configuration, context);
30
-
31
- const throughputTime = async (referenceDate) => {
32
- try {
33
- const { quantity } = context;
34
- const stock = await adapter.stock(referenceDate);
35
- const notInStockQuantity = Math.max(quantity - stock, 0);
36
- const productionTime = await adapter.productionTime(notInStockQuantity);
37
- const commissioningTime = await adapter.commissioningTime(quantity);
38
- return Math.max(commissioningTime + productionTime, 0);
39
- } catch (error) {
40
- log(error.message, { level: LogLevel.Error, ...error });
41
- return NaN;
42
- }
43
- };
44
-
45
- return {
46
- configurationError: () => {
47
- try {
48
- const error = adapter.configurationError();
49
- return error;
50
- } catch {
51
- return WarehousingError.ADAPTER_NOT_FOUND;
52
- }
53
- },
54
-
55
- isActive: () => {
56
- try {
57
- return adapter.isActive();
58
- } catch (error) {
59
- log(error.message, { level: LogLevel.Error });
60
- return false;
61
- }
62
- },
63
-
64
- estimatedStock: async () => {
65
- try {
66
- const referenceDate = getReferenceDate(context);
67
- const quantity = await adapter.stock(referenceDate);
68
- return {
69
- quantity,
70
- };
71
- } catch (error) {
72
- log(error.message, { level: LogLevel.Error, ...error });
73
- return null;
74
- }
75
- },
76
-
77
- estimatedDispatch: async () => {
78
- try {
79
- const { deliveryProvider } = context;
80
- const referenceDate = getReferenceDate(context);
81
-
82
- const warehousingThroughputTime = await throughputTime(referenceDate);
83
- if (!Number.isFinite(warehousingThroughputTime)) {
84
- return {
85
- shipping: null,
86
- earliestDelivery: null,
87
- };
88
- }
89
-
90
- // Calculate shipping date
91
- const shippingTimestamp = referenceDate.getTime() + warehousingThroughputTime;
92
- const shipping = new Date(shippingTimestamp);
93
-
94
- // Calculate earliest delivery date
95
- const actions = await DeliveryDirector.actions(deliveryProvider, context, unchainedAPI);
96
- const deliveryThroughputTime =
97
- await actions.estimatedDeliveryThroughput(warehousingThroughputTime);
98
- const earliestDelivery = Number.isFinite(deliveryThroughputTime)
99
- ? new Date(shippingTimestamp + deliveryThroughputTime)
100
- : null;
101
-
102
- return {
103
- shipping,
104
- earliestDelivery,
105
- };
106
- } catch (error) {
107
- log(error.message, { level: LogLevel.Error, ...error });
108
- return {};
109
- }
110
- },
111
-
112
- tokenMetadata: async (chainTokenId) => {
113
- try {
114
- const referenceDate = getReferenceDate(context);
115
- const tokenMetadata = await adapter.tokenMetadata(chainTokenId, referenceDate);
116
- return tokenMetadata;
117
- } catch (error) {
118
- log(error.message, { level: LogLevel.Error, ...error });
119
- return {};
120
- }
121
- },
122
-
123
- isInvalidateable: async (chainTokenId) => {
124
- try {
125
- const referenceDate = getReferenceDate(context);
126
- const isInvalidateable = await adapter.isInvalidateable(chainTokenId, referenceDate);
127
- return isInvalidateable;
128
- } catch (error) {
129
- log(error.message, { level: LogLevel.Error, ...error });
130
- return false;
131
- }
132
- },
133
-
134
- tokenize: async () => {
135
- try {
136
- const tokens = await adapter.tokenize();
137
- const { order, orderPosition } = warehousingContext;
138
- return tokens.map((token) => {
139
- return {
140
- ...token,
141
- userId: order.userId,
142
- productId: orderPosition.productId,
143
- orderPositionId: orderPosition._id,
144
- };
145
- });
146
- } catch (error) {
147
- log(error.message, { level: LogLevel.Error, ...error });
148
- return [];
149
- }
150
- },
151
- };
152
- },
153
- };
@@ -1,6 +0,0 @@
1
- export enum WarehousingError {
2
- ADAPTER_NOT_FOUND = 'ADAPTER_NOT_FOUND',
3
- NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',
4
- INCOMPLETE_CONFIGURATION = 'INCOMPLETE_CONFIGURATION',
5
- WRONG_CREDENTIALS = 'WRONG_CREDENTIALS',
6
- }
@@ -1,4 +0,0 @@
1
- export enum WarehousingProviderType {
2
- PHYSICAL = 'PHYSICAL',
3
- VIRTUAL = 'VIRTUAL',
4
- }
package/src/types.ts DELETED
@@ -1,116 +0,0 @@
1
- import { IBaseAdapter, IBaseDirector } from '@unchainedshop/utils';
2
- import type { TimestampFields } from '@unchainedshop/mongodb';
3
- import { DeliveryProvider } from '@unchainedshop/core-delivery';
4
- import { Product } from '@unchainedshop/core-products';
5
- import { Order } from '@unchainedshop/core-orders';
6
- import { OrderPosition } from '@unchainedshop/core-orders';
7
- import { UnchainedCore } from '@unchainedshop/core';
8
-
9
- export enum WarehousingProviderType {
10
- PHYSICAL = 'PHYSICAL',
11
- VIRTUAL = 'VIRTUAL',
12
- }
13
-
14
- export type WarehousingConfiguration = Array<{ key: string; value: string }>;
15
-
16
- export type WarehousingProvider = {
17
- _id?: string;
18
- type: WarehousingProviderType;
19
- adapterKey: string;
20
- configuration: WarehousingConfiguration;
21
- } & TimestampFields;
22
-
23
- export type WarehousingProviderQuery = {
24
- type?: WarehousingProviderType;
25
- };
26
-
27
- export enum WarehousingError {
28
- ADAPTER_NOT_FOUND = 'ADAPTER_NOT_FOUND',
29
- NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',
30
- INCOMPLETE_CONFIGURATION = 'INCOMPLETE_CONFIGURATION',
31
- WRONG_CREDENTIALS = 'WRONG_CREDENTIALS',
32
- }
33
-
34
- export type TokenSurrogate = {
35
- _id?: string;
36
- userId?: string;
37
- walletAddress?: string;
38
- invalidatedDate?: Date;
39
- expiryDate?: Date;
40
- quantity: number;
41
- contractAddress: string;
42
- chainId: string;
43
- chainTokenId: string;
44
- productId: string;
45
- orderPositionId: string;
46
- meta: any;
47
- };
48
-
49
- export enum TokenStatus {
50
- CENTRALIZED = 'CENTRALIZED',
51
- EXPORTING = 'EXPORTING',
52
- DECENTRALIZED = 'DECENTRALIZED',
53
- }
54
-
55
- export interface WarehousingContext {
56
- deliveryProvider?: DeliveryProvider;
57
- product?: Product;
58
- token?: TokenSurrogate;
59
- quantity?: number;
60
- referenceDate?: Date;
61
- locale?: Intl.Locale;
62
- order?: Order;
63
- warehousingProviderId?: string;
64
- orderPosition?: OrderPosition;
65
- }
66
-
67
- export type EstimatedDispatch = {
68
- shipping?: Date;
69
- earliestDelivery?: Date;
70
- };
71
-
72
- export type EstimatedStock = { quantity: number } | null;
73
-
74
- export type WarehousingAdapterActions = {
75
- configurationError: () => WarehousingError;
76
- isActive: () => boolean;
77
- stock: (referenceDate: Date) => Promise<number>;
78
- productionTime: (quantityToProduce: number) => Promise<number>;
79
- commissioningTime: (quantity: number) => Promise<number>;
80
- tokenize: () => Promise<Array<Omit<TokenSurrogate, 'userId' | 'productId' | 'orderPositionId'>>>;
81
- tokenMetadata: (chainTokenId: string, referenceDate: Date) => Promise<any>;
82
- isInvalidateable: (chainTokenId: string, referenceDate: Date) => Promise<boolean>;
83
- };
84
-
85
- export type IWarehousingAdapter = IBaseAdapter & {
86
- orderIndex: number;
87
- initialConfiguration: WarehousingConfiguration;
88
- typeSupported: (type: WarehousingProviderType) => boolean;
89
-
90
- actions: (
91
- config: WarehousingConfiguration,
92
- context: WarehousingContext & UnchainedCore,
93
- ) => WarehousingAdapterActions;
94
- };
95
-
96
- export type IWarehousingDirector = IBaseDirector<IWarehousingAdapter> & {
97
- actions: (
98
- warehousingProvider: WarehousingProvider,
99
- warehousingContext: WarehousingContext,
100
- unchainedAPI: UnchainedCore,
101
- ) => Promise<{
102
- configurationError: () => WarehousingError;
103
- isActive: () => boolean;
104
- estimatedStock: () => Promise<EstimatedStock>;
105
- estimatedDispatch: () => Promise<EstimatedDispatch>;
106
- tokenize: () => Promise<Array<TokenSurrogate>>;
107
- tokenMetadata: (chainTokenId: string) => Promise<any>;
108
- isInvalidateable: (chainTokenId: string) => Promise<boolean>;
109
- }>;
110
- };
111
-
112
- export interface WarehousingInterface {
113
- _id: string;
114
- label: string;
115
- version: string;
116
- }