rerobe-js-orm 2.3.8 → 2.3.9

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.
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const js_base64_1 = require("js-base64");
4
3
  const OrderFactory_1 = require("./OrderFactory");
5
4
  const Order_1 = require("../../models/Order");
5
+ const OrderHelpers_1 = require("../../helpers/OrderHelpers");
6
+ const orderHelpers = new OrderHelpers_1.default();
6
7
  class OrderFromApp extends OrderFactory_1.default {
7
8
  createOrder(order) {
8
9
  const { id, orderNumber, amount, currencyCode = 'SEK', shippingDetails, appliedDiscount, products, paymentType, paymentMethod, shippingType, shopifyId, shopifyOrderNumber, state, userId, email, salesChannel, tags, paymentDetails, financialStatus, fulfillmentStatus, } = order;
@@ -34,23 +35,7 @@ class OrderFromApp extends OrderFactory_1.default {
34
35
  amount: discount.toFixed(2),
35
36
  currencyCode,
36
37
  };
37
- const lineItems = products.map((item) => ({
38
- title: item.title,
39
- quantity: 1,
40
- variant: {
41
- id: item.variantId ? js_base64_1.Base64.atob(item.variantId) : '',
42
- },
43
- brand: item.brand,
44
- originalTotalPrice: {
45
- amount: item.price,
46
- currencyCode,
47
- },
48
- image: {
49
- originalSrc: item.imageUrls && !!item.imageUrls.length ? item.imageUrls[0] : '',
50
- },
51
- shopifyProductId: item.shopifyId || null,
52
- productId: item.documentId,
53
- }));
38
+ const lineItems = products.map((product) => (orderHelpers.buildLineItemFromProduct(product, currencyCode)));
54
39
  const rerobeOrder = new Order_1.default({
55
40
  id,
56
41
  appliedDiscount,
@@ -3,5 +3,5 @@ export default class OrderHelpers {
3
3
  getShippingTypeFromShopifyObj(shippingLines: ShippingLineRest[] | null | undefined): string;
4
4
  getPaymentTypeUsingTags(tags: string | null | undefined): string | null;
5
5
  getSalesChannelUsingTags(tags: string | null | undefined): string | null;
6
- buildLineItemFromProduct(product: CompleteProduct, currencyCode: string): ReRobeOrderLineItem;
6
+ buildLineItemFromProduct(product: CompleteProduct, currencyCode: string, quantity?: number): ReRobeOrderLineItem;
7
7
  }
@@ -53,16 +53,21 @@ class OrderHelpers {
53
53
  }
54
54
  return null;
55
55
  }
56
- buildLineItemFromProduct(product, currencyCode) {
56
+ buildLineItemFromProduct(product, currencyCode, quantity = 1) {
57
+ const q = quantity || 1;
57
58
  return {
58
59
  title: product.title,
59
- quantity: 1,
60
+ quantity: q,
60
61
  variant: {
61
62
  id: product.variantId || '',
62
63
  },
63
64
  brand: product.brand,
64
65
  originalTotalPrice: {
65
- amount: product.price,
66
+ amount: q * Number(Number(product.price).toFixed(2)),
67
+ currencyCode,
68
+ },
69
+ originalUnitPrice: {
70
+ amount: Number(Number(product.price).toFixed(2)),
66
71
  currencyCode,
67
72
  },
68
73
  image: {
@@ -14,5 +14,7 @@ export default class OrderFormState extends FormState {
14
14
  lineItemLike: CompleteProduct | ReRobeOrderLineItem;
15
15
  quantity: number;
16
16
  }): void;
17
+ discountChangeHandler(val: string): void;
18
+ shippingPriceChangeHandler(val: string): void;
17
19
  private fieldFactory;
18
20
  }
@@ -47,29 +47,42 @@ class OrderFormState extends FormState_1.default {
47
47
  // Add new product to line items
48
48
  if (val.lineItemLike.documentId) {
49
49
  const product = val.lineItemLike;
50
- const lineItemToUpdate = this.orderHelpers.buildLineItemFromProduct(product, this.fields.currencyCode.inputValue || 'SEK');
50
+ const lineItemToUpdate = this.orderHelpers.buildLineItemFromProduct(product, this.fields.currencyCode.inputValue || 'SEK', val.quantity);
51
51
  this.fields.lineItems.selectedValues = [...currentlineItems, lineItemToUpdate];
52
- return;
53
52
  }
54
53
  // Update quantity of a line item
55
54
  if (val.quantity && val.lineItemLike.productId) {
56
55
  const lineItem = val.lineItemLike;
57
56
  const quantity = val.quantity;
58
- const updatedLineItem = Object.assign(Object.assign({}, lineItem), { quantity, originalTotalPrice: Object.assign(Object.assign({}, lineItem.originalTotalPrice), { amount: Number((_a = lineItem.originalTotalPrice) === null || _a === void 0 ? void 0 : _a.amount) * quantity }) });
57
+ const updatedLineItem = Object.assign(Object.assign({}, lineItem), { quantity, originalTotalPrice: Object.assign(Object.assign({}, lineItem.originalTotalPrice), { amount: Number((_a = lineItem.originalUnitPrice) === null || _a === void 0 ? void 0 : _a.amount) * quantity }) });
59
58
  const lineItemToUpdateIndex = currentlineItems.findIndex((li) => (li.productId === lineItem.productId));
60
59
  const lineItemsClone = [...currentlineItems];
61
60
  lineItemsClone.splice(lineItemToUpdateIndex, 1, updatedLineItem);
62
61
  const newLineItems = lineItemsClone;
63
62
  this.fields.lineItems.selectedValues = newLineItems;
64
- return;
65
63
  }
66
64
  // Remove a line item
67
65
  if (val.lineItemLike.productId && !val.quantity) {
68
66
  const lineItem = val.lineItemLike;
69
67
  const newLineItems = currentlineItems.filter((li) => (li.productId !== lineItem.productId));
70
68
  this.fields.lineItems.selectedValues = newLineItems;
71
- return;
72
69
  }
70
+ // Update subtotal price
71
+ this.fields.subtotalPrice.inputValue = this.fields.lineItems.selectedValues
72
+ .map((item) => { var _a; return Number((_a = item.originalTotalPrice) === null || _a === void 0 ? void 0 : _a.amount); })
73
+ .reduce((a, b) => a + b, 0);
74
+ // Update total price
75
+ this.fields.totalPrice.inputValue = Number((Number(this.fields.subtotalPrice.inputValue) - Number(this.fields.totalDiscount.inputValue)) + Number(this.fields.totalShippingPrice.inputValue)).toFixed(2);
76
+ }
77
+ discountChangeHandler(val) {
78
+ this.fields.totalDiscount.inputValue = val;
79
+ // Update total price
80
+ this.fields.totalPrice.inputValue = Number((Number(this.fields.subtotalPrice.inputValue) - Number(val)) + Number(this.fields.totalShippingPrice.inputValue)).toFixed(2);
81
+ }
82
+ shippingPriceChangeHandler(val) {
83
+ this.fields.totalShippingPrice.inputValue = val;
84
+ // Update total price
85
+ this.fields.totalPrice.inputValue = Number((Number(this.fields.subtotalPrice.inputValue) - Number(this.fields.totalDiscount.inputValue)) + Number(val)).toFixed(2);
73
86
  }
74
87
  fieldFactory(fieldType, fieldKey, fieldOptions) {
75
88
  let options = fieldOptions || [];
@@ -86,8 +99,14 @@ class OrderFormState extends FormState_1.default {
86
99
  // @ts-ignore
87
100
  inputValue = this.props[fieldKey] ? this.props[fieldKey].amount : 0;
88
101
  }
102
+ let onChangeHandler = (val) => this.textInputChangeHandler(fieldKey, String(val));
103
+ if (fieldKey === 'totalDiscount') {
104
+ onChangeHandler = (val) => this.discountChangeHandler(String(val));
105
+ }
106
+ if (fieldKey === 'totalShippingPrice') {
107
+ onChangeHandler = (val) => this.shippingPriceChangeHandler(String(val));
108
+ }
89
109
  const valid = !!this.props[fieldKey];
90
- const onChangeHandler = (val) => this.textInputChangeHandler(fieldKey, String(val));
91
110
  return {
92
111
  inputValue,
93
112
  valid,
@@ -174,6 +174,7 @@ interface OrderLineItem {
174
174
  quantity: number;
175
175
  variant: ProductVariant | null;
176
176
  originalTotalPrice?: Money;
177
+ originalUnitPrice?: Money;
177
178
  discountedTotalPrice?: Money;
178
179
  image?: Image;
179
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rerobe-js-orm",
3
- "version": "2.3.8",
3
+ "version": "2.3.9",
4
4
  "description": "ReRobe's Javascript ORM Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",