fix: make packages/medusa/src/services/discount.js pass eslint (#553)
This commit is contained in:
1 parent
fe599c709e
commit
1e13c831ab
2 files changed
+40
-43
No files matched your search
@@ -3,7 +3,6 @@
|
|||||||
/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/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-provider.js
|
/packages/medusa/src/services/fulfillment-provider.js
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
Reference in new issue
Block a user