fix: merge
This commit is contained in:
commit
7897610620
25 files changed
+508
-89
No files matched your search
@@ -244,6 +244,8 @@ class WebshipperFulfillmentService extends FulfillmentService {
|
||||
|
||||
if (!webshipperOrder) {
|
||||
let invoice
|
||||
let certificateOfOrigin
|
||||
|
||||
if (this.invoiceGenerator_) {
|
||||
const base64Invoice = await this.invoiceGenerator_.createInvoice(
|
||||
fromOrder,
|
||||
@@ -263,6 +265,27 @@ class WebshipperFulfillmentService extends FulfillmentService {
|
||||
.catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
if (this.invoiceGenerator_.createCertificateOfOrigin) {
|
||||
const base64Coo = await this.invoiceGenerator_.createCertificateOfOrigin(
|
||||
fromOrder,
|
||||
fulfillmentItems
|
||||
)
|
||||
|
||||
certificateOfOrigin = await this.client_.documents
|
||||
.create({
|
||||
type: "documents",
|
||||
attributes: {
|
||||
document_size: this.options_.document_size || "A4",
|
||||
document_format: "PDF",
|
||||
base64: base64Coo,
|
||||
document_type: "certificate",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let id = fulfillment.id
|
||||
@@ -335,14 +358,25 @@ class WebshipperFulfillmentService extends FulfillmentService {
|
||||
country_code: methodData.drop_point_country_code.toUpperCase(),
|
||||
}
|
||||
}
|
||||
if (invoice) {
|
||||
|
||||
if (invoice || certificateOfOrigin) {
|
||||
const docData = []
|
||||
if (invoice) {
|
||||
docData.push({
|
||||
id: invoice.data.id,
|
||||
type: invoice.data.type,
|
||||
})
|
||||
}
|
||||
|
||||
if (certificateOfOrigin) {
|
||||
docData.push({
|
||||
id: certificateOfOrigin.data.id,
|
||||
type: certificateOfOrigin.data.type,
|
||||
})
|
||||
}
|
||||
|
||||
newOrder.relationships.documents = {
|
||||
data: [
|
||||
{
|
||||
id: invoice.data.id,
|
||||
type: invoice.data.type,
|
||||
},
|
||||
],
|
||||
data: docData,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ class ContentfulService extends BaseService {
|
||||
let assets = []
|
||||
await Promise.all(
|
||||
product.images
|
||||
.filter((image) => image !== product.thumbnail)
|
||||
.filter((image) => image.url !== product.thumbnail)
|
||||
.map(async (image, i) => {
|
||||
const asset = await environment.createAsset({
|
||||
fields: {
|
||||
@@ -89,8 +89,8 @@ class ContentfulService extends BaseService {
|
||||
file: {
|
||||
"en-US": {
|
||||
contentType: "image/xyz",
|
||||
fileName: image,
|
||||
upload: image,
|
||||
fileName: image.url,
|
||||
upload: image.url,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -450,7 +450,7 @@ class ContentfulService extends BaseService {
|
||||
]
|
||||
|
||||
// Update came directly from product variant service so only act on a couple
|
||||
// of fields. When the update comes from the product we want to ensure
|
||||
// of fields. When the update comes from the product we want to ensure
|
||||
// references are set up correctly so we run through everything.
|
||||
if (variant.fields) {
|
||||
const found = variant.fields.find((f) => updateFields.includes(f))
|
||||
|
||||
@@ -2090,9 +2090,9 @@ camelcase@^5.0.0, camelcase@^5.3.1:
|
||||
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
|
||||
|
||||
caniuse-lite@^1.0.30001181:
|
||||
version "1.0.30001207"
|
||||
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001207.tgz#364d47d35a3007e528f69adb6fecb07c2bb2cc50"
|
||||
integrity sha512-UPQZdmAsyp2qfCTiMU/zqGSWOYaY9F9LL61V8f+8MrubsaDGpaHD9HRV/EWZGULZn0Hxu48SKzI5DgFwTvHuYw==
|
||||
version "1.0.30001208"
|
||||
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001208.tgz#a999014a35cebd4f98c405930a057a0d75352eb9"
|
||||
integrity sha512-OE5UE4+nBOro8Dyvv0lfx+SRtfVIOM9uhKqFmJeUbGriqhhStgp1A0OyBpgy3OUF8AhYCT+PVwPC1gMl2ZcQMA==
|
||||
|
||||
capture-exit@^2.0.0:
|
||||
version "2.0.0"
|
||||
@@ -2359,17 +2359,17 @@ copy-descriptor@^0.1.0:
|
||||
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=
|
||||
|
||||
core-js-compat@^3.8.1, core-js-compat@^3.9.0:
|
||||
version "3.10.0"
|
||||
resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.10.0.tgz#3600dc72869673c110215ee7a005a8609dea0fe1"
|
||||
integrity sha512-9yVewub2MXNYyGvuLnMHcN1k9RkvB7/ofktpeKTIaASyB88YYqGzUnu0ywMMhJrDHOMiTjSHWGzR+i7Wb9Z1kQ==
|
||||
version "3.10.1"
|
||||
resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.10.1.tgz#62183a3a77ceeffcc420d907a3e6fc67d9b27f1c"
|
||||
integrity sha512-ZHQTdTPkqvw2CeHiZC970NNJcnwzT6YIueDMASKt+p3WbZsLXOcoD392SkcWhkC0wBBHhlfhqGKKsNCQUozYtg==
|
||||
dependencies:
|
||||
browserslist "^4.16.3"
|
||||
semver "7.0.0"
|
||||
|
||||
core-js@^3.6.5, core-js@^3.7.0:
|
||||
version "3.10.0"
|
||||
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.10.0.tgz#9a020547c8b6879f929306949e31496bbe2ae9b3"
|
||||
integrity sha512-MQx/7TLgmmDVamSyfE+O+5BHvG1aUGj/gHhLn1wVtm2B5u1eVIPvh7vkfjwWKNCjrTJB8+He99IntSQ1qP+vYQ==
|
||||
version "3.10.1"
|
||||
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.10.1.tgz#e683963978b6806dcc6c0a4a8bd4ab0bdaf3f21a"
|
||||
integrity sha512-pwCxEXnj27XG47mu7SXAwhLP3L5CrlvCB91ANUkIz40P27kUcvNfSdvyZJ9CLHiVoKSp+TTChMQMSKQEH/IQxA==
|
||||
|
||||
core-util-is@1.0.2, core-util-is@~1.0.0:
|
||||
version "1.0.2"
|
||||
@@ -2617,9 +2617,9 @@ ee-first@1.1.1:
|
||||
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
|
||||
|
||||
electron-to-chromium@^1.3.649:
|
||||
version "1.3.709"
|
||||
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.709.tgz#d7be0b5686a2fdfe8bad898faa3a428d04d8f656"
|
||||
integrity sha512-LolItk2/ikSGQ7SN8UkuKVNMBZp3RG7Itgaxj1npsHRzQobj9JjMneZOZfLhtwlYBe5fCJ75k+cVCiDFUs23oA==
|
||||
version "1.3.710"
|
||||
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.710.tgz#b33d316e5d6de92b916e766d8a478d19796ffe11"
|
||||
integrity sha512-b3r0E2o4yc7mNmBeJviejF1rEx49PUBi+2NPa7jHEX3arkAXnVgLhR0YmV8oi6/Qf3HH2a8xzQmCjHNH0IpXWQ==
|
||||
|
||||
emoji-regex@^7.0.1:
|
||||
version "7.0.3"
|
||||
|
||||
@@ -18,6 +18,17 @@ class OrderSubscriber {
|
||||
|
||||
this.fulfillmentService_ = fulfillmentService
|
||||
|
||||
|
||||
// Swaps
|
||||
// order.swap_received <--- Will be deprecated
|
||||
// swap.created
|
||||
// swap.received
|
||||
// swap.shipment_created
|
||||
// swap.payment_completed
|
||||
// swap.payment_captured
|
||||
// swap.refund_processed
|
||||
|
||||
|
||||
eventBusService.subscribe(
|
||||
"order.shipment_created",
|
||||
async ({ id, fulfillment_id }) => {
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
import { humanizeAmount } from "medusa-core-utils"
|
||||
|
||||
class OrderSubscriber {
|
||||
constructor({
|
||||
segmentService,
|
||||
eventBusService,
|
||||
swapService,
|
||||
lineItemService,
|
||||
fulfillmentService,
|
||||
}) {
|
||||
this.fulfillmentService_ = fulfillmentService
|
||||
|
||||
this.lineItemService_ = lineItemService
|
||||
|
||||
this.swapService_ = swapService
|
||||
|
||||
this.segmentService_ = segmentService
|
||||
|
||||
eventBusService.subscribe(
|
||||
"swap.shipment_created",
|
||||
async ({ id, fulfillment_id }) => {
|
||||
const [swap, swapReport] = await this.gatherSwapReport(id)
|
||||
const fulfillment = await this.fulfillmentService_.retrieve(
|
||||
fulfillment_id
|
||||
)
|
||||
|
||||
const currency = swapReport.currency
|
||||
const total = humanizeAmount(swap.difference_due, currency)
|
||||
const reporting_total = await this.segmentService_.getReportingValue(
|
||||
currency,
|
||||
total
|
||||
)
|
||||
|
||||
return await segmentService.track({
|
||||
event: "Swap Shipped",
|
||||
userId: swap.order.customer_id,
|
||||
timestamp: fulfillment.shipped_at,
|
||||
properties: {
|
||||
reporting_total,
|
||||
total,
|
||||
...swapReport,
|
||||
},
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
eventBusService.subscribe("swap.payment_completed", async ({ id }) => {
|
||||
const [swap, swapReport] = await this.gatherSwapReport(id)
|
||||
|
||||
const currency = swapReport.currency
|
||||
const total = humanizeAmount(swap.difference_due, currency)
|
||||
const reporting_total = await this.segmentService_.getReportingValue(
|
||||
currency,
|
||||
total
|
||||
)
|
||||
|
||||
return await segmentService.track({
|
||||
event: "Swap Confirmed",
|
||||
userId: swap.order.customer_id,
|
||||
timestamp: swap.confirmed_at,
|
||||
properties: {
|
||||
reporting_total,
|
||||
total,
|
||||
...swapReport,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
eventBusService.subscribe("swap.created", async ({ id }) => {
|
||||
const [swap, swapReport] = await this.gatherSwapReport(id)
|
||||
|
||||
return await segmentService.track({
|
||||
event: "Swap Created",
|
||||
userId: swap.order.customer_id,
|
||||
timestamp: swap.created_at,
|
||||
properties: swapReport,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async gatherSwapReport(id) {
|
||||
const swap = await this.swapService_.retrieve(id, {
|
||||
relations: [
|
||||
"order",
|
||||
"additional_items",
|
||||
"additional_items.variant",
|
||||
"return_order",
|
||||
"return_order.items",
|
||||
"return_order.shipping_method",
|
||||
],
|
||||
})
|
||||
|
||||
const currency = swap.order.currency_code
|
||||
|
||||
const newItems = await Promise.all(
|
||||
swap.additional_items.map(async (i) => {
|
||||
const price = humanizeAmount(i.unit_price, currency)
|
||||
const reporting_price = await this.segmentService_.getReportingValue(
|
||||
currency,
|
||||
price
|
||||
)
|
||||
|
||||
return {
|
||||
name: i.title,
|
||||
product_id: i.variant.product_id,
|
||||
variant: i.variant.sku,
|
||||
quantity: i.quantity,
|
||||
price,
|
||||
reporting_price,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const returnItems = await Promise.all(
|
||||
swap.return_order.items.map(async (ri) => {
|
||||
const i = await this.lineItemService_.retrieve(ri.item_id, {
|
||||
relations: ["variant"],
|
||||
})
|
||||
const price = humanizeAmount(i.unit_price, currency)
|
||||
const reporting_price = await this.segmentService_.getReportingValue(
|
||||
currency,
|
||||
price
|
||||
)
|
||||
|
||||
return {
|
||||
name: i.title,
|
||||
product_id: i.variant.product_id,
|
||||
variant: i.variant.sku,
|
||||
quantity: ri.quantity,
|
||||
price,
|
||||
reporting_price,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return [
|
||||
swap,
|
||||
{
|
||||
swap_id: swap.id,
|
||||
order_id: swap.order_id,
|
||||
new_items: newItems,
|
||||
return_items: returnItems,
|
||||
currency,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
export default OrderSubscriber
|
||||
@@ -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",
|
||||
|
||||
@@ -11,6 +11,7 @@ describe("POST /admin/discounts", () => {
|
||||
payload: {
|
||||
code: "TEST",
|
||||
rule: {
|
||||
description: "Test",
|
||||
type: "fixed",
|
||||
value: 10,
|
||||
allocation: "total",
|
||||
@@ -33,6 +34,7 @@ describe("POST /admin/discounts", () => {
|
||||
expect(DiscountServiceMock.create).toHaveBeenCalledWith({
|
||||
code: "TEST",
|
||||
rule: {
|
||||
description: "Test",
|
||||
type: "fixed",
|
||||
value: 10,
|
||||
allocation: "total",
|
||||
@@ -51,6 +53,7 @@ describe("POST /admin/discounts", () => {
|
||||
payload: {
|
||||
code: "10%OFF",
|
||||
rule: {
|
||||
description: "Test",
|
||||
value: 10,
|
||||
allocation: "total",
|
||||
},
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -36,6 +36,9 @@ import { MedusaError, Validator } from "medusa-core-utils"
|
||||
* type: array
|
||||
* items:
|
||||
* type: string
|
||||
* usage_limit:
|
||||
* type: number
|
||||
* description: Maximum times the discount can be used
|
||||
* metadata:
|
||||
* description: An optional set of key-value pairs to hold additional information.
|
||||
* type: object
|
||||
@@ -64,14 +67,14 @@ export default async (req, res) => {
|
||||
.required(),
|
||||
allocation: Validator.string().required(),
|
||||
valid_for: Validator.array().items(Validator.string()),
|
||||
usage_limit: Validator.number()
|
||||
.positive()
|
||||
.optional(),
|
||||
})
|
||||
.required(),
|
||||
is_disabled: Validator.boolean().default(false),
|
||||
starts_at: Validator.date().optional(),
|
||||
ends_at: Validator.date().optional(),
|
||||
usage_limit: Validator.number()
|
||||
.positive()
|
||||
.optional(),
|
||||
regions: Validator.array()
|
||||
.items(Validator.string())
|
||||
.optional(),
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -64,14 +64,14 @@ export default async (req, res) => {
|
||||
value: Validator.number().required(),
|
||||
allocation: Validator.string().required(),
|
||||
valid_for: Validator.array().items(Validator.string()),
|
||||
usage_limit: Validator.number()
|
||||
.positive()
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
is_disabled: Validator.boolean().optional(),
|
||||
starts_at: Validator.date().optional(),
|
||||
ends_at: Validator.date().optional(),
|
||||
usage_limit: Validator.number()
|
||||
.positive()
|
||||
.optional(),
|
||||
regions: Validator.array()
|
||||
.items(Validator.string())
|
||||
.optional(),
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
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'`
|
||||
)
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "discount_rule" ALTER COLUMN "description" DROP NOT NULL`
|
||||
)
|
||||
await queryRunner.query(
|
||||
`COMMENT ON COLUMN "discount_rule"."description" IS NULL`
|
||||
)
|
||||
}
|
||||
|
||||
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`
|
||||
)
|
||||
await queryRunner.query(
|
||||
`COMMENT ON COLUMN "discount_rule"."description" IS NULL`
|
||||
)
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "discount_rule" ALTER COLUMN "description" SET NOT NULL`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ export class DiscountRule {
|
||||
@PrimaryColumn()
|
||||
id: string
|
||||
|
||||
@Column()
|
||||
@Column({ nullable: true })
|
||||
description: string
|
||||
|
||||
@Column({
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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: discount.usage_limit,
|
||||
}
|
||||
|
||||
const created = await discountRepo.create(toCreate)
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user