chore(payment): Payment module DML (#10553)

* chore(payment): Payment module DML

* rm log

* migration
This commit is contained in:
Carlos R. L. Rodrigues
2024-12-11 13:09:10 -03:00
committed by GitHub
parent 91cd9aad47
commit 0264294ab5
14 changed files with 706 additions and 1039 deletions

View File

@@ -1,120 +1,37 @@
import { BigNumberRawValue } from "@medusajs/framework/types"
import {
BigNumber,
generateEntityId,
MikroOrmBigNumberProperty,
PaymentSessionStatus,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Enum,
ManyToOne,
OneToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model, PaymentSessionStatus } from "@medusajs/framework/utils"
import Payment from "./payment"
import PaymentCollection from "./payment-collection"
@Entity({ tableName: "payment_session" })
export default class PaymentSession {
[OptionalProps]?: "status" | "data"
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
currency_code: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({
columnType: "jsonb",
const PaymentSession = model
.define("PaymentSession", {
id: model.id({ prefix: "payses" }).primaryKey(),
currency_code: model.text(),
amount: model.bigNumber(),
provider_id: model.text(),
data: model.json().default({}),
context: model.json().nullable(),
status: model
.enum(PaymentSessionStatus)
.default(PaymentSessionStatus.PENDING),
authorized_at: model.dateTime().nullable(),
payment_collection: model.belongsTo<() => typeof PaymentCollection>(
() => PaymentCollection,
{
mappedBy: "payment_sessions",
}
),
payment: model
.hasOne(() => Payment, {
mappedBy: "payment_session",
})
.nullable(),
metadata: model.json().nullable(),
})
raw_amount: BigNumberRawValue
.indexes([
{
name: "IDX_payment_session_payment_collection_id",
on: ["payment_collection_id"],
},
])
@Property({ columnType: "text" })
provider_id: string
@Property({ columnType: "jsonb" })
data: Record<string, unknown> = {}
@Property({ columnType: "jsonb", nullable: true })
context: Record<string, unknown> | null
@Enum({
items: () => PaymentSessionStatus,
})
status: PaymentSessionStatus = PaymentSessionStatus.PENDING
@Property({
columnType: "timestamptz",
nullable: true,
})
authorized_at: Date | null = null
@ManyToOne(() => PaymentCollection, {
persist: false,
})
payment_collection: Rel<PaymentCollection>
@ManyToOne({
entity: () => PaymentCollection,
columnType: "text",
index: "IDX_payment_session_payment_collection_id",
fieldName: "payment_collection_id",
mapToPk: true,
})
payment_collection_id: string
@OneToOne({
entity: () => Payment,
nullable: true,
mappedBy: "payment_session",
})
payment?: Rel<Payment> | null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | 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,
index: "IDX_payment_session_deleted_at",
})
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "payses")
this.payment_collection_id ??=
this.payment_collection_id ?? this.payment_collection?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "payses")
this.payment_collection_id ??=
this.payment_collection_id ?? this.payment_collection?.id
}
}
export default PaymentSession