rerobe-js-orm 2.4.76 → 2.4.77
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.
|
@@ -4,6 +4,18 @@ const OrderFactory_1 = require("./OrderFactory");
|
|
|
4
4
|
const Order_1 = require("../../models/Order");
|
|
5
5
|
const OrderHelpers_1 = require("../../helpers/OrderHelpers");
|
|
6
6
|
const orderHelpers = new OrderHelpers_1.default();
|
|
7
|
+
const fulfillmentStatusRestMapping = {
|
|
8
|
+
fulfilled: 'FULFILLED',
|
|
9
|
+
null: '',
|
|
10
|
+
partial: 'PARTIALLY_FULFILLED',
|
|
11
|
+
restocked: 'RESTOCKED',
|
|
12
|
+
pending: 'PENDING_FULFILLMENT',
|
|
13
|
+
open: 'OPEN',
|
|
14
|
+
success: 'SUCCESS',
|
|
15
|
+
cancelled: 'CANCELED',
|
|
16
|
+
error: 'ERROR',
|
|
17
|
+
failure: 'FAILURE',
|
|
18
|
+
};
|
|
7
19
|
class OrderFromShopifyWebhook extends OrderFactory_1.default {
|
|
8
20
|
createOrder(order) {
|
|
9
21
|
const { id, financial_status, fulfillment_status, cancel_reason, line_items, discount_applications, shipping_address, subtotal_price_set, total_price_set, total_shipping_price_set, currency, cancelled_at, closed_at, created_at, order_number, processed_at, updated_at, shipping_lines, tax_lines, name, total_discounts, tags = '', } = order;
|
|
@@ -78,23 +90,123 @@ class OrderFromShopifyWebhook extends OrderFactory_1.default {
|
|
|
78
90
|
}
|
|
79
91
|
: null;
|
|
80
92
|
const fulfillments = order.fulfillments
|
|
81
|
-
? order.fulfillments.map((item) => (
|
|
93
|
+
? order.fulfillments.map((item) => ({
|
|
94
|
+
name: item.name,
|
|
95
|
+
id: item.id,
|
|
96
|
+
createdAt: item.created_at,
|
|
97
|
+
updatedAt: item.updated_at,
|
|
98
|
+
location: {
|
|
82
99
|
id: item.location_id || '',
|
|
83
|
-
},
|
|
84
|
-
|
|
85
|
-
|
|
100
|
+
},
|
|
101
|
+
shipmentStatus: item.shipment_status ? item.shipment_status.toUpperCase() : '',
|
|
102
|
+
service: {
|
|
86
103
|
serviceType: item.service ? item.service.toUpperCase() : '',
|
|
87
|
-
},
|
|
104
|
+
},
|
|
105
|
+
status: item.status ? item.status.toUpperCase() : '',
|
|
106
|
+
trackingInfo: {
|
|
88
107
|
company: item.tracking_company,
|
|
89
108
|
number: item.tracking_numbers && !!item.tracking_numbers.length ? item.tracking_numbers[0] : '',
|
|
90
109
|
url: item.tracking_urls && !!item.tracking_urls.length ? item.tracking_urls[0] : '',
|
|
91
|
-
},
|
|
110
|
+
},
|
|
111
|
+
fulfillmentLineItems: item.line_items
|
|
92
112
|
? item.line_items.map((lineItem) => ({
|
|
93
|
-
lineItem,
|
|
113
|
+
lineItem: { shopifyProductId: lineItem === null || lineItem === void 0 ? void 0 : lineItem.product_id },
|
|
94
114
|
quantity: lineItem.quantity,
|
|
95
115
|
}))
|
|
96
|
-
: []
|
|
116
|
+
: [],
|
|
117
|
+
}))
|
|
118
|
+
: [];
|
|
119
|
+
const refunds = order.refunds
|
|
120
|
+
? order.refunds.map((rawRefundItem) => {
|
|
121
|
+
const currencyCode = currency;
|
|
122
|
+
const refundedShippingAmount = !rawRefundItem.order_adjustments.length
|
|
123
|
+
? 0
|
|
124
|
+
: rawRefundItem.order_adjustments.reduce((result, item) => {
|
|
125
|
+
if (item.kind === 'shipping_refund') {
|
|
126
|
+
result = result - Number(item.amount);
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}, 0);
|
|
130
|
+
const totalRefundedAmount = !rawRefundItem.transactions.length
|
|
131
|
+
? 0
|
|
132
|
+
: rawRefundItem.transactions.reduce((result, item) => {
|
|
133
|
+
if (item.kind === 'refund') {
|
|
134
|
+
result = result + Number(item.amount);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}, 0);
|
|
138
|
+
let refundDiscountAmount = 0;
|
|
139
|
+
const subtotalRefundedAmount = !rawRefundItem.refund_line_items.length
|
|
140
|
+
? 0
|
|
141
|
+
: rawRefundItem.refund_line_items.reduce((result, item) => {
|
|
142
|
+
if (item.line_item) {
|
|
143
|
+
const itemSubtotal = Number(Number(item.line_item.price) * item.quantity);
|
|
144
|
+
result = result + itemSubtotal;
|
|
145
|
+
refundDiscountAmount = refundDiscountAmount + itemSubtotal - Number(item.subtotal);
|
|
146
|
+
}
|
|
147
|
+
return result;
|
|
148
|
+
}, 0);
|
|
149
|
+
let refundLineItems = !rawRefundItem.refund_line_items || !rawRefundItem.refund_line_items.length
|
|
150
|
+
? []
|
|
151
|
+
: rawRefundItem.refund_line_items.map((item) => {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
const quantityRefund = !item.location_id ? item.quantity : 0;
|
|
154
|
+
const quantityCancel = item.location_id ? item.quantity : 0;
|
|
155
|
+
const discountRefundAmount = Number(Number((_a = item.line_item) === null || _a === void 0 ? void 0 : _a.price) * item.quantity) - Number(item.subtotal);
|
|
156
|
+
const subtotalRefundAmount = !item.location_id ? item.subtotal : 0;
|
|
157
|
+
const subtotalCanceledAmount = item.location_id ? item.subtotal : 0;
|
|
158
|
+
return {
|
|
159
|
+
lineItem: { shopifyProductId: (_b = item.line_item) === null || _b === void 0 ? void 0 : _b.product_id },
|
|
160
|
+
quantityRefund,
|
|
161
|
+
quantityCancel,
|
|
162
|
+
discountRefund: { currencyCode, amount: discountRefundAmount },
|
|
163
|
+
subtotalRefund: { currencyCode, amount: subtotalRefundAmount },
|
|
164
|
+
subtotalCanceled: { currencyCode, amount: subtotalCanceledAmount },
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
if (refundLineItems.length) {
|
|
168
|
+
refundLineItems = refundLineItems.reduce((result, item) => {
|
|
169
|
+
var _a;
|
|
170
|
+
const existedLineItemsIds = !result.length ? [] : result.map((itm) => itm.lineItem.shopifyProductId);
|
|
171
|
+
const existedLineItemIdx = existedLineItemsIds.indexOf((_a = item.lineItem) === null || _a === void 0 ? void 0 : _a.shopifyProductId);
|
|
172
|
+
if (existedLineItemIdx < 0) {
|
|
173
|
+
result.push(item);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
result[existedLineItemIdx] = {
|
|
177
|
+
lineItem: result[existedLineItemIdx].lineItem,
|
|
178
|
+
quantityRefund: result[existedLineItemIdx].quantityRefund + item.quantityRefund,
|
|
179
|
+
quantityCancel: result[existedLineItemIdx].quantityCancel + item.quantityCancel,
|
|
180
|
+
discountRefund: Object.assign(Object.assign({}, result[existedLineItemIdx].discountRefund), { amount: result[existedLineItemIdx].discountRefund.amount + item.discountRefund.amount }),
|
|
181
|
+
subtotalRefund: Object.assign(Object.assign({}, result[existedLineItemIdx].subtotalRefund), { amount: result[existedLineItemIdx].subtotalRefund.amount + item.subtotalRefund.amount }),
|
|
182
|
+
subtotalCanceled: Object.assign(Object.assign({}, result[existedLineItemIdx].subtotalCanceled), { amount: result[existedLineItemIdx].subtotalCanceled.amount + item.subtotalCanceled.amount }),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
return result;
|
|
186
|
+
}, []);
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
documentId: rawRefundItem.id,
|
|
190
|
+
createdAt: rawRefundItem.created_at,
|
|
191
|
+
note: rawRefundItem.note,
|
|
192
|
+
totalRefunded: { amount: totalRefundedAmount, currencyCode },
|
|
193
|
+
subtotalRefunded: { amount: subtotalRefundedAmount, currencyCode },
|
|
194
|
+
refundDiscount: { amount: refundDiscountAmount, currencyCode },
|
|
195
|
+
refundShipping: { amount: refundedShippingAmount, currencyCode },
|
|
196
|
+
totalTax: { amount: 0, currencyCode },
|
|
197
|
+
refundLineItems,
|
|
198
|
+
};
|
|
199
|
+
})
|
|
97
200
|
: [];
|
|
201
|
+
const totalRefunded = {
|
|
202
|
+
currencyCode: currency,
|
|
203
|
+
amount: !refunds || !refunds.length
|
|
204
|
+
? 0
|
|
205
|
+
: refunds.reduce((result, item) => {
|
|
206
|
+
result = result + Number(item.totalRefunded.amount);
|
|
207
|
+
return result;
|
|
208
|
+
}, 0),
|
|
209
|
+
};
|
|
98
210
|
const taxLines = tax_lines
|
|
99
211
|
? tax_lines.map((item) => ({
|
|
100
212
|
rate: item.rate,
|
|
@@ -150,13 +262,17 @@ class OrderFromShopifyWebhook extends OrderFactory_1.default {
|
|
|
150
262
|
},
|
|
151
263
|
source: '',
|
|
152
264
|
};
|
|
153
|
-
|
|
265
|
+
// @ts-ignore
|
|
266
|
+
const fulfillmentStatus = !fulfillment_status ? '' : fulfillmentStatusRestMapping[fulfillment_status];
|
|
267
|
+
const rerobeOrder = new Order_1.default(Object.assign(Object.assign({}, order), { id: orderHelpers.getOrderIdFromShopifyObj(order), shopifyId: `gid://shopify/Order/${id}`, shopifyOrderNumber: name ? Number(name.split('#')[1]) : 0, financialStatus: financial_status ? financial_status.toUpperCase() : '', fulfillmentStatus, cancelReason: cancel_reason ? cancel_reason.toUpperCase() : '', lineItems,
|
|
154
268
|
subtotalPrice,
|
|
155
269
|
totalPrice,
|
|
156
270
|
totalShippingPrice,
|
|
157
|
-
totalDiscount,
|
|
271
|
+
totalDiscount,
|
|
272
|
+
totalRefunded, canceledAt: cancelled_at, closedAt: closed_at, createdAt: created_at, orderNumber: order_number, processedAt: processed_at, updatedAt: updated_at, shippingAddress,
|
|
158
273
|
discountApplications,
|
|
159
274
|
fulfillments,
|
|
275
|
+
refunds,
|
|
160
276
|
taxLines,
|
|
161
277
|
shippingLine, state: Order_1.default.ORDER_STATES.completed, shippingType: orderHelpers.getShippingTypeFromShopifyObj(shipping_lines), paymentType: orderHelpers.getPaymentTypeUsingTags(tags), salesChannel: orderHelpers.getSalesChannelUsingTags(tags), tags: tags ? tags.split(', ') : [] }));
|
|
162
278
|
return rerobeOrder.toObj();
|
|
@@ -492,6 +492,25 @@ declare type OrderLineItemRest = {
|
|
|
492
492
|
total_discount_set?: MoneyBagRest;
|
|
493
493
|
discount_allocations?: DiscountAllocationRest[];
|
|
494
494
|
origin_location?: AddressInputRest;
|
|
495
|
+
line_item?: {
|
|
496
|
+
[key: string]: any;
|
|
497
|
+
};
|
|
498
|
+
subtotal?: number | string;
|
|
499
|
+
location_id?: string | null;
|
|
500
|
+
};
|
|
501
|
+
declare type RefundLineItemRest = {
|
|
502
|
+
id: number | string;
|
|
503
|
+
line_item?: {
|
|
504
|
+
[key: string]: any;
|
|
505
|
+
};
|
|
506
|
+
line_item_id: number;
|
|
507
|
+
location_id?: number | string | null;
|
|
508
|
+
quantity: number;
|
|
509
|
+
restock_type: string;
|
|
510
|
+
total_tax?: number;
|
|
511
|
+
total_tax_set?: MoneyBagRest;
|
|
512
|
+
subtotal?: number | string;
|
|
513
|
+
subtotal_set?: MoneyBagRest;
|
|
495
514
|
};
|
|
496
515
|
declare type CancelReasonRestTypes = 'customer' | 'declined' | 'fraud' | 'inventory' | 'other';
|
|
497
516
|
declare type FinancialStatusRestTypes = 'authorized' | 'paid' | 'partially_paid' | 'partially_refunded' | 'pending' | 'refunded' | 'voided';
|
|
@@ -524,17 +543,23 @@ declare type DiscountCodeRest = {
|
|
|
524
543
|
declare type OrderRefundRest = {
|
|
525
544
|
id: number | string;
|
|
526
545
|
order_id: number | string;
|
|
527
|
-
refund_line_items:
|
|
546
|
+
refund_line_items: RefundLineItemRest[];
|
|
528
547
|
created_at?: string;
|
|
529
548
|
note?: string;
|
|
530
549
|
user_id?: number | string;
|
|
531
550
|
processed_at?: string;
|
|
551
|
+
order_adjustments: any[];
|
|
552
|
+
transactions: any[];
|
|
553
|
+
admin_graphql_api_id?: string | null;
|
|
554
|
+
restock?: boolean;
|
|
555
|
+
duties?: any[];
|
|
556
|
+
total_duties_set?: MoneyBagRest;
|
|
532
557
|
};
|
|
533
558
|
declare type AddressInputRest = {
|
|
534
559
|
address1: string;
|
|
535
560
|
address2?: string;
|
|
536
561
|
city: string;
|
|
537
|
-
country
|
|
562
|
+
country?: string;
|
|
538
563
|
company?: string | null;
|
|
539
564
|
first_name?: string | null;
|
|
540
565
|
last_name?: string | null;
|
|
@@ -568,7 +593,7 @@ declare type FulfillmentRest = {
|
|
|
568
593
|
service?: FulfillmentServiceRestTypes | string | null;
|
|
569
594
|
shipment_status?: FulfillmentShipmentStatusRestTypes | string | null;
|
|
570
595
|
status?: FulfillmentStatusRestTypes | string | null;
|
|
571
|
-
tracking_company?: string;
|
|
596
|
+
tracking_company?: string | null;
|
|
572
597
|
tracking_numbers?: string[] | number[];
|
|
573
598
|
tracking_urls?: string[];
|
|
574
599
|
updated_at?: string;
|