From ddeaf57f598b5bde8d048fdf63199ba8789348e0 Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Thu, 14 Oct 2021 10:52:18 +0300 Subject: [PATCH 1/4] fix: make packages/medusa/src/services/claim.js pass eslint (#551) --- .eslintignore | 1 - packages/medusa/src/services/claim.js | 48 ++++++++++++--------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/.eslintignore b/.eslintignore index 8111735959..5f029b0700 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,7 +2,6 @@ /packages/medusa/src/services/cart.js /packages/medusa/src/services/claim-item.js -/packages/medusa/src/services/claim.js /packages/medusa/src/services/customer.js /packages/medusa/src/services/discount.js /packages/medusa/src/services/draft-order.js diff --git a/packages/medusa/src/services/claim.js b/packages/medusa/src/services/claim.js index 9d60a28626..8e73dbf83e 100644 --- a/packages/medusa/src/services/claim.js +++ b/packages/medusa/src/services/claim.js @@ -1,7 +1,5 @@ -import _ from "lodash" -import { Validator, MedusaError } from "medusa-core-utils" +import { MedusaError } from "medusa-core-utils" import { BaseService } from "medusa-interfaces" -import { Brackets } from "typeorm" class ClaimService extends BaseService { static Events = { @@ -102,7 +100,7 @@ class ClaimService extends BaseService { } update(id, data) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const claimRepo = manager.getCustomRepository(this.claimRepository_) const claim = await this.retrieve(id, { relations: ["shipping_methods"] }) @@ -113,13 +111,7 @@ class ClaimService extends BaseService { ) } - const { - claim_items, - shipping_methods, - metadata, - fulfillment_status, - no_notification, - } = data + const { claim_items, shipping_methods, metadata, no_notification } = data if (metadata) { claim.metadata = this.setMetadata_(claim, metadata) @@ -183,9 +175,11 @@ class ClaimService extends BaseService { * Creates a Claim on an Order. Claims consists of items that are claimed and * optionally items to be sent as replacement for the claimed items. The * shipping address that the new items will be shipped to + * @param {Object} data - the object containing all data required to create a claim + * @return {Object} created claim */ create(data) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const claimRepo = manager.getCustomRepository(this.claimRepository_) const { @@ -257,8 +251,8 @@ class ClaimService extends BaseService { let toRefund = refund_amount if (type === "refund" && typeof refund_amount === "undefined") { - const lines = claim_items.map(ci => { - const orderItem = order.items.find(oi => oi.id === ci.item_id) + const lines = claim_items.map((ci) => { + const orderItem = order.items.find((oi) => oi.id === ci.item_id) return { ...orderItem, quantity: ci.quantity, @@ -274,7 +268,7 @@ class ClaimService extends BaseService { } const newItems = await Promise.all( - additional_items.map(i => + additional_items.map((i) => this.lineItemService_ .withTransaction(manager) .generate(i.variant_id, order.region_id, i.quantity) @@ -332,7 +326,7 @@ class ClaimService extends BaseService { await this.returnService_.withTransaction(manager).create({ order_id: order.id, claim_order_id: result.id, - items: claim_items.map(ci => ({ + items: claim_items.map((ci) => ({ item_id: ci.item_id, quantity: ci.quantity, metadata: ci.metadata, @@ -362,7 +356,7 @@ class ClaimService extends BaseService { ) { const { metadata, no_notification } = config - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const claim = await this.retrieve(id, { relations: [ "additional_items", @@ -429,7 +423,7 @@ class ClaimService extends BaseService { is_claim: true, no_notification: evaluatedNoNotification, }, - claim.additional_items.map(i => ({ + claim.additional_items.map((i) => ({ item_id: i.id, quantity: i.quantity, })), @@ -445,7 +439,7 @@ class ClaimService extends BaseService { for (const item of claim.additional_items) { const fulfillmentItem = successfullyFulfilled.find( - f => item.id === f.item_id + (f) => item.id === f.item_id ) if (fulfillmentItem) { @@ -485,7 +479,7 @@ class ClaimService extends BaseService { } async cancelFulfillment(fulfillmentId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const canceled = await this.fulfillmentService_ .withTransaction(manager) .cancelFulfillment(fulfillmentId) @@ -508,7 +502,7 @@ class ClaimService extends BaseService { } async processRefund(id) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const claim = await this.retrieve(id, { relations: ["order", "order.payments"], }) @@ -560,7 +554,7 @@ class ClaimService extends BaseService { ) { const { metadata, no_notification } = config - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const claim = await this.retrieve(id, { relations: ["additional_items"], }) @@ -584,7 +578,7 @@ class ClaimService extends BaseService { claim.fulfillment_status = "shipped" for (const i of claim.additional_items) { - const shipped = shipment.items.find(si => si.item_id === i.id) + const shipped = shipment.items.find((si) => si.item_id === i.id) if (shipped) { const shippedQty = (i.shipped_quantity || 0) + shipped.quantity await this.lineItemService_.withTransaction(manager).update(i.id, { @@ -617,7 +611,7 @@ class ClaimService extends BaseService { } async cancel(id) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const claim = await this.retrieve(id, { relations: ["return_order", "fulfillments", "order", "order.refunds"], }) @@ -665,6 +659,7 @@ class ClaimService extends BaseService { /** * @param {Object} selector - the query object for find + * @param {Object} config - the config object containing query settings * @return {Promise} the result of the find operation */ async list( @@ -678,7 +673,8 @@ class ClaimService extends BaseService { /** * Gets an order by id. - * @param {string} orderId - id of order to retrieve + * @param {string} claimId - id of order to retrieve + * @param {Object} config - the config object containing query settings * @return {Promise} the order document */ async retrieve(claimId, config = {}) { @@ -717,7 +713,7 @@ class ClaimService 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 fe599c709e1f7e0109ae69bd1d0efb396f959e6a Mon Sep 17 00:00:00 2001 From: Rachel Date: Thu, 14 Oct 2021 01:31:23 -0700 Subject: [PATCH 2/4] fix: make medusa/src/services/fulfillment.js pass eslint (#557) * fix: make /packages/medusa/src/services/gift-card.js pass eslint * fix: make medusa/src/services/fulfillment.js pass eslint * chore: restore eslintignore change * chore: clean up diffs --- .eslintignore | 1 - packages/medusa/src/services/fulfillment.js | 41 +++++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.eslintignore b/.eslintignore index 5f029b0700..1a96ca41aa 100644 --- a/.eslintignore +++ b/.eslintignore @@ -7,7 +7,6 @@ /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 diff --git a/packages/medusa/src/services/fulfillment.js b/packages/medusa/src/services/fulfillment.js index ed0b4e4f80..b2cd6b71bc 100644 --- a/packages/medusa/src/services/fulfillment.js +++ b/packages/medusa/src/services/fulfillment.js @@ -1,10 +1,9 @@ -import _ from "lodash" import { BaseService } from "medusa-interfaces" import { MedusaError } from "medusa-core-utils" /** * Handles Fulfillments - * @implements BaseService + * @extends BaseService */ class FulfillmentService extends BaseService { constructor({ @@ -61,7 +60,7 @@ class FulfillmentService extends BaseService { } partitionItems_(shippingMethods, items) { - let partitioned = [] + const partitioned = [] // partition order items to their dedicated shipping method for (const method of shippingMethods) { const temp = { shipping_method: method } @@ -95,12 +94,12 @@ class FulfillmentService 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 === item_id) + const item = order.items.find((i) => i.id === item_id) return transformer(item, quantity) }) ) - return toReturn.filter(i => !!i) + return toReturn.filter((i) => !!i) } /** @@ -137,6 +136,7 @@ class FulfillmentService extends BaseService { /** * Retrieves a fulfillment by its id. * @param {string} id - the id of the fulfillment to retrieve + * @param {object} config - optional values to include with fulfillmentRepository query * @return {Fulfillment} the fulfillment */ async retrieve(id, config = {}) { @@ -165,11 +165,11 @@ class FulfillmentService extends BaseService { * those partitions. * @param {Order} order - order to create fulfillment for * @param {{ item_id: string, quantity: number}[]} itemsToFulfill - the items in the order to fulfill - * @param {object} metadata - potential metadata to add + * @param {object} custom - potential custom values to add * @return {Fulfillment[]} the created fulfillments */ async createFulfillment(order, itemsToFulfill, custom = {}) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const fulfillmentRepository = manager.getCustomRepository( this.fulfillmentRepository_ ) @@ -190,18 +190,19 @@ class FulfillmentService extends BaseService { const ful = fulfillmentRepository.create({ ...custom, provider_id: shipping_method.shipping_option.provider_id, - items: items.map(i => ({ item_id: i.id, quantity: i.quantity })), + items: items.map((i) => ({ item_id: i.id, quantity: i.quantity })), data: {}, }) - let result = await fulfillmentRepository.save(ful) + const result = await fulfillmentRepository.save(ful) - result.data = await this.fulfillmentProviderService_.createFulfillment( - shipping_method, - items, - { ...order }, - { ...result } - ) + result.data = + await this.fulfillmentProviderService_.createFulfillment( + shipping_method, + items, + { ...order }, + { ...result } + ) return fulfillmentRepository.save(result) }) @@ -220,7 +221,7 @@ class FulfillmentService extends BaseService { * */ cancelFulfillment(fulfillmentOrId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { let id = fulfillmentOrId if (typeof fulfillmentOrId === "object") { id = fulfillmentOrId.id @@ -260,9 +261,9 @@ class FulfillmentService extends BaseService { /** * Creates a shipment by marking a fulfillment as shipped. Adds - * tracking numbers and potentially more metadata. + * tracking links and potentially more metadata. * @param {Order} fulfillmentId - the fulfillment to ship - * @param {TrackingLink[]} trackingNumbers - tracking numbers for the shipment + * @param {TrackingLink[]} trackingLinks - tracking links for the shipment * @param {object} config - potential configuration settings, such as no_notification and metadata * @return {Fulfillment} the shipped fulfillment */ @@ -276,7 +277,7 @@ class FulfillmentService extends BaseService { ) { const { metadata, no_notification } = config - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const fulfillmentRepository = manager.getCustomRepository( this.fulfillmentRepository_ ) @@ -298,7 +299,7 @@ class FulfillmentService extends BaseService { const now = new Date() fulfillment.shipped_at = now - fulfillment.tracking_links = trackingLinks.map(tl => + fulfillment.tracking_links = trackingLinks.map((tl) => trackingLinkRepo.create(tl) ) From 1e13c831ab955d4221a887e921ee9d38b0b27225 Mon Sep 17 00:00:00 2001 From: Vladyslav Date: Thu, 14 Oct 2021 11:36:58 +0300 Subject: [PATCH 3/4] fix: make packages/medusa/src/services/discount.js pass eslint (#553) --- .eslintignore | 1 - packages/medusa/src/services/discount.js | 82 ++++++++++++------------ 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/.eslintignore b/.eslintignore index 1a96ca41aa..a3ff74ff07 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/customer.js -/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 diff --git a/packages/medusa/src/services/discount.js b/packages/medusa/src/services/discount.js index 70f404f378..bb796208ce 100644 --- a/packages/medusa/src/services/discount.js +++ b/packages/medusa/src/services/discount.js @@ -1,14 +1,11 @@ -import _ from "lodash" -import randomize from "randomatic" import { BaseService } from "medusa-interfaces" import { Validator, MedusaError } from "medusa-core-utils" -import { MedusaErrorCodes } from "medusa-core-utils/dist/errors" import { parse, toSeconds } from "iso8601-duration" import { Brackets, ILike } from "typeorm" /** * Provides layer to manipulate discounts. - * @implements BaseService + * @implements {BaseService} */ class DiscountService extends BaseService { constructor({ @@ -17,7 +14,6 @@ class DiscountService extends BaseService { discountRuleRepository, giftCardRepository, totalsService, - productVariantService, productService, regionService, eventBusService, @@ -80,21 +76,13 @@ class DiscountService extends BaseService { id: Validator.string().optional(), description: Validator.string().optional(), type: Validator.string().required(), - value: Validator.number() - .min(0) - .required(), + value: Validator.number().min(0).required(), allocation: Validator.string().required(), valid_for: Validator.array().optional(), created_at: Validator.date().optional(), - updated_at: Validator.date() - .allow(null) - .optional(), - deleted_at: Validator.date() - .allow(null) - .optional(), - metadata: Validator.object() - .allow(null) - .optional(), + updated_at: Validator.date().allow(null).optional(), + deleted_at: Validator.date().allow(null).optional(), + metadata: Validator.object().allow(null).optional(), }) const { value, error } = schema.validate(discountRule) @@ -117,6 +105,7 @@ class DiscountService extends BaseService { /** * @param {Object} selector - the query object for find + * @param {Object} config - the config object containing query settings * @return {Promise} the result of the find operation */ async list(selector = {}, config = { relations: [], skip: 0, take: 10 }) { @@ -130,6 +119,7 @@ class DiscountService extends BaseService { /** * @param {Object} selector - the query object for find + * @param {Object} config - the config object containing query settings * @return {Promise} the result of the find operation */ async listAndCount( @@ -153,11 +143,11 @@ class DiscountService extends BaseService { delete where.code - query.where = qb => { + query.where = (qb) => { qb.where(where) qb.andWhere( - new Brackets(qb => { + new Brackets((qb) => { qb.where({ code: ILike(`%${q}%`) }) }) ) @@ -176,19 +166,19 @@ class DiscountService extends BaseService { * @return {Promise} the result of the create operation */ async create(discount) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRepo = manager.getCustomRepository(this.discountRepository_) const ruleRepo = manager.getCustomRepository(this.discountRuleRepository_) if (discount.rule?.valid_for) { - discount.rule.valid_for = discount.rule.valid_for.map(id => ({ id })) + discount.rule.valid_for = discount.rule.valid_for.map((id) => ({ id })) } const validatedRule = this.validateDiscountRule_(discount.rule) if (discount.regions) { discount.regions = await Promise.all( - discount.regions.map(regionId => + discount.regions.map((regionId) => this.regionService_.withTransaction(manager).retrieve(regionId) ) ) @@ -209,6 +199,7 @@ class DiscountService extends BaseService { /** * Gets a discount by id. * @param {string} discountId - id of discount to retrieve + * @param {Object} config - the config object containing query settings * @return {Promise} the discount */ async retrieve(discountId, config = {}) { @@ -233,6 +224,7 @@ class DiscountService extends BaseService { /** * Gets a discount by discount code. * @param {string} discountCode - discount code of discount to retrieve + * @param {array} relations - list of relations * @return {Promise} the discount document */ async retrieveByCode(discountCode, relations = []) { @@ -269,7 +261,7 @@ class DiscountService extends BaseService { * @return {Promise} the result of the update operation */ async update(discountId, update) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRepo = manager.getCustomRepository(this.discountRepository_) const discount = await this.retrieve(discountId) @@ -287,7 +279,7 @@ class DiscountService extends BaseService { if (regions) { discount.regions = await Promise.all( - regions.map(regionId => this.regionService_.retrieve(regionId)) + regions.map((regionId) => this.regionService_.retrieve(regionId)) ) } @@ -298,7 +290,9 @@ class DiscountService extends BaseService { if (rule) { discount.rule = this.validateDiscountRule_(rule) if (rule.valid_for) { - discount.rule.valid_for = discount.rule.valid_for.map(id => ({ id })) + discount.rule.valid_for = discount.rule.valid_for.map((id) => ({ + id, + })) } } @@ -314,11 +308,11 @@ class DiscountService extends BaseService { /** * Creates a dynamic code for a discount id. * @param {string} discountId - the id of the discount to create a code for - * @param {string} code - the code to identify the discount by + * @param {Object} data - the object containing a code to identify the discount by * @return {Promise} the newly created dynamic code */ async createDynamicCode(discountId, data) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRepo = manager.getCustomRepository(this.discountRepository_) const discount = await this.retrieve(discountId) @@ -367,13 +361,15 @@ class DiscountService extends BaseService { * @return {Promise} the newly created dynamic code */ async deleteDynamicCode(discountId, code) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRepo = manager.getCustomRepository(this.discountRepository_) const discount = await discountRepo.findOne({ where: { parent_discount_id: discountId, code }, }) - if (!discount) return Promise.resolve() + if (!discount) { + return Promise.resolve() + } await discountRepo.softRemove(discount) @@ -388,7 +384,7 @@ class DiscountService extends BaseService { * @return {Promise} the result of the update operation */ async addValidProduct(discountId, productId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRuleRepo = manager.getCustomRepository( this.discountRuleRepository_ ) @@ -399,7 +395,7 @@ class DiscountService extends BaseService { const { rule } = discount - const exists = rule.valid_for.find(p => p.id === productId) + const exists = rule.valid_for.find((p) => p.id === productId) // If product is already present, we return early if (exists) { return rule @@ -421,7 +417,7 @@ class DiscountService extends BaseService { * @return {Promise} the result of the update operation */ async removeValidProduct(discountId, productId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRuleRepo = manager.getCustomRepository( this.discountRuleRepository_ ) @@ -432,13 +428,13 @@ class DiscountService extends BaseService { const { rule } = discount - const exists = rule.valid_for.find(p => p.id === productId) + const exists = rule.valid_for.find((p) => p.id === productId) // If product is not present, we return early if (!exists) { return rule } - rule.valid_for = rule.valid_for.filter(p => p.id !== productId) + rule.valid_for = rule.valid_for.filter((p) => p.id !== productId) const updated = await discountRuleRepo.save(rule) return updated @@ -452,14 +448,14 @@ class DiscountService extends BaseService { * @return {Promise} the result of the update operation */ async addRegion(discountId, regionId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRepo = manager.getCustomRepository(this.discountRepository_) const discount = await this.retrieve(discountId, { relations: ["regions"], }) - const exists = discount.regions.find(r => r.id === regionId) + const exists = discount.regions.find((r) => r.id === regionId) // If region is already present, we return early if (exists) { return discount @@ -481,20 +477,20 @@ class DiscountService extends BaseService { * @return {Promise} the result of the update operation */ async removeRegion(discountId, regionId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRepo = manager.getCustomRepository(this.discountRepository_) const discount = await this.retrieve(discountId, { relations: ["regions"], }) - const exists = discount.regions.find(r => r.id === regionId) + const exists = discount.regions.find((r) => r.id === regionId) // If region is not present, we return early if (!exists) { return discount } - discount.regions = discount.regions.filter(r => r.id !== regionId) + discount.regions = discount.regions.filter((r) => r.id !== regionId) const updated = await discountRepo.save(discount) return updated @@ -507,12 +503,14 @@ class DiscountService extends BaseService { * @return {Promise} the result of the delete operation */ async delete(discountId) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const discountRepo = manager.getCustomRepository(this.discountRepository_) const discount = await discountRepo.findOne({ where: { id: discountId } }) - if (!discount) return Promise.resolve() + if (!discount) { + return Promise.resolve() + } await discountRepo.softRemove(discount) @@ -522,7 +520,7 @@ class DiscountService extends BaseService { /** * Decorates a discount. - * @param {Discount} discount - the discount to decorate. + * @param {string} discountId - id of discount to decorate * @param {string[]} fields - the fields to include. * @param {string[]} expandFields - fields to expand. * @return {Discount} return the decorated discount. From a1ada9309388bba629626650938370ffb05cdb40 Mon Sep 17 00:00:00 2001 From: saurabh042 Date: Thu, 14 Oct 2021 14:19:57 +0530 Subject: [PATCH 4/4] chore/lint : Make packages/medusa/src/services/order.js pass linting #515 (#556) --- .eslintignore | 1 - packages/medusa/src/services/order.js | 119 +++++++++++++------------- 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/.eslintignore b/.eslintignore index a3ff74ff07..1faf09169d 100644 --- a/.eslintignore +++ b/.eslintignore @@ -13,7 +13,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 diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index c63e3d2cdb..07e1a96901 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 {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. + * @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) }) }