chore: scope campaign identifier index with deleted at (#7821)
This commit is contained in:
1 parent
e8d6025374
commit
7414034ac4
4 files changed
+70
-10
No files matched your search
+30
-1
@@ -1,9 +1,9 @@
|
|||||||
import { IPromotionModuleService } from "@medusajs/types"
|
import { IPromotionModuleService } from "@medusajs/types"
|
||||||
|
import { Modules } from "@medusajs/utils"
|
||||||
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
import { moduleIntegrationTestRunner } from "medusa-test-utils"
|
||||||
import { CampaignBudgetType } from "../../../../../../core/utils/src/promotion/index"
|
import { CampaignBudgetType } from "../../../../../../core/utils/src/promotion/index"
|
||||||
import { createCampaigns } from "../../../__fixtures__/campaigns"
|
import { createCampaigns } from "../../../__fixtures__/campaigns"
|
||||||
import { createPromotions } from "../../../__fixtures__/promotion"
|
import { createPromotions } from "../../../__fixtures__/promotion"
|
||||||
import { Modules } from "@medusajs/utils"
|
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
@@ -170,6 +170,35 @@ moduleIntegrationTestRunner<IPromotionModuleService>({
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should create a campaign with a campaign identitifer of a deleted campaign", async () => {
|
||||||
|
const campaign = await service.createCampaigns({
|
||||||
|
name: "test",
|
||||||
|
campaign_identifier: "test",
|
||||||
|
})
|
||||||
|
|
||||||
|
await service.softDeleteCampaigns(campaign.id)
|
||||||
|
|
||||||
|
const recreatedCampaign = await service.createCampaigns({
|
||||||
|
name: "test",
|
||||||
|
campaign_identifier: "test",
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(recreatedCampaign).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
name: "test",
|
||||||
|
campaign_identifier: "test",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const error = await service
|
||||||
|
.restoreCampaigns(campaign.id)
|
||||||
|
.catch((e) => e)
|
||||||
|
|
||||||
|
expect(error.message).toEqual(
|
||||||
|
"Promotion campaign with campaign_identifier: test, already exists."
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("updateCampaigns", () => {
|
describe("updateCampaigns", () => {
|
||||||
|
|||||||
@@ -99,13 +99,14 @@
|
|||||||
"schema": "public",
|
"schema": "public",
|
||||||
"indexes": [
|
"indexes": [
|
||||||
{
|
{
|
||||||
"keyName": "IDX_campaign_identifier_unique",
|
"keyName": "IDX_promotion_campaign_campaign_identifier_unique",
|
||||||
"columnNames": [
|
"columnNames": [
|
||||||
"campaign_identifier"
|
"campaign_identifier"
|
||||||
],
|
],
|
||||||
"composite": false,
|
"composite": false,
|
||||||
"primary": false,
|
"primary": false,
|
||||||
"unique": true
|
"unique": false,
|
||||||
|
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_promotion_campaign_campaign_identifier_unique\" ON \"promotion_campaign\" (campaign_identifier) WHERE deleted_at IS NULL"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"keyName": "promotion_campaign_pkey",
|
"keyName": "promotion_campaign_pkey",
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { Migration } from "@mikro-orm/migrations"
|
||||||
|
|
||||||
|
export class Migration20240624153824 extends Migration {
|
||||||
|
async up(): Promise<void> {
|
||||||
|
this.addSql(
|
||||||
|
'alter table if exists "promotion_campaign" drop constraint if exists "IDX_campaign_identifier_unique";'
|
||||||
|
)
|
||||||
|
this.addSql(
|
||||||
|
'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_promotion_campaign_campaign_identifier_unique" ON "promotion_campaign" (campaign_identifier) WHERE deleted_at IS NULL;'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
async down(): Promise<void> {
|
||||||
|
this.addSql(
|
||||||
|
'drop index if exists "IDX_promotion_campaign_campaign_identifier_unique";'
|
||||||
|
)
|
||||||
|
this.addSql(
|
||||||
|
'alter table if exists "promotion_campaign" add constraint "IDX_campaign_identifier_unique" unique ("campaign_identifier");'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
import { DAL } from "@medusajs/types"
|
import { DAL } from "@medusajs/types"
|
||||||
import { DALUtils, Searchable, generateEntityId } from "@medusajs/utils"
|
import {
|
||||||
|
DALUtils,
|
||||||
|
Searchable,
|
||||||
|
createPsqlIndexStatementHelper,
|
||||||
|
generateEntityId,
|
||||||
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Collection,
|
Collection,
|
||||||
@@ -12,7 +17,6 @@ import {
|
|||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
Property,
|
Property,
|
||||||
Rel,
|
Rel,
|
||||||
Unique,
|
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
import CampaignBudget from "./campaign-budget"
|
import CampaignBudget from "./campaign-budget"
|
||||||
import Promotion from "./promotion"
|
import Promotion from "./promotion"
|
||||||
@@ -24,7 +28,15 @@ type OptionalFields =
|
|||||||
| "ends_at"
|
| "ends_at"
|
||||||
| DAL.SoftDeletableEntityDateColumns
|
| DAL.SoftDeletableEntityDateColumns
|
||||||
|
|
||||||
@Entity({ tableName: "promotion_campaign" })
|
const tableName = "promotion_campaign"
|
||||||
|
const CampaignUniqueCampaignIdentifier = createPsqlIndexStatementHelper({
|
||||||
|
tableName,
|
||||||
|
columns: ["campaign_identifier"],
|
||||||
|
unique: true,
|
||||||
|
where: "deleted_at IS NULL",
|
||||||
|
})
|
||||||
|
|
||||||
|
@Entity({ tableName })
|
||||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||||
export default class Campaign {
|
export default class Campaign {
|
||||||
[OptionalProps]?: OptionalFields | OptionalRelations
|
[OptionalProps]?: OptionalFields | OptionalRelations
|
||||||
@@ -41,10 +53,7 @@ export default class Campaign {
|
|||||||
description: string | null = null
|
description: string | null = null
|
||||||
|
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
@Unique({
|
@CampaignUniqueCampaignIdentifier.MikroORMIndex()
|
||||||
name: "IDX_campaign_identifier_unique",
|
|
||||||
properties: ["campaign_identifier"],
|
|
||||||
})
|
|
||||||
campaign_identifier: string
|
campaign_identifier: string
|
||||||
|
|
||||||
@Property({
|
@Property({
|
||||||
|
|||||||
Reference in new issue
Block a user