chore(order): dml (#10292)

* ../../core/types/src/dml/index.ts

* ../../core/types/src/dml/index.ts

* fix: relationships mapping

* handle nullable foreign keys types

* handle nullable foreign keys types

* handle nullable foreign keys types

* continue to update product category repository

* fix all product category repositories issues

* fix product category service types

* fix product module service types

* fix product module service types

* fix repository template type

* refactor: use a singleton DMLToMikroORM factory instance

Since the MikroORM MetadataStorage is global, we will also have to turn DML
to MikroORM entities conversion use a global bucket as well

* refactor: update product module to use DML in tests

* wip: tests

* WIP product linkable fixes

* continue type fixing and start test fixing

* test: fix more tests

* fix repository

* fix pivot table computaion + fix mikro orm repository

* fix many to many management and configuration

* fix many to many management and configuration

* fix many to many management and configuration

* update product tag relation configuration

* Introduce experimental dml hooks to fix some issues with categories

* more fixes

* fix product tests

* add missing id prefixes

* fix product category handle management

* test: fix more failing tests

* test: make it all green

* test: fix breaking tests

* fix: build issues

* fix: build issues

* fix: more breaking tests

* refactor: fix issues after merge

* refactor: fix issues after merge

* refactor: surpress types error

* test: fix DML failing tests

* improve many to many inference + tests

* Wip fix columns from product entity

* remove product model before create hook and manage handle validation and transformation at the service level

* test: fix breaking unit tests

* fix: product module service to not update handle on product update

* fix define link and joiner config

* test: fix joiner config test

* test: fix joiner config test

* fix joiner config primary keys

* Fix joiner config builder

* Fix joiner config builder

* test: remove only modifier from test

* refactor: remove hooks usage from product collection

* refactor: remove hooks usage from product-option

* refactor: remove hooks usage for computing category handle

* refactor: remove hooks usage from productCategory model

* refactor: remove hooks from DML

* refactor: remove cruft

* order dml

* cleanup

* re add foerign key indexes

* wip

* chore: remove unused types

* wip

* changes

* rm raw

* autoincrement

* wip

* rel

* refactor: cleanup

* migration and models configuration adjustments

* cleanup

* number searchable

* fix random ordering

* fix

* test: fix product-category tests

* test: update breaking DML tests

* test: array assertion to not care about ordering

* fix: temporarily apply id ordering for products

* types

* wip

* WIP type improvements

* update order models

* partially fix types temporarely

* rel

* fix: recursive type issue

* improve type inference breaks

* improve type inference breaks

* update models

* rm nullable

* default value

* repository

* update default value handling

* fix unit tests

* WIP

* toMikroORM

* fix relations

* cascades

* fix

* experimental dml hooks

* rm migration

* serial

* nullable autoincrement

* fix model

* model changes

* fix one to one DML

* order test

* fix addresses

* fix unit tests

* Re align dml entity name inference

* update model table name config

* update model table name config

* revert

* update return relation

* WIP

* hasOne

* models

* fix

* model

* initial commit

* cart service

* order module

* utils unit test

* index engine

* changeset

* merge

* fix hasOne with fk

* update

* free text filter per entity

* tests

* prod category

* property string many to many

* fix big number

* link modules migration set names

* merge

* shipping option rules

* serializer

* unit test

* fix test mikro orm init

* fix test mikro orm init

* Maintain merge object properties

* fix test mikro orm init

* prevent unit test from connecting to db

* wip

* fix test

* fix test

* link test

* schema

* models

* auto increment

* hook

* model hooks

* order

* wip

* orm version

* request return field

* fix return configuration on order model

* workflows

* core flows

* unit test

* test

* base repo

* test

* base repo

* test fix

* inventory move

* locking inventory

* test

* free text fix

* rm timeout mock

* migrate fulfillment values

* v6.4.3

* cleanup

* link-modules update sql

* revert test

* remove fake timers

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
Co-authored-by: Harminder Virk <virk.officials@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Carlos R. L. Rodrigues
2025-01-21 08:04:47 -05:00
committed by GitHub
co-authored by adrien2p Harminder Virk Oli Juhl
parent 05c8a67d8e
commit cc73802ab3
178 changed files with 5731 additions and 7120 deletions
+87 -209
View File
@@ -1,214 +1,92 @@
import { BigNumberRawValue, DAL } from "@medusajs/framework/types"
import {
BigNumber,
DALUtils,
MikroOrmBigNumberProperty,
createPsqlIndexStatementHelper,
generateEntityId,
DmlEntity,
DMLEntitySchemaBuilder,
model,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import Claim from "./claim"
import Exchange from "./exchange"
import Order from "./order"
import Return from "./return"
import { OrderClaim } from "./claim"
import { OrderExchange } from "./exchange"
import { Order } from "./order"
import { Return } from "./return"
type OptionalLineItemProps = DAL.ModelDateColumns
const tableName = "order_transaction"
const ReferenceIdIndex = createPsqlIndexStatementHelper({
tableName,
columns: "reference_id",
where: "deleted_at IS NOT NULL",
})
const OrderIdIndex = createPsqlIndexStatementHelper({
tableName,
columns: "order_id",
where: "deleted_at IS NOT NULL",
})
const ReturnIdIndex = createPsqlIndexStatementHelper({
tableName,
columns: "return_id",
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const ExchangeIdIndex = createPsqlIndexStatementHelper({
tableName,
columns: ["exchange_id"],
where: "exchange_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const ClaimIdIndex = createPsqlIndexStatementHelper({
tableName,
columns: ["claim_id"],
where: "claim_id IS NOT NULL AND deleted_at IS NOT NULL",
})
const CurrencyCodeIndex = createPsqlIndexStatementHelper({
tableName,
columns: "currency_code",
where: "deleted_at IS NOT NULL",
})
const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName,
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
const OrderIdVersionIndex = createPsqlIndexStatementHelper({
tableName,
columns: ["order_id", "version"],
where: "deleted_at IS NOT NULL",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
@OrderIdVersionIndex.MikroORMIndex()
export default class OrderTransaction {
[OptionalProps]?: OptionalLineItemProps
@PrimaryKey({ columnType: "text" })
id: string
@ManyToOne({
entity: () => Order,
columnType: "text",
fieldName: "order_id",
onDelete: "cascade",
mapToPk: true,
const _OrderTransaction = model
.define("OrderTransaction", {
id: model.id({ prefix: "ordtrx" }).primaryKey(),
version: model.number().default(1),
amount: model.bigNumber(),
currency_code: model.text(),
reference: model.text().nullable(),
reference_id: model.text().nullable(),
order: model.belongsTo<() => typeof Order>(() => Order, {
mappedBy: "transactions",
}),
return: model
.belongsTo<() => typeof Return>(() => Return, {
mappedBy: "transactions",
})
.nullable(),
exchange: model
.belongsTo<() => typeof OrderExchange>(() => OrderExchange, {
mappedBy: "transactions",
})
.nullable(),
claim: model
.belongsTo<() => typeof OrderClaim>(() => OrderClaim, {
mappedBy: "transactions",
})
.nullable(),
})
@OrderIdIndex.MikroORMIndex()
order_id: string
.indexes([
{
name: "IDX_order_transaction_reference_id",
on: ["reference_id"],
unique: false,
where: "deleted_at IS NOT NULL",
},
{
name: "IDX_order_transaction_order_id",
on: ["order_id"],
unique: false,
where: "deleted_at IS NOT NULL",
},
{
name: "IDX_order_transaction_return_id",
on: ["return_id"],
unique: false,
where: "return_id IS NOT NULL AND deleted_at IS NOT NULL",
},
{
name: "IDX_order_transaction_exchange_id",
on: ["exchange_id"],
unique: false,
where: "exchange_id IS NOT NULL AND deleted_at IS NOT NULL",
},
{
name: "IDX_order_transaction_claim_id",
on: ["claim_id"],
unique: false,
where: "claim_id IS NOT NULL AND deleted_at IS NOT NULL",
},
{
name: "IDX_order_transaction_currency_code",
on: ["currency_code"],
unique: false,
where: "deleted_at IS NOT NULL",
},
{
name: "IDX_order_transaction_deleted_at",
on: ["deleted_at"],
unique: false,
where: "deleted_at IS NOT NULL",
},
{
name: "IDX_order_transaction_order_id_version",
on: ["order_id", "version"],
unique: false,
where: "deleted_at IS NOT NULL",
},
])
@ManyToOne(() => Order, {
persist: false,
})
order: Rel<Order>
@ManyToOne({
entity: () => Return,
mapToPk: true,
fieldName: "return_id",
columnType: "text",
nullable: true,
})
@ReturnIdIndex.MikroORMIndex()
return_id: string | null = null
@ManyToOne(() => Return, {
persist: false,
nullable: true,
})
return: Rel<Return>
@ManyToOne({
entity: () => Exchange,
mapToPk: true,
fieldName: "exchange_id",
columnType: "text",
nullable: true,
})
@ExchangeIdIndex.MikroORMIndex()
exchange_id: string | null
@ManyToOne(() => Exchange, {
persist: false,
nullable: true,
})
exchange: Rel<Exchange>
@ManyToOne({
entity: () => Claim,
mapToPk: true,
fieldName: "claim_id",
columnType: "text",
nullable: true,
})
@ClaimIdIndex.MikroORMIndex()
claim_id: string | null
@ManyToOne(() => Claim, {
persist: false,
nullable: true,
})
claim: Rel<Claim>
@Property({
columnType: "integer",
defaultRaw: "1",
})
version: number = 1
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@Property({ columnType: "text" })
@CurrencyCodeIndex.MikroORMIndex()
currency_code: string
@Property({
columnType: "text",
nullable: true,
})
reference: string | null = null
@Property({
columnType: "text",
nullable: true,
})
@ReferenceIdIndex.MikroORMIndex()
reference_id: string | null = null
@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 })
@DeletedAtIndex.MikroORMIndex()
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ordtrx")
this.order_id ??= this.order?.id
this.return_id ??= this.return?.id
this.claim_id ??= this.claim?.id
this.exchange_id ??= this.exchange?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ordtrx")
this.order_id ??= this.order?.id
this.return_id ??= this.return?.id
this.claim_id ??= this.claim?.id
this.exchange_id ??= this.exchange?.id
}
}
export const OrderTransaction = _OrderTransaction as DmlEntity<
DMLEntitySchemaBuilder<(typeof _OrderTransaction)["schema"]>,
"OrderTransaction"
>