feat(medusa): Add endpoint for retrieving a DiscountCondition (#1525)

This commit is contained in:
Oliver Windall Juhl
2022-05-17 11:17:17 +02:00
committed by GitHub
parent b02f2652be
commit a87e1cdf65
10 changed files with 450 additions and 42 deletions

View File

@@ -1,5 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/admin/discounts GET /admin/discounts/:id/conditions/:condition_id should get condition 1`] = `
Object {
"created_at": Any<String>,
"discount_rule": Object {
"allocation": "total",
"created_at": Any<String>,
"deleted_at": null,
"description": null,
"id": Any<String>,
"metadata": null,
"type": "percentage",
"updated_at": Any<String>,
"value": 10,
},
"discount_rule_id": Any<String>,
"id": "test-condition",
"operator": "in",
"type": "products",
"updated_at": Any<String>,
}
`;
exports[`/admin/discounts GET /admin/discounts/:id/conditions/:condition_id should get condition with expand + fields 1`] = `
Object {
"id": "test-condition",
"products": Array [
Object {
"collection_id": null,
"created_at": Any<String>,
"deleted_at": null,
"description": null,
"discountable": true,
"external_id": null,
"handle": null,
"height": null,
"hs_code": null,
"id": "test-product",
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"origin_country": null,
"profile_id": Any<String>,
"status": "draft",
"subtitle": null,
"thumbnail": null,
"title": "Practical Frozen Fish",
"type_id": Any<String>,
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
"type": "products",
}
`;
exports[`/admin/discounts GET /admin/discounts/:id/conditions/:condition_id throws if condition does not belong to discount: DiscountCondition with id test-condition was not found for Discount test-discount-2 1`] = `"Request failed with status code 404"`;
exports[`/admin/discounts GET /admin/discounts/:id/conditions/:condition_id throws if condition does not exist: DiscountCondition with id test-condition was not found 1`] = `"Request failed with status code 404"`;
exports[`/admin/discounts POST /admin/discounts fails if multiple types of resources are provided on update 1`] = `
Object {
"message": "Only one of products, product_types is allowed, Only one of product_types, products is allowed",

View File

@@ -431,6 +431,7 @@ describe("/admin/discounts", () => {
await adminSeeder(dbConnection)
await discountSeeder(dbConnection)
} catch (err) {
console.log(err)
throw err
}
})
@@ -1326,7 +1327,7 @@ describe("/admin/discounts", () => {
expect.assertions(2)
const api = useApi()
const response = await api
await api
.post(
"/admin/discounts",
{
@@ -2032,4 +2033,156 @@ describe("/admin/discounts", () => {
}
})
})
describe("GET /admin/discounts/:id/conditions/:condition_id", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
} catch (err) {
console.log(err)
}
const prod = await simpleProductFactory(dbConnection, {
type: "pants",
id: "test-product",
})
await simpleDiscountFactory(dbConnection, {
id: "test-discount",
code: "TEST",
rule: {
type: "percentage",
value: "10",
allocation: "total",
conditions: [
{
id: "test-condition",
type: "products",
operator: "in",
products: [prod.id],
},
],
},
})
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should get condition", async () => {
const api = useApi()
const discountCondition = await api
.get("/admin/discounts/test-discount/conditions/test-condition", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
const cond = discountCondition.data.discount_condition
expect(discountCondition.status).toEqual(200)
expect(cond).toMatchSnapshot({
id: "test-condition",
type: "products",
operator: "in",
created_at: expect.any(String),
updated_at: expect.any(String),
discount_rule_id: expect.any(String),
discount_rule: {
id: expect.any(String),
updated_at: expect.any(String),
created_at: expect.any(String),
},
})
})
it("should get condition with expand + fields", async () => {
const api = useApi()
const discountCondition = await api
.get(
"/admin/discounts/test-discount/conditions/test-condition?expand=products&fields=id,type",
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
const cond = discountCondition.data.discount_condition
console.log(cond.products)
expect(discountCondition.status).toEqual(200)
expect(cond).toMatchSnapshot({
id: "test-condition",
type: "products",
products: [
{
id: "test-product",
profile_id: expect.any(String),
type_id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
})
})
it("throws if condition does not exist", async () => {
const api = useApi()
const prod2 = await simpleProductFactory(dbConnection, { type: "pants" })
try {
await api.post(
"/admin/discounts/test-discount/conditions/does-not-exist",
{
products: [prod2.id],
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
} catch (error) {
expect(error.message).toMatchSnapshot(
"DiscountCondition with id test-condition was not found"
)
}
})
it("throws if condition does not belong to discount", async () => {
const api = useApi()
const prod2 = await simpleProductFactory(dbConnection, { type: "pants" })
try {
await api.post(
"/admin/discounts/test-discount-2/conditions/test-condition",
{
products: [prod2.id],
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
} catch (error) {
expect(error.message).toMatchSnapshot(
"DiscountCondition with id test-condition was not found for Discount test-discount-2"
)
}
})
})
})