chore(): Reorganize modules (#7210)

**What**
Move all modules to the modules directory
This commit is contained in:
Adrien de Peretti
2024-05-02 17:33:34 +02:00
committed by GitHub
parent 7a351eef09
commit 4eae25e1ef
870 changed files with 91 additions and 62 deletions

View File

@@ -0,0 +1,443 @@
import { Modules } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { createCampaigns } from "../../../__fixtures__/campaigns"
import { createPromotions } from "../../../__fixtures__/promotion"
jest.setTimeout(30000)
moduleIntegrationTestRunner({
moduleName: Modules.PROMOTION,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPromotionModuleService>) => {
describe("Promotion Module Service: Campaigns", () => {
describe("listAndCountCampaigns", () => {
beforeEach(async () => {
await createCampaigns(MikroOrmWrapper.forkManager())
})
it("should return all campaigns and its count", async () => {
const [campaigns, count] = await service.listAndCountCampaigns()
expect(count).toEqual(2)
expect(campaigns).toEqual([
{
id: "campaign-id-1",
name: "campaign 1",
description: "test description",
currency: "USD",
campaign_identifier: "test-1",
starts_at: expect.any(Date),
ends_at: expect.any(Date),
budget: expect.any(Object),
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
},
{
id: "campaign-id-2",
name: "campaign 1",
description: "test description",
currency: "USD",
campaign_identifier: "test-2",
starts_at: expect.any(Date),
ends_at: expect.any(Date),
budget: expect.any(Object),
created_at: expect.any(Date),
updated_at: expect.any(Date),
deleted_at: null,
},
])
})
it("should return all campaigns based on config select and relations param", async () => {
const [campaigns, count] = await service.listAndCountCampaigns(
{
id: ["campaign-id-1"],
},
{
relations: ["budget"],
select: ["name", "budget.limit"],
}
)
expect(count).toEqual(1)
expect(campaigns).toEqual([
{
id: "campaign-id-1",
name: "campaign 1",
budget: expect.objectContaining({
id: expect.any(String),
limit: 1000,
}),
},
])
})
})
describe("createCampaigns", () => {
it("should throw an error when required params are not passed", async () => {
const error = await service
.createCampaigns([
{
name: "test",
} as any,
])
.catch((e) => e)
expect(error.message).toContain(
"Value for Campaign.campaign_identifier is required, 'undefined' found"
)
})
it("should create a basic campaign successfully", async () => {
const startsAt = new Date("01/01/2024")
const endsAt = new Date("01/01/2025")
const [createdCampaign] = await service.createCampaigns([
{
name: "test",
campaign_identifier: "test",
starts_at: startsAt,
ends_at: endsAt,
},
])
const campaign = await service.retrieveCampaign(createdCampaign.id)
expect(campaign).toEqual(
expect.objectContaining({
name: "test",
campaign_identifier: "test",
starts_at: startsAt,
ends_at: endsAt,
})
)
})
it("should create a campaign with campaign budget successfully", async () => {
const startsAt = new Date("01/01/2024")
const endsAt = new Date("01/01/2025")
const [createdPromotion] = await service.createCampaigns([
{
name: "test",
campaign_identifier: "test",
starts_at: startsAt,
ends_at: endsAt,
budget: {
limit: 1000,
type: "usage",
used: 10,
},
},
])
const campaign = await service.retrieveCampaign(createdPromotion.id, {
relations: ["budget"],
})
expect(campaign).toEqual(
expect.objectContaining({
name: "test",
campaign_identifier: "test",
starts_at: startsAt,
ends_at: endsAt,
budget: expect.objectContaining({
limit: 1000,
type: "usage",
used: 10,
}),
})
)
})
it("should create a basic campaign with promotions successfully", async () => {
await createPromotions(MikroOrmWrapper.forkManager())
const startsAt = new Date("01/01/2024")
const endsAt = new Date("01/01/2025")
const [createdCampaign] = await service.createCampaigns([
{
name: "test",
campaign_identifier: "test",
starts_at: startsAt,
ends_at: endsAt,
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
},
])
const campaign = await service.retrieveCampaign(createdCampaign.id, {
relations: ["promotions"],
})
expect(campaign).toEqual(
expect.objectContaining({
name: "test",
campaign_identifier: "test",
starts_at: startsAt,
ends_at: endsAt,
promotions: [
expect.objectContaining({
id: "promotion-id-1",
}),
expect.objectContaining({
id: "promotion-id-2",
}),
],
})
)
})
})
describe("updateCampaigns", () => {
it("should throw an error when required params are not passed", async () => {
const error = await service
.updateCampaigns([
{
name: "test",
} as any,
])
.catch((e) => e)
expect(error.message).toContain('Campaign with id "" not found')
})
it("should update the attributes of a campaign successfully", async () => {
await createCampaigns(MikroOrmWrapper.forkManager())
const [updatedCampaign] = await service.updateCampaigns([
{
id: "campaign-id-1",
description: "test description 1",
currency: "EUR",
campaign_identifier: "new",
starts_at: new Date("01/01/2024"),
ends_at: new Date("01/01/2025"),
},
])
expect(updatedCampaign).toEqual(
expect.objectContaining({
description: "test description 1",
currency: "EUR",
campaign_identifier: "new",
starts_at: new Date("01/01/2024"),
ends_at: new Date("01/01/2025"),
})
)
})
it("should update the attributes of a campaign budget successfully", async () => {
await createCampaigns(MikroOrmWrapper.forkManager())
const [updatedCampaign] = await service.updateCampaigns([
{
id: "campaign-id-1",
budget: {
limit: 100,
used: 100,
},
},
])
expect(updatedCampaign).toEqual(
expect.objectContaining({
budget: expect.objectContaining({
limit: 100,
used: 100,
}),
})
)
})
it("should update promotions of a campaign successfully", async () => {
await createCampaigns(MikroOrmWrapper.forkManager())
await createPromotions(MikroOrmWrapper.forkManager())
const [updatedCampaign] = await service.updateCampaigns([
{
id: "campaign-id-1",
description: "test description 1",
currency: "EUR",
campaign_identifier: "new",
starts_at: new Date("01/01/2024"),
ends_at: new Date("01/01/2025"),
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
},
])
expect(updatedCampaign).toEqual(
expect.objectContaining({
description: "test description 1",
currency: "EUR",
campaign_identifier: "new",
starts_at: new Date("01/01/2024"),
ends_at: new Date("01/01/2025"),
promotions: [
expect.objectContaining({
id: "promotion-id-1",
}),
expect.objectContaining({
id: "promotion-id-2",
}),
],
})
)
})
it("should remove promotions of the campaign successfully", async () => {
await createCampaigns(MikroOrmWrapper.forkManager())
await createPromotions(MikroOrmWrapper.forkManager())
await service.updateCampaigns({
id: "campaign-id-1",
promotions: [{ id: "promotion-id-1" }, { id: "promotion-id-2" }],
})
const updatedCampaign = await service.updateCampaigns({
id: "campaign-id-1",
promotions: [{ id: "promotion-id-1" }],
})
expect(updatedCampaign).toEqual(
expect.objectContaining({
promotions: [
expect.objectContaining({
id: "promotion-id-1",
}),
],
})
)
})
})
describe("retrieveCampaign", () => {
beforeEach(async () => {
await createCampaigns(MikroOrmWrapper.forkManager())
})
const id = "campaign-id-1"
it("should return campaign for the given id", async () => {
const campaign = await service.retrieveCampaign(id)
expect(campaign).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when campaign with id does not exist", async () => {
let error
try {
await service.retrieveCampaign("does-not-exist")
} catch (e) {
error = e
}
expect(error.message).toEqual(
"Campaign 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.retrieveCampaign(undefined as unknown as string)
} catch (e) {
error = e
}
expect(error.message).toEqual("campaign - id must be defined")
})
it("should return campaign based on config select param", async () => {
const campaign = await service.retrieveCampaign(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(campaign))
expect(serialized).toEqual({
id,
})
})
})
describe("deleteCampaigns", () => {
it("should delete the campaigns given an id successfully", async () => {
const [createdCampaign] = await service.createCampaigns([
{
name: "test",
campaign_identifier: "test",
starts_at: new Date("01/01/2024"),
ends_at: new Date("01/01/2025"),
},
])
await service.deleteCampaigns([createdCampaign.id])
const campaigns = await service.listCampaigns(
{
id: [createdCampaign.id],
},
{ withDeleted: true }
)
expect(campaigns).toHaveLength(0)
})
})
describe("softDeleteCampaigns", () => {
it("should soft delete the campaigns given an id successfully", async () => {
const [createdCampaign] = await service.createCampaigns([
{
name: "test",
campaign_identifier: "test",
starts_at: new Date("01/01/2024"),
ends_at: new Date("01/01/2025"),
},
])
await service.softDeleteCampaigns([createdCampaign.id])
const campaigns = await service.listCampaigns({
id: [createdCampaign.id],
})
expect(campaigns).toHaveLength(0)
})
})
describe("restoreCampaigns", () => {
it("should restore the campaigns given an id successfully", async () => {
const [createdCampaign] = await service.createCampaigns([
{
name: "test",
campaign_identifier: "test",
starts_at: new Date("01/01/2024"),
ends_at: new Date("01/01/2025"),
},
])
await service.softDeleteCampaigns([createdCampaign.id])
let campaigns = await service.listCampaigns({
id: [createdCampaign.id],
})
expect(campaigns).toHaveLength(0)
await service.restoreCampaigns([createdCampaign.id])
campaigns = await service.listCampaigns({ id: [createdCampaign.id] })
expect(campaigns).toHaveLength(1)
})
})
})
},
})

