Skip to content

Commit

Permalink
refactor: migrate promotion module (#10410)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored Dec 11, 2024
1 parent 777582a commit fad85a9
Show file tree
Hide file tree
Showing 22 changed files with 662 additions and 725 deletions.
6 changes: 6 additions & 0 deletions .changeset/little-hounds-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/promotion": patch
"@medusajs/utils": patch
---

refactor: migrate promotion module
16 changes: 1 addition & 15 deletions packages/core/utils/src/dml/__tests__/entity-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "user",
},
created_at: {
Expand Down Expand Up @@ -2508,7 +2507,7 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
onDelete: undefined,
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -2595,7 +2594,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "owner",
},
created_at: {
Expand Down Expand Up @@ -2687,7 +2685,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "user",
cascade: ["persist", "soft-remove"],
onDelete: "cascade",
Expand Down Expand Up @@ -2851,9 +2848,7 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "user",
cascade: ["persist", "soft-remove"],
onDelete: "cascade",
},
created_at: {
Expand Down Expand Up @@ -3040,7 +3035,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
},
email_id: {
columnType: "text",
Expand Down Expand Up @@ -3251,7 +3245,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "owner",
},
email_id: {
Expand Down Expand Up @@ -3356,7 +3349,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
cascade: ["persist", "soft-remove"],
mappedBy: "user",
},
Expand Down Expand Up @@ -3532,7 +3524,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
cascade: ["persist", "soft-remove"],
mappedBy: "user",
},
Expand Down Expand Up @@ -4493,7 +4484,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "user",
},
created_at: {
Expand Down Expand Up @@ -4691,7 +4681,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "user",
},
created_at: {
Expand Down Expand Up @@ -5326,7 +5315,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "user",
},
created_at: {
Expand Down Expand Up @@ -5526,7 +5514,6 @@ describe("Entity builder", () => {
reference: "1:1",
name: "email",
entity: "Email",
nullable: false,
mappedBy: "user",
},
created_at: {
Expand Down Expand Up @@ -5838,7 +5825,6 @@ describe("Entity builder", () => {
entity: "User",
mappedBy: "parent",
name: "child",
nullable: false,
reference: "1:1",
},
created_at: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { HasOneWithForeignKey } from "../../relations/has-one-fk"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"
import { applyEntityIndexes } from "../mikro-orm/apply-indexes"
import { parseEntityName } from "./parse-entity-name"
import { BelongsTo } from "../../relations"

type Context = {
MANY_TO_MANY_TRACKED_RELATIONS: Record<string, boolean>
Expand Down Expand Up @@ -138,24 +139,32 @@ function validateManyToManyRelationshipWithoutMappedBy({
export function defineHasOneRelationship(
MikroORMEntity: EntityConstructor<any>,
relationship: RelationshipMetadata,
relatedEntity: DmlEntity<
Record<string, PropertyType<any> | RelationshipType<any>>,
any
>,
{ relatedModelName }: { relatedModelName: string },
cascades: EntityCascades<string[]>
) {
const shouldRemoveRelated = !!cascades.delete?.includes(relationship.name)
const { schema: relationSchema } = relatedEntity.parse()

let mappedBy: string | undefined = camelToSnakeCase(MikroORMEntity.name)
if ("mappedBy" in relationship) {
mappedBy = relationship.mappedBy
}

const isOthersideBelongsTo =
!!mappedBy && BelongsTo.isBelongsTo(relationSchema[mappedBy])

const oneToOneOptions = {
entity: relatedModelName,
nullable: relationship.nullable,
...(relationship.nullable ? { nullable: relationship.nullable } : {}),
...(mappedBy ? { mappedBy } : {}),
onDelete: shouldRemoveRelated ? "cascade" : undefined,
} as OneToOneOptions<any, any>

if (shouldRemoveRelated) {
if (shouldRemoveRelated && !isOthersideBelongsTo) {
oneToOneOptions.cascade = ["persist", "soft-remove"] as any
}

Expand All @@ -182,7 +191,7 @@ export function defineHasOneWithFKRelationship(

OneToOne({
entity: relatedModelName,
nullable: relationship.nullable,
...(relationship.nullable ? { nullable: relationship.nullable } : {}),
...(mappedBy ? { mappedBy } : {}),
cascade: shouldRemoveRelated
? (["persist", "soft-remove"] as any)
Expand Down Expand Up @@ -697,6 +706,7 @@ export function defineRelationship(
defineHasOneRelationship(
MikroORMEntity,
relationship,
relatedEntity,
relatedEntityInfo,
cascades
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function applyIndexes(
) {
field.indexes.forEach((index) => {
const providerEntityIdIndexStatement = createPsqlIndexStatementHelper({
name: index.name,
tableName,
columns: [field.fieldName],
unique: index.type === "unique",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CreateCampaignDTO } from "@medusajs/framework/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Campaign } from "@models"
import { defaultCampaignsData } from "./data"
import { toMikroORMEntity } from "@medusajs/framework/utils"

export * from "./data"

Expand All @@ -12,7 +13,7 @@ export async function createCampaigns(
const campaigns: Campaign[] = []

for (let campaignData of campaignsData) {
let campaign = manager.create(Campaign, campaignData)
let campaign = manager.create(toMikroORMEntity(Campaign), campaignData)

manager.persist(campaign)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
IPromotionModuleService,
PromotionDTO,
} from "@medusajs/framework/types"
import { isPresent } from "@medusajs/framework/utils"
import { isPresent, toMikroORMEntity } from "@medusajs/framework/utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Promotion } from "@models"
import { defaultPromotionsData } from "./data"
Expand All @@ -17,7 +17,7 @@ export async function createPromotions(
const promotions: Promotion[] = []

for (let promotionData of promotionsData) {
let promotion = manager.create(Promotion, promotionData)
let promotion = manager.create(toMikroORMEntity(Promotion), promotionData)

manager.persist(promotion)
await manager.flush()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ moduleIntegrationTestRunner({
)
})

it("should create a buyget promotion with rules successfully", async () => {
it("should create a budget promotion with rules successfully", async () => {
const createdPromotion = await createDefaultPromotion(service, {
type: PromotionType.BUYGET,
application_method: {
Expand Down Expand Up @@ -638,7 +638,7 @@ moduleIntegrationTestRunner({
application_method: expect.objectContaining({
target_type: "order",
allocation: "across",
max_quantity: 0,
max_quantity: null,
}),
})
)
Expand Down
2 changes: 1 addition & 1 deletion packages/modules/promotion/mikro-orm.config.dev.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as entities from "./src/models"
import { defineMikroOrmCliConfig, Modules } from "@medusajs/framework/utils"

export default defineMikroOrmCliConfig(Modules.PRODUCT, {
export default defineMikroOrmCliConfig(Modules.PROMOTION, {
entities: Object.values(entities),
})
Loading

0 comments on commit fad85a9

Please sign in to comment.