@reactionary/source 0.0.52 → 0.2.16
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/.env-template +19 -0
- package/.github/workflows/pull-request.yml +3 -1
- package/.github/workflows/release.yml +9 -0
- package/.vscode/extensions.json +0 -2
- package/LICENSE +21 -0
- package/README.md +175 -23
- package/core/package.json +6 -3
- package/core/src/cache/cache.interface.ts +1 -0
- package/core/src/cache/index.ts +4 -0
- package/core/src/cache/memory-cache.ts +30 -2
- package/core/src/cache/noop-cache.ts +15 -1
- package/core/src/cache/redis-cache.ts +20 -0
- package/core/src/client/client-builder.ts +71 -54
- package/core/src/client/client.ts +9 -47
- package/core/src/client/index.ts +2 -0
- package/core/src/decorators/index.ts +1 -0
- package/core/src/decorators/reactionary.decorator.ts +203 -34
- package/core/src/index.ts +6 -19
- package/core/src/initialization.ts +1 -18
- package/core/src/metrics/metrics.ts +67 -0
- package/core/src/providers/analytics.provider.ts +1 -6
- package/core/src/providers/base.provider.ts +5 -69
- package/core/src/providers/cart.provider.ts +15 -55
- package/core/src/providers/category.provider.ts +7 -11
- package/core/src/providers/checkout.provider.ts +17 -15
- package/core/src/providers/identity.provider.ts +6 -8
- package/core/src/providers/index.ts +2 -1
- package/core/src/providers/inventory.provider.ts +15 -5
- package/core/src/providers/order-search.provider.ts +29 -0
- package/core/src/providers/order.provider.ts +47 -15
- package/core/src/providers/price.provider.ts +30 -36
- package/core/src/providers/product-search.provider.ts +61 -0
- package/core/src/providers/product.provider.ts +71 -12
- package/core/src/providers/profile.provider.ts +74 -14
- package/core/src/providers/store.provider.ts +3 -5
- package/core/src/schemas/capabilities.schema.ts +10 -3
- package/core/src/schemas/errors/generic.error.ts +9 -0
- package/core/src/schemas/errors/index.ts +4 -0
- package/core/src/schemas/errors/invalid-input.error.ts +9 -0
- package/core/src/schemas/errors/invalid-output.error.ts +9 -0
- package/core/src/schemas/errors/not-found.error.ts +9 -0
- package/core/src/schemas/index.ts +7 -0
- package/core/src/schemas/models/analytics.model.ts +2 -1
- package/core/src/schemas/models/base.model.ts +6 -24
- package/core/src/schemas/models/cart.model.ts +5 -8
- package/core/src/schemas/models/category.model.ts +4 -9
- package/core/src/schemas/models/checkout.model.ts +6 -7
- package/core/src/schemas/models/cost.model.ts +4 -3
- package/core/src/schemas/models/currency.model.ts +2 -1
- package/core/src/schemas/models/identifiers.model.ts +106 -62
- package/core/src/schemas/models/identity.model.ts +10 -19
- package/core/src/schemas/models/index.ts +2 -1
- package/core/src/schemas/models/inventory.model.ts +8 -5
- package/core/src/schemas/models/order-search.model.ts +28 -0
- package/core/src/schemas/models/order.model.ts +20 -26
- package/core/src/schemas/models/payment.model.ts +14 -17
- package/core/src/schemas/models/price.model.ts +11 -11
- package/core/src/schemas/models/product-search.model.ts +42 -0
- package/core/src/schemas/models/product.model.ts +64 -22
- package/core/src/schemas/models/profile.model.ts +19 -22
- package/core/src/schemas/models/shipping-method.model.ts +24 -29
- package/core/src/schemas/models/store.model.ts +9 -5
- package/core/src/schemas/mutations/analytics.mutation.ts +8 -7
- package/core/src/schemas/mutations/base.mutation.ts +2 -1
- package/core/src/schemas/mutations/cart.mutation.ts +33 -33
- package/core/src/schemas/mutations/checkout.mutation.ts +23 -30
- package/core/src/schemas/mutations/identity.mutation.ts +4 -3
- package/core/src/schemas/mutations/profile.mutation.ts +38 -3
- package/core/src/schemas/queries/base.query.ts +2 -1
- package/core/src/schemas/queries/cart.query.ts +3 -3
- package/core/src/schemas/queries/category.query.ts +18 -18
- package/core/src/schemas/queries/checkout.query.ts +7 -9
- package/core/src/schemas/queries/identity.query.ts +2 -1
- package/core/src/schemas/queries/index.ts +2 -1
- package/core/src/schemas/queries/inventory.query.ts +5 -5
- package/core/src/schemas/queries/order-search.query.ts +10 -0
- package/core/src/schemas/queries/order.query.ts +3 -2
- package/core/src/schemas/queries/price.query.ts +10 -4
- package/core/src/schemas/queries/product-search.query.ts +16 -0
- package/core/src/schemas/queries/product.query.ts +13 -6
- package/core/src/schemas/queries/profile.query.ts +5 -2
- package/core/src/schemas/queries/store.query.ts +6 -5
- package/core/src/schemas/result.ts +107 -0
- package/core/src/schemas/session.schema.ts +4 -4
- package/core/src/test/reactionary.decorator.spec.ts +249 -0
- package/core/src/zod-utils.ts +19 -0
- package/core/tsconfig.json +1 -1
- package/core/tsconfig.spec.json +2 -26
- package/core/vitest.config.ts +14 -0
- package/documentation/1-purpose.md +114 -0
- package/documentation/2-getting-started.md +229 -0
- package/documentation/3-querying-and-changing-data.md +74 -0
- package/documentation/4-product-data.md +107 -0
- package/documentation/5-cart-and-checkout.md +211 -0
- package/documentation/6-product-search.md +143 -0
- package/documentation/7-marketing.md +3 -0
- package/eslint.config.mjs +1 -0
- package/examples/node/eslint.config.mjs +1 -4
- package/examples/node/package.json +10 -3
- package/examples/node/project.json +4 -1
- package/examples/node/src/basic/basic-node-provider-model-extension.spec.ts +22 -23
- package/examples/node/src/basic/basic-node-provider-query-extension.spec.ts +15 -11
- package/examples/node/src/basic/basic-node-setup.spec.ts +44 -28
- package/examples/node/src/basic/client-creation.spec.ts +53 -0
- package/examples/node/src/capabilities/cart.spec.ts +255 -0
- package/examples/node/src/capabilities/category.spec.ts +193 -0
- package/examples/node/src/capabilities/checkout.spec.ts +341 -0
- package/examples/node/src/capabilities/identity.spec.ts +93 -0
- package/examples/node/src/capabilities/inventory.spec.ts +66 -0
- package/examples/node/src/capabilities/order-search.spec.ts +265 -0
- package/examples/node/src/capabilities/order.spec.ts +91 -0
- package/examples/node/src/capabilities/price.spec.ts +51 -0
- package/examples/node/src/capabilities/product-search.spec.ts +293 -0
- package/examples/node/src/capabilities/product.spec.ts +122 -0
- package/examples/node/src/capabilities/profile.spec.ts +316 -0
- package/examples/node/src/capabilities/store.spec.ts +26 -0
- package/examples/node/src/utils.ts +147 -0
- package/examples/node/tsconfig.json +9 -12
- package/examples/node/tsconfig.lib.json +1 -2
- package/examples/node/tsconfig.spec.json +2 -14
- package/examples/node/vitest.config.ts +14 -0
- package/migrations.json +22 -5
- package/nx.json +8 -47
- package/package.json +24 -96
- package/providers/algolia/README.md +39 -2
- package/providers/algolia/package.json +2 -1
- package/providers/algolia/src/core/initialize.ts +7 -14
- package/providers/algolia/src/index.ts +2 -4
- package/providers/algolia/src/providers/index.ts +1 -0
- package/providers/algolia/src/providers/product-search.provider.ts +241 -0
- package/providers/algolia/src/schema/capabilities.schema.ts +2 -3
- package/providers/algolia/src/schema/index.ts +3 -0
- package/providers/algolia/src/schema/search.schema.ts +8 -8
- package/providers/algolia/tsconfig.json +1 -1
- package/providers/algolia/tsconfig.lib.json +1 -1
- package/providers/algolia/tsconfig.spec.json +2 -14
- package/providers/algolia/vitest.config.ts +14 -0
- package/providers/commercetools/README.md +30 -3
- package/providers/commercetools/package.json +2 -1
- package/providers/commercetools/src/core/client.ts +178 -99
- package/providers/commercetools/src/core/initialize.ts +130 -74
- package/providers/commercetools/src/core/token-cache.ts +45 -0
- package/providers/commercetools/src/index.ts +3 -2
- package/providers/commercetools/src/providers/cart.provider.ts +281 -341
- package/providers/commercetools/src/providers/category.provider.ts +223 -138
- package/providers/commercetools/src/providers/checkout.provider.ts +631 -449
- package/providers/commercetools/src/providers/identity.provider.ts +50 -29
- package/providers/commercetools/src/providers/index.ts +2 -2
- package/providers/commercetools/src/providers/inventory.provider.ts +76 -74
- package/providers/commercetools/src/providers/order-search.provider.ts +220 -0
- package/providers/commercetools/src/providers/order.provider.ts +96 -61
- package/providers/commercetools/src/providers/price.provider.ts +147 -117
- package/providers/commercetools/src/providers/product-search.provider.ts +528 -0
- package/providers/commercetools/src/providers/product.provider.ts +249 -74
- package/providers/commercetools/src/providers/profile.provider.ts +445 -28
- package/providers/commercetools/src/providers/store.provider.ts +54 -40
- package/providers/commercetools/src/schema/capabilities.schema.ts +3 -1
- package/providers/commercetools/src/schema/commercetools.schema.ts +17 -3
- package/providers/commercetools/src/schema/configuration.schema.ts +1 -0
- package/providers/commercetools/src/schema/session.schema.ts +7 -0
- package/providers/commercetools/src/test/caching.spec.ts +82 -0
- package/providers/commercetools/src/test/identity.spec.ts +109 -0
- package/providers/commercetools/src/test/test-utils.ts +21 -19
- package/providers/commercetools/tsconfig.json +1 -1
- package/providers/commercetools/tsconfig.lib.json +1 -1
- package/providers/commercetools/tsconfig.spec.json +2 -14
- package/providers/commercetools/vitest.config.ts +15 -0
- package/providers/fake/README.md +20 -4
- package/providers/fake/package.json +2 -1
- package/providers/fake/src/core/initialize.ts +47 -49
- package/providers/fake/src/providers/analytics.provider.ts +5 -7
- package/providers/fake/src/providers/cart.provider.ts +163 -92
- package/providers/fake/src/providers/category.provider.ts +78 -50
- package/providers/fake/src/providers/checkout.provider.ts +254 -0
- package/providers/fake/src/providers/identity.provider.ts +57 -65
- package/providers/fake/src/providers/index.ts +6 -2
- package/providers/fake/src/providers/inventory.provider.ts +40 -36
- package/providers/fake/src/providers/order-search.provider.ts +78 -0
- package/providers/fake/src/providers/order.provider.ts +106 -0
- package/providers/fake/src/providers/price.provider.ts +93 -41
- package/providers/fake/src/providers/product-search.provider.ts +206 -0
- package/providers/fake/src/providers/product.provider.ts +56 -41
- package/providers/fake/src/providers/profile.provider.ts +147 -0
- package/providers/fake/src/providers/store.provider.ts +30 -20
- package/providers/fake/src/schema/capabilities.schema.ts +5 -1
- package/providers/fake/src/test/cart.provider.spec.ts +59 -80
- package/providers/fake/src/test/category.provider.spec.ts +145 -87
- package/providers/fake/src/test/checkout.provider.spec.ts +222 -0
- package/providers/fake/src/test/order-search.provider.spec.ts +50 -0
- package/providers/fake/src/test/order.provider.spec.ts +44 -0
- package/providers/fake/src/test/price.provider.spec.ts +50 -45
- package/providers/fake/src/test/product.provider.spec.ts +15 -7
- package/providers/fake/src/test/profile.provider.spec.ts +167 -0
- package/providers/fake/tsconfig.json +1 -1
- package/providers/fake/tsconfig.lib.json +1 -1
- package/providers/fake/tsconfig.spec.json +2 -12
- package/providers/fake/vitest.config.ts +14 -0
- package/providers/medusa/README.md +30 -0
- package/providers/medusa/TESTING.md +98 -0
- package/providers/medusa/eslint.config.mjs +19 -0
- package/providers/medusa/package.json +22 -0
- package/providers/medusa/project.json +34 -0
- package/providers/medusa/src/core/client.ts +370 -0
- package/providers/medusa/src/core/initialize.ts +78 -0
- package/providers/medusa/src/index.ts +13 -0
- package/providers/medusa/src/providers/cart.provider.ts +575 -0
- package/providers/medusa/src/providers/category.provider.ts +247 -0
- package/providers/medusa/src/providers/checkout.provider.ts +636 -0
- package/providers/medusa/src/providers/identity.provider.ts +137 -0
- package/providers/medusa/src/providers/inventory.provider.ts +173 -0
- package/providers/medusa/src/providers/order-search.provider.ts +202 -0
- package/providers/medusa/src/providers/order.provider.ts +226 -0
- package/providers/medusa/src/providers/price.provider.ts +140 -0
- package/providers/medusa/src/providers/product-search.provider.ts +243 -0
- package/providers/medusa/src/providers/product.provider.ts +261 -0
- package/providers/medusa/src/providers/profile.provider.ts +392 -0
- package/providers/medusa/src/schema/capabilities.schema.ts +18 -0
- package/providers/medusa/src/schema/configuration.schema.ts +11 -0
- package/providers/medusa/src/schema/medusa.schema.ts +31 -0
- package/providers/medusa/src/test/cart.provider.spec.ts +240 -0
- package/providers/medusa/src/test/category.provider.spec.ts +231 -0
- package/providers/medusa/src/test/checkout.spec.ts +349 -0
- package/providers/medusa/src/test/identity.provider.spec.ts +122 -0
- package/providers/medusa/src/test/inventory.provider.spec.ts +88 -0
- package/providers/medusa/src/test/large-cart.provider.spec.ts +103 -0
- package/providers/medusa/src/test/price.provider.spec.ts +104 -0
- package/providers/medusa/src/test/product.provider.spec.ts +146 -0
- package/providers/medusa/src/test/search.provider.spec.ts +203 -0
- package/providers/medusa/src/test/test-utils.ts +13 -0
- package/providers/medusa/src/utils/medusa-helpers.ts +89 -0
- package/providers/medusa/tsconfig.json +21 -0
- package/providers/medusa/tsconfig.lib.json +9 -0
- package/providers/medusa/tsconfig.spec.json +4 -0
- package/providers/medusa/vitest.config.ts +15 -0
- package/providers/meilisearch/README.md +48 -0
- package/providers/meilisearch/eslint.config.mjs +22 -0
- package/providers/meilisearch/package.json +13 -0
- package/providers/meilisearch/project.json +34 -0
- package/providers/meilisearch/src/core/initialize.ts +21 -0
- package/providers/meilisearch/src/index.ts +6 -0
- package/providers/meilisearch/src/providers/index.ts +1 -0
- package/providers/meilisearch/src/providers/order-search.provider.ts +222 -0
- package/providers/meilisearch/src/providers/product-search.provider.ts +251 -0
- package/providers/meilisearch/src/schema/capabilities.schema.ts +10 -0
- package/providers/meilisearch/src/schema/configuration.schema.ts +11 -0
- package/providers/meilisearch/src/schema/index.ts +3 -0
- package/providers/meilisearch/src/schema/search.schema.ts +14 -0
- package/providers/meilisearch/tsconfig.json +24 -0
- package/providers/meilisearch/tsconfig.lib.json +10 -0
- package/providers/meilisearch/tsconfig.spec.json +4 -0
- package/providers/meilisearch/vitest.config.ts +14 -0
- package/providers/posthog/package.json +2 -1
- package/providers/posthog/tsconfig.json +1 -1
- package/tsconfig.base.json +5 -0
- package/vitest.config.ts +10 -0
- package/core/src/providers/search.provider.ts +0 -18
- package/core/src/schemas/models/search.model.ts +0 -36
- package/core/src/schemas/queries/search.query.ts +0 -9
- package/examples/next/.swcrc +0 -30
- package/examples/next/eslint.config.mjs +0 -21
- package/examples/next/index.d.ts +0 -6
- package/examples/next/next-env.d.ts +0 -5
- package/examples/next/next.config.js +0 -31
- package/examples/next/project.json +0 -9
- package/examples/next/public/.gitkeep +0 -0
- package/examples/next/public/favicon.ico +0 -0
- package/examples/next/src/app/global.css +0 -0
- package/examples/next/src/app/layout.tsx +0 -18
- package/examples/next/src/app/page.module.scss +0 -2
- package/examples/next/src/app/page.tsx +0 -47
- package/examples/next/src/instrumentation.ts +0 -9
- package/examples/next/tsconfig.json +0 -44
- package/examples/node/jest.config.ts +0 -10
- package/jest.config.ts +0 -6
- package/jest.preset.js +0 -3
- package/providers/algolia/jest.config.ts +0 -10
- package/providers/algolia/src/providers/product.provider.ts +0 -66
- package/providers/algolia/src/providers/search.provider.ts +0 -106
- package/providers/algolia/src/test/search.provider.spec.ts +0 -91
- package/providers/commercetools/jest.config.cjs +0 -10
- package/providers/commercetools/src/providers/search.provider.ts +0 -96
- package/providers/commercetools/src/test/cart.provider.spec.ts +0 -199
- package/providers/commercetools/src/test/category.provider.spec.ts +0 -168
- package/providers/commercetools/src/test/checkout.provider.spec.ts +0 -312
- package/providers/commercetools/src/test/identity.provider.spec.ts +0 -88
- package/providers/commercetools/src/test/inventory.provider.spec.ts +0 -41
- package/providers/commercetools/src/test/price.provider.spec.ts +0 -81
- package/providers/commercetools/src/test/product.provider.spec.ts +0 -80
- package/providers/commercetools/src/test/profile.provider.spec.ts +0 -49
- package/providers/commercetools/src/test/search.provider.spec.ts +0 -61
- package/providers/commercetools/src/test/store.provider.spec.ts +0 -37
- package/providers/fake/jest.config.cjs +0 -10
- package/providers/fake/src/providers/search.provider.ts +0 -132
|
@@ -3,68 +3,79 @@ import type {
|
|
|
3
3
|
Cache,
|
|
4
4
|
Order,
|
|
5
5
|
OrderQueryById,
|
|
6
|
-
Currency
|
|
7
|
-
|
|
6
|
+
Currency,
|
|
7
|
+
OrderIdentifier,
|
|
8
|
+
CostBreakDown,
|
|
9
|
+
OrderStatus,
|
|
10
|
+
OrderItem,
|
|
11
|
+
ProductVariantIdentifier,
|
|
12
|
+
ItemCostBreakdown,
|
|
13
|
+
Result,
|
|
14
|
+
NotFoundError,
|
|
15
|
+
} from '@reactionary/core';
|
|
16
|
+
import { OrderProvider, OrderQueryByIdSchema, OrderSchema, Reactionary, success, error } from '@reactionary/core';
|
|
8
17
|
import type z from 'zod';
|
|
9
18
|
import type { CommercetoolsConfiguration } from '../schema/configuration.schema.js';
|
|
10
|
-
import { CommercetoolsClient } from '../core/client.js';
|
|
11
19
|
import type { Order as CTOrder } from '@commercetools/platform-sdk';
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
import { type CommercetoolsOrderIdentifier } from '../schema/commercetools.schema.js';
|
|
21
|
+
import type { CommercetoolsAPI } from '../core/client.js';
|
|
22
|
+
|
|
23
|
+
export class CommercetoolsOrderProvider extends OrderProvider {
|
|
16
24
|
protected config: CommercetoolsConfiguration;
|
|
25
|
+
protected commercetools: CommercetoolsAPI;
|
|
17
26
|
|
|
18
27
|
constructor(
|
|
19
28
|
config: CommercetoolsConfiguration,
|
|
20
|
-
|
|
21
|
-
|
|
29
|
+
cache: Cache,
|
|
30
|
+
context: RequestContext,
|
|
31
|
+
commercetools: CommercetoolsAPI
|
|
22
32
|
) {
|
|
23
|
-
super(
|
|
33
|
+
super(cache, context);
|
|
24
34
|
|
|
25
35
|
this.config = config;
|
|
36
|
+
this.commercetools = commercetools;
|
|
26
37
|
}
|
|
27
38
|
|
|
28
|
-
protected async getClient(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
protected async getClient() {
|
|
40
|
+
const client = await this.commercetools.getClient();
|
|
41
|
+
return client
|
|
42
|
+
.withProjectKey({ projectKey: this.config.projectKey })
|
|
43
|
+
.me()
|
|
44
|
+
.orders();
|
|
34
45
|
}
|
|
35
46
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
47
|
+
@Reactionary({
|
|
48
|
+
inputSchema: OrderQueryByIdSchema,
|
|
49
|
+
outputSchema: OrderSchema,
|
|
50
|
+
})
|
|
51
|
+
public override async getById(payload: OrderQueryById): Promise<Result<Order, NotFoundError>> {
|
|
52
|
+
const client = await this.getClient();
|
|
39
53
|
|
|
40
54
|
try {
|
|
41
55
|
const remote = await client
|
|
42
|
-
.withProjectKey({ projectKey: this.config.projectKey })
|
|
43
|
-
.orders()
|
|
44
56
|
.withId({ ID: payload.order.key })
|
|
45
57
|
.get()
|
|
46
58
|
.execute();
|
|
47
59
|
|
|
48
|
-
return this.parseSingle(remote.body
|
|
60
|
+
return success(this.parseSingle(remote.body));
|
|
49
61
|
} catch (e) {
|
|
50
|
-
return
|
|
62
|
+
return error<NotFoundError>({
|
|
63
|
+
type: 'NotFound',
|
|
64
|
+
identifier: payload
|
|
65
|
+
})
|
|
51
66
|
}
|
|
52
67
|
}
|
|
53
68
|
|
|
69
|
+
protected parseSingle(_body: unknown): Order {
|
|
70
|
+
const remote = _body as CTOrder;
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
result.identifier = CommercetoolsOrderIdentifierSchema.parse({
|
|
60
|
-
key: remote.id,
|
|
61
|
-
version: remote.version || 0,
|
|
62
|
-
});
|
|
63
|
-
|
|
72
|
+
const identifier = {
|
|
73
|
+
key: remote.id,
|
|
74
|
+
version: remote.version || 0,
|
|
75
|
+
} satisfies CommercetoolsOrderIdentifier;
|
|
64
76
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
result.description = remote.custom?.fields['description'] || '';
|
|
77
|
+
const name = remote.custom?.fields['name'] || '';
|
|
78
|
+
const description = remote.custom?.fields['description'] || '';
|
|
68
79
|
|
|
69
80
|
const grandTotal = remote.totalPrice.centAmount || 0;
|
|
70
81
|
const shippingTotal = remote.shippingInfo?.price.centAmount || 0;
|
|
@@ -75,7 +86,7 @@ export class CommercetoolsOrderProvider<
|
|
|
75
86
|
const surchargeTotal = 0;
|
|
76
87
|
const currency = remote.totalPrice.currencyCode as Currency;
|
|
77
88
|
|
|
78
|
-
|
|
89
|
+
const price = {
|
|
79
90
|
totalTax: {
|
|
80
91
|
value: taxTotal / 100,
|
|
81
92
|
currency,
|
|
@@ -100,33 +111,45 @@ export class CommercetoolsOrderProvider<
|
|
|
100
111
|
value: grandTotal / 100,
|
|
101
112
|
currency,
|
|
102
113
|
},
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
114
|
+
} satisfies CostBreakDown;
|
|
115
|
+
|
|
116
|
+
let orderStatus: OrderStatus = 'AwaitingPayment';
|
|
117
|
+
if (remote.paymentState === 'Pending' && remote.orderState === 'Open') {
|
|
118
|
+
orderStatus = 'AwaitingPayment';
|
|
119
|
+
} else if (
|
|
120
|
+
remote.paymentState === 'Paid' &&
|
|
121
|
+
remote.orderState === 'Confirmed'
|
|
122
|
+
) {
|
|
123
|
+
orderStatus = 'ReleasedToFulfillment';
|
|
109
124
|
}
|
|
110
125
|
if (remote.shipmentState === 'Ready' && remote.orderState === 'Confirmed') {
|
|
111
|
-
|
|
126
|
+
orderStatus = 'ReleasedToFulfillment';
|
|
112
127
|
}
|
|
113
|
-
if (
|
|
114
|
-
|
|
128
|
+
if (
|
|
129
|
+
(remote.shipmentState === 'Shipped' ||
|
|
130
|
+
remote.shipmentState === 'Delivered') &&
|
|
131
|
+
remote.orderState === 'Completed'
|
|
132
|
+
) {
|
|
133
|
+
orderStatus = 'Shipped';
|
|
115
134
|
}
|
|
116
135
|
|
|
136
|
+
const items = new Array<OrderItem>();
|
|
117
137
|
for (const remoteItem of remote.lineItems) {
|
|
118
|
-
const
|
|
138
|
+
const identifier = {
|
|
139
|
+
key: remoteItem.id
|
|
140
|
+
} satisfies OrderIdentifier;
|
|
119
141
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
142
|
+
const variant = {
|
|
143
|
+
sku: remoteItem.variant.sku || ''
|
|
144
|
+
} satisfies ProductVariantIdentifier;
|
|
145
|
+
const quantity = remoteItem.quantity;
|
|
123
146
|
|
|
124
147
|
const unitPrice = remoteItem.price.value.centAmount;
|
|
125
148
|
const totalPrice = remoteItem.totalPrice.centAmount || 0;
|
|
126
149
|
const totalDiscount = remoteItem.price.discounted?.value.centAmount || 0;
|
|
127
150
|
const unitDiscount = totalDiscount / remoteItem.quantity;
|
|
128
151
|
|
|
129
|
-
|
|
152
|
+
const price = {
|
|
130
153
|
unitPrice: {
|
|
131
154
|
value: unitPrice / 100,
|
|
132
155
|
currency,
|
|
@@ -143,21 +166,33 @@ export class CommercetoolsOrderProvider<
|
|
|
143
166
|
value: totalDiscount / 100,
|
|
144
167
|
currency,
|
|
145
168
|
},
|
|
146
|
-
};
|
|
169
|
+
} satisfies ItemCostBreakdown;
|
|
170
|
+
|
|
171
|
+
const item = {
|
|
172
|
+
identifier,
|
|
173
|
+
inventoryStatus: 'NotAllocated',
|
|
174
|
+
price,
|
|
175
|
+
quantity,
|
|
176
|
+
variant
|
|
177
|
+
} satisfies OrderItem;
|
|
147
178
|
|
|
148
|
-
|
|
179
|
+
items.push(item);
|
|
149
180
|
}
|
|
150
181
|
|
|
151
|
-
result
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
182
|
+
const result = {
|
|
183
|
+
identifier,
|
|
184
|
+
name,
|
|
185
|
+
description,
|
|
186
|
+
price,
|
|
187
|
+
items,
|
|
188
|
+
inventoryStatus: 'NotAllocated',
|
|
189
|
+
paymentInstructions: [],
|
|
190
|
+
userId: {
|
|
191
|
+
userId: ''
|
|
155
192
|
},
|
|
156
|
-
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
|
|
193
|
+
orderStatus
|
|
194
|
+
} satisfies Order;
|
|
160
195
|
|
|
161
|
-
|
|
196
|
+
return result;
|
|
162
197
|
}
|
|
163
198
|
}
|
|
@@ -1,150 +1,180 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
CustomerPriceQuerySchema,
|
|
3
|
+
ListPriceQuerySchema,
|
|
4
|
+
PriceProvider,
|
|
5
|
+
PriceSchema,
|
|
6
|
+
Reactionary,
|
|
7
|
+
success,
|
|
8
|
+
} from '@reactionary/core';
|
|
9
|
+
import type {
|
|
10
|
+
RequestContext,
|
|
11
|
+
Price,
|
|
12
|
+
Cache,
|
|
13
|
+
Currency,
|
|
14
|
+
CustomerPriceQuery,
|
|
15
|
+
ListPriceQuery,
|
|
16
|
+
PriceIdentifier,
|
|
17
|
+
MonetaryAmount,
|
|
18
|
+
Result,
|
|
19
|
+
} from '@reactionary/core';
|
|
4
20
|
import type { CommercetoolsConfiguration } from '../schema/configuration.schema.js';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
protected config: CommercetoolsConfiguration;
|
|
11
|
-
|
|
21
|
+
import type {
|
|
22
|
+
Price as CTPrice,
|
|
23
|
+
ProductVariant as CTProductVariant,
|
|
24
|
+
} from '@commercetools/platform-sdk';
|
|
25
|
+
import type { CommercetoolsAPI } from '../core/client.js';
|
|
12
26
|
|
|
27
|
+
export class CommercetoolsPriceProvider extends PriceProvider {
|
|
28
|
+
protected config: CommercetoolsConfiguration;
|
|
29
|
+
protected commercetools: CommercetoolsAPI;
|
|
13
30
|
|
|
14
|
-
constructor(
|
|
15
|
-
|
|
31
|
+
constructor(
|
|
32
|
+
config: CommercetoolsConfiguration,
|
|
33
|
+
cache: Cache,
|
|
34
|
+
context: RequestContext,
|
|
35
|
+
commercetools: CommercetoolsAPI
|
|
36
|
+
) {
|
|
37
|
+
super(cache, context);
|
|
16
38
|
|
|
17
39
|
this.config = config;
|
|
40
|
+
this.commercetools = commercetools;
|
|
18
41
|
}
|
|
19
42
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
43
|
+
@Reactionary({
|
|
44
|
+
inputSchema: CustomerPriceQuerySchema,
|
|
45
|
+
outputSchema: PriceSchema,
|
|
46
|
+
})
|
|
47
|
+
public override async getCustomerPrice(
|
|
48
|
+
payload: CustomerPriceQuery
|
|
49
|
+
): Promise<Result<Price>> {
|
|
50
|
+
const client = await this.getClient();
|
|
51
|
+
const priceChannelId = 'ee6e75e9-c9ab-4e2f-85f1-d8c734d0cb86';
|
|
52
|
+
|
|
53
|
+
const response = await client
|
|
54
|
+
.productProjections()
|
|
55
|
+
.get({
|
|
56
|
+
queryArgs: {
|
|
57
|
+
staged: false,
|
|
58
|
+
priceCountry: this.context.taxJurisdiction.countryCode,
|
|
59
|
+
priceCustomerGroup: undefined,
|
|
60
|
+
priceChannel: priceChannelId,
|
|
61
|
+
priceCurrency: this.context.languageContext.currencyCode,
|
|
62
|
+
where: 'variants(sku in (:skus)) OR (masterVariant(sku in (:skus))) ',
|
|
63
|
+
'var.skus': [payload.variant.sku],
|
|
64
|
+
limit: 1,
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
.execute();
|
|
68
|
+
|
|
69
|
+
const result = response.body.results[0];
|
|
70
|
+
const sku = [result.masterVariant, ...result.variants].find(
|
|
71
|
+
(x) => x.sku === payload.variant.sku
|
|
24
72
|
);
|
|
25
|
-
return client.withProjectKey({ projectKey: this.config.projectKey }).productProjections()
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
public override async getBySKUs(payload: PriceQueryBySku[], reqCtx: RequestContext): Promise<T[]> {
|
|
31
|
-
|
|
32
|
-
const client = await this.getClient(reqCtx);
|
|
33
73
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const channels = await this.getChannels(reqCtx);
|
|
37
|
-
|
|
38
|
-
const response = await client.get({
|
|
39
|
-
queryArgs: {
|
|
40
|
-
staged: false,
|
|
41
|
-
priceCountry: reqCtx.taxJurisdiction.countryCode,
|
|
42
|
-
priceCustomerGroup: undefined,
|
|
43
|
-
priceChannel: channels.offerChannelGUID,
|
|
44
|
-
priceCurrency: reqCtx.languageContext.currencyCode,
|
|
45
|
-
// storeProjection: reqCtx.storeIdentifier?.key || undefined,
|
|
46
|
-
where: 'variants(sku in (:skus)) OR (masterVariant(sku in (:skus))) ',
|
|
47
|
-
'var.skus': payload.map(p => p.sku.key),
|
|
48
|
-
limit: payload.length,
|
|
49
|
-
},
|
|
50
|
-
}).execute();
|
|
51
|
-
|
|
52
|
-
const result = [];
|
|
53
|
-
const allReturnedVariants = [...response.body.results.map(x => x.variants).flat(), ...response.body.results.map(x => x.masterVariant).flat()];
|
|
54
|
-
// Now we need to match the skus requested with the prices returned.
|
|
55
|
-
for(const p of payload) {
|
|
56
|
-
const foundSku = allReturnedVariants.find(v => v.sku === p.sku.key);
|
|
57
|
-
|
|
58
|
-
if (!foundSku) {
|
|
59
|
-
result.push(this.createEmptyPriceResult(p.sku.key, reqCtx.languageContext.currencyCode ));
|
|
60
|
-
} else {
|
|
61
|
-
result.push(this.parseSingle(foundSku, reqCtx));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return result;
|
|
74
|
+
return success(this.parseSingle(sku, { includeDiscounts: true }));
|
|
66
75
|
}
|
|
67
76
|
|
|
77
|
+
@Reactionary({
|
|
78
|
+
inputSchema: ListPriceQuerySchema,
|
|
79
|
+
outputSchema: PriceSchema,
|
|
80
|
+
})
|
|
81
|
+
public override async getListPrice(payload: ListPriceQuery): Promise<Result<Price>> {
|
|
82
|
+
const client = await this.getClient();
|
|
83
|
+
const priceChannelId = 'ee6e75e9-c9ab-4e2f-85f1-d8c734d0cb86';
|
|
84
|
+
|
|
85
|
+
const response = await client
|
|
86
|
+
.productProjections()
|
|
87
|
+
.get({
|
|
88
|
+
queryArgs: {
|
|
89
|
+
staged: false,
|
|
90
|
+
priceCountry: this.context.taxJurisdiction.countryCode,
|
|
91
|
+
priceCustomerGroup: undefined,
|
|
92
|
+
priceChannel: priceChannelId,
|
|
93
|
+
priceCurrency: this.context.languageContext.currencyCode,
|
|
94
|
+
where: 'variants(sku in (:skus)) OR (masterVariant(sku in (:skus))) ',
|
|
95
|
+
'var.skus': [payload.variant.sku],
|
|
96
|
+
limit: 1,
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
.execute();
|
|
100
|
+
|
|
101
|
+
const result = response.body.results[0];
|
|
102
|
+
const sku = [result.masterVariant, ...result.variants].find(
|
|
103
|
+
(x) => x.sku === payload.variant.sku
|
|
104
|
+
);
|
|
68
105
|
|
|
69
|
-
|
|
70
|
-
payload: PriceQueryBySku,
|
|
71
|
-
reqCtx: RequestContext
|
|
72
|
-
): Promise<T> {
|
|
73
|
-
return this.getBySKUs([payload], reqCtx).then(r => r[0]);
|
|
106
|
+
return success(this.parseSingle(sku));
|
|
74
107
|
}
|
|
75
108
|
|
|
109
|
+
protected async getClient() {
|
|
110
|
+
const client = await this.commercetools.getClient();
|
|
111
|
+
return client.withProjectKey({ projectKey: this.config.projectKey });
|
|
112
|
+
}
|
|
76
113
|
|
|
77
|
-
|
|
78
|
-
|
|
114
|
+
protected parseSingle(
|
|
115
|
+
_body: unknown,
|
|
116
|
+
options = { includeDiscounts: false }
|
|
117
|
+
): Price {
|
|
79
118
|
const body = _body as CTProductVariant;
|
|
80
119
|
const price = body.price as CTPrice | undefined;
|
|
81
120
|
|
|
82
121
|
if (!price) {
|
|
83
|
-
return this.createEmptyPriceResult(body.sku
|
|
122
|
+
return this.createEmptyPriceResult(body.sku!);
|
|
84
123
|
}
|
|
85
124
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
value: (price.value.centAmount / 100),
|
|
125
|
+
let unitPrice = {
|
|
126
|
+
value: price.value.centAmount / 100,
|
|
89
127
|
currency: price.value.currencyCode as Currency,
|
|
90
|
-
};
|
|
128
|
+
} satisfies MonetaryAmount;
|
|
91
129
|
|
|
92
|
-
if (
|
|
93
|
-
const
|
|
94
|
-
const tp: TieredPrice = TieredPriceSchema.parse({});
|
|
95
|
-
tp.minimumQuantity = x.minimumQuantity;
|
|
96
|
-
tp.price = {
|
|
97
|
-
value: (x.value.centAmount / 100),
|
|
98
|
-
currency: x.value.currencyCode as Currency,
|
|
99
|
-
};
|
|
100
|
-
return tp;
|
|
101
|
-
});
|
|
102
|
-
base.tieredPrices = p;
|
|
103
|
-
}
|
|
130
|
+
if (options.includeDiscounts) {
|
|
131
|
+
const discountedPrice = price.discounted?.value || price.value;
|
|
104
132
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
}
|
|
133
|
+
unitPrice = {
|
|
134
|
+
value: discountedPrice.centAmount / 100,
|
|
135
|
+
currency: price.value.currencyCode as Currency,
|
|
136
|
+
} satisfies MonetaryAmount;
|
|
137
|
+
}
|
|
110
138
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
139
|
+
const identifier = {
|
|
140
|
+
variant: {
|
|
141
|
+
sku: body.sku!,
|
|
142
|
+
},
|
|
143
|
+
} satisfies PriceIdentifier;
|
|
115
144
|
|
|
116
|
-
|
|
145
|
+
const result = {
|
|
146
|
+
identifier,
|
|
147
|
+
tieredPrices: [],
|
|
148
|
+
unitPrice,
|
|
149
|
+
} satisfies Price;
|
|
117
150
|
|
|
151
|
+
return result;
|
|
118
152
|
}
|
|
119
153
|
|
|
120
|
-
protected async getChannels(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
listChannelGUID: undefined
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
154
|
+
protected async getChannels() {
|
|
155
|
+
const adminClient = await this.commercetools.getAdminClient();
|
|
156
|
+
|
|
157
|
+
const offerPriceChannelPromise = adminClient
|
|
158
|
+
.withProjectKey({ projectKey: this.config.projectKey })
|
|
159
|
+
.channels()
|
|
160
|
+
.withKey({ key: 'Offer Price' })
|
|
161
|
+
.get()
|
|
162
|
+
.execute();
|
|
163
|
+
const listPriceChannelPromise = adminClient
|
|
164
|
+
.withProjectKey({ projectKey: this.config.projectKey })
|
|
165
|
+
.channels()
|
|
166
|
+
.withKey({ key: 'List Price' })
|
|
167
|
+
.get()
|
|
168
|
+
.execute();
|
|
169
|
+
|
|
170
|
+
const [offerChannel, listChannel] = await Promise.all([
|
|
171
|
+
offerPriceChannelPromise,
|
|
172
|
+
listPriceChannelPromise,
|
|
173
|
+
]);
|
|
144
174
|
|
|
145
175
|
return {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
176
|
+
offer: offerChannel.body.id,
|
|
177
|
+
list: listChannel.body.id,
|
|
178
|
+
};
|
|
149
179
|
}
|
|
150
180
|
}
|