feat(medusa): added endpoints for rule attribute/operator/values options (#6911)

what:

adds endpoints that returns attribute options, operator options and value options for a particular rule.
This commit is contained in:
Riqwan Thamir
2024-04-04 11:56:17 +02:00
committed by GitHub
parent 9ca38eba04
commit 483bf98a49
15 changed files with 642 additions and 16 deletions

View File

@@ -1,5 +1,11 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import {
ICustomerModuleService,
IProductModuleService,
IPromotionModuleService,
IRegionModuleService,
ISalesChannelModuleService,
} from "@medusajs/types"
import { PromotionType } from "@medusajs/utils"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
import { createAdminUser } from "../../../../helpers/create-admin-user"
@@ -16,6 +22,11 @@ medusaIntegrationTestRunner({
let appContainer
let standardPromotion
let promotionModule: IPromotionModuleService
let regionService: IRegionModuleService
let productService: IProductModuleService
let customerService: ICustomerModuleService
let salesChannelService: ISalesChannelModuleService
const promotionRule = {
operator: "eq",
attribute: "old_attr",
@@ -25,6 +36,12 @@ medusaIntegrationTestRunner({
beforeAll(async () => {
appContainer = getContainer()
promotionModule = appContainer.resolve(ModuleRegistrationName.PROMOTION)
regionService = appContainer.resolve(ModuleRegistrationName.REGION)
productService = appContainer.resolve(ModuleRegistrationName.PRODUCT)
customerService = appContainer.resolve(ModuleRegistrationName.CUSTOMER)
salesChannelService = appContainer.resolve(
ModuleRegistrationName.SALES_CHANNEL
)
})
beforeEach(async () => {
@@ -636,6 +653,302 @@ medusaIntegrationTestRunner({
)
})
})
describe("GET /admin/promotions/rule-attribute-options/:ruleType", () => {
it("should throw error when ruleType is invalid", async () => {
const { response } = await api
.get(
`/admin/promotions/rule-attribute-options/does-not-exist`,
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message: "Invalid param rule_type (does-not-exist)",
})
})
it("return all rule attributes for a valid ruleType", async () => {
const response = await api.get(
`/admin/promotions/rule-attribute-options/rules`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.attributes).toEqual([
{
id: "currency",
label: "Currency code",
required: true,
value: "currency_code",
},
{
id: "customer_group",
label: "Customer Group",
required: false,
value: "customer_group.id",
},
{
id: "region",
label: "Region",
required: false,
value: "region.id",
},
{
id: "country",
label: "Country",
required: false,
value: "shipping_address.country_code",
},
{
id: "sales_channel",
label: "Sales Channel",
required: false,
value: "sales_channel.id",
},
])
})
})
describe("GET /admin/promotions/rule-operator-options", () => {
it("return all rule operators", async () => {
const response = await api.get(
`/admin/promotions/rule-operator-options`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.operators).toEqual([
{
id: "in",
label: "In",
value: "in",
},
{
id: "eq",
label: "Equals",
value: "eq",
},
{
id: "ne",
label: "Not In",
value: "ne",
},
])
})
})
describe("GET /admin/promotions/rule-value-options/:ruleType/:ruleAttributeId", () => {
it("should throw error when ruleType is invalid", async () => {
const { response } = await api
.get(
`/admin/promotions/rule-value-options/does-not-exist/region`,
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message: "Invalid param rule_type (does-not-exist)",
})
})
it("should throw error when ruleAttributeId is invalid", async () => {
const { response } = await api
.get(
`/admin/promotions/rule-value-options/rules/does-not-exist`,
adminHeaders
)
.catch((e) => e)
expect(response.status).toEqual(400)
expect(response.data).toEqual({
type: "invalid_data",
message: "Invalid rule attribute - does-not-exist",
})
})
it("should return all values based on rule types", async () => {
const [region1, region2] = await regionService.create([
{ name: "North America", currency_code: "usd" },
{ name: "Europe", currency_code: "eur" },
])
let response = await api.get(
`/admin/promotions/rule-value-options/rules/region`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values.length).toEqual(2)
expect(response.data.values).toEqual(
expect.arrayContaining([
{
label: "North America",
value: region1.id,
},
{
label: "Europe",
value: region2.id,
},
])
)
response = await api.get(
`/admin/promotions/rule-value-options/rules/currency?limit=2`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values.length).toEqual(2)
expect(response.data.values).toEqual(
expect.arrayContaining([
{
label: "United Arab Emirates Dirham",
value: "aed",
},
{
label: "Afghan Afghani",
value: "afn",
},
])
)
const group = await customerService.createCustomerGroup({
name: "VIP",
})
response = await api.get(
`/admin/promotions/rule-value-options/rules/customer_group`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values).toEqual([
{
label: "VIP",
value: group.id,
},
])
const salesChannel = await salesChannelService.create({
name: "Instagram",
})
response = await api.get(
`/admin/promotions/rule-value-options/rules/sales_channel`,
adminHeaders
)
expect(response.status).toEqual(200)
// TODO: This is returning a default sales channel, but very flakily
// Figure out why this happens and fix
// expect(response.data.values.length).toEqual(1)
expect(response.data.values).toEqual(
expect.arrayContaining([
{ label: "Instagram", value: salesChannel.id },
])
)
response = await api.get(
`/admin/promotions/rule-value-options/rules/country?limit=2`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values.length).toEqual(2)
expect(response.data.values).toEqual(
expect.arrayContaining([
{ label: "Andorra", value: "ad" },
{ label: "United Arab Emirates", value: "ae" },
])
)
const [product1, product2] = await productService.create([
{ title: "test product 1" },
{ title: "test product 2" },
])
response = await api.get(
`/admin/promotions/rule-value-options/target-rules/product`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values.length).toEqual(2)
expect(response.data.values).toEqual(
expect.arrayContaining([
{ label: "test product 1", value: product1.id },
{ label: "test product 2", value: product2.id },
])
)
const category = await productService.createCategory({
name: "test category 1",
parent_category_id: null,
})
response = await api.get(
`/admin/promotions/rule-value-options/target-rules/product_category`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values).toEqual([
{ label: "test category 1", value: category.id },
])
const collection = await productService.createCollections({
title: "test collection 1",
})
response = await api.get(
`/admin/promotions/rule-value-options/target-rules/product_collection`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values).toEqual([
{ label: "test collection 1", value: collection.id },
])
const type = await productService.createTypes({
value: "test type",
})
response = await api.get(
`/admin/promotions/rule-value-options/target-rules/product_type`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values).toEqual([
{ label: "test type", value: type.id },
])
const [tag1, tag2] = await productService.createTags([
{ value: "test tag 1" },
{ value: "test tag 2" },
])
response = await api.get(
`/admin/promotions/rule-value-options/target-rules/product_tag`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.values.length).toEqual(2)
expect(response.data.values).toEqual(
expect.arrayContaining([
{ label: "test tag 1", value: tag1.id },
{ label: "test tag 2", value: tag2.id },
])
)
})
})
})
},
})