Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate promotion module #10410

Merged
merged 25 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5dcf909
refactor: migrate promotion module
thetutlage Dec 3, 2024
c75eb60
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 3, 2024
a16b43c
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 4, 2024
30e6d18
test: fix breaking tests
thetutlage Dec 4, 2024
0a6f4d6
refactor: remove dead code
thetutlage Dec 4, 2024
8bfe843
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 4, 2024
f7ff4df
tests: fix dml unit tests
thetutlage Dec 4, 2024
473dc2e
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 5, 2024
6b11eae
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 6, 2024
882c6a5
Merge branch 'develop' into feat/promotion-dml
olivermrbl Dec 6, 2024
a931beb
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 9, 2024
520d11d
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 10, 2024
0e45950
wip
thetutlage Dec 10, 2024
ab80666
fix relation definition of application method on promotion
adrien2p Dec 10, 2024
4286306
fix one to one soft remove cascade definition to prevent circular def
adrien2p Dec 10, 2024
e9b016a
fix: breaking unit tests
thetutlage Dec 10, 2024
11eb63e
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 10, 2024
21f7eb1
fix: issues with hasOne cascade
thetutlage Dec 10, 2024
dbcdf92
refactor: reformat migration
thetutlage Dec 10, 2024
1e214dc
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 10, 2024
b961449
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 10, 2024
5e48165
Merge branch 'develop' into feat/promotion-dml
thetutlage Dec 11, 2024
3a577b8
refactor: remove any from campaign model
thetutlage Dec 11, 2024
8d469f9
fix: apply index name when applying indexes
thetutlage Dec 11, 2024
fdc11a8
Create little-hounds-build.md
thetutlage Dec 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,
thetutlage marked this conversation as resolved.
Show resolved Hide resolved
}),
})
)
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
Loading