@reactionary/provider-commercetools 0.2.2 → 0.2.3

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.
@@ -14,6 +14,7 @@ import { CommercetoolsCategoryProvider } from "../providers/category.provider.js
14
14
  import {
15
15
  CommercetoolsCheckoutProvider,
16
16
  CommercetoolsOrderProvider,
17
+ CommercetoolsOrderSearchProvider,
17
18
  CommercetoolsProfileProvider,
18
19
  CommercetoolsStoreProvider
19
20
  } from "../providers/index.js";
@@ -112,6 +113,14 @@ function withCommercetoolsCapabilities(configuration, capabilities) {
112
113
  commercetoolsClient
113
114
  );
114
115
  }
116
+ if (caps.orderSearch) {
117
+ client.orderSearch = new CommercetoolsOrderSearchProvider(
118
+ config,
119
+ cache,
120
+ context,
121
+ commercetoolsClient
122
+ );
123
+ }
115
124
  return client;
116
125
  };
117
126
  }
package/index.js CHANGED
@@ -3,6 +3,7 @@ export * from "./core/initialize.js";
3
3
  export * from "./providers/cart.provider.js";
4
4
  export * from "./providers/identity.provider.js";
5
5
  export * from "./providers/inventory.provider.js";
6
+ export * from "./providers/order-search.provider.js";
6
7
  export * from "./providers/price.provider.js";
7
8
  export * from "./providers/product.provider.js";
8
9
  export * from "./providers/product-search.provider.js";
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@reactionary/provider-commercetools",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.2.2",
7
+ "@reactionary/core": "0.2.3",
8
8
  "debug": "^4.4.3",
9
9
  "zod": "4.1.9",
10
10
  "@commercetools/ts-client": "^4.2.1",
@@ -2,6 +2,7 @@ export * from "./cart.provider.js";
2
2
  export * from "./category.provider.js";
3
3
  export * from "./identity.provider.js";
4
4
  export * from "./inventory.provider.js";
5
+ export * from "./order-search.provider.js";
5
6
  export * from "./price.provider.js";
6
7
  export * from "./product.provider.js";
7
8
  export * from "./profile.provider.js";
