feat(medusa,types,core-flows,utils): added delete endpoints for campaigns and promotions (#6152)
what: adds delete endpoints for: - campaigns (RESOLVES CORE-1686) - promotions (RESOLVES CORE-1680)
This commit is contained in:
1 parent
76291823f4
commit
99045848fd
25 files changed
+699
-84
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
"@medusajs/types": patch
|
||||||
|
"@medusajs/core-flows": patch
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa,types,core-flows,utils): added delete endpoints for campaigns and promotions
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
|
import path from "path"
|
||||||
|
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||||
|
import { useApi } from "../../../../environment-helpers/use-api"
|
||||||
|
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||||
|
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||||
|
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||||
|
|
||||||
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
|
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||||
|
const adminHeaders = {
|
||||||
|
headers: { "x-medusa-access-token": "test_token" },
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("DELETE /admin/campaigns/:id", () => {
|
||||||
|
let dbConnection
|
||||||
|
let appContainer
|
||||||
|
let shutdownServer
|
||||||
|
let promotionModuleService: IPromotionModuleService
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||||
|
dbConnection = await initDb({ cwd, env } as any)
|
||||||
|
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||||
|
appContainer = getContainer()
|
||||||
|
promotionModuleService = appContainer.resolve(
|
||||||
|
ModuleRegistrationName.PROMOTION
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.shutdown()
|
||||||
|
await shutdownServer()
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await adminSeeder(dbConnection)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.teardown()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should delete campaign successfully", async () => {
|
||||||
|
const [createdCampaign] = await promotionModuleService.createCampaigns([
|
||||||
|
{
|
||||||
|
name: "test",
|
||||||
|
campaign_identifier: "test",
|
||||||
|
starts_at: new Date("01/01/2024"),
|
||||||
|
ends_at: new Date("01/01/2025"),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
const api = useApi() as any
|
||||||
|
const deleteRes = await api.delete(
|
||||||
|
`/admin/campaigns/${createdCampaign.id}`,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(deleteRes.status).toEqual(200)
|
||||||
|
|
||||||
|
const campaigns = await promotionModuleService.listCampaigns({
|
||||||
|
id: [createdCampaign.id],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(campaigns.length).toEqual(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
|
import path from "path"
|
||||||
|
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||||
|
import { useApi } from "../../../../environment-helpers/use-api"
|
||||||
|
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||||
|
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||||
|
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||||
|
|
||||||
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
|
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||||
|
const adminHeaders = {
|
||||||
|
headers: { "x-medusa-access-token": "test_token" },
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("DELETE /admin/promotions/:id", () => {
|
||||||
|
let dbConnection
|
||||||
|
let appContainer
|
||||||
|
let shutdownServer
|
||||||
|
let promotionModuleService: IPromotionModuleService
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||||
|
dbConnection = await initDb({ cwd, env } as any)
|
||||||
|
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||||
|
appContainer = getContainer()
|
||||||
|
promotionModuleService = appContainer.resolve(
|
||||||
|
ModuleRegistrationName.PROMOTION
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.shutdown()
|
||||||
|
await shutdownServer()
|
||||||
|
})
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await adminSeeder(dbConnection)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const db = useDb()
|
||||||
|
await db.teardown()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should delete promotion successfully", async () => {
|
||||||
|
const createdPromotion = await promotionModuleService.create({
|
||||||
|
code: "TEST",
|
||||||
|
type: "standard",
|
||||||
|
application_method: {
|
||||||
|
type: "fixed",
|
||||||
|
target_type: "order",
|
||||||
|
value: "100",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const api = useApi() as any
|
||||||
|
const deleteRes = await api.delete(
|
||||||
|
`/admin/promotions/${createdPromotion.id}`,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(deleteRes.status).toEqual(200)
|
||||||
|
|
||||||
|
const promotions = await promotionModuleService.list({
|
||||||
|
id: [createdPromotion.id],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(promotions.length).toEqual(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -3,7 +3,6 @@ import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
|||||||
import { createPromotionsStep } from "../../handlers/promotion"
|
import { createPromotionsStep } from "../../handlers/promotion"
|
||||||
|
|
||||||
type WorkflowInput = { promotionsData: CreatePromotionDTO[] }
|
type WorkflowInput = { promotionsData: CreatePromotionDTO[] }
|
||||||
type WorkflowOutput = PromotionDTO[]
|
|
||||||
|
|
||||||
export const createPromotionsWorkflowId = "create-promotions"
|
export const createPromotionsWorkflowId = "create-promotions"
|
||||||
export const createPromotionsWorkflow = createWorkflow(
|
export const createPromotionsWorkflow = createWorkflow(
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||||
|
import { deleteCampaignsStep } from "../../handlers/promotion"
|
||||||
|
|
||||||
|
type WorkflowInput = { ids: string[] }
|
||||||
|
|
||||||
|
export const deleteCampaignsWorkflowId = "delete-campaigns"
|
||||||
|
export const deleteCampaignsWorkflow = createWorkflow(
|
||||||
|
deleteCampaignsWorkflowId,
|
||||||
|
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||||
|
return deleteCampaignsStep(input.ids)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||||
|
import { deletePromotionsStep } from "../../handlers/promotion"
|
||||||
|
|
||||||
|
type WorkflowInput = { ids: string[] }
|
||||||
|
|
||||||
|
export const deletePromotionsWorkflowId = "delete-promotions"
|
||||||
|
export const deletePromotionsWorkflow = createWorkflow(
|
||||||
|
deletePromotionsWorkflowId,
|
||||||
|
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||||
|
return deletePromotionsStep(input.ids)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
export * from "./create-campaigns"
|
export * from "./create-campaigns"
|
||||||
export * from "./create-promotions"
|
export * from "./create-promotions"
|
||||||
|
export * from "./delete-campaigns"
|
||||||
|
export * from "./delete-promotions"
|
||||||
export * from "./update-campaigns"
|
export * from "./update-campaigns"
|
||||||
export * from "./update-promotions"
|
export * from "./update-promotions"
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
|
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
|
export const deleteCampaignsStepId = "delete-campaigns"
|
||||||
|
export const deleteCampaignsStep = createStep(
|
||||||
|
deleteCampaignsStepId,
|
||||||
|
async (ids: string[], { container }) => {
|
||||||
|
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||||
|
ModuleRegistrationName.PROMOTION
|
||||||
|
)
|
||||||
|
|
||||||
|
await promotionModule.softDeleteCampaigns(ids)
|
||||||
|
|
||||||
|
return new StepResponse(void 0, ids)
|
||||||
|
},
|
||||||
|
async (idsToRestore, { container }) => {
|
||||||
|
if (!idsToRestore?.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||||
|
ModuleRegistrationName.PROMOTION
|
||||||
|
)
|
||||||
|
|
||||||
|
await promotionModule.restoreCampaigns(idsToRestore)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
|
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
|
export const deletePromotionsStepId = "delete-promotions"
|
||||||
|
export const deletePromotionsStep = createStep(
|
||||||
|
deletePromotionsStepId,
|
||||||
|
async (ids: string[], { container }) => {
|
||||||
|
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||||
|
ModuleRegistrationName.PROMOTION
|
||||||
|
)
|
||||||
|
|
||||||
|
await promotionModule.softDelete(ids)
|
||||||
|
|
||||||
|
return new StepResponse(void 0, ids)
|
||||||
|
},
|
||||||
|
async (idsToRestore, { container }) => {
|
||||||
|
if (!idsToRestore?.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const promotionModule = container.resolve<IPromotionModuleService>(
|
||||||
|
ModuleRegistrationName.PROMOTION
|
||||||
|
)
|
||||||
|
|
||||||
|
await promotionModule.restore(idsToRestore)
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
export * from "./create-campaigns"
|
export * from "./create-campaigns"
|
||||||
export * from "./create-promotions"
|
export * from "./create-promotions"
|
||||||
|
export * from "./delete-campaigns"
|
||||||
|
export * from "./delete-promotions"
|
||||||
export * from "./update-campaigns"
|
export * from "./update-campaigns"
|
||||||
export * from "./update-promotions"
|
export * from "./update-promotions"
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
import { updateCampaignsWorkflow } from "@medusajs/core-flows"
|
import {
|
||||||
|
deleteCampaignsWorkflow,
|
||||||
|
updateCampaignsWorkflow,
|
||||||
|
} from "@medusajs/core-flows"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { IPromotionModuleService } from "@medusajs/types"
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||||
@@ -39,3 +42,25 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|||||||
|
|
||||||
res.status(200).json({ campaign: result[0] })
|
res.status(200).json({ campaign: result[0] })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||||
|
const id = req.params.id
|
||||||
|
const manager = req.scope.resolve("manager")
|
||||||
|
const deleteCampaigns = deleteCampaignsWorkflow(req.scope)
|
||||||
|
|
||||||
|
const { errors } = await deleteCampaigns.run({
|
||||||
|
input: { ids: [id] },
|
||||||
|
context: { manager },
|
||||||
|
throwOnError: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (Array.isArray(errors) && errors[0]) {
|
||||||
|
throw errors[0].error
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
id,
|
||||||
|
object: "campaign",
|
||||||
|
deleted: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
import { updatePromotionsWorkflow } from "@medusajs/core-flows"
|
import {
|
||||||
|
deletePromotionsWorkflow,
|
||||||
|
updatePromotionsWorkflow,
|
||||||
|
} from "@medusajs/core-flows"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { IPromotionModuleService } from "@medusajs/types"
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||||
@@ -36,3 +39,25 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|||||||
|
|
||||||
res.status(200).json({ promotion: result[0] })
|
res.status(200).json({ promotion: result[0] })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||||
|
const id = req.params.id
|
||||||
|
const manager = req.scope.resolve("manager")
|
||||||
|
const deletePromotions = deletePromotionsWorkflow(req.scope)
|
||||||
|
|
||||||
|
const { errors } = await deletePromotions.run({
|
||||||
|
input: { ids: [id] },
|
||||||
|
context: { manager },
|
||||||
|
throwOnError: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (Array.isArray(errors) && errors[0]) {
|
||||||
|
throw errors[0].error
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
id,
|
||||||
|
object: "promotion",
|
||||||
|
deleted: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
+59
-9
@@ -385,20 +385,70 @@ describe("Promotion Module Service: Campaigns", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("deleteCampaigns", () => {
|
describe("deleteCampaigns", () => {
|
||||||
beforeEach(async () => {
|
|
||||||
await createCampaigns(repositoryManager)
|
|
||||||
})
|
|
||||||
|
|
||||||
const id = "campaign-id-1"
|
|
||||||
|
|
||||||
it("should delete the campaigns given an id successfully", async () => {
|
it("should delete the campaigns given an id successfully", async () => {
|
||||||
await service.deleteCampaigns([id])
|
const [createdCampaign] = await service.createCampaigns([
|
||||||
|
{
|
||||||
|
name: "test",
|
||||||
|
campaign_identifier: "test",
|
||||||
|
starts_at: new Date("01/01/2024"),
|
||||||
|
ends_at: new Date("01/01/2025"),
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
const campaigns = await service.list({
|
await service.deleteCampaigns([createdCampaign.id])
|
||||||
id: [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)
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
+44
-6
@@ -762,23 +762,61 @@ describe("Promotion Service", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("delete", () => {
|
describe("delete", () => {
|
||||||
beforeEach(async () => {
|
it("should soft delete the promotions given an id successfully", async () => {
|
||||||
await createPromotions(repositoryManager)
|
const createdPromotion = await service.create({
|
||||||
|
code: "TEST",
|
||||||
|
type: "standard",
|
||||||
|
})
|
||||||
|
|
||||||
|
await service.delete([createdPromotion.id])
|
||||||
|
|
||||||
|
const promotions = await service.list(
|
||||||
|
{
|
||||||
|
id: [createdPromotion.id],
|
||||||
|
},
|
||||||
|
{ withDeleted: true }
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(promotions).toHaveLength(0)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
const id = "promotion-id-1"
|
describe("softDelete", () => {
|
||||||
|
it("should soft delete the promotions given an id successfully", async () => {
|
||||||
|
const createdPromotion = await service.create({
|
||||||
|
code: "TEST",
|
||||||
|
type: "standard",
|
||||||
|
})
|
||||||
|
|
||||||
it("should delete the promotions given an id successfully", async () => {
|
await service.softDelete([createdPromotion.id])
|
||||||
await service.delete([id])
|
|
||||||
|
|
||||||
const promotions = await service.list({
|
const promotions = await service.list({
|
||||||
id: [id],
|
id: [createdPromotion.id],
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(promotions).toHaveLength(0)
|
expect(promotions).toHaveLength(0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("restore", () => {
|
||||||
|
it("should restore the promotions given an id successfully", async () => {
|
||||||
|
const createdPromotion = await service.create({
|
||||||
|
code: "TEST",
|
||||||
|
type: "standard",
|
||||||
|
})
|
||||||
|
|
||||||
|
await service.softDelete([createdPromotion.id])
|
||||||
|
|
||||||
|
let promotions = await service.list({ id: [createdPromotion.id] })
|
||||||
|
|
||||||
|
expect(promotions).toHaveLength(0)
|
||||||
|
await service.restore([createdPromotion.id])
|
||||||
|
|
||||||
|
promotions = await service.list({ id: [createdPromotion.id] })
|
||||||
|
expect(promotions).toHaveLength(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("addPromotionRules", () => {
|
describe("addPromotionRules", () => {
|
||||||
let promotion
|
let promotion
|
||||||
|
|
||||||
|
|||||||
@@ -365,8 +365,7 @@
|
|||||||
"localTableName": "public.promotion",
|
"localTableName": "public.promotion",
|
||||||
"referencedColumnNames": ["id"],
|
"referencedColumnNames": ["id"],
|
||||||
"referencedTableName": "public.campaign",
|
"referencedTableName": "public.campaign",
|
||||||
"deleteRule": "set null",
|
"deleteRule": "set null"
|
||||||
"updateRule": "cascade"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -518,6 +517,7 @@
|
|||||||
"localTableName": "public.application_method",
|
"localTableName": "public.application_method",
|
||||||
"referencedColumnNames": ["id"],
|
"referencedColumnNames": ["id"],
|
||||||
"referencedTableName": "public.promotion",
|
"referencedTableName": "public.promotion",
|
||||||
|
"deleteRule": "cascade",
|
||||||
"updateRule": "cascade"
|
"updateRule": "cascade"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -758,6 +758,38 @@
|
|||||||
"primary": false,
|
"primary": false,
|
||||||
"nullable": false,
|
"nullable": false,
|
||||||
"mappedType": "text"
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "timestamptz",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"length": 6,
|
||||||
|
"default": "now()",
|
||||||
|
"mappedType": "datetime"
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "timestamptz",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"length": 6,
|
||||||
|
"default": "now()",
|
||||||
|
"mappedType": "datetime"
|
||||||
|
},
|
||||||
|
"deleted_at": {
|
||||||
|
"name": "deleted_at",
|
||||||
|
"type": "timestamptz",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": true,
|
||||||
|
"length": 6,
|
||||||
|
"mappedType": "datetime"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"name": "promotion_rule_value",
|
"name": "promotion_rule_value",
|
||||||
|
|||||||
+4
-4
@@ -1,6 +1,6 @@
|
|||||||
import { Migration } from "@mikro-orm/migrations"
|
import { Migration } from "@mikro-orm/migrations"
|
||||||
|
|
||||||
export class Migration20240117090706 extends Migration {
|
export class Migration20240122070028 extends Migration {
|
||||||
async up(): Promise<void> {
|
async up(): Promise<void> {
|
||||||
this.addSql(
|
this.addSql(
|
||||||
'create table "campaign" ("id" text not null, "name" text not null, "description" text null, "currency" text null, "campaign_identifier" text not null, "starts_at" timestamptz null, "ends_at" timestamptz null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "campaign_pkey" primary key ("id"));'
|
'create table "campaign" ("id" text not null, "name" text not null, "description" text null, "currency" text null, "campaign_identifier" text not null, "starts_at" timestamptz null, "ends_at" timestamptz null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "campaign_pkey" primary key ("id"));'
|
||||||
@@ -63,7 +63,7 @@ export class Migration20240117090706 extends Migration {
|
|||||||
)
|
)
|
||||||
|
|
||||||
this.addSql(
|
this.addSql(
|
||||||
'create table "promotion_rule_value" ("id" text not null, "promotion_rule_id" text not null, "value" text not null, constraint "promotion_rule_value_pkey" primary key ("id"));'
|
'create table "promotion_rule_value" ("id" text not null, "promotion_rule_id" text not null, "value" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "promotion_rule_value_pkey" primary key ("id"));'
|
||||||
)
|
)
|
||||||
this.addSql(
|
this.addSql(
|
||||||
'create index "IDX_promotion_rule_promotion_rule_value_id" on "promotion_rule_value" ("promotion_rule_id");'
|
'create index "IDX_promotion_rule_promotion_rule_value_id" on "promotion_rule_value" ("promotion_rule_id");'
|
||||||
@@ -74,11 +74,11 @@ export class Migration20240117090706 extends Migration {
|
|||||||
)
|
)
|
||||||
|
|
||||||
this.addSql(
|
this.addSql(
|
||||||
'alter table "promotion" add constraint "promotion_campaign_id_foreign" foreign key ("campaign_id") references "campaign" ("id") on update cascade on delete set null;'
|
'alter table "promotion" add constraint "promotion_campaign_id_foreign" foreign key ("campaign_id") references "campaign" ("id") on delete set null;'
|
||||||
)
|
)
|
||||||
|
|
||||||
this.addSql(
|
this.addSql(
|
||||||
'alter table "application_method" add constraint "application_method_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade;'
|
'alter table "application_method" add constraint "application_method_promotion_id_foreign" foreign key ("promotion_id") references "promotion" ("id") on update cascade on delete cascade;'
|
||||||
)
|
)
|
||||||
|
|
||||||
this.addSql(
|
this.addSql(
|
||||||
@@ -4,12 +4,13 @@ import {
|
|||||||
ApplicationMethodTypeValues,
|
ApplicationMethodTypeValues,
|
||||||
DAL,
|
DAL,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { PromotionUtils, generateEntityId } from "@medusajs/utils"
|
import { DALUtils, PromotionUtils, generateEntityId } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Collection,
|
Collection,
|
||||||
Entity,
|
Entity,
|
||||||
Enum,
|
Enum,
|
||||||
|
Filter,
|
||||||
Index,
|
Index,
|
||||||
ManyToMany,
|
ManyToMany,
|
||||||
OnInit,
|
OnInit,
|
||||||
@@ -25,10 +26,10 @@ type OptionalFields =
|
|||||||
| "value"
|
| "value"
|
||||||
| "max_quantity"
|
| "max_quantity"
|
||||||
| "allocation"
|
| "allocation"
|
||||||
| "deleted_at"
|
| DAL.SoftDeletableEntityDateColumns
|
||||||
| DAL.EntityDateColumns
|
|
||||||
|
|
||||||
@Entity()
|
@Entity({ tableName: "application_method" })
|
||||||
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class ApplicationMethod {
|
export default class ApplicationMethod {
|
||||||
[OptionalProps]?: OptionalFields
|
[OptionalProps]?: OptionalFields
|
||||||
|
|
||||||
@@ -58,6 +59,7 @@ export default class ApplicationMethod {
|
|||||||
|
|
||||||
@OneToOne({
|
@OneToOne({
|
||||||
entity: () => Promotion,
|
entity: () => Promotion,
|
||||||
|
onDelete: "cascade",
|
||||||
})
|
})
|
||||||
promotion: Promotion
|
promotion: Promotion
|
||||||
|
|
||||||
@@ -84,7 +86,7 @@ export default class ApplicationMethod {
|
|||||||
updated_at: Date
|
updated_at: Date
|
||||||
|
|
||||||
@Property({ columnType: "timestamptz", nullable: true })
|
@Property({ columnType: "timestamptz", nullable: true })
|
||||||
deleted_at: Date | null
|
deleted_at: Date | null = null
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { CampaignBudgetTypeValues, DAL } from "@medusajs/types"
|
import { CampaignBudgetTypeValues, DAL } from "@medusajs/types"
|
||||||
import { PromotionUtils, generateEntityId } from "@medusajs/utils"
|
import { DALUtils, PromotionUtils, generateEntityId } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Entity,
|
Entity,
|
||||||
Enum,
|
Enum,
|
||||||
|
Filter,
|
||||||
Index,
|
Index,
|
||||||
OnInit,
|
OnInit,
|
||||||
OneToOne,
|
OneToOne,
|
||||||
@@ -17,10 +18,10 @@ type OptionalFields =
|
|||||||
| "description"
|
| "description"
|
||||||
| "limit"
|
| "limit"
|
||||||
| "used"
|
| "used"
|
||||||
| "deleted_at"
|
| DAL.SoftDeletableEntityDateColumns
|
||||||
| DAL.EntityDateColumns
|
|
||||||
|
|
||||||
@Entity()
|
@Entity({ tableName: "campaign_budget" })
|
||||||
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class CampaignBudget {
|
export default class CampaignBudget {
|
||||||
[OptionalProps]?: OptionalFields
|
[OptionalProps]?: OptionalFields
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ export default class CampaignBudget {
|
|||||||
@OneToOne({
|
@OneToOne({
|
||||||
entity: () => Campaign,
|
entity: () => Campaign,
|
||||||
})
|
})
|
||||||
campaign?: Campaign
|
campaign?: Campaign | null
|
||||||
|
|
||||||
@Property({
|
@Property({
|
||||||
columnType: "numeric",
|
columnType: "numeric",
|
||||||
@@ -42,7 +43,7 @@ export default class CampaignBudget {
|
|||||||
serializer: Number,
|
serializer: Number,
|
||||||
default: null,
|
default: null,
|
||||||
})
|
})
|
||||||
limit: number | null
|
limit?: number | null
|
||||||
|
|
||||||
@Property({
|
@Property({
|
||||||
columnType: "numeric",
|
columnType: "numeric",
|
||||||
@@ -67,7 +68,7 @@ export default class CampaignBudget {
|
|||||||
updated_at: Date
|
updated_at: Date
|
||||||
|
|
||||||
@Property({ columnType: "timestamptz", nullable: true })
|
@Property({ columnType: "timestamptz", nullable: true })
|
||||||
deleted_at: Date | null
|
deleted_at: Date | null = null
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { DAL } from "@medusajs/types"
|
import { DAL } from "@medusajs/types"
|
||||||
import { generateEntityId } from "@medusajs/utils"
|
import { DALUtils, generateEntityId } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Collection,
|
Collection,
|
||||||
Entity,
|
Entity,
|
||||||
|
Filter,
|
||||||
OnInit,
|
OnInit,
|
||||||
OneToMany,
|
OneToMany,
|
||||||
OneToOne,
|
OneToOne,
|
||||||
@@ -15,17 +16,16 @@ import {
|
|||||||
import CampaignBudget from "./campaign-budget"
|
import CampaignBudget from "./campaign-budget"
|
||||||
import Promotion from "./promotion"
|
import Promotion from "./promotion"
|
||||||
|
|
||||||
|
type OptionalRelations = "budget"
|
||||||
type OptionalFields =
|
type OptionalFields =
|
||||||
| "description"
|
| "description"
|
||||||
| "currency"
|
| "currency"
|
||||||
| "starts_at"
|
| "starts_at"
|
||||||
| "ends_at"
|
| "ends_at"
|
||||||
| "deleted_at"
|
| DAL.SoftDeletableEntityDateColumns
|
||||||
| DAL.EntityDateColumns
|
|
||||||
|
|
||||||
type OptionalRelations = "budget"
|
@Entity({ tableName: "campaign" })
|
||||||
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
@Entity()
|
|
||||||
export default class Campaign {
|
export default class Campaign {
|
||||||
[OptionalProps]?: OptionalFields | OptionalRelations
|
[OptionalProps]?: OptionalFields | OptionalRelations
|
||||||
|
|
||||||
@@ -36,10 +36,10 @@ export default class Campaign {
|
|||||||
name: string
|
name: string
|
||||||
|
|
||||||
@Property({ columnType: "text", nullable: true })
|
@Property({ columnType: "text", nullable: true })
|
||||||
description: string | null
|
description?: string | null
|
||||||
|
|
||||||
@Property({ columnType: "text", nullable: true })
|
@Property({ columnType: "text", nullable: true })
|
||||||
currency: string | null
|
currency?: string | null
|
||||||
|
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
@Unique({
|
@Unique({
|
||||||
@@ -52,13 +52,13 @@ export default class Campaign {
|
|||||||
columnType: "timestamptz",
|
columnType: "timestamptz",
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
starts_at: Date | null
|
starts_at?: Date | null
|
||||||
|
|
||||||
@Property({
|
@Property({
|
||||||
columnType: "timestamptz",
|
columnType: "timestamptz",
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
ends_at: Date | null
|
ends_at?: Date | null
|
||||||
|
|
||||||
@OneToOne({
|
@OneToOne({
|
||||||
entity: () => CampaignBudget,
|
entity: () => CampaignBudget,
|
||||||
@@ -66,7 +66,7 @@ export default class Campaign {
|
|||||||
cascade: ["soft-remove"] as any,
|
cascade: ["soft-remove"] as any,
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
budget: CampaignBudget | null
|
budget?: CampaignBudget | null
|
||||||
|
|
||||||
@OneToMany(() => Promotion, (promotion) => promotion.campaign, {
|
@OneToMany(() => Promotion, (promotion) => promotion.campaign, {
|
||||||
orphanRemoval: true,
|
orphanRemoval: true,
|
||||||
@@ -89,7 +89,7 @@ export default class Campaign {
|
|||||||
updated_at: Date
|
updated_at: Date
|
||||||
|
|
||||||
@Property({ columnType: "timestamptz", nullable: true })
|
@Property({ columnType: "timestamptz", nullable: true })
|
||||||
deleted_at: Date | null
|
deleted_at: Date | null = null
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
|
import { DALUtils, generateEntityId } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Entity,
|
Entity,
|
||||||
|
Filter,
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
OnInit,
|
OnInit,
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
Property,
|
Property,
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
|
|
||||||
import { generateEntityId } from "@medusajs/utils"
|
|
||||||
import PromotionRule from "./promotion-rule"
|
import PromotionRule from "./promotion-rule"
|
||||||
|
|
||||||
@Entity()
|
@Entity({ tableName: "promotion_rule_value" })
|
||||||
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class PromotionRuleValue {
|
export default class PromotionRuleValue {
|
||||||
@PrimaryKey({ columnType: "text" })
|
@PrimaryKey({ columnType: "text" })
|
||||||
id!: string
|
id!: string
|
||||||
@@ -25,6 +26,24 @@ export default class PromotionRuleValue {
|
|||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
value: string
|
value: string
|
||||||
|
|
||||||
|
@Property({
|
||||||
|
onCreate: () => new Date(),
|
||||||
|
columnType: "timestamptz",
|
||||||
|
defaultRaw: "now()",
|
||||||
|
})
|
||||||
|
created_at: Date
|
||||||
|
|
||||||
|
@Property({
|
||||||
|
onCreate: () => new Date(),
|
||||||
|
onUpdate: () => new Date(),
|
||||||
|
columnType: "timestamptz",
|
||||||
|
defaultRaw: "now()",
|
||||||
|
})
|
||||||
|
updated_at: Date
|
||||||
|
|
||||||
|
@Property({ columnType: "timestamptz", nullable: true })
|
||||||
|
deleted_at: Date | null = null
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
this.id = generateEntityId(this.id, "prorulval")
|
this.id = generateEntityId(this.id, "prorulval")
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import { DAL, PromotionRuleOperatorValues } from "@medusajs/types"
|
import { DAL, PromotionRuleOperatorValues } from "@medusajs/types"
|
||||||
import { PromotionUtils, generateEntityId } from "@medusajs/utils"
|
import { DALUtils, PromotionUtils, generateEntityId } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Cascade,
|
Cascade,
|
||||||
Collection,
|
Collection,
|
||||||
Entity,
|
Entity,
|
||||||
Enum,
|
Enum,
|
||||||
|
Filter,
|
||||||
Index,
|
Index,
|
||||||
ManyToMany,
|
ManyToMany,
|
||||||
OnInit,
|
OnInit,
|
||||||
@@ -18,10 +19,11 @@ import ApplicationMethod from "./application-method"
|
|||||||
import Promotion from "./promotion"
|
import Promotion from "./promotion"
|
||||||
import PromotionRuleValue from "./promotion-rule-value"
|
import PromotionRuleValue from "./promotion-rule-value"
|
||||||
|
|
||||||
type OptionalFields = "description" | "deleted_at" | DAL.EntityDateColumns
|
type OptionalFields = "description" | DAL.SoftDeletableEntityDateColumns
|
||||||
type OptionalRelations = "values" | "promotions"
|
type OptionalRelations = "values" | "promotions"
|
||||||
|
|
||||||
@Entity()
|
@Entity({ tableName: "promotion_rule" })
|
||||||
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class PromotionRule {
|
export default class PromotionRule {
|
||||||
[OptionalProps]?: OptionalFields | OptionalRelations
|
[OptionalProps]?: OptionalFields | OptionalRelations
|
||||||
|
|
||||||
@@ -29,7 +31,7 @@ export default class PromotionRule {
|
|||||||
id!: string
|
id!: string
|
||||||
|
|
||||||
@Property({ columnType: "text", nullable: true })
|
@Property({ columnType: "text", nullable: true })
|
||||||
description: string | null
|
description?: string | null
|
||||||
|
|
||||||
@Index({ name: "IDX_promotion_rule_attribute" })
|
@Index({ name: "IDX_promotion_rule_attribute" })
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
@@ -69,7 +71,7 @@ export default class PromotionRule {
|
|||||||
updated_at: Date
|
updated_at: Date
|
||||||
|
|
||||||
@Property({ columnType: "timestamptz", nullable: true })
|
@Property({ columnType: "timestamptz", nullable: true })
|
||||||
deleted_at: Date | null
|
deleted_at: Date | null = null
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { DAL, PromotionType } from "@medusajs/types"
|
import { DAL, PromotionType } from "@medusajs/types"
|
||||||
import { PromotionUtils, generateEntityId } from "@medusajs/utils"
|
import { DALUtils, PromotionUtils, generateEntityId } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Collection,
|
Collection,
|
||||||
Entity,
|
Entity,
|
||||||
Enum,
|
Enum,
|
||||||
|
Filter,
|
||||||
Index,
|
Index,
|
||||||
ManyToMany,
|
ManyToMany,
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
@@ -19,10 +20,11 @@ import ApplicationMethod from "./application-method"
|
|||||||
import Campaign from "./campaign"
|
import Campaign from "./campaign"
|
||||||
import PromotionRule from "./promotion-rule"
|
import PromotionRule from "./promotion-rule"
|
||||||
|
|
||||||
type OptionalFields = "is_automatic" | "deleted_at" | DAL.EntityDateColumns
|
type OptionalFields = "is_automatic" | DAL.SoftDeletableEntityDateColumns
|
||||||
type OptionalRelations = "application_method" | "campaign"
|
type OptionalRelations = "application_method" | "campaign"
|
||||||
|
|
||||||
@Entity()
|
@Entity({ tableName: "promotion" })
|
||||||
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class Promotion {
|
export default class Promotion {
|
||||||
[OptionalProps]?: OptionalFields | OptionalRelations
|
[OptionalProps]?: OptionalFields | OptionalRelations
|
||||||
|
|
||||||
@@ -41,11 +43,12 @@ export default class Promotion {
|
|||||||
joinColumn: "campaign",
|
joinColumn: "campaign",
|
||||||
fieldName: "campaign_id",
|
fieldName: "campaign_id",
|
||||||
nullable: true,
|
nullable: true,
|
||||||
|
cascade: ["soft-remove"] as any,
|
||||||
})
|
})
|
||||||
campaign: Campaign
|
campaign?: Campaign | null
|
||||||
|
|
||||||
@Property({ columnType: "boolean", default: false })
|
@Property({ columnType: "boolean", default: false })
|
||||||
is_automatic?: boolean = false
|
is_automatic: boolean = false
|
||||||
|
|
||||||
@Index({ name: "IDX_promotion_type" })
|
@Index({ name: "IDX_promotion_type" })
|
||||||
@Enum(() => PromotionUtils.PromotionType)
|
@Enum(() => PromotionUtils.PromotionType)
|
||||||
@@ -81,7 +84,7 @@ export default class Promotion {
|
|||||||
updated_at: Date
|
updated_at: Date
|
||||||
|
|
||||||
@Property({ columnType: "timestamptz", nullable: true })
|
@Property({ columnType: "timestamptz", nullable: true })
|
||||||
deleted_at: Date | null
|
deleted_at: Date | null = null
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import {
|
|||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
ModuleJoinerConfig,
|
ModuleJoinerConfig,
|
||||||
PromotionTypes,
|
PromotionTypes,
|
||||||
|
RestoreReturn,
|
||||||
|
SoftDeleteReturn,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ApplicationMethodTargetType,
|
ApplicationMethodTargetType,
|
||||||
@@ -14,8 +16,16 @@ import {
|
|||||||
MedusaContext,
|
MedusaContext,
|
||||||
MedusaError,
|
MedusaError,
|
||||||
isString,
|
isString,
|
||||||
|
mapObjectTo,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { ApplicationMethod, Promotion } from "@models"
|
import {
|
||||||
|
ApplicationMethod,
|
||||||
|
Campaign,
|
||||||
|
CampaignBudget,
|
||||||
|
Promotion,
|
||||||
|
PromotionRule,
|
||||||
|
PromotionRuleValue,
|
||||||
|
} from "@models"
|
||||||
import {
|
import {
|
||||||
ApplicationMethodService,
|
ApplicationMethodService,
|
||||||
CampaignBudgetService,
|
CampaignBudgetService,
|
||||||
@@ -42,29 +52,37 @@ import {
|
|||||||
validateApplicationMethodAttributes,
|
validateApplicationMethodAttributes,
|
||||||
validatePromotionRuleAttributes,
|
validatePromotionRuleAttributes,
|
||||||
} from "@utils"
|
} from "@utils"
|
||||||
import { joinerConfig } from "../joiner-config"
|
import {
|
||||||
|
LinkableKeys,
|
||||||
|
entityNameToLinkableKeysMap,
|
||||||
|
joinerConfig,
|
||||||
|
} from "../joiner-config"
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
baseRepository: DAL.RepositoryService
|
baseRepository: DAL.RepositoryService
|
||||||
promotionService: PromotionService
|
promotionService: PromotionService<any>
|
||||||
applicationMethodService: ApplicationMethodService
|
applicationMethodService: ApplicationMethodService<any>
|
||||||
promotionRuleService: PromotionRuleService
|
promotionRuleService: PromotionRuleService<any>
|
||||||
promotionRuleValueService: PromotionRuleValueService
|
promotionRuleValueService: PromotionRuleValueService<any>
|
||||||
campaignService: CampaignService
|
campaignService: CampaignService<any>
|
||||||
campaignBudgetService: CampaignBudgetService
|
campaignBudgetService: CampaignBudgetService<any>
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class PromotionModuleService<
|
export default class PromotionModuleService<
|
||||||
TPromotion extends Promotion = Promotion
|
TPromotion extends Promotion = Promotion,
|
||||||
|
TPromotionRule extends PromotionRule = PromotionRule,
|
||||||
|
TPromotionRuleValue extends PromotionRuleValue = PromotionRuleValue,
|
||||||
|
TCampaign extends Campaign = Campaign,
|
||||||
|
TCampaignBudget extends CampaignBudget = CampaignBudget
|
||||||
> implements PromotionTypes.IPromotionModuleService
|
> implements PromotionTypes.IPromotionModuleService
|
||||||
{
|
{
|
||||||
protected baseRepository_: DAL.RepositoryService
|
protected baseRepository_: DAL.RepositoryService
|
||||||
protected promotionService_: PromotionService
|
protected promotionService_: PromotionService<TPromotion>
|
||||||
protected applicationMethodService_: ApplicationMethodService
|
protected applicationMethodService_: ApplicationMethodService
|
||||||
protected promotionRuleService_: PromotionRuleService
|
protected promotionRuleService_: PromotionRuleService<TPromotionRule>
|
||||||
protected promotionRuleValueService_: PromotionRuleValueService
|
protected promotionRuleValueService_: PromotionRuleValueService<TPromotionRuleValue>
|
||||||
protected campaignService_: CampaignService
|
protected campaignService_: CampaignService<TCampaign>
|
||||||
protected campaignBudgetService_: CampaignBudgetService
|
protected campaignBudgetService_: CampaignBudgetService<TCampaignBudget>
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
{
|
{
|
||||||
@@ -809,12 +827,84 @@ export default class PromotionModuleService<
|
|||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
@InjectTransactionManager("baseRepository_")
|
||||||
async delete(
|
async delete(
|
||||||
ids: string | string[],
|
ids: string[] | string,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const promotionIds = Array.isArray(ids) ? ids : [ids]
|
const idsToDelete = Array.isArray(ids) ? ids : [ids]
|
||||||
|
|
||||||
await this.promotionService_.delete(promotionIds, sharedContext)
|
await this.promotionService_.delete(idsToDelete, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async softDelete<
|
||||||
|
TReturnableLinkableKeys extends string = Lowercase<
|
||||||
|
keyof typeof LinkableKeys
|
||||||
|
>
|
||||||
|
>(
|
||||||
|
ids: string | string[],
|
||||||
|
{ returnLinkableKeys }: SoftDeleteReturn<TReturnableLinkableKeys> = {},
|
||||||
|
sharedContext: Context = {}
|
||||||
|
): Promise<Record<Lowercase<keyof typeof LinkableKeys>, string[]> | void> {
|
||||||
|
const idsToDelete = Array.isArray(ids) ? ids : [ids]
|
||||||
|
let [_, cascadedEntitiesMap] = await this.softDelete_(
|
||||||
|
idsToDelete,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
let mappedCascadedEntitiesMap
|
||||||
|
if (returnLinkableKeys) {
|
||||||
|
mappedCascadedEntitiesMap = mapObjectTo<
|
||||||
|
Record<Lowercase<keyof typeof LinkableKeys>, string[]>
|
||||||
|
>(cascadedEntitiesMap, entityNameToLinkableKeysMap, {
|
||||||
|
pick: returnLinkableKeys,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
protected async softDelete_(
|
||||||
|
promotionIds: string[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<[TPromotion[], Record<string, unknown[]>]> {
|
||||||
|
return await this.promotionService_.softDelete(promotionIds, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async restore<
|
||||||
|
TReturnableLinkableKeys extends string = Lowercase<
|
||||||
|
keyof typeof LinkableKeys
|
||||||
|
>
|
||||||
|
>(
|
||||||
|
ids: string | string[],
|
||||||
|
{ returnLinkableKeys }: RestoreReturn<TReturnableLinkableKeys> = {},
|
||||||
|
sharedContext: Context = {}
|
||||||
|
): Promise<Record<Lowercase<keyof typeof LinkableKeys>, string[]> | void> {
|
||||||
|
const idsToRestore = Array.isArray(ids) ? ids : [ids]
|
||||||
|
const [_, cascadedEntitiesMap] = await this.restore_(
|
||||||
|
idsToRestore,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
let mappedCascadedEntitiesMap
|
||||||
|
if (returnLinkableKeys) {
|
||||||
|
mappedCascadedEntitiesMap = mapObjectTo<
|
||||||
|
Record<Lowercase<keyof typeof LinkableKeys>, string[]>
|
||||||
|
>(cascadedEntitiesMap, entityNameToLinkableKeysMap, {
|
||||||
|
pick: returnLinkableKeys,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
async restore_(
|
||||||
|
ids: string[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<[TPromotion[], Record<string, unknown[]>]> {
|
||||||
|
return await this.promotionService_.restore(ids, sharedContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
@InjectManager("baseRepository_")
|
||||||
@@ -1149,8 +1239,76 @@ export default class PromotionModuleService<
|
|||||||
ids: string | string[],
|
ids: string | string[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const campaignIds = Array.isArray(ids) ? ids : [ids]
|
const idsToDelete = Array.isArray(ids) ? ids : [ids]
|
||||||
|
|
||||||
await this.promotionService_.delete(campaignIds, sharedContext)
|
await this.campaignService_.delete(idsToDelete, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async softDeleteCampaigns<TReturnableLinkableKeys extends string>(
|
||||||
|
ids: string | string[],
|
||||||
|
{ returnLinkableKeys }: SoftDeleteReturn<TReturnableLinkableKeys> = {},
|
||||||
|
sharedContext: Context = {}
|
||||||
|
): Promise<Record<Lowercase<keyof typeof LinkableKeys>, string[]> | void> {
|
||||||
|
const idsToDelete = Array.isArray(ids) ? ids : [ids]
|
||||||
|
let [_, cascadedEntitiesMap] = await this.softDeleteCampaigns_(
|
||||||
|
idsToDelete,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
let mappedCascadedEntitiesMap
|
||||||
|
if (returnLinkableKeys) {
|
||||||
|
mappedCascadedEntitiesMap = mapObjectTo<
|
||||||
|
Record<Lowercase<keyof typeof LinkableKeys>, string[]>
|
||||||
|
>(cascadedEntitiesMap, entityNameToLinkableKeysMap, {
|
||||||
|
pick: returnLinkableKeys,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
protected async softDeleteCampaigns_(
|
||||||
|
campaignIds: string[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<[TCampaign[], Record<string, unknown[]>]> {
|
||||||
|
return await this.campaignService_.softDelete(campaignIds, sharedContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectManager("baseRepository_")
|
||||||
|
async restoreCampaigns<
|
||||||
|
TReturnableLinkableKeys extends string = Lowercase<
|
||||||
|
keyof typeof LinkableKeys
|
||||||
|
>
|
||||||
|
>(
|
||||||
|
ids: string | string[],
|
||||||
|
{ returnLinkableKeys }: RestoreReturn<TReturnableLinkableKeys> = {},
|
||||||
|
sharedContext: Context = {}
|
||||||
|
): Promise<Record<Lowercase<keyof typeof LinkableKeys>, string[]> | void> {
|
||||||
|
const idsToRestore = Array.isArray(ids) ? ids : [ids]
|
||||||
|
const [_, cascadedEntitiesMap] = await this.restoreCampaigns_(
|
||||||
|
idsToRestore,
|
||||||
|
sharedContext
|
||||||
|
)
|
||||||
|
|
||||||
|
let mappedCascadedEntitiesMap
|
||||||
|
if (returnLinkableKeys) {
|
||||||
|
mappedCascadedEntitiesMap = mapObjectTo<
|
||||||
|
Record<Lowercase<keyof typeof LinkableKeys>, string[]>
|
||||||
|
>(cascadedEntitiesMap, entityNameToLinkableKeysMap, {
|
||||||
|
pick: returnLinkableKeys,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return mappedCascadedEntitiesMap ? mappedCascadedEntitiesMap : void 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectTransactionManager("baseRepository_")
|
||||||
|
async restoreCampaigns_(
|
||||||
|
ids: string[],
|
||||||
|
@MedusaContext() sharedContext: Context = {}
|
||||||
|
): Promise<[TCampaign[], Record<string, unknown[]>]> {
|
||||||
|
return await this.campaignService_.restore(ids, sharedContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { FindConfig } from "../common"
|
import { FindConfig } from "../common"
|
||||||
|
import { RestoreReturn, SoftDeleteReturn } from "../dal"
|
||||||
import { IModuleService } from "../modules-sdk"
|
import { IModuleService } from "../modules-sdk"
|
||||||
import { Context } from "../shared-context"
|
import { Context } from "../shared-context"
|
||||||
import {
|
import {
|
||||||
@@ -13,7 +14,6 @@ import {
|
|||||||
RemovePromotionRuleDTO,
|
RemovePromotionRuleDTO,
|
||||||
UpdatePromotionDTO,
|
UpdatePromotionDTO,
|
||||||
} from "./common"
|
} from "./common"
|
||||||
|
|
||||||
import { CreateCampaignDTO, UpdateCampaignDTO } from "./mutations"
|
import { CreateCampaignDTO, UpdateCampaignDTO } from "./mutations"
|
||||||
|
|
||||||
export interface IPromotionModuleService extends IModuleService {
|
export interface IPromotionModuleService extends IModuleService {
|
||||||
@@ -66,6 +66,18 @@ export interface IPromotionModuleService extends IModuleService {
|
|||||||
delete(ids: string[], sharedContext?: Context): Promise<void>
|
delete(ids: string[], sharedContext?: Context): Promise<void>
|
||||||
delete(ids: string, sharedContext?: Context): Promise<void>
|
delete(ids: string, sharedContext?: Context): Promise<void>
|
||||||
|
|
||||||
|
softDelete<TReturnableLinkableKeys extends string = string>(
|
||||||
|
promotionIds: string | string[],
|
||||||
|
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<Record<string, string[]> | void>
|
||||||
|
|
||||||
|
restore<TReturnableLinkableKeys extends string = string>(
|
||||||
|
promotionIds: string | string[],
|
||||||
|
config?: RestoreReturn<TReturnableLinkableKeys>,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<Record<string, string[]> | void>
|
||||||
|
|
||||||
addPromotionRules(
|
addPromotionRules(
|
||||||
promotionId: string,
|
promotionId: string,
|
||||||
rulesData: CreatePromotionRuleDTO[],
|
rulesData: CreatePromotionRuleDTO[],
|
||||||
@@ -130,4 +142,16 @@ export interface IPromotionModuleService extends IModuleService {
|
|||||||
|
|
||||||
deleteCampaigns(ids: string[], sharedContext?: Context): Promise<void>
|
deleteCampaigns(ids: string[], sharedContext?: Context): Promise<void>
|
||||||
deleteCampaigns(ids: string, sharedContext?: Context): Promise<void>
|
deleteCampaigns(ids: string, sharedContext?: Context): Promise<void>
|
||||||
|
|
||||||
|
softDeleteCampaigns<TReturnableLinkableKeys extends string = string>(
|
||||||
|
campaignIds: string | string[],
|
||||||
|
config?: SoftDeleteReturn<TReturnableLinkableKeys>,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<Record<string, string[]> | void>
|
||||||
|
|
||||||
|
restoreCampaigns<TReturnableLinkableKeys extends string = string>(
|
||||||
|
campaignIds: string | string[],
|
||||||
|
config?: RestoreReturn<TReturnableLinkableKeys>,
|
||||||
|
sharedContext?: Context
|
||||||
|
): Promise<Record<string, string[]> | void>
|
||||||
}
|
}
|
||||||
@@ -17,9 +17,9 @@ import {
|
|||||||
EntityName,
|
EntityName,
|
||||||
FilterQuery as MikroFilterQuery,
|
FilterQuery as MikroFilterQuery,
|
||||||
} from "@mikro-orm/core/typings"
|
} from "@mikro-orm/core/typings"
|
||||||
import { isString, MedusaError } from "../../common"
|
import { MedusaError, isString } from "../../common"
|
||||||
import { MedusaContext } from "../../decorators"
|
import { MedusaContext } from "../../decorators"
|
||||||
import { buildQuery, InjectTransactionManager } from "../../modules-sdk"
|
import { InjectTransactionManager, buildQuery } from "../../modules-sdk"
|
||||||
import {
|
import {
|
||||||
getSoftDeletedCascadedEntitiesIdsMappedBy,
|
getSoftDeletedCascadedEntitiesIdsMappedBy,
|
||||||
transactionWrapper,
|
transactionWrapper,
|
||||||
|
|||||||
Reference in new issue
Block a user