@unchainedshop/core-warehousing 5.0.0-alpha.1 → 5.0.0-alpha.10
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.
package/README.md
CHANGED
|
@@ -13,81 +13,23 @@ npm install @unchainedshop/core-warehousing
|
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
import { configureWarehousingModule, WarehousingProviderType } from '@unchainedshop/core-warehousing';
|
|
16
|
+
The platform initializes this module as `platform.unchainedAPI.modules.warehousing`. Register a warehousing plugin before platform startup; the base preset includes the Store adapter.
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
```typescript
|
|
19
|
+
import { WarehousingProviderType } from '@unchainedshop/core-warehousing';
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
const
|
|
21
|
+
const { warehousing } = platform.unchainedAPI.modules;
|
|
22
|
+
const provider = await warehousing.create({
|
|
23
23
|
type: WarehousingProviderType.PHYSICAL,
|
|
24
|
-
adapterKey: 'shop.unchained.warehousing.
|
|
24
|
+
adapterKey: 'shop.unchained.warehousing.store',
|
|
25
|
+
configuration: [],
|
|
25
26
|
});
|
|
26
|
-
|
|
27
|
-
// Find providers
|
|
28
|
-
const providers = await warehousingModule.findProviders({});
|
|
27
|
+
const tokens = await warehousing.findTokensForUser({ userId: 'user-123' });
|
|
29
28
|
```
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
### Module Configuration
|
|
34
|
-
|
|
35
|
-
| Export | Description |
|
|
36
|
-
|--------|-------------|
|
|
37
|
-
| `configureWarehousingModule` | Configure and return the warehousing module |
|
|
38
|
-
|
|
39
|
-
### Queries
|
|
40
|
-
|
|
41
|
-
| Method | Description |
|
|
42
|
-
|--------|-------------|
|
|
43
|
-
| `findProvider` | Find provider by ID |
|
|
44
|
-
| `findProviders` | Find providers with filtering |
|
|
45
|
-
| `count` | Count providers |
|
|
46
|
-
| `providerExists` | Check if provider exists |
|
|
47
|
-
| `findSupported` | Find providers for product context |
|
|
48
|
-
| `findInterface` | Get provider interface definition |
|
|
49
|
-
|
|
50
|
-
### Mutations
|
|
51
|
-
|
|
52
|
-
| Method | Description |
|
|
53
|
-
|--------|-------------|
|
|
54
|
-
| `create` | Create a new warehousing provider |
|
|
55
|
-
| `update` | Update provider configuration |
|
|
56
|
-
| `delete` | Soft delete a provider |
|
|
57
|
-
|
|
58
|
-
### Token Surrogates
|
|
59
|
-
|
|
60
|
-
For tokenized products (NFTs):
|
|
61
|
-
|
|
62
|
-
| Method | Description |
|
|
63
|
-
|--------|-------------|
|
|
64
|
-
| `findTokenSurrogate` | Find token surrogate |
|
|
65
|
-
| `createTokenSurrogate` | Create token surrogate |
|
|
66
|
-
| `updateTokenSurrogate` | Update token surrogate |
|
|
67
|
-
| `deleteTokenSurrogate` | Delete token surrogate |
|
|
68
|
-
| `invalidateTokenSurrogates` | Invalidate surrogates for product |
|
|
69
|
-
|
|
70
|
-
### Constants
|
|
71
|
-
|
|
72
|
-
| Export | Description |
|
|
73
|
-
|--------|-------------|
|
|
74
|
-
| `WarehousingProviderType` | Provider types (PHYSICAL, VIRTUAL) |
|
|
75
|
-
|
|
76
|
-
### Types
|
|
77
|
-
|
|
78
|
-
| Export | Description |
|
|
79
|
-
|--------|-------------|
|
|
80
|
-
| `WarehousingProvider` | Provider document type |
|
|
81
|
-
| `TokenSurrogate` | Token surrogate document type |
|
|
82
|
-
| `WarehousingModule` | Module interface type |
|
|
83
|
-
|
|
84
|
-
## Events
|
|
30
|
+
Provider records and tokens live in this module. Registered warehousing adapters implement stock and availability; `unchainedAPI.services.products` exposes inventory and dispatch simulations, and `unchainedAPI.services.warehousing` provides token helpers.
|
|
85
31
|
|
|
86
|
-
|
|
87
|
-
|-------|-------------|
|
|
88
|
-
| `WAREHOUSING_PROVIDER_CREATE` | Provider created |
|
|
89
|
-
| `WAREHOUSING_PROVIDER_UPDATE` | Provider updated |
|
|
90
|
-
| `WAREHOUSING_PROVIDER_REMOVE` | Provider deleted |
|
|
32
|
+
See the [module guide](https://docs.unchained.shop/platform-configuration/modules/warehousing), [public exports](src/warehousing-index.ts), and [module implementation](src/module/configureWarehousingModule.ts).
|
|
91
33
|
|
|
92
34
|
## License
|
|
93
35
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildDbIndexes
|
|
1
|
+
import { buildDbIndexes } from '@unchainedshop/mongodb';
|
|
2
2
|
export const TokenStatus = {
|
|
3
3
|
CENTRALIZED: 'CENTRALIZED',
|
|
4
4
|
EXPORTING: 'EXPORTING',
|
|
@@ -6,53 +6,36 @@ export const TokenStatus = {
|
|
|
6
6
|
};
|
|
7
7
|
export const TokenSurrogateCollection = async (db) => {
|
|
8
8
|
const TokenSurrogates = db.collection('token_surrogates');
|
|
9
|
-
if (!isDocumentDBCompatModeEnabled()) {
|
|
10
|
-
await buildDbIndexes(TokenSurrogates, [
|
|
11
|
-
{
|
|
12
|
-
index: {
|
|
13
|
-
tokenSerialNumber: 'text',
|
|
14
|
-
userId: 'text',
|
|
15
|
-
productId: 'text',
|
|
16
|
-
_id: 'text',
|
|
17
|
-
walletAddress: 'text',
|
|
18
|
-
contractAddress: 'text',
|
|
19
|
-
},
|
|
20
|
-
options: {
|
|
21
|
-
weights: {
|
|
22
|
-
_id: 9,
|
|
23
|
-
tokenSerialNumber: 8,
|
|
24
|
-
userId: 3,
|
|
25
|
-
productId: 6,
|
|
26
|
-
contractAddress: 5,
|
|
27
|
-
walletAddress: 4,
|
|
28
|
-
status: 1,
|
|
29
|
-
},
|
|
30
|
-
name: 'token_fulltext_search',
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
], { rebuild: true });
|
|
34
|
-
}
|
|
35
9
|
await buildDbIndexes(TokenSurrogates, [
|
|
36
10
|
{
|
|
37
11
|
index: {
|
|
38
|
-
tokenSerialNumber:
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
index: {
|
|
48
|
-
productId: 1,
|
|
12
|
+
tokenSerialNumber: 'text',
|
|
13
|
+
userId: 'text',
|
|
14
|
+
productId: 'text',
|
|
15
|
+
_id: 'text',
|
|
16
|
+
walletAddress: 'text',
|
|
17
|
+
contractAddress: 'text',
|
|
49
18
|
},
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
19
|
+
options: {
|
|
20
|
+
weights: {
|
|
21
|
+
_id: 9,
|
|
22
|
+
tokenSerialNumber: 8,
|
|
23
|
+
userId: 3,
|
|
24
|
+
productId: 6,
|
|
25
|
+
contractAddress: 5,
|
|
26
|
+
walletAddress: 4,
|
|
27
|
+
status: 1,
|
|
28
|
+
},
|
|
29
|
+
name: 'token_fulltext_search',
|
|
54
30
|
},
|
|
55
31
|
},
|
|
32
|
+
], { rebuild: true });
|
|
33
|
+
await buildDbIndexes(TokenSurrogates, [
|
|
34
|
+
{ index: { tokenSerialNumber: 1 } },
|
|
35
|
+
{ index: { userId: 1 } },
|
|
36
|
+
{ index: { orderPositionId: 1 } },
|
|
37
|
+
{ index: { walletAddress: 1 }, options: { sparse: true } },
|
|
38
|
+
{ index: { productId: 1, 'meta.cancelled': 1 }, options: { sparse: true } },
|
|
56
39
|
]);
|
|
57
40
|
return TokenSurrogates;
|
|
58
41
|
};
|
|
@@ -6,21 +6,8 @@ export const WarehousingProviderType = {
|
|
|
6
6
|
export const WarehousingProvidersCollection = async (db) => {
|
|
7
7
|
const WarehousingProviders = db.collection('warehousing-providers');
|
|
8
8
|
await buildDbIndexes(WarehousingProviders, [
|
|
9
|
-
{
|
|
10
|
-
|
|
11
|
-
type: 1,
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
index: {
|
|
16
|
-
created: 1,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
index: {
|
|
21
|
-
deleted: 1,
|
|
22
|
-
},
|
|
23
|
-
},
|
|
9
|
+
{ index: { deleted: 1, type: 1 } },
|
|
10
|
+
{ index: { deleted: 1, created: 1 } },
|
|
24
11
|
]);
|
|
25
12
|
return WarehousingProviders;
|
|
26
13
|
};
|
|
@@ -31,9 +31,7 @@ export declare const configureWarehousingModule: ({ db }: ModuleInput<Record<str
|
|
|
31
31
|
walletAddresses: string[];
|
|
32
32
|
}, options?: mongodb.FindOptions) => Promise<TokenSurrogate[]>;
|
|
33
33
|
findProviders: (query?: WarehousingProviderQuery, options?: mongodb.FindOptions) => Promise<WarehousingProvider[]>;
|
|
34
|
-
allProviders: (
|
|
35
|
-
clear: () => void;
|
|
36
|
-
};
|
|
34
|
+
allProviders: import("@unchainedshop/utils/lib/memoize-with-ttl.js").MemoizedWithTTL<[], mongodb.WithId<WarehousingProvider>[]>;
|
|
37
35
|
providerExists: ({ warehousingProviderId, }: {
|
|
38
36
|
warehousingProviderId: string;
|
|
39
37
|
}) => Promise<boolean>;
|
|
@@ -45,7 +43,7 @@ export declare const configureWarehousingModule: ({ db }: ModuleInput<Record<str
|
|
|
45
43
|
walletAddress: string;
|
|
46
44
|
}) => Promise<mongodb.WithId<TokenSurrogate> | null>;
|
|
47
45
|
invalidateToken: (tokenId: string) => Promise<mongodb.WithId<TokenSurrogate> | null>;
|
|
48
|
-
|
|
46
|
+
buildAccessKeyFromToken: (token: TokenSurrogate) => Promise<string>;
|
|
49
47
|
create: (doc: Omit<WarehousingProvider, "_id" | "created"> & Pick<Partial<WarehousingProvider>, "_id">) => Promise<WarehousingProvider>;
|
|
50
48
|
update: (warehousingProviderId: string, doc: Partial<WarehousingProvider>) => Promise<mongodb.WithId<WarehousingProvider> | null>;
|
|
51
49
|
delete: (providerId: string) => Promise<mongodb.WithId<WarehousingProvider> | null>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
|
-
import { generateDbFilterById, generateDbObjectId, escapeRegexString,
|
|
2
|
+
import { generateDbFilterById, generateDbObjectId, escapeRegexString, } from '@unchainedshop/mongodb';
|
|
3
3
|
import { WarehousingProvidersCollection, } from "../db/WarehousingProvidersCollection.js";
|
|
4
4
|
import { TokenSurrogateCollection } from "../db/TokenSurrogateCollection.js";
|
|
5
5
|
import { memoizeWithTTL } from '@unchainedshop/utils';
|
|
@@ -10,7 +10,6 @@ const WAREHOUSING_PROVIDER_EVENTS = [
|
|
|
10
10
|
'TOKEN_OWNERSHIP_CHANGED',
|
|
11
11
|
'TOKEN_INVALIDATED',
|
|
12
12
|
];
|
|
13
|
-
const allProvidersTtlMs = process.env.NODE_ENV === 'production' ? 60000 : 1;
|
|
14
13
|
export const buildFindSelector = ({ includeDeleted = false, queryString, warehousingProviderIds, type, } = {}) => {
|
|
15
14
|
const selector = includeDeleted ? {} : { deleted: null };
|
|
16
15
|
if (warehousingProviderIds) {
|
|
@@ -28,7 +27,6 @@ export const buildFindSelector = ({ includeDeleted = false, queryString, warehou
|
|
|
28
27
|
export const buildTokenFindSelector = ({ queryString, walletAddressExists, ...rest }) => {
|
|
29
28
|
const selector = { ...(rest || {}) };
|
|
30
29
|
if (queryString) {
|
|
31
|
-
assertDocumentDBCompatMode();
|
|
32
30
|
selector.$text = { $search: queryString };
|
|
33
31
|
}
|
|
34
32
|
if (walletAddressExists !== undefined) {
|
|
@@ -39,10 +37,8 @@ export const buildTokenFindSelector = ({ queryString, walletAddressExists, ...re
|
|
|
39
37
|
export const configureWarehousingModule = async ({ db }) => {
|
|
40
38
|
registerEvents(WAREHOUSING_PROVIDER_EVENTS);
|
|
41
39
|
const WarehousingProviders = await WarehousingProvidersCollection(db);
|
|
40
|
+
const allProviders = memoizeWithTTL(async () => WarehousingProviders.find({ deleted: null }, { sort: { created: 1, _id: 1 } }).toArray(), { ttl: process.env.NODE_ENV === 'production' ? 60000 : 1 });
|
|
42
41
|
const TokenSurrogates = await TokenSurrogateCollection(db);
|
|
43
|
-
const allProviders = memoizeWithTTL(async function () {
|
|
44
|
-
return WarehousingProviders.find({ deleted: null }, { sort: { created: 1 } }).toArray();
|
|
45
|
-
}, allProvidersTtlMs);
|
|
46
42
|
return {
|
|
47
43
|
count: async (query = {}) => {
|
|
48
44
|
const providerCount = await WarehousingProviders.countDocuments(buildFindSelector(query));
|
|
@@ -81,7 +77,7 @@ export const configureWarehousingModule = async ({ db }) => {
|
|
|
81
77
|
const userTokens = await TokenSurrogates.find(selector, options).toArray();
|
|
82
78
|
return userTokens;
|
|
83
79
|
},
|
|
84
|
-
findProviders: async (query = {}, options = { sort: { created: 1 } }) => {
|
|
80
|
+
findProviders: async (query = {}, options = { sort: { created: 1, _id: 1 } }) => {
|
|
85
81
|
const providers = WarehousingProviders.find(buildFindSelector(query), options);
|
|
86
82
|
return providers.toArray();
|
|
87
83
|
},
|
|
@@ -129,10 +125,7 @@ export const configureWarehousingModule = async ({ db }) => {
|
|
|
129
125
|
await emit('TOKEN_INVALIDATED', { token });
|
|
130
126
|
return token;
|
|
131
127
|
},
|
|
132
|
-
|
|
133
|
-
const token = await TokenSurrogates.findOne(generateDbFilterById(tokenId));
|
|
134
|
-
if (!token)
|
|
135
|
-
return null;
|
|
128
|
+
buildAccessKeyFromToken: async (token) => {
|
|
136
129
|
const payload = [
|
|
137
130
|
token._id,
|
|
138
131
|
token.walletAddress || token.userId,
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-warehousing",
|
|
3
3
|
"description": "Inventory and stock management module for the Unchained Engine",
|
|
4
|
-
"version": "5.0.0-alpha.
|
|
4
|
+
"version": "5.0.0-alpha.10",
|
|
5
|
+
"files": [
|
|
6
|
+
"lib"
|
|
7
|
+
],
|
|
5
8
|
"main": "lib/warehousing-index.js",
|
|
6
9
|
"types": "lib/warehousing-index.d.ts",
|
|
7
10
|
"type": "module",
|
|
@@ -35,12 +38,12 @@
|
|
|
35
38
|
},
|
|
36
39
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
37
40
|
"dependencies": {
|
|
38
|
-
"@unchainedshop/events": "^5.0.0-alpha.
|
|
39
|
-
"@unchainedshop/
|
|
40
|
-
"@unchainedshop/utils": "^5.0.0-alpha.
|
|
41
|
+
"@unchainedshop/events": "^5.0.0-alpha.10",
|
|
42
|
+
"@unchainedshop/mongodb": "^5.0.0-alpha.10",
|
|
43
|
+
"@unchainedshop/utils": "^5.0.0-alpha.10"
|
|
41
44
|
},
|
|
42
45
|
"devDependencies": {
|
|
43
|
-
"@types/node": "^
|
|
44
|
-
"typescript": "^
|
|
46
|
+
"@types/node": "^26.2.0",
|
|
47
|
+
"typescript": "^6.0.3"
|
|
45
48
|
}
|
|
46
49
|
}
|