@@ -0,0 +1,170 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result)
9
+ __defProp(target, key, result);
10
+ return result;
11
+ };
12
+ import {
13
+ OrderSearchProvider,
14
+ OrderSearchQueryByTermSchema,
15
+ OrderSearchResultSchema,
16
+ Reactionary,
17
+ success
18
+ } from "@reactionary/core";
19
+ import createDebug from "debug";
20
+ const debug = createDebug("reactionary:commercetools:order-search");
21
+ class CommercetoolsOrderSearchProvider extends OrderSearchProvider {
22
+ constructor(config, cache, context, client) {
23
+ super(cache, context);
24
+ this.config = config;
25
+ this.client = client;
26
+ }
27
+ async getClient() {
28
+ const client = await this.client.getClient();
29
+ return client.withProjectKey({ projectKey: this.config.projectKey }).me().orders();
30
+ }
31
+ async queryByTerm(payload) {
32
+ debug("queryByTerm", payload);
33
+ const client = await this.getClient();
34
+ const where = [];
35
+ if (payload.search) {
36
+ if (payload.search.term) {
37
+ debug("Search by term is not implemented yet in CommercetoolsOrderSearchProvider");
38
+ }
39
+ if (payload.search.partNumber) {
40
+ for (const partNumber of payload.search.partNumber) {
41
+ where.push(`lineItems(variant(sku="${partNumber}"))`);
42
+ }
43
+ }
44
+ if (payload.search.userId) {
45
+ where.push(`customerId="${payload.search.userId}"`);
46
+ }
47
+ if (payload.search.orderStatus) {
48
+ const orderStatusWhere = payload.search.orderStatus.map((x) => {
49
+ if (x === "AwaitingPayment") {
50
+ return `Open`;
51
+ }
52
+ if (x === "ReleasedToFulfillment") {
53
+ return `Confirmed`;
54
+ }
55
+ if (x === "Shipped") {
56
+ return `Completed`;
57
+ }
58
+ if (x === "Cancelled") {
59
+ return `Cancelled`;
60
+ }
61
+ return `orderState="${x}"`;
62
+ }).join(" OR ");
63
+ where.push(orderStatusWhere);
64
+ }
65
+ if (payload.search.startDate) {
66
+ where.push(`createdAt >= "${payload.search.startDate}"`);
67
+ }
68
+ if (payload.search.endDate) {
69
+ where.push(`createdAt <= "${payload.search.endDate}"`);
70
+ }
71
+ }
72
+ const response = await client.get({
73
+ queryArgs: {
74
+ where,
75
+ withTotal: true,
76
+ limit: payload.search.paginationOptions.pageSize,
77
+ offset: (payload.search.paginationOptions.pageNumber - 1) * payload.search.paginationOptions.pageSize
78
+ }
79
+ }).execute();
80
+ const responseBody = response.body;
81
+ const result = this.parsePaginatedResult(
82
+ responseBody,
83
+ payload
84
+ );
85
+ if (debug.enabled) {
86
+ debug(
87
+ `Search for term "${payload.search.term}" returned ${responseBody.results.length} orders (page ${payload.search.paginationOptions.pageNumber} of ${result.totalPages})`
88
+ );
89
+ }
90
+ return success(result);
91
+ }
92
+ parseAddress(remote) {
93
+ return {
94
+ countryCode: remote.country || "",
95
+ firstName: remote.firstName || "",
96
+ lastName: remote.lastName || "",
97
+ streetAddress: remote.streetName || "",
98
+ streetNumber: remote.streetNumber || "",
99
+ postalCode: remote.postalCode || "",
100
+ city: remote.city || "",
101
+ identifier: {
102
+ nickName: ""
103
+ },
104
+ region: ""
105
+ };
106
+ }
107
+ parseSingle(body) {
108
+ const identifier = { key: body.id };
109
+ const userId = {
110
+ userId: body.customerId || body.anonymousId || ""
111
+ };
112
+ const customerName = `${body.billingAddress?.firstName} ${body.billingAddress?.lastName}`;
113
+ const shippingAddress = this.parseAddress(body.shippingAddress);
114
+ const orderDate = body.createdAt;
115
+ let orderStatus = "AwaitingPayment";
116
+ if (body.paymentState === "Pending" && body.orderState === "Open") {
117
+ orderStatus = "AwaitingPayment";
118
+ } else if (body.paymentState === "Paid" && body.orderState === "Confirmed") {
119
+ orderStatus = "ReleasedToFulfillment";
120
+ }
121
+ if (body.shipmentState === "Ready" && body.orderState === "Confirmed") {
122
+ orderStatus = "ReleasedToFulfillment";
123
+ }
124
+ if ((body.shipmentState === "Shipped" || body.shipmentState === "Delivered") && body.orderState === "Completed") {
125
+ orderStatus = "Shipped";
126
+ }
127
+ const inventoryStatus = body.shipmentState;
128
+ const totalAmount = {
129
+ currency: body.totalPrice ? body.totalPrice.currencyCode : this.context.languageContext.currencyCode,
130
+ value: body.totalPrice ? body.totalPrice.centAmount / 100 : 0
131
+ };
132
+ const order = {
133
+ identifier,
134
+ userId,
135
+ customerName,
136
+ shippingAddress,
137
+ orderDate,
138
+ orderStatus,
139
+ inventoryStatus,
140
+ totalAmount
141
+ };
142
+ return order;
143
+ }
144
+ parsePaginatedResult(body, query) {
145
+ const identifier = {
146
+ ...query.search
147
+ };
148
+ const orders = body.results.map((o) => {
149
+ return this.parseSingle(o);
150
+ });
151
+ const result = {
152
+ identifier,
153
+ pageNumber: (Math.ceil(body.offset / body.limit) || 0) + 1,
154
+ pageSize: body.limit,
155
+ totalCount: body.total || 0,
156
+ totalPages: Math.ceil((body.total || 0) / body.limit || 0) + 1,
157
+ items: orders
158
+ };
159
+ return result;
160
+ }
161
+ }
162
+ __decorateClass([
163
+ Reactionary({
164
+ inputSchema: OrderSearchQueryByTermSchema,
165
+ outputSchema: OrderSearchResultSchema
166
+ })
167
+ ], CommercetoolsOrderSearchProvider.prototype, "queryByTerm", 1);
168
+ export {
169
+ CommercetoolsOrderSearchProvider
170
+ };
@@ -6,6 +6,7 @@ const CommercetoolsCapabilitiesSchema = CapabilitiesSchema.pick({
6
6
  cart: true,
7
7
  checkout: true,
8
8
  order: true,
9
+ orderSearch: true,
9
10
  inventory: true,
10
11
  price: true,
11
12
  category: true,
package/src/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './core/initialize.js';
3
3
  export * from './providers/cart.provider.js';
4
4
  export * from './providers/identity.provider.js';
5
5
  export * from './providers/inventory.provider.js';
6
+ export * from './providers/order-search.provider.js';
6
7
  export * from './providers/price.provider.js';
7
8
  export * from './providers/product.provider.js';
8
9
  export * from './providers/product-search.provider.js';
@@ -2,6 +2,7 @@ export * from './cart.provider.js';
2
2
  export * from './category.provider.js';
3
3
  export * from './identity.provider.js';
4
4
  export * from './inventory.provider.js';
5
+ export * from './order-search.provider.js';
5
6
  export * from './price.provider.js';
6
7
  export * from './product.provider.js';
7
8
  export * from './profile.provider.js';
@@ -0,0 +1,104 @@
1
+ import type { RequestContext, Cache, OrderSearchQueryByTerm, OrderSearchResult, Result } from '@reactionary/core';
2
+ import { OrderSearchProvider } from '@reactionary/core';
3
+ import type { CommercetoolsConfiguration } from '../schema/configuration.schema.js';
4
+ import type { CommercetoolsClient } from '../core/client.js';
5
+ import type { Order as CTOrder, Address as CTAddress, OrderPagedQueryResponse } from '@commercetools/platform-sdk';
6
+ export declare class CommercetoolsOrderSearchProvider extends OrderSearchProvider {
7
+ protected config: CommercetoolsConfiguration;
8
+ protected client: CommercetoolsClient;
9
+ constructor(config: CommercetoolsConfiguration, cache: Cache, context: RequestContext, client: CommercetoolsClient);
10
+ protected getClient(): Promise<import("@commercetools/platform-sdk").ByProjectKeyMeOrdersRequestBuilder>;
11
+ queryByTerm(payload: OrderSearchQueryByTerm): Promise<Result<OrderSearchResult>>;
12
+ protected parseAddress(remote: CTAddress): {
13
+ countryCode: string;
14
+ firstName: string;
15
+ lastName: string;
16
+ streetAddress: string;
17
+ streetNumber: string;
18
+ postalCode: string;
19
+ city: string;
20
+ identifier: {
21
+ nickName: string;
22
+ };
23
+ region: string;
24
+ };
25
+ protected parseSingle(body: CTOrder): {
26
+ identifier: {
27
+ key: string;
28
+ };
29
+ userId: {
30
+ userId: string;
31
+ };
32
+ customerName: string;
33
+ shippingAddress: {
34
+ countryCode: string;
35
+ firstName: string;
36
+ lastName: string;
37
+ streetAddress: string;
38
+ streetNumber: string;
39
+ postalCode: string;
40
+ city: string;
41
+ identifier: {
42
+ nickName: string;
43
+ };
44
+ region: string;
45
+ };
46
+ orderDate: string;
47
+ orderStatus: "AwaitingPayment" | "ReleasedToFulfillment" | "Shipped";
48
+ inventoryStatus: "NotAllocated" | "Allocated" | "Backordered" | "Preordered";
49
+ totalAmount: {
50
+ value: number;
51
+ currency: "AED" | "AFN" | "ALL" | "AMD" | "ANG" | "AOA" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "BGN" | "BHD" | "BIF" | "BMD" | "BND" | "BOB" | "BOV" | "BRL" | "BSD" | "BTN" | "BWP" | "BYN" | "BZD" | "CAD" | "CDF" | "CHE" | "CHF" | "CHW" | "CLF" | "CLP" | "CNY" | "COP" | "COU" | "CRC" | "CUC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "DOP" | "DZD" | "EGP" | "ERN" | "ETB" | "EUR" | "FJD" | "FKP" | "GBP" | "GEL" | "GHS" | "GIP" | "GMD" | "GNF" | "GTQ" | "GYD" | "HKD" | "HNL" | "HRK" | "HTG" | "HUF" | "IDR" | "ILS" | "INR" | "IQD" | "IRR" | "ISK" | "JMD" | "JOD" | "JPY" | "KES" | "KGS" | "KHR" | "KMF" | "KPW" | "KRW" | "KWD" | "KYD" | "KZT" | "LAK" | "LBP" | "LKR" | "LRD" | "LSL" | "LYD" | "MAD" | "MDL" | "MGA" | "MKD" | "MMK" | "MNT" | "MOP" | "MRU" | "MUR" | "MVR" | "MWK" | "MXN" | "MXV" | "MYR" | "MZN" | "NAD" | "NGN" | "NIO" | "NOK" | "NPR" | "NZD" | "OMR" | "PAB" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "QAR" | "RON" | "RSD" | "RUB" | "RWF" | "SAR" | "SBD" | "SCR" | "SDG" | "SEK" | "SGD" | "SHP" | "SLE" | "SLL" | "SOS" | "SRD" | "SSP" | "STN" | "SYP" | "SZL" | "THB" | "TJS" | "TMT" | "TND" | "TOP" | "TRY" | "TTD" | "TVD" | "TWD" | "TZS" | "UAH" | "UGX" | "USD" | "USN" | "UYI" | "UYU" | "UYW" | "UZS" | "VED" | "VES" | "VND" | "VUV" | "WST" | "XAF" | "XAG" | "XAU" | "XBA" | "XBB" | "XBC" | "XBD" | "XCD" | "XDR" | "XOF" | "XPD" | "XPF" | "XPT" | "XSU" | "XTS" | "XUA" | "XXX" | "YER" | "ZAR" | "ZMW" | "ZWL";
52
+ };
53
+ };
54
+ protected parsePaginatedResult(body: OrderPagedQueryResponse, query: OrderSearchQueryByTerm): {
55
+ identifier: {
56
+ term: string;
57
+ filters: string[];
58
+ paginationOptions: {
59
+ pageNumber: number;
60
+ pageSize: number;
61
+ };
62
+ partNumber?: string[] | undefined;
63
+ orderStatus?: ("AwaitingPayment" | "ReleasedToFulfillment" | "Shipped" | "Cancelled")[] | undefined;
64
+ userId?: {
65
+ userId: string;
66
+ } | undefined;
67
+ startDate?: string | undefined;
68
+ endDate?: string | undefined;
69
+ };
70
+ pageNumber: number;
71
+ pageSize: number;
72
+ totalCount: number;
73
+ totalPages: number;
74
+ items: {
75
+ identifier: {
76
+ key: string;
77
+ };
78
+ userId: {
79
+ userId: string;
80
+ };
81
+ customerName: string;
82
+ orderDate: string;
83
+ orderStatus: "AwaitingPayment" | "ReleasedToFulfillment" | "Shipped" | "Cancelled";
84
+ inventoryStatus: "NotAllocated" | "Allocated" | "Backordered" | "Preordered";
85
+ totalAmount: {
86
+ value: number;
87
+ currency: "AED" | "AFN" | "ALL" | "AMD" | "ANG" | "AOA" | "ARS" | "AUD" | "AWG" | "AZN" | "BAM" | "BBD" | "BDT" | "BGN" | "BHD" | "BIF" | "BMD" | "BND" | "BOB" | "BOV" | "BRL" | "BSD" | "BTN" | "BWP" | "BYN" | "BZD" | "CAD" | "CDF" | "CHE" | "CHF" | "CHW" | "CLF" | "CLP" | "CNY" | "COP" | "COU" | "CRC" | "CUC" | "CUP" | "CVE" | "CZK" | "DJF" | "DKK" | "DOP" | "DZD" | "EGP" | "ERN" | "ETB" | "EUR" | "FJD" | "FKP" | "GBP" | "GEL" | "GHS" | "GIP" | "GMD" | "GNF" | "GTQ" | "GYD" | "HKD" | "HNL" | "HRK" | "HTG" | "HUF" | "IDR" | "ILS" | "INR" | "IQD" | "IRR" | "ISK" | "JMD" | "JOD" | "JPY" | "KES" | "KGS" | "KHR" | "KMF" | "KPW" | "KRW" | "KWD" | "KYD" | "KZT" | "LAK" | "LBP" | "LKR" | "LRD" | "LSL" | "LYD" | "MAD" | "MDL" | "MGA" | "MKD" | "MMK" | "MNT" | "MOP" | "MRU" | "MUR" | "MVR" | "MWK" | "MXN" | "MXV" | "MYR" | "MZN" | "NAD" | "NGN" | "NIO" | "NOK" | "NPR" | "NZD" | "OMR" | "PAB" | "PEN" | "PGK" | "PHP" | "PKR" | "PLN" | "PYG" | "QAR" | "RON" | "RSD" | "RUB" | "RWF" | "SAR" | "SBD" | "SCR" | "SDG" | "SEK" | "SGD" | "SHP" | "SLE" | "SLL" | "SOS" | "SRD" | "SSP" | "STN" | "SYP" | "SZL" | "THB" | "TJS" | "TMT" | "TND" | "TOP" | "TRY" | "TTD" | "TVD" | "TWD" | "TZS" | "UAH" | "UGX" | "USD" | "USN" | "UYI" | "UYU" | "UYW" | "UZS" | "VED" | "VES" | "VND" | "VUV" | "WST" | "XAF" | "XAG" | "XAU" | "XBA" | "XBB" | "XBC" | "XBD" | "XCD" | "XDR" | "XOF" | "XPD" | "XPF" | "XPT" | "XSU" | "XTS" | "XUA" | "XXX" | "YER" | "ZAR" | "ZMW" | "ZWL";
88
+ };
89
+ shippingAddress?: {
90
+ identifier: {
91
+ nickName: string;
92
+ };
93
+ firstName: string;
94
+ lastName: string;
95
+ streetAddress: string;
96
+ streetNumber: string;
97
+ city: string;
98
+ region: string;
99
+ postalCode: string;
100
+ countryCode: string;
101
+ } | undefined;
102
+ }[];
103
+ };
104
+ }
@@ -11,5 +11,6 @@ export declare const CommercetoolsCapabilitiesSchema: z.ZodObject<{
11
11
  profile: z.ZodOptional<z.ZodBoolean>;
12
12
  store: z.ZodOptional<z.ZodBoolean>;
13
13
  productSearch: z.ZodOptional<z.ZodBoolean>;
14
+ orderSearch: z.ZodOptional<z.ZodBoolean>;
14
15
  }, z.core.$loose>;
15
16
  export type CommercetoolsCapabilities = z.infer<typeof CommercetoolsCapabilitiesSchema>;