fix(medusa): Move discount usage from rule to discount

This commit is contained in:
olivermrbl
2021-03-29 10:15:41 +02:00
parent 08bb111e29
commit d9cd52a177
17 changed files with 188 additions and 59 deletions
@@ -38,6 +38,8 @@ describe("POST /admin/discounts/:discount_id/regions/:region_id", () => {
"is_disabled",
"rule_id",
"parent_discount_id",
"usage_limit",
"usage_count",
"starts_at",
"ends_at",
"created_at",
@@ -38,6 +38,8 @@ describe("POST /admin/discounts/:discount_id/variants/:variant_id", () => {
"is_disabled",
"rule_id",
"parent_discount_id",
"usage_limit",
"usage_count",
"starts_at",
"ends_at",
"created_at",
@@ -9,6 +9,8 @@ const defaultFields = [
"is_disabled",
"rule_id",
"parent_discount_id",
"usage_limit",
"usage_count",
"starts_at",
"ends_at",
"created_at",
@@ -9,6 +9,8 @@ const defaultFields = [
"is_disabled",
"rule_id",
"parent_discount_id",
"usage_limit",
"usage_count",
"starts_at",
"ends_at",
"created_at",
@@ -9,6 +9,8 @@ const defaultFields = [
"is_disabled",
"rule_id",
"parent_discount_id",
"usage_limit",
"usage_count",
"starts_at",
"ends_at",
"created_at",
@@ -26,6 +26,7 @@ export default async (req, res) => {
const schema = Validator.object().keys({
code: Validator.string().required(),
usage_limit: Validator.number().default(1),
metadata: Validator.object().optional(),
})
@@ -62,6 +62,8 @@ export const defaultFields = [
"is_disabled",
"rule_id",
"parent_discount_id",
"usage_limit",
"usage_count",
"starts_at",
"ends_at",
"created_at",
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm"
export class discountUsage1617002207608 implements MigrationInterface {
name = "discountUsage1617002207608"
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "discount_rule" DROP COLUMN "usage_limit"`
)
await queryRunner.query(
`ALTER TABLE "discount_rule" DROP COLUMN "usage_count"`
)
await queryRunner.query(`ALTER TABLE "discount" ADD "usage_limit" integer`)
await queryRunner.query(
`ALTER TABLE "discount" ADD "usage_count" integer NOT NULL DEFAULT '0'`
)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "discount" DROP COLUMN "usage_count"`)
await queryRunner.query(`ALTER TABLE "discount" DROP COLUMN "usage_limit"`)
await queryRunner.query(
`ALTER TABLE "discount_rule" ADD "usage_count" integer NOT NULL DEFAULT '0'`
)
await queryRunner.query(
`ALTER TABLE "discount_rule" ADD "usage_limit" integer`
)
}
}
@@ -63,12 +63,6 @@ export class DiscountRule {
})
valid_for: Product[]
@Column({ nullable: true })
usage_limit: number
@Column({ default: 0 })
usage_count: number
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@@ -121,12 +115,6 @@ export class DiscountRule {
* type: array
* items:
* $ref: "#/components/schemas/product"
* usage_limit:
* description: "The maximum number of times that a discount can be used."
* type: integer
* usage_count:
* description: "The number of times a discount has been used."
* type: integer
* created_at:
* description: "The date with timezone at which the resource was created."
* type: string
+12
View File
@@ -68,6 +68,12 @@ export class Discount {
})
regions: Region[]
@Column({ nullable: true })
usage_limit: number
@Column({ default: 0 })
usage_count: number
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@@ -127,6 +133,12 @@ export class Discount {
* type: array
* items:
* $ref: "#/components/schemas/region"
* usage_limit:
* description: "The maximum number of times that a discount can be used."
* type: integer
* usage_count:
* description: "The number of times a discount has been used."
* type: integer
* created_at:
* description: "The date with timezone at which the resource was created."
* type: string
+9 -12
View File
@@ -1467,10 +1467,9 @@ describe("CartService", () => {
id: IdMap.getId("limit-reached"),
code: "limit-reached",
regions: [{ id: IdMap.getId("good") }],
rule: {
usage_count: 2,
usage_limit: 2,
},
rule: {},
usage_count: 2,
usage_limit: 2,
})
}
if (code === "null-count") {
@@ -1478,10 +1477,9 @@ describe("CartService", () => {
id: IdMap.getId("null-count"),
code: "null-count",
regions: [{ id: IdMap.getId("good") }],
rule: {
usage_count: null,
usage_limit: 2,
},
rule: {},
usage_count: null,
usage_limit: 2,
})
}
if (code === "FREESHIPPING") {
@@ -1630,10 +1628,9 @@ describe("CartService", () => {
id: IdMap.getId("null-count"),
code: "null-count",
regions: [{ id: IdMap.getId("good") }],
rule: {
usage_count: 0,
usage_limit: 2,
},
usage_count: 0,
usage_limit: 2,
rule: {},
},
],
discount_total: 0,
+3 -3
View File
@@ -814,10 +814,10 @@ class CartService extends BaseService {
const rule = discount.rule
// if limit is set and reached, we make an early exit
if (rule?.usage_limit) {
rule.usage_count = rule.usage_count || 0
if (discount.usage_limit) {
discount.usage_count = discount.usage_count || 0
if (rule.usage_limit === rule.usage_count)
if (discount.usage_limit === discount.usage_count)
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Discount has been used maximum allowed times"
+1 -7
View File
@@ -83,13 +83,6 @@ class DiscountService extends BaseService {
.required(),
allocation: Validator.string().required(),
valid_for: Validator.array().optional(),
usage_limit: Validator.number()
.positive()
.allow(null)
.optional(),
usage_count: Validator.number()
.positive()
.optional(),
created_at: Validator.date().optional(),
updated_at: Validator.date()
.allow(null)
@@ -337,6 +330,7 @@ class DiscountService extends BaseService {
is_disabled: false,
code: data.code.toUpperCase(),
parent_discount_id: discount.id,
usage_limit: data.usage_limit,
}
const created = await discountRepo.create(toCreate)
+2 -5
View File
@@ -48,12 +48,9 @@ class OrderSubscriber {
await Promise.all(
order.discounts.map(async d => {
const usageCount = d.rule?.usage_count || 0
const usageCount = d?.usage_count || 0
return this.discountService_.update(d.id, {
rule: {
...d.rule,
usage_count: usageCount + 1,
},
usage_count: usageCount + 1,
})
})
)