feat(utils): Introduce promiseAll util (#5543)

This commit is contained in:
Adrien de Peretti authored and GitHub committed 2023-11-08 08:48:48 +01:00
1 parent e4ce2f4e07
commit f90ba02087
99 files changed
+464 -297

No files matched your search

@@ -41,6 +41,7 @@
"medusa-interfaces": "^1.3.7"
},
"dependencies": {
"@medusajs/utils": "workspace:^",
"axios": "^0.19.2",
"axios-rate-limit": "^1.2.1",
"express": "^4.17.1",
@@ -1,8 +1,9 @@
import { MedusaError, humanizeAmount } from "medusa-core-utils"
import { humanizeAmount, MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces"
import Brightpearl from "../utils/brightpearl"
import { updateInventoryAndReservations } from "@medusajs/medusa"
import { promiseAll } from "@medusajs/utils"
class BrightpearlService extends BaseService {
constructor(
@@ -117,7 +118,7 @@ class BrightpearlService extends BaseService {
httpMethod: "POST",
uriTemplate: `${this.options.backend_url}/brightpearl/inventory-update`,
bodyTemplate:
"{\"account\": \"${account-code}\", \"lifecycle_event\": \"${lifecycle-event}\", \"resource_type\": \"${resource-type}\", \"id\": \"${resource-id}\" }",
'{"account": "${account-code}", "lifecycle_event": "${lifecycle-event}", "resource_type": "${resource-type}", "id": "${resource-id}" }',
contentType: "application/json",
idSetAccepted: false,
},
@@ -195,7 +196,7 @@ class BrightpearlService extends BaseService {
variants.filter((variant) => !!variant.sku).map((v) => [v.sku, v])
)
const variantUpdates = await Promise.all(
const variantUpdates = await promiseAll(
bpProducts.map(async (bpProduct) => {
const { SKU: sku, productId } = bpProduct
@@ -278,7 +279,7 @@ class BrightpearlService extends BaseService {
this.logger_.info("Synchronizing inventory levels")
await Promise.all(
await promiseAll(
bpProducts.map(async (bpProduct, index) => {
if (index % 100 === 0) {
this.logger_.info(
@@ -291,7 +292,7 @@ class BrightpearlService extends BaseService {
const productAvailability = availabilities[productId]
if (productAvailability) {
await Promise.all(
await promiseAll(
locations.map(async (location) => {
const warehouseData =
productAvailability.warehouses[
@@ -355,9 +356,7 @@ class BrightpearlService extends BaseService {
}
const externallyReservedQuantityAdjustment =
bpinStock -
bpOnHand -
inventoryLevel.reserved_quantity
bpinStock - bpOnHand - inventoryLevel.reserved_quantity
if (externallyReservedQuantityAdjustment === 0) {
return
@@ -455,7 +454,7 @@ class BrightpearlService extends BaseService {
productAvailability.warehouses[`${location.metadata.bp_id}`]
)
await Promise.all(
await promiseAll(
locations.map(async (location) => {
// TODO: Assuming we have a 1 to 1 mapping of inventory items
const inventoryLevel = inventoryMap[location.id][0]
@@ -703,7 +702,7 @@ class BrightpearlService extends BaseService {
order.metadata.brightpearl_sales_order_id
)
const rows = await Promise.all(
const rows = await promiseAll(
lineItems.map(async (item) => {
const reservations = lineItemReservationsMap.get(item.id)
const variant = variantMap.get(item.variant_id)
@@ -1337,7 +1336,7 @@ class BrightpearlService extends BaseService {
async gatherRowsFromOrderIds(ids) {
const client = await this.getClient()
const orders = await Promise.all(ids.map((i) => client.orders.retrieve(i)))
const orders = await promiseAll(ids.map((i) => client.orders.retrieve(i)))
let rows = []
for (const o of orders) {
@@ -1477,7 +1476,7 @@ class BrightpearlService extends BaseService {
) {
const { region } = fromOrder
const lines = await Promise.all(
const lines = await promiseAll(
fromOrder.items.map(async (item) => {
const bpProduct = await this.retrieveProductBySKU(item.variant.sku)