From d37dc7b2defbb4fbed8b218c2f1532be5c685a15 Mon Sep 17 00:00:00 2001 From: saurabh singh Date: Thu, 14 Oct 2021 10:33:38 +0530 Subject: [PATCH 1/7] chore: Make packages/medusa/src/services/order.js pass linting #515 --- packages/medusa/src/services/order.js | 119 +++++++++++++------------- 1 file changed, 61 insertions(+), 58 deletions(-) diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index c63e3d2cdb..2ecfcad685 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -178,6 +178,7 @@ class OrderService extends BaseService { /** * @param {Object} selector - the query object for find + * @param {Object} config - the config to be used for find * @return {Promise} the result of the find operation */ async list( @@ -187,9 +188,8 @@ class OrderService extends BaseService { const orderRepo = this.manager_.getCustomRepository(this.orderRepository_) const query = this.buildQuery_(selector, config) - const { select, relations, totalsToSelect } = this.transformQueryForTotals_( - config - ) + const { select, relations, totalsToSelect } = + this.transformQueryForTotals_(config) if (select && select.length) { query.select = select @@ -201,7 +201,7 @@ class OrderService extends BaseService { const raw = await orderRepo.find(query) - return raw.map(r => this.decorateTotals_(r, totalsToSelect)) + return raw.map((r) => this.decorateTotals_(r, totalsToSelect)) } async listAndCount( @@ -231,11 +231,11 @@ class OrderService extends BaseService { }, } - query.where = qb => { + query.where = (qb) => { qb.where(where) qb.andWhere( - new Brackets(qb => { + new Brackets((qb) => { qb.where(`shipping_address.first_name ILIKE :qfn`, { qfn: `%${q}%`, }) @@ -246,20 +246,19 @@ class OrderService extends BaseService { } } - const { select, relations, totalsToSelect } = this.transformQueryForTotals_( - config - ) + const { select, relations, totalsToSelect } = + this.transformQueryForTotals_(config) if (select && select.length) { query.select = select } - let rels = relations + const rels = relations delete query.relations const raw = await orderRepo.findWithRelations(rels, query) const count = await orderRepo.count(query) - const orders = raw.map(r => this.decorateTotals_(r, totalsToSelect)) + const orders = raw.map((r) => this.decorateTotals_(r, totalsToSelect)) return [orders, count] } @@ -289,7 +288,7 @@ class OrderService extends BaseService { "swaps.additional_items.refundable", ] - const totalsToSelect = select.filter(v => totalFields.includes(v)) + const totalsToSelect = select.filter((v) => totalFields.includes(v)) if (totalsToSelect.length > 0) { const relationSet = new Set(relations) relationSet.add("items") @@ -305,7 +304,7 @@ class OrderService extends BaseService { relationSet.add("region") relations = [...relationSet] - select = select.filter(v => !totalFields.includes(v)) + select = select.filter((v) => !totalFields.includes(v)) } return { @@ -318,15 +317,15 @@ class OrderService extends BaseService { /** * Gets an order by id. * @param {string} orderId - id of order to retrieve + * @param {Object} config - config of order to retrieve * @return {Promise} the order document */ async retrieve(orderId, config = {}) { const orderRepo = this.manager_.getCustomRepository(this.orderRepository_) const validatedId = this.validateId_(orderId) - const { select, relations, totalsToSelect } = this.transformQueryForTotals_( - config - ) + const { select, relations, totalsToSelect } = + this.transformQueryForTotals_(config) const query = { where: { id: validatedId }, @@ -357,14 +356,14 @@ class OrderService extends BaseService { /** * Gets an order by cart id. * @param {string} cartId - cart id to find order + * @param {Object} config - the config to be used to find order * @return {Promise} the order document */ async retrieveByCartId(cartId, config = {}) { const orderRepo = this.manager_.getCustomRepository(this.orderRepository_) - const { select, relations, totalsToSelect } = this.transformQueryForTotals_( - config - ) + const { select, relations, totalsToSelect } = + this.transformQueryForTotals_(config) const query = { where: { cart_id: cartId }, @@ -397,7 +396,7 @@ class OrderService extends BaseService { * @return {Promise} the order document */ async existsByCartId(cartId) { - const order = await this.retrieveByCartId(cartId).catch(_ => undefined) + const order = await this.retrieveByCartId(cartId).catch((_) => undefined) if (!order) { return false } @@ -409,7 +408,7 @@ class OrderService extends BaseService { * @return {Promise} the result of the find operation */ async completeOrder(orderId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId) if (order.status === "canceled") { @@ -428,7 +427,7 @@ class OrderService extends BaseService { } ) - await completeOrderJob.finished().catch(error => { + await completeOrderJob.finished().catch((error) => { throw error }) @@ -445,7 +444,7 @@ class OrderService extends BaseService { * @return {Promise} resolves to the creation result. */ async createFromCart(cartId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const cart = await this.cartService_ .withTransaction(manager) .retrieve(cartId, { @@ -619,6 +618,7 @@ class OrderService extends BaseService { * @param {string} fulfillmentId - the fulfillment that has now been shipped * @param {TrackingLink[]} trackingLinks - array of tracking numebers * associated with the shipment + * @param {Object} config - the config of the order that has been shipped * @param {Dictionary} metadata - optional metadata to add to * the fulfillment * @return {order} the resulting order following the update. @@ -634,7 +634,7 @@ class OrderService extends BaseService { ) { const { metadata, no_notification } = config - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId, { relations: ["items"] }) const shipment = await this.fulfillmentService_.retrieve(fulfillmentId) @@ -666,7 +666,7 @@ class OrderService extends BaseService { order.fulfillment_status = "shipped" for (const item of order.items) { - const shipped = shipmentRes.items.find(si => si.item_id === item.id) + const shipped = shipmentRes.items.find((si) => si.item_id === item.id) if (shipped) { const shippedQty = (item.shipped_quantity || 0) + shipped.quantity if (shippedQty !== item.quantity) { @@ -700,11 +700,11 @@ class OrderService extends BaseService { /** * Creates an order - * @param {object} order - the order to create + * @param {object} data - the data to create an order * @return {Promise} resolves to the creation result. */ async create(data) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const orderRepo = manager.getCustomRepository(this.orderRepository_) const order = orderRepo.create(data) const result = await orderRepo.save(order) @@ -720,7 +720,7 @@ class OrderService extends BaseService { /** * Updates the order's billing address. - * @param {string} orderId - the id of the order to update + * @param {object} order - the order to update * @param {object} address - the value to set the billing address to * @return {Promise} the result of the update operation */ @@ -755,7 +755,7 @@ class OrderService extends BaseService { /** * Updates the order's shipping address. - * @param {string} orderId - the id of the order to update + * @param {object} order - the order to update * @param {object} address - the value to set the shipping address to * @return {Promise} the result of the update operation */ @@ -787,7 +787,7 @@ class OrderService extends BaseService { } async addShippingMethod(orderId, optionId, data, config = {}) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId, { select: ["subtotal"], relations: [ @@ -845,7 +845,7 @@ class OrderService extends BaseService { * @return {Promise} resolves to the update result. */ async update(orderId, update) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId) if (order.status === "canceled") { @@ -874,13 +874,7 @@ class OrderService extends BaseService { ) } - const { - metadata, - items, - billing_address, - shipping_address, - ...rest - } = update + const { ...rest } = update if ("metadata" in update) { order.metadata = this.setMetadata_(order, update.metadata) @@ -932,7 +926,7 @@ class OrderService extends BaseService { * @return {Promise} result of the update operation. */ async cancel(orderId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId, { relations: [ "fulfillments", @@ -952,16 +946,16 @@ class OrderService extends BaseService { } const throwErrorIf = (arr, pred, type) => - arr?.filter(pred).find(_ => { + arr?.filter(pred).find((_) => { throw new MedusaError( MedusaError.Types.NOT_ALLOWED, `All ${type} must be canceled before canceling an order` ) }) - const notCanceled = o => !o.canceled_at + const notCanceled = (o) => !o.canceled_at throwErrorIf(order.fulfillments, notCanceled, "fulfillments") - throwErrorIf(order.returns, r => r.status !== "canceled", "returns") + throwErrorIf(order.returns, (r) => r.status !== "canceled", "returns") throwErrorIf(order.swaps, notCanceled, "swaps") throwErrorIf(order.claims, notCanceled, "claims") @@ -1000,7 +994,7 @@ class OrderService extends BaseService { * @return {Promise} result of the update operation. */ async capturePayment(orderId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const orderRepo = manager.getCustomRepository(this.orderRepository_) const order = await this.retrieve(orderId, { relations: ["payments"] }) @@ -1017,7 +1011,7 @@ class OrderService extends BaseService { const result = await this.paymentProviderService_ .withTransaction(manager) .capturePayment(p) - .catch(err => { + .catch((err) => { this.eventBus_ .withTransaction(manager) .emit(OrderService.Events.PAYMENT_CAPTURE_FAILED, { @@ -1039,7 +1033,7 @@ class OrderService extends BaseService { } order.payments = payments - order.payment_status = payments.every(p => p.captured_at !== null) + order.payment_status = payments.every((p) => p.captured_at !== null) ? "captured" : "requires_action" @@ -1095,6 +1089,8 @@ class OrderService extends BaseService { * we need to partition the order items, such that they can be sent * to their respective fulfillment provider. * @param {string} orderId - id of order to cancel. + * @param {Object} itemsToFulfill - items to fulfil. + * @param {Object} config - the config to cancel. * @return {Promise} result of the update operation. */ async createFulfillment( @@ -1107,7 +1103,7 @@ class OrderService extends BaseService { ) { const { metadata, no_notification } = config - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { // NOTE: we are telling the service to calculate all totals for us which // will add to what is fetched from the database. We want this to happen // so that we get all order details. These will thereafter be forwarded @@ -1169,7 +1165,7 @@ class OrderService extends BaseService { // Update all line items to reflect fulfillment for (const item of order.items) { const fulfillmentItem = successfullyFulfilled.find( - f => item.id === f.item_id + (f) => item.id === f.item_id ) if (fulfillmentItem) { @@ -1217,10 +1213,10 @@ class OrderService extends BaseService { /** * Cancels a fulfillment (if related to an order) * @param {string} fulfillmentId - the ID of the fulfillment to cancel - * @returns updated order + * @return {Promise} updated order */ async cancelFulfillment(fulfillmentId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const canceled = await this.fulfillmentService_ .withTransaction(manager) .cancelFulfillment(fulfillmentId) @@ -1264,12 +1260,12 @@ class OrderService extends BaseService { async getFulfillmentItems_(order, items, transformer) { const toReturn = await Promise.all( items.map(async ({ item_id, quantity }) => { - const item = order.items.find(i => i.id.equals(item_id)) + const item = order.items.find((i) => i.id.equals(item_id)) return transformer(item, quantity) }) ) - return toReturn.filter(i => !!i) + return toReturn.filter((i) => !!i) } /** @@ -1279,7 +1275,7 @@ class OrderService extends BaseService { * @return {Promise} the result of the update operation */ async archive(orderId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId) if (order.status !== ("completed" || "refunded")) { @@ -1298,6 +1294,12 @@ class OrderService extends BaseService { /** * Refunds a given amount back to the customer. + * @param {string} orderId - id of the order to refund. + * @param {Double} refundAmount - the amount to refund. + * @param {string} reason - the reason to refund. + * @param {string} note - note for refund. + * @param {Object} config - the config for refund. + * @return {Promise} the result of the refund operation. */ async createRefund( orderId, @@ -1310,7 +1312,7 @@ class OrderService extends BaseService { ) { const { no_notification } = config - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId, { select: ["refundable_amount", "total", "refunded_total"], relations: ["payments"], @@ -1380,7 +1382,7 @@ class OrderService extends BaseService { } if (totalsFields.includes("items.refundable")) { - order.items = order.items.map(i => ({ + order.items = order.items.map((i) => ({ ...i, refundable: this.totalsService_.getLineItemRefund(order, { ...i, @@ -1395,7 +1397,7 @@ class OrderService extends BaseService { order.swaps.length ) { for (const s of order.swaps) { - s.additional_items = s.additional_items.map(i => ({ + s.additional_items = s.additional_items.map((i) => ({ ...i, refundable: this.totalsService_.getLineItemRefund(order, { ...i, @@ -1418,10 +1420,11 @@ class OrderService extends BaseService { * mismatches. * @param {string} orderId - the order to return. * @param {object} receivedReturn - the received return + * @param {float} customRefundAmount - the custom refund amount return * @return {Promise} the result of the update operation */ async registerReturnReceived(orderId, receivedReturn, customRefundAmount) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const order = await this.retrieve(orderId, { select: ["total", "refunded_total", "refundable_amount"], relations: ["items", "returns", "payments"], @@ -1441,7 +1444,7 @@ class OrderService extends BaseService { ) } - let refundAmount = customRefundAmount || receivedReturn.refund_amount + const refundAmount = customRefundAmount || receivedReturn.refund_amount const orderRepo = manager.getCustomRepository(this.orderRepository_) @@ -1510,7 +1513,7 @@ class OrderService extends BaseService { const keyPath = `metadata.${key}` return this.orderModel_ .updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } }) - .catch(err => { + .catch((err) => { throw new MedusaError(MedusaError.Types.DB_ERROR, err.message) }) } From c0bfc49a9c5b8c4e87e1279a81758af56ef671e7 Mon Sep 17 00:00:00 2001 From: saurabh singh Date: Thu, 14 Oct 2021 10:38:37 +0530 Subject: [PATCH 2/7] chore: Make packages/medusa/src/services/order.js pass linting #515 --- packages/medusa/src/services/order.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index 2ecfcad685..07e1a96901 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -1295,7 +1295,7 @@ class OrderService extends BaseService { /** * Refunds a given amount back to the customer. * @param {string} orderId - id of the order to refund. - * @param {Double} refundAmount - the amount to refund. + * @param {float} refundAmount - the amount to refund. * @param {string} reason - the reason to refund. * @param {string} note - note for refund. * @param {Object} config - the config for refund. From 1439450e63db253c0ae08d1a577421a1b88fe280 Mon Sep 17 00:00:00 2001 From: saurabh singh Date: Thu, 14 Oct 2021 10:42:35 +0530 Subject: [PATCH 3/7] chore: Make packages/medusa/src/services/order.js pass linting #515 --- .eslintignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index 8111735959..32ec6fa5e3 100644 --- a/.eslintignore +++ b/.eslintignore @@ -16,7 +16,6 @@ /packages/medusa/src/services/note.js /packages/medusa/src/services/notification.js /packages/medusa/src/services/oauth.js -/packages/medusa/src/services/order.js /packages/medusa/src/services/payment-provider.js /packages/medusa/src/services/product-collection.js /packages/medusa/src/services/product-variant.js From 25adc7d26b927baeff56ebb08b4c7f6a2c027562 Mon Sep 17 00:00:00 2001 From: saurabh singh Date: Thu, 14 Oct 2021 21:49:13 +0530 Subject: [PATCH 4/7] chore: Make packages/medusa/src/services/inventory.js pass linting #509 --- .eslintignore | 3 +-- packages/medusa/src/services/inventory.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.eslintignore b/.eslintignore index 32ec6fa5e3..4e1f81a356 100644 --- a/.eslintignore +++ b/.eslintignore @@ -7,10 +7,9 @@ /packages/medusa/src/services/discount.js /packages/medusa/src/services/draft-order.js /packages/medusa/src/services/event-bus.js -/packages/medusa/src/services/fulfillment-provider.js + /packages/medusa/src/services/fulfillment.js /packages/medusa/src/services/idempotency-key.js -/packages/medusa/src/services/inventory.js /packages/medusa/src/services/line-item.js /packages/medusa/src/services/middleware.js /packages/medusa/src/services/note.js diff --git a/packages/medusa/src/services/inventory.js b/packages/medusa/src/services/inventory.js index 944a7e6ffe..3883506b30 100644 --- a/packages/medusa/src/services/inventory.js +++ b/packages/medusa/src/services/inventory.js @@ -29,19 +29,19 @@ class InventoryService extends BaseService { /** * Updates the inventory of a variant based on a given adjustment. - * @params {string} variantId - the id of the variant to update - * @params {number} adjustment - the number to adjust the inventory quantity by + * @param {string} variantId - the id of the variant to update + * @param {number} adjustment - the number to adjust the inventory quantity by * @return {Promise} resolves to the update result. */ async adjustInventory(variantId, adjustment) { - //if variantId is undefined – ergo. a custom item – then do nothing + // if variantId is undefined – ergo. a custom item – then do nothing if (typeof variantId === "undefined" || variantId === null) { return } - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const variant = await this.productVariantService_.retrieve(variantId) - //if inventory is managed then update + // if inventory is managed then update if (variant.manage_inventory) { return await this.productVariantService_ .withTransaction(manager) @@ -55,13 +55,13 @@ class InventoryService extends BaseService { * Checks if the inventory of a variant can cover a given quantity. Will * return true if the variant doesn't have managed inventory or if the variant * allows backorders or if the inventory quantity is greater than `quantity`. - * @params {string} variantId - the id of the variant to check - * @params {number} quantity - the number of units to check availability for + * @param {string} variantId - the id of the variant to check + * @param {number} quantity - the number of units to check availability for * @return {boolean} true if the inventory covers the quantity */ async confirmInventory(variantId, quantity) { - //if variantId is undefined then confirm inventory as it - //is a custom item that is not managed + // if variantId is undefined then confirm inventory as it + // is a custom item that is not managed if (typeof variantId === "undefined" || variantId === null) { return true } From 144eec3680fe8b30593b127d4870e5949217cfa4 Mon Sep 17 00:00:00 2001 From: saurabh singh Date: Thu, 14 Oct 2021 22:01:34 +0530 Subject: [PATCH 5/7] chore: Make packages/medusa/src/services/inventory.js pass linting #509 --- .eslintignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index 4e1f81a356..efeb90a97b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -7,7 +7,7 @@ /packages/medusa/src/services/discount.js /packages/medusa/src/services/draft-order.js /packages/medusa/src/services/event-bus.js - +/packages/medusa/src/services/fulfillment-provider.js /packages/medusa/src/services/fulfillment.js /packages/medusa/src/services/idempotency-key.js /packages/medusa/src/services/line-item.js From 7941338217b1a2a6505a304cb895faacafa8cd2e Mon Sep 17 00:00:00 2001 From: saurabh singh Date: Thu, 14 Oct 2021 22:25:42 +0530 Subject: [PATCH 6/7] chore: Make packages/medusa/src/services/fulfillment-provider.js pass linting #505 --- .eslintignore | 1 - packages/medusa/src/services/fulfillment-provider.js | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintignore b/.eslintignore index efeb90a97b..abe1141d33 100644 --- a/.eslintignore +++ b/.eslintignore @@ -7,7 +7,6 @@ /packages/medusa/src/services/discount.js /packages/medusa/src/services/draft-order.js /packages/medusa/src/services/event-bus.js -/packages/medusa/src/services/fulfillment-provider.js /packages/medusa/src/services/fulfillment.js /packages/medusa/src/services/idempotency-key.js /packages/medusa/src/services/line-item.js diff --git a/packages/medusa/src/services/fulfillment-provider.js b/packages/medusa/src/services/fulfillment-provider.js index dbad8ac1c4..85705fcf33 100644 --- a/packages/medusa/src/services/fulfillment-provider.js +++ b/packages/medusa/src/services/fulfillment-provider.js @@ -28,7 +28,7 @@ class FulfillmentProviderService { async listFulfillmentOptions(providers) { const result = await Promise.all( - providers.map(async p => { + providers.map(async (p) => { const provider = await this.retrieveProvider(p) return { provider_id: p, @@ -41,7 +41,8 @@ class FulfillmentProviderService { } /** - * @returns {FulfillmentService} the payment fulfillment provider + * @param {string} provider_id - the provider id + * @return {FulfillmentService} the payment fulfillment provider */ retrieveProvider(provider_id) { try { From 8d84eb9ba5e34550983bcdc63cf6b151cdc2cc53 Mon Sep 17 00:00:00 2001 From: saurabh042 Date: Tue, 19 Oct 2021 22:30:12 +0530 Subject: [PATCH 7/7] chore: Make packages/medusa/src/services/inventory.js pass linting #509 chore: Make packages/medusa/src/services/inventory.js pass linting #509 --- .eslintignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index 2d6404f950..2580696792 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,7 +3,6 @@ /packages/medusa/src/services/cart.js /packages/medusa/src/services/claim-item.js /packages/medusa/src/services/event-bus.js -/packages/medusa/src/services/fulfillment.js /packages/medusa/src/services/fulfillment-provider.js /packages/medusa/src/services/middleware.js /packages/medusa/src/services/oauth.js