feat(types,utils): add promotions create with application method (#5945)

What:

- Promotions can be created with its bare attributes
- Promotions one to one relationship with ApplicationMethod can be created with its attributes + validation

RESOLVES CORE-1592
RESOLVES CORE-1595
This commit is contained in:
Riqwan Thamir
2023-12-21 11:04:50 +00:00
committed by GitHub
parent c41f3002f3
commit 890e76a5c5
38 changed files with 1497 additions and 55 deletions
@@ -0,0 +1,132 @@
import { IPromotionModuleService } from "@medusajs/types"
import { PromotionType } from "@medusajs/utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { initialize } from "../../../../src"
import { DB_URL, MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000)
describe("Promotion Service", () => {
let service: IPromotionModuleService
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = MikroOrmWrapper.forkManager()
service = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PROMOTION_DB_SCHEMA,
},
})
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("create", () => {
it("should throw an error when required params are not passed", async () => {
const error = await service
.create([
{
type: PromotionType.STANDARD,
} as any,
])
.catch((e) => e)
expect(error.message).toContain(
"Value for Promotion.code is required, 'undefined' found"
)
})
it("should create a basic promotion successfully", async () => {
const [createdPromotion] = await service.create([
{
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
},
])
const [promotion] = await service.list({
id: [createdPromotion.id],
})
expect(promotion).toEqual(
expect.objectContaining({
code: "PROMOTION_TEST",
is_automatic: false,
type: "standard",
})
)
})
it("should create a promotion with order application method successfully", async () => {
const [createdPromotion] = await service.create([
{
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
application_method: {
type: "fixed",
target_type: "order",
value: 100,
},
},
])
const [promotion] = await service.list({
id: [createdPromotion.id],
})
expect(promotion).toEqual(
expect.objectContaining({
code: "PROMOTION_TEST",
is_automatic: false,
type: "standard",
})
)
})
it("should throw error when creating an item application method without allocation", async () => {
const error = await service
.create([
{
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
application_method: {
type: "fixed",
target_type: "item",
value: 100,
},
},
])
.catch((e) => e)
expect(error.message).toContain(
"application_method.allocation should be either 'across OR each' when application_method.target_type is either 'shipping OR item'"
)
})
it("should throw error when creating an item application, each allocation, without max quanity", async () => {
const error = await service
.create([
{
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
application_method: {
type: "fixed",
allocation: "each",
target_type: "shipping",
value: 100,
},
},
])
.catch((e) => e)
expect(error.message).toContain(
"application_method.max_quantity is required when application_method.allocation is 'each'"
)
})
})
})
@@ -0,0 +1,71 @@
import { PromotionType } from "@medusajs/utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PromotionRepository } from "@repositories"
import { PromotionService } from "@services"
import { createPromotions } from "../../../__fixtures__/promotion"
import { MikroOrmWrapper } from "../../../utils"
jest.setTimeout(30000)
describe("Promotion Service", () => {
let service: PromotionService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
const promotionRepository = new PromotionRepository({
manager: repositoryManager,
})
service = new PromotionService({
promotionRepository: promotionRepository,
})
await createPromotions(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("create", () => {
it("should throw an error when required params are not passed", async () => {
const error = await service
.create([
{
type: PromotionType.STANDARD,
} as any,
])
.catch((e) => e)
expect(error.message).toContain(
"Value for Promotion.code is required, 'undefined' found"
)
})
it("should create a promotion successfully", async () => {
await service.create([
{
code: "PROMOTION_TEST",
type: PromotionType.STANDARD,
},
])
const [promotion] = await service.list({
code: ["PROMOTION_TEST"],
})
expect(promotion).toEqual(
expect.objectContaining({
code: "PROMOTION_TEST",
is_automatic: false,
type: "standard",
})
)
})
})
})