Merge branch 'chore/lint' into chore/lint
This commit is contained in:
+1
-2
@@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
/packages/medusa/src/services/cart.js
|
/packages/medusa/src/services/cart.js
|
||||||
/packages/medusa/src/services/claim-item.js
|
/packages/medusa/src/services/claim-item.js
|
||||||
/packages/medusa/src/services/claim.js
|
|
||||||
/packages/medusa/src/services/customer.js
|
/packages/medusa/src/services/customer.js
|
||||||
/packages/medusa/src/services/discount.js
|
|
||||||
/packages/medusa/src/services/draft-order.js
|
/packages/medusa/src/services/draft-order.js
|
||||||
/packages/medusa/src/services/event-bus.js
|
/packages/medusa/src/services/event-bus.js
|
||||||
/packages/medusa/src/services/fulfillment.js
|
/packages/medusa/src/services/fulfillment.js
|
||||||
|
/packages/medusa/src/services/fulfillment-provider.js
|
||||||
/packages/medusa/src/services/idempotency-key.js
|
/packages/medusa/src/services/idempotency-key.js
|
||||||
/packages/medusa/src/services/line-item.js
|
/packages/medusa/src/services/line-item.js
|
||||||
/packages/medusa/src/services/middleware.js
|
/packages/medusa/src/services/middleware.js
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import _ from "lodash"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import { Brackets } from "typeorm"
|
|
||||||
|
|
||||||
class ClaimService extends BaseService {
|
class ClaimService extends BaseService {
|
||||||
static Events = {
|
static Events = {
|
||||||
@@ -102,7 +100,7 @@ class ClaimService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update(id, data) {
|
update(id, data) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||||
const claim = await this.retrieve(id, { relations: ["shipping_methods"] })
|
const claim = await this.retrieve(id, { relations: ["shipping_methods"] })
|
||||||
|
|
||||||
@@ -113,13 +111,7 @@ class ClaimService extends BaseService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const { claim_items, shipping_methods, metadata, no_notification } = data
|
||||||
claim_items,
|
|
||||||
shipping_methods,
|
|
||||||
metadata,
|
|
||||||
fulfillment_status,
|
|
||||||
no_notification,
|
|
||||||
} = data
|
|
||||||
|
|
||||||
if (metadata) {
|
if (metadata) {
|
||||||
claim.metadata = this.setMetadata_(claim, 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
|
* 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
|
* optionally items to be sent as replacement for the claimed items. The
|
||||||
* shipping address that the new items will be shipped to
|
* 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) {
|
create(data) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
const claimRepo = manager.getCustomRepository(this.claimRepository_)
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -257,8 +251,8 @@ class ClaimService extends BaseService {
|
|||||||
|
|
||||||
let toRefund = refund_amount
|
let toRefund = refund_amount
|
||||||
if (type === "refund" && typeof refund_amount === "undefined") {
|
if (type === "refund" && typeof refund_amount === "undefined") {
|
||||||
const lines = claim_items.map(ci => {
|
const lines = claim_items.map((ci) => {
|
||||||
const orderItem = order.items.find(oi => oi.id === ci.item_id)
|
const orderItem = order.items.find((oi) => oi.id === ci.item_id)
|
||||||
return {
|
return {
|
||||||
...orderItem,
|
...orderItem,
|
||||||
quantity: ci.quantity,
|
quantity: ci.quantity,
|
||||||
@@ -274,7 +268,7 @@ class ClaimService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const newItems = await Promise.all(
|
const newItems = await Promise.all(
|
||||||
additional_items.map(i =>
|
additional_items.map((i) =>
|
||||||
this.lineItemService_
|
this.lineItemService_
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.generate(i.variant_id, order.region_id, i.quantity)
|
.generate(i.variant_id, order.region_id, i.quantity)
|
||||||
@@ -332,7 +326,7 @@ class ClaimService extends BaseService {
|
|||||||
await this.returnService_.withTransaction(manager).create({
|
await this.returnService_.withTransaction(manager).create({
|
||||||
order_id: order.id,
|
order_id: order.id,
|
||||||
claim_order_id: result.id,
|
claim_order_id: result.id,
|
||||||
items: claim_items.map(ci => ({
|
items: claim_items.map((ci) => ({
|
||||||
item_id: ci.item_id,
|
item_id: ci.item_id,
|
||||||
quantity: ci.quantity,
|
quantity: ci.quantity,
|
||||||
metadata: ci.metadata,
|
metadata: ci.metadata,
|
||||||
@@ -362,7 +356,7 @@ class ClaimService extends BaseService {
|
|||||||
) {
|
) {
|
||||||
const { metadata, no_notification } = config
|
const { metadata, no_notification } = config
|
||||||
|
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const claim = await this.retrieve(id, {
|
const claim = await this.retrieve(id, {
|
||||||
relations: [
|
relations: [
|
||||||
"additional_items",
|
"additional_items",
|
||||||
@@ -429,7 +423,7 @@ class ClaimService extends BaseService {
|
|||||||
is_claim: true,
|
is_claim: true,
|
||||||
no_notification: evaluatedNoNotification,
|
no_notification: evaluatedNoNotification,
|
||||||
},
|
},
|
||||||
claim.additional_items.map(i => ({
|
claim.additional_items.map((i) => ({
|
||||||
item_id: i.id,
|
item_id: i.id,
|
||||||
quantity: i.quantity,
|
quantity: i.quantity,
|
||||||
})),
|
})),
|
||||||
@@ -445,7 +439,7 @@ class ClaimService extends BaseService {
|
|||||||
|
|
||||||
for (const item of claim.additional_items) {
|
for (const item of claim.additional_items) {
|
||||||
const fulfillmentItem = successfullyFulfilled.find(
|
const fulfillmentItem = successfullyFulfilled.find(
|
||||||
f => item.id === f.item_id
|
(f) => item.id === f.item_id
|
||||||
)
|
)
|
||||||
|
|
||||||
if (fulfillmentItem) {
|
if (fulfillmentItem) {
|
||||||
@@ -485,7 +479,7 @@ class ClaimService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async cancelFulfillment(fulfillmentId) {
|
async cancelFulfillment(fulfillmentId) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const canceled = await this.fulfillmentService_
|
const canceled = await this.fulfillmentService_
|
||||||
.withTransaction(manager)
|
.withTransaction(manager)
|
||||||
.cancelFulfillment(fulfillmentId)
|
.cancelFulfillment(fulfillmentId)
|
||||||
@@ -508,7 +502,7 @@ class ClaimService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async processRefund(id) {
|
async processRefund(id) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const claim = await this.retrieve(id, {
|
const claim = await this.retrieve(id, {
|
||||||
relations: ["order", "order.payments"],
|
relations: ["order", "order.payments"],
|
||||||
})
|
})
|
||||||
@@ -560,7 +554,7 @@ class ClaimService extends BaseService {
|
|||||||
) {
|
) {
|
||||||
const { metadata, no_notification } = config
|
const { metadata, no_notification } = config
|
||||||
|
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const claim = await this.retrieve(id, {
|
const claim = await this.retrieve(id, {
|
||||||
relations: ["additional_items"],
|
relations: ["additional_items"],
|
||||||
})
|
})
|
||||||
@@ -584,7 +578,7 @@ class ClaimService extends BaseService {
|
|||||||
claim.fulfillment_status = "shipped"
|
claim.fulfillment_status = "shipped"
|
||||||
|
|
||||||
for (const i of claim.additional_items) {
|
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) {
|
if (shipped) {
|
||||||
const shippedQty = (i.shipped_quantity || 0) + shipped.quantity
|
const shippedQty = (i.shipped_quantity || 0) + shipped.quantity
|
||||||
await this.lineItemService_.withTransaction(manager).update(i.id, {
|
await this.lineItemService_.withTransaction(manager).update(i.id, {
|
||||||
@@ -617,7 +611,7 @@ class ClaimService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async cancel(id) {
|
async cancel(id) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const claim = await this.retrieve(id, {
|
const claim = await this.retrieve(id, {
|
||||||
relations: ["return_order", "fulfillments", "order", "order.refunds"],
|
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} selector - the query object for find
|
||||||
|
* @param {Object} config - the config object containing query settings
|
||||||
* @return {Promise} the result of the find operation
|
* @return {Promise} the result of the find operation
|
||||||
*/
|
*/
|
||||||
async list(
|
async list(
|
||||||
@@ -678,7 +673,8 @@ class ClaimService extends BaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an order by id.
|
* 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<Order>} the order document
|
* @return {Promise<Order>} the order document
|
||||||
*/
|
*/
|
||||||
async retrieve(claimId, config = {}) {
|
async retrieve(claimId, config = {}) {
|
||||||
@@ -717,7 +713,7 @@ class ClaimService extends BaseService {
|
|||||||
const keyPath = `metadata.${key}`
|
const keyPath = `metadata.${key}`
|
||||||
return this.orderModel_
|
return this.orderModel_
|
||||||
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
|
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
|
||||||
.catch(err => {
|
.catch((err) => {
|
||||||
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
import _ from "lodash"
|
|
||||||
import randomize from "randomatic"
|
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
import { Validator, MedusaError } from "medusa-core-utils"
|
||||||
import { MedusaErrorCodes } from "medusa-core-utils/dist/errors"
|
|
||||||
import { parse, toSeconds } from "iso8601-duration"
|
import { parse, toSeconds } from "iso8601-duration"
|
||||||
import { Brackets, ILike } from "typeorm"
|
import { Brackets, ILike } from "typeorm"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides layer to manipulate discounts.
|
* Provides layer to manipulate discounts.
|
||||||
* @implements BaseService
|
* @implements {BaseService}
|
||||||
*/
|
*/
|
||||||
class DiscountService extends BaseService {
|
class DiscountService extends BaseService {
|
||||||
constructor({
|
constructor({
|
||||||
@@ -17,7 +14,6 @@ class DiscountService extends BaseService {
|
|||||||
discountRuleRepository,
|
discountRuleRepository,
|
||||||
giftCardRepository,
|
giftCardRepository,
|
||||||
totalsService,
|
totalsService,
|
||||||
productVariantService,
|
|
||||||
productService,
|
productService,
|
||||||
regionService,
|
regionService,
|
||||||
eventBusService,
|
eventBusService,
|
||||||
@@ -80,21 +76,13 @@ class DiscountService extends BaseService {
|
|||||||
id: Validator.string().optional(),
|
id: Validator.string().optional(),
|
||||||
description: Validator.string().optional(),
|
description: Validator.string().optional(),
|
||||||
type: Validator.string().required(),
|
type: Validator.string().required(),
|
||||||
value: Validator.number()
|
value: Validator.number().min(0).required(),
|
||||||
.min(0)
|
|
||||||
.required(),
|
|
||||||
allocation: Validator.string().required(),
|
allocation: Validator.string().required(),
|
||||||
valid_for: Validator.array().optional(),
|
valid_for: Validator.array().optional(),
|
||||||
created_at: Validator.date().optional(),
|
created_at: Validator.date().optional(),
|
||||||
updated_at: Validator.date()
|
updated_at: Validator.date().allow(null).optional(),
|
||||||
.allow(null)
|
deleted_at: Validator.date().allow(null).optional(),
|
||||||
.optional(),
|
metadata: Validator.object().allow(null).optional(),
|
||||||
deleted_at: Validator.date()
|
|
||||||
.allow(null)
|
|
||||||
.optional(),
|
|
||||||
metadata: Validator.object()
|
|
||||||
.allow(null)
|
|
||||||
.optional(),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const { value, error } = schema.validate(discountRule)
|
const { value, error } = schema.validate(discountRule)
|
||||||
@@ -117,6 +105,7 @@ class DiscountService extends BaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} selector - the query object for find
|
* @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
|
* @return {Promise} the result of the find operation
|
||||||
*/
|
*/
|
||||||
async list(selector = {}, config = { relations: [], skip: 0, take: 10 }) {
|
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} selector - the query object for find
|
||||||
|
* @param {Object} config - the config object containing query settings
|
||||||
* @return {Promise} the result of the find operation
|
* @return {Promise} the result of the find operation
|
||||||
*/
|
*/
|
||||||
async listAndCount(
|
async listAndCount(
|
||||||
@@ -153,11 +143,11 @@ class DiscountService extends BaseService {
|
|||||||
|
|
||||||
delete where.code
|
delete where.code
|
||||||
|
|
||||||
query.where = qb => {
|
query.where = (qb) => {
|
||||||
qb.where(where)
|
qb.where(where)
|
||||||
|
|
||||||
qb.andWhere(
|
qb.andWhere(
|
||||||
new Brackets(qb => {
|
new Brackets((qb) => {
|
||||||
qb.where({ code: ILike(`%${q}%`) })
|
qb.where({ code: ILike(`%${q}%`) })
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -176,19 +166,19 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the create operation
|
* @return {Promise} the result of the create operation
|
||||||
*/
|
*/
|
||||||
async create(discount) {
|
async create(discount) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
const ruleRepo = manager.getCustomRepository(this.discountRuleRepository_)
|
const ruleRepo = manager.getCustomRepository(this.discountRuleRepository_)
|
||||||
|
|
||||||
if (discount.rule?.valid_for) {
|
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)
|
const validatedRule = this.validateDiscountRule_(discount.rule)
|
||||||
|
|
||||||
if (discount.regions) {
|
if (discount.regions) {
|
||||||
discount.regions = await Promise.all(
|
discount.regions = await Promise.all(
|
||||||
discount.regions.map(regionId =>
|
discount.regions.map((regionId) =>
|
||||||
this.regionService_.withTransaction(manager).retrieve(regionId)
|
this.regionService_.withTransaction(manager).retrieve(regionId)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -209,6 +199,7 @@ class DiscountService extends BaseService {
|
|||||||
/**
|
/**
|
||||||
* Gets a discount by id.
|
* Gets a discount by id.
|
||||||
* @param {string} discountId - id of discount to retrieve
|
* @param {string} discountId - id of discount to retrieve
|
||||||
|
* @param {Object} config - the config object containing query settings
|
||||||
* @return {Promise<Discount>} the discount
|
* @return {Promise<Discount>} the discount
|
||||||
*/
|
*/
|
||||||
async retrieve(discountId, config = {}) {
|
async retrieve(discountId, config = {}) {
|
||||||
@@ -233,6 +224,7 @@ class DiscountService extends BaseService {
|
|||||||
/**
|
/**
|
||||||
* Gets a discount by discount code.
|
* Gets a discount by discount code.
|
||||||
* @param {string} discountCode - discount code of discount to retrieve
|
* @param {string} discountCode - discount code of discount to retrieve
|
||||||
|
* @param {array} relations - list of relations
|
||||||
* @return {Promise<Discount>} the discount document
|
* @return {Promise<Discount>} the discount document
|
||||||
*/
|
*/
|
||||||
async retrieveByCode(discountCode, relations = []) {
|
async retrieveByCode(discountCode, relations = []) {
|
||||||
@@ -269,7 +261,7 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async update(discountId, update) {
|
async update(discountId, update) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
|
|
||||||
const discount = await this.retrieve(discountId)
|
const discount = await this.retrieve(discountId)
|
||||||
@@ -287,7 +279,7 @@ class DiscountService extends BaseService {
|
|||||||
|
|
||||||
if (regions) {
|
if (regions) {
|
||||||
discount.regions = await Promise.all(
|
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) {
|
if (rule) {
|
||||||
discount.rule = this.validateDiscountRule_(rule)
|
discount.rule = this.validateDiscountRule_(rule)
|
||||||
if (rule.valid_for) {
|
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.
|
* Creates a dynamic code for a discount id.
|
||||||
* @param {string} discountId - the id of the discount to create a code for
|
* @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
|
* @return {Promise} the newly created dynamic code
|
||||||
*/
|
*/
|
||||||
async createDynamicCode(discountId, data) {
|
async createDynamicCode(discountId, data) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
|
|
||||||
const discount = await this.retrieve(discountId)
|
const discount = await this.retrieve(discountId)
|
||||||
@@ -367,13 +361,15 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the newly created dynamic code
|
* @return {Promise} the newly created dynamic code
|
||||||
*/
|
*/
|
||||||
async deleteDynamicCode(discountId, code) {
|
async deleteDynamicCode(discountId, code) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
const discount = await discountRepo.findOne({
|
const discount = await discountRepo.findOne({
|
||||||
where: { parent_discount_id: discountId, code },
|
where: { parent_discount_id: discountId, code },
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!discount) return Promise.resolve()
|
if (!discount) {
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
await discountRepo.softRemove(discount)
|
await discountRepo.softRemove(discount)
|
||||||
|
|
||||||
@@ -388,7 +384,7 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async addValidProduct(discountId, productId) {
|
async addValidProduct(discountId, productId) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRuleRepo = manager.getCustomRepository(
|
const discountRuleRepo = manager.getCustomRepository(
|
||||||
this.discountRuleRepository_
|
this.discountRuleRepository_
|
||||||
)
|
)
|
||||||
@@ -399,7 +395,7 @@ class DiscountService extends BaseService {
|
|||||||
|
|
||||||
const { rule } = discount
|
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 product is already present, we return early
|
||||||
if (exists) {
|
if (exists) {
|
||||||
return rule
|
return rule
|
||||||
@@ -421,7 +417,7 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async removeValidProduct(discountId, productId) {
|
async removeValidProduct(discountId, productId) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRuleRepo = manager.getCustomRepository(
|
const discountRuleRepo = manager.getCustomRepository(
|
||||||
this.discountRuleRepository_
|
this.discountRuleRepository_
|
||||||
)
|
)
|
||||||
@@ -432,13 +428,13 @@ class DiscountService extends BaseService {
|
|||||||
|
|
||||||
const { rule } = discount
|
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 product is not present, we return early
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
return rule
|
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)
|
const updated = await discountRuleRepo.save(rule)
|
||||||
return updated
|
return updated
|
||||||
@@ -452,14 +448,14 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async addRegion(discountId, regionId) {
|
async addRegion(discountId, regionId) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
|
|
||||||
const discount = await this.retrieve(discountId, {
|
const discount = await this.retrieve(discountId, {
|
||||||
relations: ["regions"],
|
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 region is already present, we return early
|
||||||
if (exists) {
|
if (exists) {
|
||||||
return discount
|
return discount
|
||||||
@@ -481,20 +477,20 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
async removeRegion(discountId, regionId) {
|
async removeRegion(discountId, regionId) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
|
|
||||||
const discount = await this.retrieve(discountId, {
|
const discount = await this.retrieve(discountId, {
|
||||||
relations: ["regions"],
|
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 region is not present, we return early
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
return discount
|
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)
|
const updated = await discountRepo.save(discount)
|
||||||
return updated
|
return updated
|
||||||
@@ -507,12 +503,14 @@ class DiscountService extends BaseService {
|
|||||||
* @return {Promise} the result of the delete operation
|
* @return {Promise} the result of the delete operation
|
||||||
*/
|
*/
|
||||||
async delete(discountId) {
|
async delete(discountId) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
const discountRepo = manager.getCustomRepository(this.discountRepository_)
|
||||||
|
|
||||||
const discount = await discountRepo.findOne({ where: { id: discountId } })
|
const discount = await discountRepo.findOne({ where: { id: discountId } })
|
||||||
|
|
||||||
if (!discount) return Promise.resolve()
|
if (!discount) {
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
await discountRepo.softRemove(discount)
|
await discountRepo.softRemove(discount)
|
||||||
|
|
||||||
@@ -522,7 +520,7 @@ class DiscountService extends BaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Decorates a discount.
|
* 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[]} fields - the fields to include.
|
||||||
* @param {string[]} expandFields - fields to expand.
|
* @param {string[]} expandFields - fields to expand.
|
||||||
* @return {Discount} return the decorated discount.
|
* @return {Discount} return the decorated discount.
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import _ from "lodash"
|
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles Fulfillments
|
* Handles Fulfillments
|
||||||
* @implements BaseService
|
* @extends BaseService
|
||||||
*/
|
*/
|
||||||
class FulfillmentService extends BaseService {
|
class FulfillmentService extends BaseService {
|
||||||
constructor({
|
constructor({
|
||||||
@@ -61,7 +60,7 @@ class FulfillmentService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
partitionItems_(shippingMethods, items) {
|
partitionItems_(shippingMethods, items) {
|
||||||
let partitioned = []
|
const partitioned = []
|
||||||
// partition order items to their dedicated shipping method
|
// partition order items to their dedicated shipping method
|
||||||
for (const method of shippingMethods) {
|
for (const method of shippingMethods) {
|
||||||
const temp = { shipping_method: method }
|
const temp = { shipping_method: method }
|
||||||
@@ -95,12 +94,12 @@ class FulfillmentService extends BaseService {
|
|||||||
async getFulfillmentItems_(order, items, transformer) {
|
async getFulfillmentItems_(order, items, transformer) {
|
||||||
const toReturn = await Promise.all(
|
const toReturn = await Promise.all(
|
||||||
items.map(async ({ item_id, quantity }) => {
|
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 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.
|
* Retrieves a fulfillment by its id.
|
||||||
* @param {string} id - the id of the fulfillment to retrieve
|
* @param {string} id - the id of the fulfillment to retrieve
|
||||||
|
* @param {object} config - optional values to include with fulfillmentRepository query
|
||||||
* @return {Fulfillment} the fulfillment
|
* @return {Fulfillment} the fulfillment
|
||||||
*/
|
*/
|
||||||
async retrieve(id, config = {}) {
|
async retrieve(id, config = {}) {
|
||||||
@@ -165,11 +165,11 @@ class FulfillmentService extends BaseService {
|
|||||||
* those partitions.
|
* those partitions.
|
||||||
* @param {Order} order - order to create fulfillment for
|
* @param {Order} order - order to create fulfillment for
|
||||||
* @param {{ item_id: string, quantity: number}[]} itemsToFulfill - the items in the order to fulfill
|
* @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
|
* @return {Fulfillment[]} the created fulfillments
|
||||||
*/
|
*/
|
||||||
async createFulfillment(order, itemsToFulfill, custom = {}) {
|
async createFulfillment(order, itemsToFulfill, custom = {}) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const fulfillmentRepository = manager.getCustomRepository(
|
const fulfillmentRepository = manager.getCustomRepository(
|
||||||
this.fulfillmentRepository_
|
this.fulfillmentRepository_
|
||||||
)
|
)
|
||||||
@@ -190,18 +190,19 @@ class FulfillmentService extends BaseService {
|
|||||||
const ful = fulfillmentRepository.create({
|
const ful = fulfillmentRepository.create({
|
||||||
...custom,
|
...custom,
|
||||||
provider_id: shipping_method.shipping_option.provider_id,
|
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: {},
|
data: {},
|
||||||
})
|
})
|
||||||
|
|
||||||
let result = await fulfillmentRepository.save(ful)
|
const result = await fulfillmentRepository.save(ful)
|
||||||
|
|
||||||
result.data = await this.fulfillmentProviderService_.createFulfillment(
|
result.data =
|
||||||
shipping_method,
|
await this.fulfillmentProviderService_.createFulfillment(
|
||||||
items,
|
shipping_method,
|
||||||
{ ...order },
|
items,
|
||||||
{ ...result }
|
{ ...order },
|
||||||
)
|
{ ...result }
|
||||||
|
)
|
||||||
|
|
||||||
return fulfillmentRepository.save(result)
|
return fulfillmentRepository.save(result)
|
||||||
})
|
})
|
||||||
@@ -220,7 +221,7 @@ class FulfillmentService extends BaseService {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
cancelFulfillment(fulfillmentOrId) {
|
cancelFulfillment(fulfillmentOrId) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
let id = fulfillmentOrId
|
let id = fulfillmentOrId
|
||||||
if (typeof fulfillmentOrId === "object") {
|
if (typeof fulfillmentOrId === "object") {
|
||||||
id = fulfillmentOrId.id
|
id = fulfillmentOrId.id
|
||||||
@@ -260,9 +261,9 @@ class FulfillmentService extends BaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a shipment by marking a fulfillment as shipped. Adds
|
* 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 {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
|
* @param {object} config - potential configuration settings, such as no_notification and metadata
|
||||||
* @return {Fulfillment} the shipped fulfillment
|
* @return {Fulfillment} the shipped fulfillment
|
||||||
*/
|
*/
|
||||||
@@ -276,7 +277,7 @@ class FulfillmentService extends BaseService {
|
|||||||
) {
|
) {
|
||||||
const { metadata, no_notification } = config
|
const { metadata, no_notification } = config
|
||||||
|
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const fulfillmentRepository = manager.getCustomRepository(
|
const fulfillmentRepository = manager.getCustomRepository(
|
||||||
this.fulfillmentRepository_
|
this.fulfillmentRepository_
|
||||||
)
|
)
|
||||||
@@ -298,7 +299,7 @@ class FulfillmentService extends BaseService {
|
|||||||
const now = new Date()
|
const now = new Date()
|
||||||
fulfillment.shipped_at = now
|
fulfillment.shipped_at = now
|
||||||
|
|
||||||
fulfillment.tracking_links = trackingLinks.map(tl =>
|
fulfillment.tracking_links = trackingLinks.map((tl) =>
|
||||||
trackingLinkRepo.create(tl)
|
trackingLinkRepo.create(tl)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user