View File

@@ -0,0 +1,189 @@
import { Modules } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { createCampaigns } from "../../../__fixtures__/campaigns"
jest.setTimeout(30000)
moduleIntegrationTestRunner({
moduleName: Modules.PROMOTION,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPromotionModuleService>) => {
describe("Promotion Service: campaign usage", () => {
beforeEach(async () => {
await createCampaigns(MikroOrmWrapper.forkManager())
})
describe("registerUsage", () => {
it("should register usage for type spend", async () => {
const createdPromotion = await service.create({
code: "TEST_PROMO_SPEND",
type: "standard",
campaign_id: "campaign-id-1",
})
await service.registerUsage([
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_express",
amount: 200,
code: createdPromotion.code!,
},
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_standard",
amount: 500,
code: createdPromotion.code!,
},
])
const campaign = await service.retrieveCampaign("campaign-id-1", {
relations: ["budget"],
})
expect(campaign.budget).toEqual(
expect.objectContaining({
type: "spend",
limit: 1000,
used: 700,
})
)
})
it("should register usage for type usage", async () => {
const createdPromotion = await service.create({
code: "TEST_PROMO_USAGE",
type: "standard",
campaign_id: "campaign-id-2",
})
await service.registerUsage([
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_express",
amount: 200,
code: createdPromotion.code!,
},
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_standard",
amount: 500,
code: createdPromotion.code!,
},
])
const campaign = await service.retrieveCampaign("campaign-id-2", {
relations: ["budget"],
})
expect(campaign.budget).toEqual(
expect.objectContaining({
type: "usage",
limit: 1000,
used: 1,
})
)
})
it("should not throw an error when compute action with code does not exist", async () => {
const response = await service
.registerUsage([
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_express",
amount: 200,
code: "DOESNOTEXIST",
},
])
.catch((e) => e)
expect(response).toEqual(undefined)
})
it("should not register usage when limit is exceed for type usage", async () => {
const createdPromotion = await service.create({
code: "TEST_PROMO_USAGE",
type: "standard",
campaign_id: "campaign-id-2",
})
await service.updateCampaigns({
id: "campaign-id-2",
budget: { used: 1000, limit: 1000 },
})
await service.registerUsage([
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_express",
amount: 200,
code: createdPromotion.code!,
},
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_standard",
amount: 500,
code: createdPromotion.code!,
},
])
const campaign = await service.retrieveCampaign("campaign-id-2", {
relations: ["budget"],
})
expect(campaign).toEqual(
expect.objectContaining({
budget: expect.objectContaining({
limit: 1000,
used: 1000,
}),
})
)
})
it("should not register usage above limit when exceeded for type spend", async () => {
const createdPromotion = await service.create({
code: "TEST_PROMO_SPEND",
type: "standard",
campaign_id: "campaign-id-1",
})
await service.updateCampaigns({
id: "campaign-id-1",
budget: { used: 900, limit: 1000 },
})
await service.registerUsage([
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_express",
amount: 100,
code: createdPromotion.code!,
},
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_standard",
amount: 100,
code: createdPromotion.code!,
},
])
const campaign = await service.retrieveCampaign("campaign-id-1", {
relations: ["budget"],
})
expect(campaign).toEqual(
expect.objectContaining({
budget: expect.objectContaining({
limit: 1000,
used: 1000,
}),
})
)
})
})
})
},
})

View File

@@ -0,0 +1,58 @@
import { PromotionType } from "@medusajs/utils"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { createPromotions } from "../../../__fixtures__/promotion"
import { IPromotionModuleService } from "@medusajs/types"
import { Modules } from "@medusajs/modules-sdk"
jest.setTimeout(30000)
moduleIntegrationTestRunner({
moduleName: Modules.PROMOTION,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IPromotionModuleService>) => {
describe("Promotion Service", () => {
beforeEach(async () => {
await createPromotions(MikroOrmWrapper.forkManager())
})
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",
})
)
})
})
})
},
})