@unchainedshop/api 2.9.4 → 2.10.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.
Files changed (63) hide show
  1. package/lib/loaders/index.js +15 -13
  2. package/lib/loaders/index.js.map +1 -1
  3. package/lib/resolvers/mutations/orders/addCartDiscount.js +5 -2
  4. package/lib/resolvers/mutations/orders/addCartDiscount.js.map +1 -1
  5. package/lib/resolvers/mutations/orders/addCartProduct.js +3 -1
  6. package/lib/resolvers/mutations/orders/addCartProduct.js.map +1 -1
  7. package/lib/resolvers/mutations/orders/addCartQuotation.js +3 -1
  8. package/lib/resolvers/mutations/orders/addCartQuotation.js.map +1 -1
  9. package/lib/resolvers/mutations/orders/addMultipleCartProducts.js +3 -1
  10. package/lib/resolvers/mutations/orders/addMultipleCartProducts.js.map +1 -1
  11. package/lib/resolvers/mutations/orders/checkoutCart.d.ts +0 -1
  12. package/lib/resolvers/mutations/orders/checkoutCart.js +5 -4
  13. package/lib/resolvers/mutations/orders/checkoutCart.js.map +1 -1
  14. package/lib/resolvers/mutations/orders/confirmOrder.d.ts +0 -1
  15. package/lib/resolvers/mutations/orders/confirmOrder.js +1 -1
  16. package/lib/resolvers/mutations/orders/confirmOrder.js.map +1 -1
  17. package/lib/resolvers/mutations/orders/createCart.js +1 -1
  18. package/lib/resolvers/mutations/orders/createCart.js.map +1 -1
  19. package/lib/resolvers/mutations/orders/emptyCart.js +7 -4
  20. package/lib/resolvers/mutations/orders/emptyCart.js.map +1 -1
  21. package/lib/resolvers/mutations/orders/rejectOrder.d.ts +0 -1
  22. package/lib/resolvers/mutations/orders/rejectOrder.js +1 -1
  23. package/lib/resolvers/mutations/orders/rejectOrder.js.map +1 -1
  24. package/lib/resolvers/mutations/orders/signPaymentProviderForCheckout.js +7 -3
  25. package/lib/resolvers/mutations/orders/signPaymentProviderForCheckout.js.map +1 -1
  26. package/lib/resolvers/mutations/orders/updateCart.js +4 -2
  27. package/lib/resolvers/mutations/orders/updateCart.js.map +1 -1
  28. package/lib/resolvers/mutations/orders/updateOrderDeliveryPickUp.js +1 -4
  29. package/lib/resolvers/mutations/orders/updateOrderDeliveryPickUp.js.map +1 -1
  30. package/lib/resolvers/mutations/orders/updateOrderDeliveryShipping.js +1 -4
  31. package/lib/resolvers/mutations/orders/updateOrderDeliveryShipping.js.map +1 -1
  32. package/lib/resolvers/mutations/orders/updateOrderPaymentCard.js +1 -4
  33. package/lib/resolvers/mutations/orders/updateOrderPaymentCard.js.map +1 -1
  34. package/lib/resolvers/mutations/orders/updateOrderPaymentGeneric.js +1 -4
  35. package/lib/resolvers/mutations/orders/updateOrderPaymentGeneric.js.map +1 -1
  36. package/lib/resolvers/mutations/orders/updateOrderPaymentInvoice.js +1 -4
  37. package/lib/resolvers/mutations/orders/updateOrderPaymentInvoice.js.map +1 -1
  38. package/lib/resolvers/mutations/utils/getOrderCart.js +3 -8
  39. package/lib/resolvers/mutations/utils/getOrderCart.js.map +1 -1
  40. package/lib/resolvers/type/user-types.js +7 -3
  41. package/lib/resolvers/type/user-types.js.map +1 -1
  42. package/lib/schema/mutation.js +3 -3
  43. package/package.json +8 -8
  44. package/src/loaders/index.ts +240 -202
  45. package/src/resolvers/mutations/orders/addCartDiscount.ts +5 -2
  46. package/src/resolvers/mutations/orders/addCartProduct.ts +8 -1
  47. package/src/resolvers/mutations/orders/addCartQuotation.ts +3 -0
  48. package/src/resolvers/mutations/orders/addMultipleCartProducts.ts +6 -1
  49. package/src/resolvers/mutations/orders/checkoutCart.ts +5 -5
  50. package/src/resolvers/mutations/orders/confirmOrder.ts +1 -2
  51. package/src/resolvers/mutations/orders/createCart.ts +2 -2
  52. package/src/resolvers/mutations/orders/emptyCart.ts +7 -4
  53. package/src/resolvers/mutations/orders/rejectOrder.ts +1 -2
  54. package/src/resolvers/mutations/orders/signPaymentProviderForCheckout.ts +6 -2
  55. package/src/resolvers/mutations/orders/updateCart.ts +3 -2
  56. package/src/resolvers/mutations/orders/updateOrderDeliveryPickUp.ts +1 -5
  57. package/src/resolvers/mutations/orders/updateOrderDeliveryShipping.ts +1 -5
  58. package/src/resolvers/mutations/orders/updateOrderPaymentCard.ts +1 -5
  59. package/src/resolvers/mutations/orders/updateOrderPaymentGeneric.ts +1 -5
  60. package/src/resolvers/mutations/orders/updateOrderPaymentInvoice.ts +1 -5
  61. package/src/resolvers/mutations/utils/getOrderCart.ts +6 -8
  62. package/src/resolvers/type/user-types.ts +7 -3
  63. package/src/schema/mutation.ts +3 -3
