fix(medusa): Add usage_count + usage_limit to discount

This commit is contained in:
olivermrbl
2021-03-17 08:42:14 +01:00
parent 90787be914
commit c513813bb6
14 changed files with 1859 additions and 1177 deletions

View File

@@ -106,6 +106,8 @@ describe("/store/carts", () => {
afterEach(async () => {
const manager = dbConnection.manager;
await manager.query(`DELETE FROM "discount"`);
await manager.query(`DELETE FROM "discount_rule"`);
await manager.query(`DELETE FROM "cart"`);
await manager.query(`DELETE FROM "customer"`);
await manager.query(
@@ -114,6 +116,21 @@ describe("/store/carts", () => {
await manager.query(`DELETE FROM "region"`);
});
it("fails on apply discount if limit has been reached", async () => {
const api = useApi();
try {
await api.post("/store/carts/test-cart", {
discounts: [{ code: "CREATED" }],
});
} catch (error) {
expect(error.response.status).toEqual(400);
expect(error.response.data.message).toEqual(
"Discount has been used maximum allowed times"
);
}
});
it("updates cart customer id", async () => {
const api = useApi();

View File

@@ -1,15 +1,43 @@
const { Customer, Region, Cart } = require("@medusajs/medusa");
const {
Customer,
Region,
Cart,
DiscountRule,
Discount,
} = require("@medusajs/medusa");
module.exports = async (connection, data = {}) => {
const manager = connection.manager;
await manager.insert(Region, {
const r = manager.create(Region, {
id: "test-region",
name: "Test Region",
currency_code: "usd",
tax_rate: 0,
});
const d = await manager.create(Discount, {
id: "test-discount",
code: "CREATED",
is_dynamic: false,
is_disabled: false,
});
const dr = await manager.create(DiscountRule, {
id: "test-discount-rule",
description: "Created",
type: "fixed",
value: 10000,
allocation: "total",
usage_limit: 2,
usage_count: 2,
});
d.rule = dr;
d.regions = [r];
await manager.save(d);
await manager.query(
`UPDATE "country" SET region_id='test-region' WHERE iso_2 = 'us'`
);

File diff suppressed because it is too large Load Diff