@reactionary/source 0.3.17 → 0.6.1
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 +22 -11
- package/core/src/schemas/models/cart.model.ts +7 -2
- package/core/src/schemas/models/identifiers.model.ts +12 -8
- package/core/src/schemas/models/price.model.ts +9 -0
- package/examples/node/package.json +7 -7
- package/examples/node/src/capabilities/cart.spec.ts +97 -15
- package/examples/node/src/capabilities/category.spec.ts +27 -32
- package/examples/node/src/capabilities/checkout.spec.ts +5 -5
- package/examples/node/src/capabilities/identity.spec.ts +5 -1
- package/examples/node/src/capabilities/inventory.spec.ts +1 -1
- package/package.json +3 -3
- package/providers/algolia/src/providers/product-search.provider.ts +19 -14
- package/providers/commercetools/src/providers/cart.provider.ts +76 -11
- package/providers/fake/src/providers/cart.provider.ts +1 -0
- package/providers/medusa/src/providers/cart.provider.ts +159 -70
- package/providers/medusa/src/providers/category.provider.ts +35 -23
- package/providers/medusa/src/providers/checkout.provider.ts +78 -41
- package/providers/medusa/src/providers/order-search.provider.ts +21 -10
- package/providers/medusa/src/providers/product-recommendations.provider.ts +10 -6
- package/providers/medusa/src/providers/product-search.provider.ts +19 -10
- package/providers/medusa/src/providers/product.provider.ts +20 -12
- package/providers/medusa/src/providers/profile.provider.ts +38 -13
- package/providers/meilisearch/src/providers/order-search.provider.ts +17 -12
- package/providers/meilisearch/src/providers/product-recommendations.provider.ts +10 -11
- package/providers/meilisearch/src/providers/product-search.provider.ts +23 -18
|
@@ -93,6 +93,14 @@ export class MedusaCategoryProvider extends CategoryProvider {
|
|
|
93
93
|
return success(this.parseSingle(candidate));
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
protected getBySlugPayload(payload: CategoryQueryBySlug) {
|
|
97
|
+
return {
|
|
98
|
+
handle: payload.slug,
|
|
99
|
+
limit: 1,
|
|
100
|
+
offset: 0,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
@Reactionary({
|
|
97
105
|
inputSchema: CategoryQueryBySlugSchema,
|
|
98
106
|
outputSchema: CategorySchema.nullable(),
|
|
@@ -102,11 +110,7 @@ export class MedusaCategoryProvider extends CategoryProvider {
|
|
|
102
110
|
): Promise<Result<Category, NotFoundError>> {
|
|
103
111
|
const sdk = await this.medusaApi.getClient();
|
|
104
112
|
|
|
105
|
-
const categoryResult = await sdk.store.category.list(
|
|
106
|
-
handle: payload.slug,
|
|
107
|
-
limit: 1,
|
|
108
|
-
offset: 0,
|
|
109
|
-
});
|
|
113
|
+
const categoryResult = await sdk.store.category.list(this.getBySlugPayload(payload));
|
|
110
114
|
if (categoryResult.count === 0) {
|
|
111
115
|
return error<NotFoundError>({
|
|
112
116
|
type: 'NotFound',
|
|
@@ -146,6 +150,17 @@ export class MedusaCategoryProvider extends CategoryProvider {
|
|
|
146
150
|
return success(results);
|
|
147
151
|
}
|
|
148
152
|
|
|
153
|
+
|
|
154
|
+
protected findChildCategoriesPayload(payload: CategoryQueryForChildCategories, actualParent: StoreProductCategory) {
|
|
155
|
+
return {
|
|
156
|
+
fields: '+metadata,+parent_category.metadata',
|
|
157
|
+
parent_category_id: actualParent.id,
|
|
158
|
+
limit: payload.paginationOptions.pageSize,
|
|
159
|
+
offset:
|
|
160
|
+
(payload.paginationOptions.pageNumber - 1) *
|
|
161
|
+
payload.paginationOptions.pageSize,
|
|
162
|
+
}
|
|
163
|
+
}
|
|
149
164
|
@Reactionary({
|
|
150
165
|
inputSchema: CategoryQueryForChildCategoriesSchema,
|
|
151
166
|
outputSchema: CategoryPaginatedResultSchema,
|
|
@@ -155,24 +170,28 @@ export class MedusaCategoryProvider extends CategoryProvider {
|
|
|
155
170
|
) {
|
|
156
171
|
const sdk = await this.medusaApi.getClient();
|
|
157
172
|
|
|
158
|
-
const
|
|
173
|
+
const actualParent = await this.resolveCategoryIdByExternalId(
|
|
159
174
|
payload.parentId.key
|
|
160
175
|
);
|
|
161
|
-
if (!
|
|
176
|
+
if (!actualParent) {
|
|
162
177
|
throw new Error('Parent category not found ' + payload.parentId.key);
|
|
163
178
|
}
|
|
164
179
|
|
|
165
|
-
const response = await sdk.store.category.list(
|
|
166
|
-
|
|
167
|
-
|
|
180
|
+
const response = await sdk.store.category.list(this.findChildCategoriesPayload(payload, actualParent));
|
|
181
|
+
|
|
182
|
+
const result = this.parsePaginatedResult(response);
|
|
183
|
+
return success(result);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
protected findTopCategoriesPayload(payload: CategoryQueryForTopCategories) {
|
|
187
|
+
return {
|
|
188
|
+
fields: '+metadata',
|
|
189
|
+
parent_category_id: 'null',
|
|
168
190
|
limit: payload.paginationOptions.pageSize,
|
|
169
191
|
offset:
|
|
170
192
|
(payload.paginationOptions.pageNumber - 1) *
|
|
171
193
|
payload.paginationOptions.pageSize,
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const result = this.parsePaginatedResult(response);
|
|
175
|
-
return success(result);
|
|
194
|
+
}
|
|
176
195
|
}
|
|
177
196
|
|
|
178
197
|
@Reactionary({
|
|
@@ -184,17 +203,9 @@ export class MedusaCategoryProvider extends CategoryProvider {
|
|
|
184
203
|
) {
|
|
185
204
|
const sdk = await this.medusaApi.getClient();
|
|
186
205
|
|
|
187
|
-
const response = await sdk.store.category.list(
|
|
188
|
-
fields: '+metadata',
|
|
189
|
-
parent_category_id: 'null',
|
|
190
|
-
limit: payload.paginationOptions.pageSize,
|
|
191
|
-
offset:
|
|
192
|
-
(payload.paginationOptions.pageNumber - 1) *
|
|
193
|
-
payload.paginationOptions.pageSize,
|
|
194
|
-
});
|
|
206
|
+
const response = await sdk.store.category.list(this.findTopCategoriesPayload(payload));
|
|
195
207
|
|
|
196
208
|
const result = this.parsePaginatedResult(response);
|
|
197
|
-
|
|
198
209
|
return success(result);
|
|
199
210
|
}
|
|
200
211
|
|
|
@@ -246,3 +257,4 @@ export class MedusaCategoryProvider extends CategoryProvider {
|
|
|
246
257
|
return result;
|
|
247
258
|
}
|
|
248
259
|
}
|
|
260
|
+
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { StoreCart, StoreCartAddress, StoreCartLineItem } from '@medusajs/types';
|
|
1
|
+
import type { StoreAddCartShippingMethods, StoreCart, StoreCartAddress, StoreCartLineItem, StoreInitializePaymentSession, StoreUpdateCart } from '@medusajs/types';
|
|
2
2
|
import type {
|
|
3
3
|
Address,
|
|
4
4
|
AddressIdentifier,
|
|
@@ -97,6 +97,21 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
97
97
|
this.config = config;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
protected initiateCheckoutForCartPayload(payload: CheckoutMutationInitiateCheckout): StoreUpdateCart {
|
|
101
|
+
return {
|
|
102
|
+
billing_address: payload.billingAddress
|
|
103
|
+
? this.mapAddressToStoreAddress(payload.billingAddress)
|
|
104
|
+
: undefined,
|
|
105
|
+
shipping_address: payload.billingAddress
|
|
106
|
+
? this.mapAddressToStoreAddress(payload.billingAddress)
|
|
107
|
+
: undefined,
|
|
108
|
+
email: payload.notificationEmail,
|
|
109
|
+
metadata: {
|
|
110
|
+
sms_notification: payload.notificationPhone,
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
100
115
|
@Reactionary({
|
|
101
116
|
inputSchema: CheckoutMutationInitiateCheckoutSchema,
|
|
102
117
|
outputSchema: CheckoutSchema,
|
|
@@ -114,18 +129,7 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
114
129
|
// zero out existing checkout data?
|
|
115
130
|
const response = await client.store.cart.update(
|
|
116
131
|
payload.cart.identifier.key,
|
|
117
|
-
|
|
118
|
-
billing_address: payload.billingAddress
|
|
119
|
-
? this.mapAddressToStoreAddress(payload.billingAddress)
|
|
120
|
-
: undefined,
|
|
121
|
-
shipping_address: payload.billingAddress
|
|
122
|
-
? this.mapAddressToStoreAddress(payload.billingAddress)
|
|
123
|
-
: undefined,
|
|
124
|
-
email: payload.notificationEmail,
|
|
125
|
-
metadata: {
|
|
126
|
-
sms_notification: payload.notificationPhone,
|
|
127
|
-
},
|
|
128
|
-
},
|
|
132
|
+
this.initiateCheckoutForCartPayload(payload),
|
|
129
133
|
{
|
|
130
134
|
fields: this.includedFields,
|
|
131
135
|
}
|
|
@@ -148,6 +152,13 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
148
152
|
return success(this.parseSingle(response.cart));
|
|
149
153
|
}
|
|
150
154
|
|
|
155
|
+
|
|
156
|
+
protected setShippingAddressPayload(payload: CheckoutMutationSetShippingAddress): StoreUpdateCart {
|
|
157
|
+
return {
|
|
158
|
+
shipping_address: this.mapAddressToStoreAddress(payload.shippingAddress),
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
151
162
|
@Reactionary({
|
|
152
163
|
inputSchema: CheckoutMutationSetShippingAddressSchema,
|
|
153
164
|
outputSchema: CheckoutSchema,
|
|
@@ -159,18 +170,21 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
159
170
|
|
|
160
171
|
const response = await client.store.cart.update(
|
|
161
172
|
payload.checkout.key,
|
|
162
|
-
|
|
163
|
-
shipping_address: this.mapAddressToStoreAddress(
|
|
164
|
-
payload.shippingAddress
|
|
165
|
-
),
|
|
166
|
-
},
|
|
173
|
+
this.setShippingAddressPayload(payload),
|
|
167
174
|
{
|
|
168
175
|
fields: this.includedFields,
|
|
169
|
-
}
|
|
176
|
+
},
|
|
170
177
|
);
|
|
171
178
|
return success(this.parseSingle(response.cart));
|
|
172
179
|
}
|
|
173
180
|
|
|
181
|
+
|
|
182
|
+
protected getAvailableShippingMethodsPayload(payload: CheckoutQueryForAvailableShippingMethods) {
|
|
183
|
+
return {
|
|
184
|
+
cart_id: payload.checkout.key,
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
174
188
|
@Reactionary({
|
|
175
189
|
inputSchema: CheckoutQueryForAvailableShippingMethodsSchema,
|
|
176
190
|
outputSchema: z.array(ShippingMethodSchema),
|
|
@@ -187,9 +201,9 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
187
201
|
}
|
|
188
202
|
|
|
189
203
|
const shippingMethodResponse =
|
|
190
|
-
await client.store.fulfillment.listCartOptions(
|
|
191
|
-
|
|
192
|
-
|
|
204
|
+
await client.store.fulfillment.listCartOptions(
|
|
205
|
+
this.getAvailableShippingMethodsPayload(payload)
|
|
206
|
+
);
|
|
193
207
|
|
|
194
208
|
const shippingMethods: ShippingMethod[] = [];
|
|
195
209
|
|
|
@@ -218,6 +232,12 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
218
232
|
return success(shippingMethods);
|
|
219
233
|
}
|
|
220
234
|
|
|
235
|
+
protected getAvailablePaymentMethodsPayload(payload: CheckoutQueryForAvailablePaymentMethods, regionId: string) {
|
|
236
|
+
return {
|
|
237
|
+
region_id: regionId,
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
221
241
|
@Reactionary({
|
|
222
242
|
inputSchema: CheckoutQueryForAvailablePaymentMethodsSchema,
|
|
223
243
|
outputSchema: z.array(PaymentMethodSchema),
|
|
@@ -234,10 +254,12 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
234
254
|
}
|
|
235
255
|
const checkout = await client.store.cart.retrieve(payload.checkout.key);
|
|
236
256
|
const paymentMethodResponse =
|
|
237
|
-
await client.store.payment.listPaymentProviders(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
257
|
+
await client.store.payment.listPaymentProviders(
|
|
258
|
+
this.getAvailablePaymentMethodsPayload(
|
|
259
|
+
payload,
|
|
260
|
+
checkout.cart.region_id || (await this.medusaApi.getActiveRegion()).id
|
|
261
|
+
)
|
|
262
|
+
);
|
|
241
263
|
|
|
242
264
|
const paymentMethods: PaymentMethod[] = [];
|
|
243
265
|
|
|
@@ -267,6 +289,18 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
267
289
|
return success(paymentMethods);
|
|
268
290
|
}
|
|
269
291
|
|
|
292
|
+
|
|
293
|
+
protected addPaymentInstructionPayload(payload: CheckoutMutationAddPaymentInstruction): StoreInitializePaymentSession {
|
|
294
|
+
return {
|
|
295
|
+
provider_id: payload.paymentInstruction.paymentMethod.method,
|
|
296
|
+
data: payload.paymentInstruction.protocolData.reduce((acc, curr) => {
|
|
297
|
+
acc[curr.key] = curr.value;
|
|
298
|
+
return acc;
|
|
299
|
+
}, {} as Record<string, string>),
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
|
|
270
304
|
@Reactionary({
|
|
271
305
|
inputSchema: CheckoutMutationAddPaymentInstructionSchema,
|
|
272
306
|
outputSchema: CheckoutSchema,
|
|
@@ -287,13 +321,7 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
287
321
|
);
|
|
288
322
|
|
|
289
323
|
const paymentSessionResponse =
|
|
290
|
-
await client.store.payment.initiatePaymentSession(cartResponse.cart,
|
|
291
|
-
provider_id: payload.paymentInstruction.paymentMethod.method,
|
|
292
|
-
data: payload.paymentInstruction.protocolData.reduce((acc, curr) => {
|
|
293
|
-
acc[curr.key] = curr.value;
|
|
294
|
-
return acc;
|
|
295
|
-
}, {} as Record<string, string>),
|
|
296
|
-
});
|
|
324
|
+
await client.store.payment.initiatePaymentSession(cartResponse.cart, this.addPaymentInstructionPayload(payload));
|
|
297
325
|
|
|
298
326
|
const updatedCartResponse = await client.store.cart.retrieve(
|
|
299
327
|
payload.checkout.key,
|
|
@@ -323,6 +351,17 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
323
351
|
throw new Error('Method not implemented.');
|
|
324
352
|
}
|
|
325
353
|
|
|
354
|
+
protected setShippingInstructionPayload(payload: CheckoutMutationSetShippingInstruction): StoreAddCartShippingMethods {
|
|
355
|
+
return {
|
|
356
|
+
option_id: payload.shippingInstruction.shippingMethod.key,
|
|
357
|
+
data: {
|
|
358
|
+
consent_for_unattended_delivery:
|
|
359
|
+
payload.shippingInstruction.consentForUnattendedDelivery + '',
|
|
360
|
+
instructions: payload.shippingInstruction.instructions || '',
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
326
365
|
@Reactionary({
|
|
327
366
|
inputSchema: CheckoutMutationSetShippingInstructionSchema,
|
|
328
367
|
outputSchema: CheckoutSchema,
|
|
@@ -335,14 +374,7 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
335
374
|
try {
|
|
336
375
|
// Set shipping method
|
|
337
376
|
if (payload.shippingInstruction.shippingMethod) {
|
|
338
|
-
await client.store.cart.addShippingMethod(medusaId.key,
|
|
339
|
-
option_id: payload.shippingInstruction.shippingMethod.key,
|
|
340
|
-
data: {
|
|
341
|
-
consent_for_unattended_delivery:
|
|
342
|
-
payload.shippingInstruction.consentForUnattendedDelivery + '',
|
|
343
|
-
instructions: payload.shippingInstruction.instructions || '',
|
|
344
|
-
},
|
|
345
|
-
});
|
|
377
|
+
await client.store.cart.addShippingMethod(medusaId.key, this.setShippingInstructionPayload(payload));
|
|
346
378
|
}
|
|
347
379
|
|
|
348
380
|
// for now, we store a backup of the shipping instruction in metadata
|
|
@@ -369,6 +401,11 @@ export class MedusaCheckoutProvider extends CheckoutProvider {
|
|
|
369
401
|
handleProviderError('set shipping method', error);
|
|
370
402
|
}
|
|
371
403
|
}
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
372
409
|
@Reactionary({
|
|
373
410
|
inputSchema: CheckoutMutationFinalizeCheckoutSchema,
|
|
374
411
|
outputSchema: CheckoutSchema,
|
|
@@ -44,14 +44,9 @@ export class MedusaOrderSearchProvider extends OrderSearchProvider {
|
|
|
44
44
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
outputSchema: OrderSearchResultSchema,
|
|
50
|
-
})
|
|
51
|
-
public async queryByTerm(payload: OrderSearchQueryByTerm): Promise<Result<OrderSearchResult>> {
|
|
52
|
-
debug('queryByTerm', payload);
|
|
47
|
+
protected queryByTermPayload(payload: OrderSearchQueryByTerm): any {
|
|
48
|
+
const filters: any = {};
|
|
53
49
|
|
|
54
|
-
const medusa = await this.medusaApi.getClient();
|
|
55
50
|
|
|
56
51
|
if (payload.search.term) {
|
|
57
52
|
debug('Searching orders by term is not supported in Medusa');
|
|
@@ -67,10 +62,12 @@ export class MedusaOrderSearchProvider extends OrderSearchProvider {
|
|
|
67
62
|
if (payload.search.endDate) {
|
|
68
63
|
debug('Searching orders by end date is not supported in Medusa');
|
|
69
64
|
}
|
|
65
|
+
|
|
70
66
|
/*
|
|
71
67
|
if (payload.search.user && payload.search.user.userId) {
|
|
72
68
|
debug('Searching orders by customer ID is not supported in Medusa');
|
|
73
69
|
} */
|
|
70
|
+
|
|
74
71
|
const statusFilter: MedusaOrderStatus[] = (payload.search.orderStatus ?? []).map((status) => {
|
|
75
72
|
let retStatus: MedusaOrderStatus = 'draft';
|
|
76
73
|
if (status === 'AwaitingPayment') {
|
|
@@ -88,15 +85,29 @@ export class MedusaOrderSearchProvider extends OrderSearchProvider {
|
|
|
88
85
|
return retStatus;
|
|
89
86
|
});
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
const response = await medusa.store.order.list({
|
|
88
|
+
return {
|
|
93
89
|
status: statusFilter,
|
|
94
90
|
limit: payload.search.paginationOptions.pageSize,
|
|
95
91
|
offset:
|
|
96
92
|
(payload.search.paginationOptions.pageNumber - 1) *
|
|
97
93
|
payload.search.paginationOptions.pageSize,
|
|
98
94
|
|
|
99
|
-
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
@Reactionary({
|
|
99
|
+
inputSchema: OrderSearchQueryByTermSchema,
|
|
100
|
+
outputSchema: OrderSearchResultSchema,
|
|
101
|
+
})
|
|
102
|
+
public async queryByTerm(payload: OrderSearchQueryByTerm): Promise<Result<OrderSearchResult>> {
|
|
103
|
+
debug('queryByTerm', payload);
|
|
104
|
+
|
|
105
|
+
const medusa = await this.medusaApi.getClient();
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
const response = await medusa.store.order.list(this.queryByTermPayload(payload));
|
|
100
111
|
|
|
101
112
|
const result = this.parsePaginatedResult(response, payload) as OrderSearchResult;
|
|
102
113
|
if (debug.enabled) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { StoreProduct, StoreProductVariant } from '@medusajs/types';
|
|
1
|
+
import type { StoreCollection, StoreProduct, StoreProductVariant } from '@medusajs/types';
|
|
2
2
|
import {
|
|
3
3
|
error,
|
|
4
4
|
ImageSchema,
|
|
@@ -42,6 +42,14 @@ export class MedusaProductRecommendationsProvider extends ProductRecommendations
|
|
|
42
42
|
this.medusaApi = medusaApi;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
|
|
46
|
+
protected getCollectionPayload(query: ProductRecommendationsByCollectionQuery, collection: StoreCollection) {
|
|
47
|
+
return {
|
|
48
|
+
collection_id: [collection.id],
|
|
49
|
+
limit: query.numberOfRecommendations,
|
|
50
|
+
fields: '+variants.id,+variants.sku,+external_id',
|
|
51
|
+
}
|
|
52
|
+
}
|
|
45
53
|
/**
|
|
46
54
|
* Get product recommendations from a Medusa collection
|
|
47
55
|
*
|
|
@@ -82,11 +90,7 @@ export class MedusaProductRecommendationsProvider extends ProductRecommendations
|
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
// Fetch products from the collection
|
|
85
|
-
const productsResponse = await client.store.product.list(
|
|
86
|
-
collection_id: [collection.id],
|
|
87
|
-
limit: query.numberOfRecommendations,
|
|
88
|
-
fields: '+variants.id,+variants.sku,+external_id',
|
|
89
|
-
});
|
|
93
|
+
const productsResponse = await client.store.product.list(this.getCollectionPayload(query, collection));
|
|
90
94
|
|
|
91
95
|
if (debug.enabled) {
|
|
92
96
|
debug(`Found ${productsResponse.products.length} products in collection`);
|
|
@@ -88,6 +88,22 @@ export class MedusaSearchProvider extends ProductSearchProvider {
|
|
|
88
88
|
return candidate || null;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
|
|
92
|
+
protected queryByTermPayload(payload: ProductSearchQueryByTerm, categoryIdToFind: string | null): any {
|
|
93
|
+
|
|
94
|
+
const finalSearch = (payload.search.term || '').trim().replace('*', '');
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
q: finalSearch,
|
|
98
|
+
...(categoryIdToFind ? { category_id: categoryIdToFind } : {}),
|
|
99
|
+
limit: payload.search.paginationOptions.pageSize,
|
|
100
|
+
fields: '+metadata.*,+external_id',
|
|
101
|
+
offset:
|
|
102
|
+
(payload.search.paginationOptions.pageNumber - 1) *
|
|
103
|
+
payload.search.paginationOptions.pageSize,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
91
107
|
@Reactionary({
|
|
92
108
|
inputSchema: ProductSearchQueryByTermSchema,
|
|
93
109
|
outputSchema: ProductSearchResultSchema,
|
|
@@ -121,16 +137,9 @@ export class MedusaSearchProvider extends ProductSearchProvider {
|
|
|
121
137
|
);
|
|
122
138
|
}
|
|
123
139
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
...(categoryIdToFind ? { category_id: categoryIdToFind } : {}),
|
|
128
|
-
limit: payload.search.paginationOptions.pageSize,
|
|
129
|
-
fields: '+metadata.*,+external_id',
|
|
130
|
-
offset:
|
|
131
|
-
(payload.search.paginationOptions.pageNumber - 1) *
|
|
132
|
-
payload.search.paginationOptions.pageSize,
|
|
133
|
-
});
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
const response = await client.store.product.list(this.queryByTermPayload(payload, categoryIdToFind));
|
|
134
143
|
|
|
135
144
|
const result = this.parsePaginatedResult(response) as ProductSearchResult;
|
|
136
145
|
if (debug.enabled) {
|
|
@@ -28,6 +28,15 @@ export class MedusaProductProvider extends ProductProvider {
|
|
|
28
28
|
this.config = config;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
protected getByIdPayload(payload: ProductQueryById) {
|
|
32
|
+
return {
|
|
33
|
+
external_id: payload.identifier.key,
|
|
34
|
+
limit: 1,
|
|
35
|
+
offset: 0,
|
|
36
|
+
fields: this.alwaysIncludedFields.join(','),
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
31
40
|
@Reactionary({
|
|
32
41
|
inputSchema: ProductQueryByIdSchema,
|
|
33
42
|
outputSchema: ProductSchema,
|
|
@@ -42,12 +51,7 @@ export class MedusaProductProvider extends ProductProvider {
|
|
|
42
51
|
debug(`Fetching product by ID: ${payload.identifier.key}`);
|
|
43
52
|
}
|
|
44
53
|
const response = await client.store.product.list(
|
|
45
|
-
|
|
46
|
-
external_id: payload.identifier.key,
|
|
47
|
-
limit: 1,
|
|
48
|
-
offset: 0,
|
|
49
|
-
fields: this.alwaysIncludedFields.join(','),
|
|
50
|
-
},
|
|
54
|
+
this.getByIdPayload(payload)
|
|
51
55
|
);
|
|
52
56
|
|
|
53
57
|
if (response.count === 0) {
|
|
@@ -59,6 +63,15 @@ export class MedusaProductProvider extends ProductProvider {
|
|
|
59
63
|
return success(this.parseSingle(response.products[0]));
|
|
60
64
|
}
|
|
61
65
|
|
|
66
|
+
protected getBySlugPayload(payload: ProductQueryBySlug) {
|
|
67
|
+
return {
|
|
68
|
+
handle: payload.slug,
|
|
69
|
+
limit: 1,
|
|
70
|
+
offset: 0,
|
|
71
|
+
fields: this.alwaysIncludedFields.join(','),
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
62
75
|
|
|
63
76
|
@Reactionary({
|
|
64
77
|
inputSchema: ProductQueryBySlugSchema,
|
|
@@ -74,12 +87,7 @@ export class MedusaProductProvider extends ProductProvider {
|
|
|
74
87
|
debug(`Fetching product by slug: ${payload.slug}`);
|
|
75
88
|
}
|
|
76
89
|
|
|
77
|
-
const response = await client.store.product.list(
|
|
78
|
-
handle: payload.slug,
|
|
79
|
-
limit: 1,
|
|
80
|
-
offset: 0,
|
|
81
|
-
fields: this.alwaysIncludedFields.join(','),
|
|
82
|
-
});
|
|
90
|
+
const response = await client.store.product.list(this.getBySlugPayload(payload));
|
|
83
91
|
|
|
84
92
|
if (debug.enabled) {
|
|
85
93
|
debug(`Found ${response.count} products for slug: ${payload.slug}`);
|
|
@@ -79,6 +79,14 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
79
79
|
return success(model);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
protected updatePayload(payload: ProfileMutationUpdate) {
|
|
83
|
+
const updateData: any = {};
|
|
84
|
+
if (payload.phone !== undefined) {
|
|
85
|
+
updateData.phone = payload.phone;
|
|
86
|
+
}
|
|
87
|
+
return updateData;
|
|
88
|
+
}
|
|
89
|
+
|
|
82
90
|
@Reactionary({
|
|
83
91
|
inputSchema: ProfileMutationUpdateSchema,
|
|
84
92
|
outputSchema: ProfileSchema,
|
|
@@ -98,14 +106,16 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
98
106
|
|
|
99
107
|
const customer = customerResponse.customer;
|
|
100
108
|
|
|
101
|
-
const updatedResponse = await client.store.customer.update({
|
|
102
|
-
phone: payload.phone ?? customer.phone,
|
|
103
|
-
}, { fields: this.includedFields.join(',') });
|
|
109
|
+
const updatedResponse = await client.store.customer.update(this.updatePayload(payload), { fields: this.includedFields.join(',') });
|
|
104
110
|
|
|
105
111
|
const model = this.parseSingle(updatedResponse.customer);
|
|
106
112
|
return success(model);
|
|
107
113
|
}
|
|
108
114
|
|
|
115
|
+
protected addShippingAddressPayload(payload: ProfileMutationAddShippingAddress) {
|
|
116
|
+
return this.createMedusaAddress(payload.address);
|
|
117
|
+
}
|
|
118
|
+
|
|
109
119
|
@Reactionary({
|
|
110
120
|
inputSchema: ProfileMutationAddShippingAddressSchema,
|
|
111
121
|
outputSchema: ProfileSchema,
|
|
@@ -115,7 +125,6 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
115
125
|
|
|
116
126
|
const client = await this.medusaApi.getClient();
|
|
117
127
|
|
|
118
|
-
const medusaAddress = this.createMedusaAddress(payload.address);
|
|
119
128
|
|
|
120
129
|
// check if any address with the same nickName exists
|
|
121
130
|
const customer = await client.store.customer.retrieve({ fields: this.includedFields.join(',') });
|
|
@@ -133,7 +142,7 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
133
142
|
});
|
|
134
143
|
}
|
|
135
144
|
|
|
136
|
-
const response = await client.store.customer.createAddress(
|
|
145
|
+
const response = await client.store.customer.createAddress(this.addShippingAddressPayload(payload), { fields: this.includedFields.join(',') });
|
|
137
146
|
if (!response.customer) {
|
|
138
147
|
return error<InvalidInputError>({
|
|
139
148
|
type: 'InvalidInput',
|
|
@@ -145,6 +154,10 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
145
154
|
return success(model);
|
|
146
155
|
}
|
|
147
156
|
|
|
157
|
+
protected updateShippingAddressPayload(payload: ProfileMutationUpdateShippingAddress) {
|
|
158
|
+
return this.createMedusaAddress(payload.address);
|
|
159
|
+
}
|
|
160
|
+
|
|
148
161
|
@Reactionary({
|
|
149
162
|
inputSchema: ProfileMutationUpdateShippingAddressSchema,
|
|
150
163
|
outputSchema: ProfileSchema,
|
|
@@ -162,8 +175,6 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
162
175
|
});
|
|
163
176
|
}
|
|
164
177
|
|
|
165
|
-
const medusaAddress = this.createMedusaAddress(payload.address);
|
|
166
|
-
|
|
167
178
|
const existingAddress = customer.customer.addresses.find(addr => addr.address_name === payload.address.identifier.nickName);
|
|
168
179
|
if (!existingAddress) {
|
|
169
180
|
return error<NotFoundError>({
|
|
@@ -172,7 +183,7 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
172
183
|
});
|
|
173
184
|
}
|
|
174
185
|
|
|
175
|
-
const response = await client.store.customer.updateAddress(existingAddress.id,
|
|
186
|
+
const response = await client.store.customer.updateAddress(existingAddress.id, this.updateShippingAddressPayload(payload), { fields: this.includedFields.join(',') });
|
|
176
187
|
if (!response.customer) {
|
|
177
188
|
return error<InvalidInputError>({
|
|
178
189
|
type: 'InvalidInput',
|
|
@@ -226,6 +237,12 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
226
237
|
}
|
|
227
238
|
|
|
228
239
|
|
|
240
|
+
protected makeShippingAddressDefaultPayload(payload: ProfileMutationMakeShippingAddressDefault) {
|
|
241
|
+
return {
|
|
242
|
+
is_default_shipping: true
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
229
246
|
@Reactionary({
|
|
230
247
|
inputSchema: ProfileMutationMakeShippingAddressDefaultSchema,
|
|
231
248
|
outputSchema: ProfileSchema,
|
|
@@ -251,15 +268,23 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
251
268
|
});
|
|
252
269
|
}
|
|
253
270
|
|
|
254
|
-
const response = await client.store.customer.updateAddress(
|
|
255
|
-
|
|
256
|
-
|
|
271
|
+
const response = await client.store.customer.updateAddress(
|
|
272
|
+
existingAddress.id,
|
|
273
|
+
this.makeShippingAddressDefaultPayload(payload),
|
|
274
|
+
{ fields: this.includedFields.join(',') }
|
|
257
275
|
);
|
|
258
276
|
|
|
259
277
|
const model = this.parseSingle(response.customer!);
|
|
260
278
|
return success(model);
|
|
261
279
|
}
|
|
262
280
|
|
|
281
|
+
|
|
282
|
+
protected setBillingAddressPayload(payload: ProfileMutationSetBillingAddress) {
|
|
283
|
+
const newAddr = this.createMedusaAddress(payload.address);
|
|
284
|
+
newAddr.is_default_billing = true;
|
|
285
|
+
return newAddr;
|
|
286
|
+
}
|
|
287
|
+
|
|
263
288
|
@Reactionary({
|
|
264
289
|
inputSchema: ProfileMutationSetBillingAddressSchema,
|
|
265
290
|
outputSchema: ProfileSchema,
|
|
@@ -289,9 +314,9 @@ export class MedusaProfileProvider extends ProfileProvider {
|
|
|
289
314
|
}
|
|
290
315
|
|
|
291
316
|
|
|
292
|
-
const newAddr = this.createMedusaAddress(payload.address);
|
|
293
|
-
newAddr.is_default_billing = true;
|
|
294
317
|
|
|
318
|
+
|
|
319
|
+
const newAddr = this.setBillingAddressPayload(payload);
|
|
295
320
|
// two scenarios: Either we already have a billing addres, in which case we update it, or we dont and we need to create it.
|
|
296
321
|
const existingBillingAddress = customer.addresses.find(addr => addr.is_default_billing);
|
|
297
322
|
if (existingBillingAddress) {
|
|
@@ -52,17 +52,7 @@ export class MeilisearchOrderSearchProvider extends OrderSearchProvider {
|
|
|
52
52
|
this.config = config;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
inputSchema: OrderSearchQueryByTermSchema,
|
|
57
|
-
outputSchema: OrderSearchResultSchema,
|
|
58
|
-
})
|
|
59
|
-
public async queryByTerm(payload: OrderSearchQueryByTerm): Promise<Result<OrderSearchResult>> {
|
|
60
|
-
const client = new MeiliSearch({
|
|
61
|
-
host: this.config.apiUrl,
|
|
62
|
-
apiKey: this.config.apiKey,
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const index = client.index(this.config.orderIndexName);
|
|
55
|
+
protected queryByTermPayload(payload: OrderSearchQueryByTerm): SearchParams {
|
|
66
56
|
|
|
67
57
|
const filters: string[] = [];
|
|
68
58
|
|
|
@@ -103,10 +93,25 @@ export class MeilisearchOrderSearchProvider extends OrderSearchProvider {
|
|
|
103
93
|
filter: filters.length > 0 ? filters.join(' AND ') : undefined,
|
|
104
94
|
sort: ['orderDateTimestamp:desc'],
|
|
105
95
|
};
|
|
96
|
+
return searchOptions
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Reactionary({
|
|
100
|
+
inputSchema: OrderSearchQueryByTermSchema,
|
|
101
|
+
outputSchema: OrderSearchResultSchema,
|
|
102
|
+
})
|
|
103
|
+
public async queryByTerm(payload: OrderSearchQueryByTerm): Promise<Result<OrderSearchResult>> {
|
|
104
|
+
const client = new MeiliSearch({
|
|
105
|
+
host: this.config.apiUrl,
|
|
106
|
+
apiKey: this.config.apiKey,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const index = client.index(this.config.orderIndexName);
|
|
110
|
+
|
|
106
111
|
|
|
107
112
|
const remote = await index.search<MeilisearchNativeOrderRecord>(
|
|
108
113
|
payload.search.term || '',
|
|
109
|
-
|
|
114
|
+
this.queryByTermPayload(payload)
|
|
110
115
|
);
|
|
111
116
|
|
|
112
117
|
const result = this.parsePaginatedResult(remote, payload) as OrderSearchResult;
|