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
@@ -428,6 +428,199 @@ describe("Promotion Service", () => {
"rules[].operator (doesnotexist) is invalid. It should be one of gte, lte, gt, lt, eq, ne, in"
)
})
it("should create a basic buyget promotion successfully", async () => {
const createdPromotion = await service
.create({
code: "PROMOTION_TEST",
type: PromotionType.BUYGET,
})
.catch((e) => e)
const [promotion] = await service.list({
id: [createdPromotion.id],
})
expect(promotion).toEqual(
expect.objectContaining({
code: "PROMOTION_TEST",
is_automatic: false,
type: PromotionType.BUYGET,
})
)
})
it("should throw an error when target_rules are not present for buyget promotion", async () => {
const error = await service
.create({
code: "PROMOTION_TEST",
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
value: "100",
buy_rules: [
{
attribute: "product_collection",
operator: "eq",
values: ["pcol_towel"],
},
],
},
})
.catch((e) => e)
expect(error.message).toContain(
"Target rules are required for buyget promotion type"
)
})
it("should throw an error when buy_rules are not present for buyget promotion", async () => {
const error = await service
.create({
code: "PROMOTION_TEST",
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
value: "100",
},
})
.catch((e) => e)
expect(error.message).toContain(
"Buy rules are required for buyget promotion type"
)
})
it("should throw an error when apply_to_quantity is not present for buyget promotion", async () => {
const error = await service
.create({
code: "PROMOTION_TEST",
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
value: "100",
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"],
},
],
},
})
.catch((e) => e)
expect(error.message).toContain(
"apply_to_quantity is a required field for Promotion type of buyget"
)
})
it("should throw an error when buy_rules_min_quantity is not present for buyget promotion", async () => {
const error = await service
.create({
code: "PROMOTION_TEST",
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
target_type: "items",
allocation: "across",
value: "100",
apply_to_quantity: 1,
buy_rules: [
{
attribute: "product_collection.id",
operator: "eq",
values: ["pcol_towel"],
},
],
target_rules: [
{
attribute: "product.id",
operator: "eq",
values: ["prod_mat"],
},
],
},
})
.catch((e) => e)
expect(error.message).toContain(
"buy_rules_min_quantity is a required field for Promotion type of buyget"
)
})
it("should create a buyget promotion with rules successfully", async () => {
const createdPromotion = await service.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",
},
],
},
})
expect(createdPromotion).toEqual(
expect.objectContaining({
code: "PROMOTION_TEST",
is_automatic: false,
type: PromotionType.BUYGET,
application_method: expect.objectContaining({
type: "fixed",
target_type: "items",
allocation: "across",
value: 100,
apply_to_quantity: 1,
buy_rules_min_quantity: 1,
target_rules: [
expect.objectContaining({
attribute: "product.id",
operator: "eq",
values: [expect.objectContaining({ value: "prod_mat" })],
}),
],
buy_rules: [
expect.objectContaining({
attribute: "product_collection.id",
operator: "eq",
values: [expect.objectContaining({ value: "pcol_towel" })],
}),
],
}),
})
)
})
})
describe("update", () => {
@@ -966,6 +1159,103 @@ describe("Promotion Service", () => {
})
})
describe("addPromotionBuyRules", () => {
let promotion
beforeEach(async () => {
;[promotion] = await service.create([
{
code: "TEST",
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
target_type: "items",
allocation: "each",
value: "100",
max_quantity: 500,
apply_to_quantity: 1,
buy_rules_min_quantity: 1,
target_rules: [
{
attribute: "product.id",
operator: "in",
values: ["prod_1", "prod_2"],
},
],
buy_rules: [
{
attribute: "product_collection.id",
operator: "eq",
values: ["pcol_towel"],
},
],
},
},
])
})
it("should throw an error when promotion with id does not exist", async () => {
let error
try {
await service.addPromotionBuyRules("does-not-exist", [])
} catch (e) {
error = e
}
expect(error.message).toEqual(
"Promotion with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.addPromotionBuyRules(undefined as unknown as string, [])
} catch (e) {
error = e
}
expect(error.message).toEqual('"promotionId" must be defined')
})
it("should successfully create buy rules for a buyget promotion", async () => {
promotion = await service.addPromotionBuyRules(promotion.id, [
{
attribute: "product.id",
operator: "in",
values: ["prod_3", "prod_4"],
},
])
expect(promotion).toEqual(
expect.objectContaining({
id: promotion.id,
application_method: expect.objectContaining({
buy_rules: expect.arrayContaining([
expect.objectContaining({
attribute: "product_collection.id",
operator: "eq",
values: expect.arrayContaining([
expect.objectContaining({ value: "pcol_towel" }),
]),
}),
expect.objectContaining({
attribute: "product.id",
operator: "in",
values: expect.arrayContaining([
expect.objectContaining({ value: "prod_3" }),
expect.objectContaining({ value: "prod_4" }),
]),
}),
]),
}),
})
)
})
})
describe("removePromotionRules", () => {
let promotion
@@ -1108,4 +1398,88 @@ describe("Promotion Service", () => {
)
})
})
describe("removePromotionBuyRules", () => {
let promotion
beforeEach(async () => {
;[promotion] = await service.create([
{
code: "TEST",
type: PromotionType.BUYGET,
application_method: {
type: "fixed",
target_type: "items",
allocation: "each",
value: "100",
max_quantity: 500,
apply_to_quantity: 1,
buy_rules_min_quantity: 1,
target_rules: [
{
attribute: "product.id",
operator: "in",
values: ["prod_1", "prod_2"],
},
],
buy_rules: [
{
attribute: "product_collection",
operator: "eq",
values: ["pcol_towel"],
},
],
},
},
])
})
it("should throw an error when promotion with id does not exist", async () => {
let error
try {
await service.removePromotionBuyRules("does-not-exist", [])
} catch (e) {
error = e
}
expect(error.message).toEqual(
"Promotion with id: does-not-exist was not found"
)
})
it("should throw an error when a id is not provided", async () => {
let error
try {
await service.removePromotionBuyRules(
undefined as unknown as string,
[]
)
} catch (e) {
error = e
}
expect(error.message).toEqual('"promotionId" must be defined')
})
it("should successfully remove rules for a promotion", async () => {
const [ruleId] = promotion.application_method.buy_rules.map(
(rule) => rule.id
)
promotion = await service.removePromotionBuyRules(promotion.id, [
{ id: ruleId },
])
expect(promotion).toEqual(
expect.objectContaining({
id: promotion.id,
application_method: expect.objectContaining({
buy_rules: [],
}),
})
)
})
})
})