rerobe-js-orm 2.3.5 → 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,4 +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, quantity?: number): ReRobeOrderLineItem;
6
7
  }
@@ -53,5 +53,29 @@ class OrderHelpers {
53
53
  }
54
54
  return null;
55
55
  }
56
+ buildLineItemFromProduct(product, currencyCode, quantity = 1) {
57
+ const q = quantity || 1;
58
+ return {
59
+ title: product.title,
60
+ quantity: q,
61
+ variant: {
62
+ id: product.variantId || '',
63
+ },
64
+ brand: product.brand,
65
+ originalTotalPrice: {
66
+ amount: q * Number(Number(product.price).toFixed(2)),
67
+ currencyCode,
68
+ },
69
+ originalUnitPrice: {
70
+ amount: Number(Number(product.price).toFixed(2)),
71
+ currencyCode,
72
+ },
73
+ image: {
74
+ originalSrc: product.imageUrls && !!product.imageUrls.length ? product.imageUrls[0] : '',
75
+ },
76
+ shopifyProductId: product.shopifyId || null,
77
+ productId: product.documentId,
78
+ };
79
+ }
56
80
  }
57
81
  exports.default = OrderHelpers;
@@ -1,12 +1,20 @@
1
1
  import FormState from '../FormState';
2
2
  import Order from '../../Order';
3
+ import OrderHelpers from '../../../helpers/OrderHelpers';
3
4
  export default class OrderFormState extends FormState {
4
5
  fields: OrderAttributesFormFields;
5
6
  props: CompleteOrder;
6
7
  opts: {
7
8
  tagOptions: string[];
8
9
  };
10
+ orderHelpers: OrderHelpers;
9
11
  constructor(props?: any, opts?: any);
10
12
  createOrder(): Order;
13
+ lineItemChangeHandler(val: {
14
+ lineItemLike: CompleteProduct | ReRobeOrderLineItem;
15
+ quantity: number;
16
+ }): void;
17
+ discountChangeHandler(val: string): void;
18
+ shippingPriceChangeHandler(val: string): void;
11
19
  private fieldFactory;
12
20
  }
@@ -3,10 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const FormState_1 = require("../FormState");
4
4
  const Order_1 = require("../../Order");
5
5
  const OrderFromFormState_1 = require("../../../factories/Order/OrderFromFormState");
6
+ const OrderHelpers_1 = require("../../../helpers/OrderHelpers");
6
7
  const options_1 = require("./options");
7
8
  class OrderFormState extends FormState_1.default {
8
9
  constructor(props, opts) {
9
10
  super();
11
+ this.orderHelpers = new OrderHelpers_1.default();
10
12
  this.props = new Order_1.default(props).toObj();
11
13
  this.opts = opts;
12
14
  this.fields.currencyCode = this.fieldFactory('textInput', 'currencyCode');
@@ -39,6 +41,49 @@ class OrderFormState extends FormState_1.default {
39
41
  const orderFactory = new OrderFromFormState_1.default();
40
42
  return orderFactory.createOrder(this);
41
43
  }
44
+ lineItemChangeHandler(val) {
45
+ var _a;
46
+ const currentlineItems = this.fields.lineItems.selectedValues;
47
+ // Add new product to line items
48
+ if (val.lineItemLike.documentId) {
49
+ const product = val.lineItemLike;
50
+ const lineItemToUpdate = this.orderHelpers.buildLineItemFromProduct(product, this.fields.currencyCode.inputValue || 'SEK', val.quantity);
51
+ this.fields.lineItems.selectedValues = [...currentlineItems, lineItemToUpdate];
52
+ }
53
+ // Update quantity of a line item
54
+ if (val.quantity && val.lineItemLike.productId) {
55
+ const lineItem = val.lineItemLike;
56
+ const quantity = val.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 }) });
58
+ const lineItemToUpdateIndex = currentlineItems.findIndex((li) => (li.productId === lineItem.productId));
59
+ const lineItemsClone = [...currentlineItems];
60
+ lineItemsClone.splice(lineItemToUpdateIndex, 1, updatedLineItem);
61
+ const newLineItems = lineItemsClone;
62
+ this.fields.lineItems.selectedValues = newLineItems;
63
+ }
64
+ // Remove a line item
65
+ if (val.lineItemLike.productId && !val.quantity) {
66
+ const lineItem = val.lineItemLike;
67
+ const newLineItems = currentlineItems.filter((li) => (li.productId !== lineItem.productId));
68
+ this.fields.lineItems.selectedValues = newLineItems;
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);
86
+ }
42
87
  fieldFactory(fieldType, fieldKey, fieldOptions) {
43
88
  let options = fieldOptions || [];
44
89
  const hidden = false;
@@ -54,8 +99,14 @@ class OrderFormState extends FormState_1.default {
54
99
  // @ts-ignore
55
100
  inputValue = this.props[fieldKey] ? this.props[fieldKey].amount : 0;
56
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
+ }
57
109
  const valid = !!this.props[fieldKey];
58
- const onChangeHandler = (val) => this.textInputChangeHandler(fieldKey, String(val));
59
110
  return {
60
111
  inputValue,
61
112
  valid,
@@ -84,9 +135,11 @@ class OrderFormState extends FormState_1.default {
84
135
  if (fieldType === 'multiSelect') {
85
136
  let selectedValues = [];
86
137
  let valid = !!selectedValues.length;
138
+ let onChangeHandler = (val) => this.multiSelectChangeHandler(fieldKey, val);
87
139
  if (fieldKey === 'lineItems') {
88
140
  selectedValues = this.props.lineItems || [];
89
141
  valid = this.props.lineItems && this.props.lineItems.length > 0;
142
+ onChangeHandler = (val) => this.lineItemChangeHandler(val);
90
143
  }
91
144
  if (fieldKey === 'tags') {
92
145
  selectedValues = [];
@@ -110,7 +163,7 @@ class OrderFormState extends FormState_1.default {
110
163
  options,
111
164
  selectedValues,
112
165
  valid,
113
- onChangeHandler: (val) => this.multiSelectChangeHandler(fieldKey, val),
166
+ onChangeHandler,
114
167
  };
115
168
  }
116
169
  return {
@@ -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
  }
@@ -226,7 +227,7 @@ declare type ProductVariant = {
226
227
  weight?: number | null;
227
228
  };
228
229
  declare type Image = {
229
- id: string;
230
+ id?: string;
230
231
  originalSrc: string;
231
232
  altText?: string;
232
233
  };
@@ -583,7 +584,7 @@ declare type OrderAttributesFormFields = {
583
584
  phone: TextInputFormField<string>;
584
585
  firstName: TextInputFormField<string>;
585
586
  lastName: TextInputFormField<string>;
586
- lineItems: MultiSelectFormField<OrderLineItem>;
587
+ lineItems: MultiSelectFormField<ReRobeOrderLineItem>;
587
588
  discountApplications: MultiSelectFormField<DiscountApplication>;
588
589
  notes: TextInputFormField<string>;
589
590
  customer: SingleSelectFormField<UserAttributes>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rerobe-js-orm",
3
- "version": "2.3.5",
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",