@@ -1,6 +1,11 @@
1
1
  import { log } from '@unchainedshop/logger';
2
2
  import { Context, Root } from '@unchainedshop/types/api.js';
3
- import { ProductNotFoundError, OrderQuantityTooLowError, InvalidIdError } from '../../../errors.js';
3
+ import {
4
+ ProductNotFoundError,
5
+ OrderQuantityTooLowError,
6
+ InvalidIdError,
7
+ OrderWrongStatusError,
8
+ } from '../../../errors.js';
4
9
  import { getOrderCart } from '../utils/getOrderCart.js';
5
10
 
6
11
  export default async function addCartProduct(
@@ -24,6 +29,8 @@ export default async function addCartProduct(
24
29
  if (!product) throw new ProductNotFoundError({ productId });
25
30
 
26
31
  const order = await getOrderCart({ orderId, user }, context);
32
+ if (!modules.orders.isCart(order)) throw new OrderWrongStatusError({ status: order.status });
33
+
27
34
  const orderPosition = await modules.orders.positions.addProductItem(
28
35
  {
29
36
  quantity,
@@ -7,6 +7,7 @@ import {
7
7
  QuotationWrongStatusError,
8
8
  OrderQuantityTooLowError,
9
9
  InvalidIdError,
10
+ OrderWrongStatusError,
10
11
  } from '../../../errors.js';
11
12
  import { getOrderCart } from '../utils/getOrderCart.js';
12
13
 
@@ -42,6 +43,8 @@ export default async function addCartQuotation(
42
43
  }
43
44
 
44
45
  const order = await getOrderCart({ orderId, user }, context);
46
+ if (!modules.orders.isCart(order)) throw new OrderWrongStatusError({ status: order.status });
47
+
45
48
  const product = await modules.products.findProduct({
46
49
  productId: quotation.productId,
47
50
  });
@@ -1,7 +1,11 @@
1
1
  import { log } from '@unchainedshop/logger';
2
2
  import { Context, Root } from '@unchainedshop/types/api.js';
3
3
  import { Configuration } from '@unchainedshop/types/common.js';
4
- import { ProductNotFoundError, OrderQuantityTooLowError } from '../../../errors.js';
4
+ import {
5
+ ProductNotFoundError,
6
+ OrderQuantityTooLowError,
7
+ OrderWrongStatusError,
8
+ } from '../../../errors.js';
5
9
  import { getOrderCart } from '../utils/getOrderCart.js';
6
10
 
7
11
  export default async function addMultipleCartProducts(
@@ -38,6 +42,7 @@ export default async function addMultipleCartProducts(
38
42
  );
39
43
 
40
44
  const order = await getOrderCart({ orderId, user }, context);
45
+ if (!modules.orders.isCart(order)) throw new OrderWrongStatusError({ status: order.status });
41
46
 
42
47
  // Reduce is used to wait for each product to be added before processing the next (sequential processing)
43
48
  return itemsWithProducts.reduce(async (positionsPromise, { product, quantity, configuration }) => {
@@ -7,7 +7,6 @@ export default async function checkoutCart(
7
7
  root: Root,
8
8
  params: {
9
9
  orderId: string;
10
- orderContext?: any;
11
10
  paymentContext?: any;
12
11
  deliveryContext?: any;
13
12
  },
@@ -18,16 +17,17 @@ export default async function checkoutCart(
18
17
 
19
18
  log('mutation checkoutCart', { orderId: forceOrderId, userId });
20
19
 
21
- const orderId = (await getOrderCart({ orderId: forceOrderId, user }, context))._id;
20
+ // Do not check for order status here! The checkout method will act accordingly
21
+ let order = await getOrderCart({ orderId: forceOrderId, user }, context);
22
22
 
23
23
  try {
24
- const order = await modules.orders.checkout(orderId, transactionContext, context);
24
+ order = await modules.orders.checkout(order._id, transactionContext, context);
25
25
  return order;
26
26
  } catch (error) {
27
- log(error.message, { userId, orderId, level: LogLevel.Error });
27
+ log(error.message, { userId, orderId: order._id, level: LogLevel.Error });
28
28
  throw new OrderCheckoutError({
29
29
  userId,
30
- orderId,
30
+ orderId: order._id,
31
31
  ...transactionContext,
32
32
  detailCode: error.name || error.code,
33
33
  detailMessage: error.message,
@@ -7,7 +7,6 @@ export default async function confirmOrder(
7
7
  root: Root,
8
8
  params: {
9
9
  orderId: string;
10
- orderContext?: any;
11
10
  paymentContext?: any;
12
11
  deliveryContext?: any;
13
12
  },
@@ -27,5 +26,5 @@ export default async function confirmOrder(
27
26
  throw new OrderWrongStatusError({ status: order.status });
28
27
  }
29
28
 
30
- return modules.orders.confirm(order._id, transactionContext, context);
29
+ return modules.orders.confirm(order, transactionContext, context);
31
30
  }
@@ -13,8 +13,8 @@ export default async function createCart(
13
13
  const order = await modules.orders.findOrder({ orderNumber });
14
14
  if (order) throw new OrderNumberAlreadyExistsError({ orderNumber });
15
15
 
16
- return services.orders.createUserCart(
17
- { user, orderNumber, countryCode: context.countryContext },
16
+ return services.orders.nextUserCart(
17
+ { user, orderNumber, countryCode: context.countryContext, forceCartCreation: true },
18
18
  context,
19
19
  );
20
20
  }
@@ -1,6 +1,7 @@
1
1
  import { log } from '@unchainedshop/logger';
2
2
  import { Context, Root } from '@unchainedshop/types/api.js';
3
3
  import { getOrderCart } from '../utils/getOrderCart.js';
4
+ import { OrderWrongStatusError } from '../../../errors.js';
4
5
 
5
6
  export default async function emptyCart(
6
7
  root: Root,
@@ -11,9 +12,11 @@ export default async function emptyCart(
11
12
 
12
13
  log('mutation emptyCart', { userId, orderId });
13
14
 
14
- const cart = await getOrderCart({ orderId, user }, context);
15
- if (!cart) return null;
15
+ const order = await getOrderCart({ orderId, user }, context);
16
+ if (!modules.orders.isCart(order)) throw new OrderWrongStatusError({ status: order.status });
16
17
 
17
- await modules.orders.positions.removePositions({ orderId: cart._id }, context);
18
- return modules.orders.findOrder({ orderId: cart._id });
18
+ if (!order) return null;
19
+
20
+ await modules.orders.positions.removePositions({ orderId: order._id }, context);
21
+ return modules.orders.findOrder({ orderId: order._id });
19
22
  }
@@ -7,7 +7,6 @@ export default async function rejectOrder(
7
7
  root: Root,
8
8
  params: {
9
9
  orderId: string;
10
- orderContext?: any;
11
10
  paymentContext?: any;
12
11
  deliveryContext?: any;
13
12
  },
@@ -25,5 +24,5 @@ export default async function rejectOrder(
25
24
  if (order.status !== OrderStatus.PENDING) {
26
25
  throw new OrderWrongStatusError({ status: order.status });
27
26
  }
28
- return modules.orders.reject(order._id, transactionContext, context);
27
+ return modules.orders.reject(order, transactionContext, context);
29
28
  }
@@ -7,8 +7,8 @@ import {
7
7
  OrderPaymentNotFoundError,
8
8
  OrderPaymentTypeError,
9
9
  OrderWrongPaymentStatusError,
10
+ UserNoCartError,
10
11
  } from '../../../errors.js';
11
- import { getOrderCart } from '../utils/getOrderCart.js';
12
12
 
13
13
  export default async function signPaymentProviderForCheckout(
14
14
  root: Root,
@@ -30,7 +30,11 @@ export default async function signPaymentProviderForCheckout(
30
30
  });
31
31
  if (!orderPayment) throw new OrderPaymentNotFoundError({ orderPaymentId });
32
32
  } else {
33
- const order = await getOrderCart({ user: context.user }, context);
33
+ const order = await modules.orders.cart({
34
+ countryContext: context.countryContext || context.user.lastLogin?.countryCode,
35
+ userId,
36
+ });
37
+ if (!order) throw new UserNoCartError({ userId });
34
38
  orderPayment = await modules.orders.payments.findOrderPayment({
35
39
  orderPaymentId: order.paymentId,
36
40
  });
@@ -2,6 +2,7 @@ import { log } from '@unchainedshop/logger';
2
2
  import { Context, Root } from '@unchainedshop/types/api.js';
3
3
  import { Address, Contact } from '@unchainedshop/types/common.js';
4
4
  import { getOrderCart } from '../utils/getOrderCart.js';
5
+ import { OrderWrongStatusError } from '../../../errors.js';
5
6
 
6
7
  interface UpdateCartParams {
7
8
  orderId?: string;
@@ -20,10 +21,10 @@ export default async function updateCart(root: Root, params: UpdateCartParams, c
20
21
  log('mutation updateCart', { userId });
21
22
 
22
23
  let order = await getOrderCart({ orderId, user }, context);
24
+ if (!modules.orders.isCart(order)) throw new OrderWrongStatusError({ status: order.status });
23
25
 
24
26
  if (meta) {
25
- await modules.orders.updateContext(order._id, meta, context);
26
- order = await modules.orders.findOrder({ orderId: order._id });
27
+ order = await modules.orders.updateContext(order._id, meta, context);
27
28
  }
28
29
 
29
30
  if (billingAddress) {
@@ -33,7 +33,7 @@ export default async function updateOrderDeliveryPickUp(
33
33
  required: DeliveryProviderType.PICKUP,
34
34
  });
35
35
 
36
- await modules.orders.deliveries.updateContext(
36
+ return modules.orders.deliveries.updateContext(
37
37
  orderDeliveryId,
38
38
  {
39
39
  orderPickUpLocationId,
@@ -41,8 +41,4 @@ export default async function updateOrderDeliveryPickUp(
41
41
  },
42
42
  context,
43
43
  );
44
-
45
- return modules.orders.deliveries.findDelivery({
46
- orderDeliveryId,
47
- });
48
44
  }
@@ -32,9 +32,5 @@ export default async function updateOrderDeliveryShipping(
32
32
  required: DeliveryProviderType.SHIPPING,
33
33
  });
34
34
 
35
- await modules.orders.deliveries.updateContext(orderDeliveryId, { address, meta }, context);
36
-
37
- return modules.orders.deliveries.findDelivery({
38
- orderDeliveryId,
39
- });
35
+ return modules.orders.deliveries.updateContext(orderDeliveryId, { address, meta }, context);
40
36
  }
@@ -31,9 +31,5 @@ export default async function updateOrderPaymentCard(
31
31
  required: PaymentProviderType.CARD,
32
32
  });
33
33
 
34
- await modules.orders.payments.updateContext(orderPayment._id, { meta }, context);
35
-
36
- return modules.orders.payments.findOrderPayment({
37
- orderPaymentId,
38
- });
34
+ return modules.orders.payments.updateContext(orderPayment._id, { meta }, context);
39
35
  }
@@ -32,9 +32,5 @@ export default async function updateOrderPaymentGeneric(
32
32
  required: PaymentProviderType.GENERIC,
33
33
  });
34
34
 
35
- await modules.orders.payments.updateContext(orderPayment._id, { meta }, context);
36
-
37
- return modules.orders.payments.findOrderPayment({
38
- orderPaymentId,
39
- });
35
+ return modules.orders.payments.updateContext(orderPayment._id, { meta }, context);
40
36
  }
@@ -32,9 +32,5 @@ export default async function updateOrderPaymentInvoice(
32
32
  required: PaymentProviderType.INVOICE,
33
33
  });
34
34
 
35
- await modules.orders.payments.updateContext(orderPayment._id, { meta }, context);
36
-
37
- return modules.orders.payments.findOrderPayment({
38
- orderPaymentId,
39
- });
35
+ return modules.orders.payments.updateContext(orderPayment._id, { meta }, context);
40
36
  }
@@ -1,21 +1,19 @@
1
1
  import { Context } from '@unchainedshop/types/api.js';
2
2
  import { User } from '@unchainedshop/types/user.js';
3
- import { OrderNotFoundError, OrderWrongStatusError } from '../../../errors.js';
3
+ import { OrderNotFoundError } from '../../../errors.js';
4
4
 
5
5
  export const getOrderCart = async (params: { orderId?: string; user: User }, context: Context) => {
6
- const { countryContext, modules, services } = context;
6
+ const { modules, services } = context;
7
7
  const { orderId, user } = params;
8
8
 
9
9
  if (orderId) {
10
10
  const order = await modules.orders.findOrder({ orderId });
11
11
  if (!order) throw new OrderNotFoundError({ orderId });
12
-
13
- if (!modules.orders.isCart(order)) throw new OrderWrongStatusError({ status: order.status });
14
12
  return order;
15
13
  }
16
14
 
17
- const cart = await modules.orders.cart({ countryContext }, user);
18
- if (cart) return cart;
19
-
20
- return services.orders.createUserCart({ user, countryCode: context.countryContext }, context);
15
+ return services.orders.nextUserCart(
16
+ { user, countryCode: context.countryContext, forceCartCreation: true },
17
+ context,
18
+ );
21
19
  };
@@ -167,7 +167,11 @@ export const User: UserHelperTypes = {
167
167
  async cart(user, params, context) {
168
168
  const { modules, countryContext } = context;
169
169
  await checkAction(context, viewUserOrders, [user, params]);
170
- return modules.orders.cart({ countryContext, orderNumber: params.orderNumber }, user);
170
+ return modules.orders.cart({
171
+ countryContext: countryContext || user.lastLogin?.countryCode,
172
+ orderNumber: params.orderNumber,
173
+ userId: user._id,
174
+ });
171
175
  },
172
176
 
173
177
  async tokens(user, params, context) {
@@ -175,12 +179,12 @@ export const User: UserHelperTypes = {
175
179
  return context.modules.warehousing.findTokensForUser(user);
176
180
  },
177
181
 
178
- country: async (user, params, context) => {
182
+ async country(user, params, context) {
179
183
  await checkAction(context, viewUserPrivateInfos, [user, params]);
180
184
  return context.services.users.getUserCountry(user, context);
181
185
  },
182
186
 
183
- enrollments: async (user, params, context) => {
187
+ async enrollments(user, params, context) {
184
188
  await checkAction(context, viewUserEnrollments, [user, params]);
185
189
  return context.modules.enrollments.findEnrollments({
186
190
  ...(params || {}),
@@ -193,7 +193,7 @@ export default [
193
193
  Process the checkout (automatically charge & deliver if possible), the cart will get
194
194
  transformed to an ordinary order if everything goes well.
195
195
  """
196
- checkoutCart(orderId: ID, orderContext: JSON, paymentContext: JSON, deliveryContext: JSON): Order!
196
+ checkoutCart(orderId: ID, paymentContext: JSON, deliveryContext: JSON): Order!
197
197
 
198
198
  """
199
199
  Change the quantity or configuration of an item in an open order.align-baselineAll
@@ -304,12 +304,12 @@ export default [
304
304
  """
305
305
  Manually confirm an order which is in progress
306
306
  """
307
- confirmOrder(orderId: ID!, orderContext: JSON, paymentContext: JSON, deliveryContext: JSON): Order!
307
+ confirmOrder(orderId: ID!, paymentContext: JSON, deliveryContext: JSON): Order!
308
308
 
309
309
  """
310
310
  Manually reject an order which is in progress
311
311
  """
312
- rejectOrder(orderId: ID!, orderContext: JSON, paymentContext: JSON, deliveryContext: JSON): Order!
312
+ rejectOrder(orderId: ID!, paymentContext: JSON, deliveryContext: JSON): Order!
313
313
 
314
314
  """
315
315
  Manually mark an unpaid/partially paid order as fully paid