rerobe-js-orm 4.5.1 → 4.5.2

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,4 +1,5 @@
1
1
  import OrderFactory from './OrderFactory';
2
2
  export default class OrderFromShopifyWebhook extends OrderFactory {
3
3
  createOrder(order: ShopifyRestApiOrderObj): ReRobeOrderObj;
4
+ createOrderV2(order: any): ReRobeOrderObj;
4
5
  }
@@ -277,5 +277,112 @@ class OrderFromShopifyWebhook extends OrderFactory_1.default {
277
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(', ') : [] }));
278
278
  return rerobeOrder.toObj();
279
279
  }
280
+ // Aligned with Shopify REST order payload that includes current_* fields
281
+ createOrderV2(order) {
282
+ const { id, name, financial_status, fulfillment_status, cancel_reason, created_at, processed_at, updated_at, currency, contact_email, email, shipping_address, line_items = [], discount_applications = [], shipping_lines = [], tax_lines = [], tags = '', cancelled_at, closed_at, order_number, current_subtotal_price_set, current_total_price_set, current_total_tax_set, current_total_discounts_set, current_shipping_price_set, } = order || {};
283
+ const moneyFromSet = (setObj) => {
284
+ const shop = setObj === null || setObj === void 0 ? void 0 : setObj.shop_money;
285
+ return shop
286
+ ? { amount: shop.amount, currencyCode: shop.currency_code }
287
+ : { amount: 0, currencyCode: currency || '' };
288
+ };
289
+ const subtotalPrice = moneyFromSet(current_subtotal_price_set);
290
+ const totalPrice = moneyFromSet(current_total_price_set);
291
+ const totalTax = moneyFromSet(current_total_tax_set);
292
+ const totalShippingPrice = moneyFromSet(current_shipping_price_set);
293
+ const totalDiscount = current_total_discounts_set
294
+ ? moneyFromSet(current_total_discounts_set)
295
+ : { amount: 0, currencyCode: currency || '' };
296
+ const shippingAddress = shipping_address
297
+ ? {
298
+ address1: shipping_address.address1,
299
+ address2: shipping_address.address2,
300
+ city: shipping_address.city,
301
+ country: shipping_address.country,
302
+ countryCode: shipping_address.country_code,
303
+ province: shipping_address.province,
304
+ zip: shipping_address.zip,
305
+ }
306
+ : null;
307
+ const lineItems = line_items.map((item) => ({
308
+ title: item.title,
309
+ quantity: item.quantity,
310
+ variant: {
311
+ id: item.variant_id ? `gid://shopify/ProductVariant/${item.variant_id}` : '',
312
+ title: item.variant_title || '',
313
+ image: { id: '', originalSrc: '' },
314
+ },
315
+ shopifyProductId: item.product_id ? `gid://shopify/Product/${item.product_id}` : null,
316
+ originalTotalPrice: moneyFromSet(item.price_set),
317
+ discountedTotalPrice: moneyFromSet(item.total_discount_set),
318
+ }));
319
+ const discountApplications = discount_applications.map((item) => ({
320
+ allocationMethod: item.allocation_method ? String(item.allocation_method).toUpperCase() : '',
321
+ targetSelection: item.target_selection ? String(item.target_selection).toUpperCase() : '',
322
+ targetType: item.target_type ? String(item.target_type).toUpperCase() : '',
323
+ value: { amount: item.value },
324
+ }));
325
+ const taxLinesV2 = tax_lines.map((item) => ({
326
+ rate: item.rate,
327
+ title: item.title,
328
+ priceSet: { amount: item.price, currencyCode: currency || '' },
329
+ }));
330
+ const shippingLine = shipping_lines && shipping_lines.length
331
+ ? {
332
+ id: '',
333
+ title: shipping_lines[0].title,
334
+ source: shipping_lines[0].source,
335
+ discountedPrice: moneyFromSet(shipping_lines[0].discounted_price_set),
336
+ originalPrice: moneyFromSet(shipping_lines[0].price_set),
337
+ taxLines: (shipping_lines[0].tax_lines || []).map((t) => ({
338
+ rate: t.rate,
339
+ title: t.title,
340
+ priceSet: { amount: t.price, currencyCode: currency || '' },
341
+ })),
342
+ }
343
+ : {
344
+ id: '',
345
+ title: '',
346
+ taxLines: [],
347
+ discountAllocations: [],
348
+ discountedPrice: { amount: 0 },
349
+ originalPrice: { amount: 0 },
350
+ source: '',
351
+ };
352
+ // @ts-ignore
353
+ const fulfillmentStatus = !fulfillment_status ? '' : fulfillmentStatusRestMapping[fulfillment_status];
354
+ const rerobeOrder = new Order_1.default({
355
+ id: orderHelpers.getOrderIdFromShopifyObj(order),
356
+ shopifyId: `gid://shopify/Order/${id}`,
357
+ shopifyOrderNumber: name ? Number(String(name).replace('#', '')) : order_number || 0,
358
+ name: name || '',
359
+ currencyCode: currency || '',
360
+ email: contact_email || email || '',
361
+ financialStatus: financial_status ? String(financial_status).toUpperCase() : '',
362
+ fulfillmentStatus,
363
+ cancelReason: cancel_reason ? String(cancel_reason).toUpperCase() : '',
364
+ createdAt: created_at,
365
+ processedAt: processed_at,
366
+ updatedAt: updated_at,
367
+ canceledAt: cancelled_at,
368
+ closedAt: closed_at,
369
+ orderNumber: order_number || 0,
370
+ shippingAddress,
371
+ lineItems,
372
+ subtotalPrice,
373
+ totalPrice,
374
+ totalTax,
375
+ totalShippingPrice,
376
+ totalDiscount,
377
+ discountApplications,
378
+ shippingLine,
379
+ tags: tags ? String(tags).split(', ').filter(Boolean) : [],
380
+ state: Order_1.default.ORDER_STATES.completed,
381
+ shippingType: orderHelpers.getShippingTypeFromShopifyObj(shipping_lines),
382
+ paymentType: orderHelpers.getPaymentTypeUsingTags(tags),
383
+ salesChannel: orderHelpers.getSalesChannelUsingTags(tags),
384
+ });
385
+ return rerobeOrder.toObj();
386
+ }
280
387
  }
281
388
  exports.default = OrderFromShopifyWebhook;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rerobe-js-orm",
3
- "version": "4.5.1",
3
+ "version": "4.5.2",
4
4
  "description": "ReRobe's Javascript ORM Framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",