feat(medusa,types): added buyget support for modules (#6159)

This commit is contained in:
Riqwan Thamir
2024-01-23 21:01:44 +01:00
committed by GitHub
parent 302323916b
commit 68d8daccd2
22 changed files with 1058 additions and 109 deletions
@@ -62,7 +62,7 @@ describe("POST /admin/promotions", () => {
)
})
it("should create a promotion successfully", async () => {
it("should create a standard promotion successfully", async () => {
const api = useApi() as any
const response = await api.post(
`/admin/promotions`,
@@ -148,4 +148,194 @@ describe("POST /admin/promotions", () => {
})
)
})
it("should throw an error if buy_rules params are not passed", async () => {
const api = useApi() as any
const { response } = await api
.post(
`/admin/promotions`,
{
code: "TEST",
type: PromotionType.BUYGET,
is_automatic: true,
application_method: {
target_type: "items",
type: "fixed",
allocation: "each",
value: "100",
max_quantity: 100,
target_rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data.message).toEqual(
"Buy rules are required for buyget promotion type"
)
})
it("should throw an error if buy_rules params are not passed", async () => {
const api = useApi() as any
const { response } = await api
.post(
`/admin/promotions`,
{
code: "TEST",
type: PromotionType.BUYGET,
is_automatic: true,
application_method: {
target_type: "items",
type: "fixed",
allocation: "each",
value: "100",
max_quantity: 100,
buy_rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data.message).toEqual(
"Target rules are required for buyget promotion type"
)
})
it("should create a buyget promotion successfully", async () => {
const api = useApi() as any
const response = await api.post(
`/admin/promotions`,
{
code: "TEST",
type: PromotionType.BUYGET,
is_automatic: true,
campaign: {
name: "test",
campaign_identifier: "test-1",
budget: {
type: "usage",
limit: 100,
},
},
application_method: {
target_type: "items",
type: "fixed",
allocation: "each",
value: "100",
max_quantity: 100,
apply_to_quantity: 1,
buy_rules_min_quantity: 1,
target_rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
buy_rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
rules: [
{
attribute: "test.test",
operator: "eq",
values: ["test1", "test2"],
},
],
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual(
expect.objectContaining({
id: expect.any(String),
code: "TEST",
type: "buyget",
is_automatic: true,
campaign: expect.objectContaining({
name: "test",
campaign_identifier: "test-1",
budget: expect.objectContaining({
type: "usage",
limit: 100,
}),
}),
application_method: expect.objectContaining({
value: 100,
max_quantity: 100,
type: "fixed",
target_type: "items",
allocation: "each",
apply_to_quantity: 1,
buy_rules_min_quantity: 1,
target_rules: [
expect.objectContaining({
operator: "eq",
attribute: "test.test",
values: expect.arrayContaining([
expect.objectContaining({ value: "test1" }),
expect.objectContaining({ value: "test2" }),
]),
}),
],
buy_rules: [
expect.objectContaining({
operator: "eq",
attribute: "test.test",
values: expect.arrayContaining([
expect.objectContaining({ value: "test1" }),
expect.objectContaining({ value: "test2" }),
]),
}),
],
}),
rules: [
expect.objectContaining({
operator: "eq",
attribute: "test.test",
values: expect.arrayContaining([
expect.objectContaining({ value: "test1" }),
expect.objectContaining({ value: "test2" }),
]),
}),
],
})
)
})
})
@@ -74,28 +74,30 @@ describe("GET /admin/promotions", () => {
)
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual({
id: expect.any(String),
code: "TEST",
campaign: null,
is_automatic: false,
type: "standard",
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
application_method: {
expect(response.data.promotion).toEqual(
expect.objectContaining({
id: expect.any(String),
promotion: expect.any(Object),
value: 100,
type: "fixed",
target_type: "order",
max_quantity: 0,
allocation: null,
code: "TEST",
campaign: null,
is_automatic: false,
type: "standard",
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
},
})
application_method: expect.objectContaining({
id: expect.any(String),
promotion: expect.any(Object),
value: 100,
type: "fixed",
target_type: "order",
max_quantity: 0,
allocation: null,
created_at: expect.any(String),
updated_at: expect.any(String),
deleted_at: null,
}),
})
)
})
it("should get the requested promotion with filtered fields and relations", async () => {
@@ -132,4 +132,58 @@ describe("POST /admin/promotions/:id", () => {
})
)
})
it("should update a buyget promotion successfully", async () => {
const createdPromotion = await promotionModuleService.create({
code: "PROMOTION_TEST",
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
value: "100",
apply_to_quantity: 1,
buy_rules_min_quantity: 1,
buy_rules: [
{
attribute: "product_collection.id",
operator: "eq",
values: ["pcol_towel"],
},
],
target_rules: [
{
attribute: "product.id",
operator: "eq",
values: "prod_mat",
},
],
},
})
const api = useApi() as any
const response = await api.post(
`/admin/promotions/${createdPromotion.id}`,
{
code: "TEST_TWO",
application_method: {
value: "200",
buy_rules_min_quantity: 6,
},
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.promotion).toEqual(
expect.objectContaining({
id: expect.any(String),
code: "TEST_TWO",
application_method: expect.objectContaining({
value: 200,
buy_rules_min_quantity: 6,
}),
})
)
})
})