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,77 +1,21 @@
import { BigNumberRawValue } from "@medusajs/framework/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import Payment from "./payment"
type OptionalCaptureProps = "created_at"
@Entity({ tableName: "capture" })
export default class Capture {
[OptionalProps]?: OptionalCaptureProps
@PrimaryKey({ columnType: "text" })
id: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@ManyToOne(() => Payment, {
onDelete: "cascade",
index: "IDX_capture_payment_id",
fieldName: "payment_id",
const Capture = model
.define("Capture", {
id: model.id({ prefix: "capt" }).primaryKey(),
amount: model.bigNumber(),
payment: model.belongsTo(() => Payment, {
mappedBy: "captures",
}),
metadata: model.json().nullable(),
created_by: model.text().nullable(),
})
payment!: Payment
.indexes([
{
name: "IDX_capture_payment_id",
on: ["payment_id"],
},
])
@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_capture_deleted_at",
})
deleted_at: Date | null = null
@Property({ columnType: "text", nullable: true })
created_by: string | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "capt")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "capt")
}
}
export default Capture

View File

@@ -1,127 +1,40 @@
import { BigNumberRawValue, DAL } from "@medusajs/framework/types"
import {
BigNumber,
DALUtils,
MikroOrmBigNumberProperty,
PaymentCollectionStatus,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Enum,
Filter,
ManyToMany,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model, PaymentCollectionStatus } from "@medusajs/framework/utils"
import Payment from "./payment"
import PaymentProvider from "./payment-provider"
import PaymentSession from "./payment-session"
type OptionalPaymentCollectionProps = "status" | DAL.ModelDateColumns
@Entity({ tableName: "payment_collection" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class PaymentCollection {
[OptionalProps]?: OptionalPaymentCollectionProps
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
currency_code: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@MikroOrmBigNumberProperty({ nullable: true })
authorized_amount: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_authorized_amount: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty({ nullable: true })
captured_amount: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_captured_amount: BigNumberRawValue | null = null
@MikroOrmBigNumberProperty({ nullable: true })
refunded_amount: BigNumber | number | null = null
@Property({ columnType: "jsonb", nullable: true })
raw_refunded_amount: BigNumberRawValue | null = null
@Property({ columnType: "text", index: "IDX_payment_collection_region_id" })
region_id: string
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
const PaymentCollection = model
.define("PaymentCollection", {
id: model.id({ prefix: "pay_col" }).primaryKey(),
currency_code: model.text(),
amount: model.bigNumber(),
authorized_amount: model.bigNumber().nullable(),
captured_amount: model.bigNumber().nullable(),
refunded_amount: model.bigNumber().nullable(),
region_id: model.text(),
completed_at: model.dateTime().nullable(),
status: model
.enum(PaymentCollectionStatus)
.default(PaymentCollectionStatus.NOT_PAID),
metadata: model.json().nullable(),
payment_providers: model.manyToMany(() => PaymentProvider, {
mappedBy: "payment_collections",
}),
payment_sessions: model.hasMany(() => PaymentSession, {
mappedBy: "payment_collection",
}),
payments: model.hasMany(() => Payment, {
mappedBy: "payment_collection",
}),
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["payment_sessions", "payments"],
})
updated_at: Date
.indexes([
{
name: "IDX_payment_collection_region_id",
on: ["region_id"],
},
])
@Property({
columnType: "timestamptz",
nullable: true,
index: "IDX_payment_collection_deleted_at",
})
deleted_at: Date | null = null
@Property({
columnType: "timestamptz",
nullable: true,
})
completed_at: Date | null = null
@Enum({
items: () => PaymentCollectionStatus,
default: PaymentCollectionStatus.NOT_PAID,
})
status: PaymentCollectionStatus = PaymentCollectionStatus.NOT_PAID
@ManyToMany(() => PaymentProvider)
payment_providers = new Collection<Rel<PaymentProvider>>(this)
@OneToMany(() => PaymentSession, (ps) => ps.payment_collection, {
cascade: [Cascade.PERSIST] as any,
})
payment_sessions = new Collection<Rel<PaymentSession>>(this)
@OneToMany(() => Payment, (payment) => payment.payment_collection, {
cascade: [Cascade.PERSIST] as any,
})
payments = new Collection<Rel<Payment>>(this)
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "pay_col")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "pay_col")
}
}
export default PaymentCollection

View File

@@ -1,64 +1,13 @@
import { generateEntityId } from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
@Entity({ tableName: "payment_method_token" })
export default class PaymentMethodToken {
@PrimaryKey({ columnType: "text" })
id: string
const PaymentMethodToken = model.define("PaymentMethodToken", {
id: model.id({ prefix: "paymttok" }).primaryKey(),
provider_id: model.text(),
data: model.json().nullable(),
name: model.text(),
type_detail: model.text().nullable(),
description_detail: model.text().nullable(),
metadata: model.json().nullable(),
})
@Property({ columnType: "text" })
provider_id: string
@Property({ columnType: "jsonb", nullable: true })
data: Record<string, unknown> | null = null
@Property({ columnType: "text" })
name: string
@Property({ columnType: "text", nullable: true })
type_detail: string | null = null
@Property({ columnType: "text", nullable: true })
description_detail: 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,
index: "IDX_payment_method_token_deleted_at",
})
deleted_at: Date | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "paymttok")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "paymttok")
}
}
export default PaymentMethodToken

View File

@@ -1,15 +1,12 @@
import { Entity, OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import PaymentCollection from "./payment-collection"
@Entity({ tableName: "payment_provider" })
export default class PaymentProvider {
[OptionalProps]?: "is_enabled"
const PaymentProvider = model.define("PaymentProvider", {
id: model.id().primaryKey(),
is_enabled: model.boolean().default(true),
payment_collections: model.manyToMany(() => PaymentCollection, {
mappedBy: "payment_providers",
}),
})
@PrimaryKey({ columnType: "text" })
id: string
@Property({
default: true,
columnType: "boolean",
})
is_enabled: boolean = true
}
export default PaymentProvider

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

View File

@@ -1,157 +1,51 @@
import { BigNumberRawValue, DAL } from "@medusajs/framework/types"
import {
BigNumber,
DALUtils,
MikroOrmBigNumberProperty,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Cascade,
Collection,
Entity,
Filter,
ManyToOne,
OnInit,
OneToMany,
OneToOne,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import Capture from "./capture"
import PaymentCollection from "./payment-collection"
import PaymentSession from "./payment-session"
import Refund from "./refund"
type OptionalPaymentProps = DAL.ModelDateColumns
const tableName = "payment"
const ProviderIdIndex = createPsqlIndexStatementHelper({
tableName,
columns: "provider_id",
})
@Entity({ tableName })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Payment {
[OptionalProps]?: OptionalPaymentProps
@PrimaryKey({ columnType: "text" })
id: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@Property({ columnType: "text" })
currency_code: string
@Property({ columnType: "text" })
@ProviderIdIndex.MikroORMIndex()
provider_id: string
@Searchable()
@Property({ columnType: "text", nullable: true })
cart_id: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true })
order_id: string | null = null
@Searchable()
@Property({ columnType: "text", nullable: true })
customer_id: string | null = null
@Property({ columnType: "jsonb", nullable: true })
data: Record<string, unknown> | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
const Payment = model
.define("Payment", {
id: model.id({ prefix: "pay" }).primaryKey(),
amount: model.bigNumber(),
currency_code: model.text(),
provider_id: model.text(),
cart_id: model.text().searchable().nullable(),
order_id: model.text().searchable().nullable(),
customer_id: model.text().searchable().nullable(),
data: model.json().nullable(),
metadata: model.json().nullable(),
captured_at: model.dateTime().nullable(),
canceled_at: model.dateTime().nullable(),
payment_collection: model.belongsTo(() => PaymentCollection, {
mappedBy: "payments",
}),
payment_session: model.belongsTo(() => PaymentSession, {
mappedBy: "payment",
}),
refunds: model.hasMany(() => Refund, {
mappedBy: "payment",
}),
captures: model.hasMany(() => Capture, {
mappedBy: "payment",
}),
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["refunds", "captures"],
})
updated_at: Date
.indexes([
{
name: "IDX_payment_provider_id",
on: ["provider_id"],
},
{
name: "IDX_payment_payment_collection_id",
on: ["payment_collection_id"],
},
{
name: "IDX_payment_payment_session_id",
on: ["payment_session_id"],
},
])
@Property({
columnType: "timestamptz",
nullable: true,
index: "IDX_payment_deleted_at",
})
deleted_at: Date | null = null
@Property({
columnType: "timestamptz",
nullable: true,
})
captured_at: Date | null = null
@Property({
columnType: "timestamptz",
nullable: true,
})
canceled_at: Date | null = null
@OneToMany(() => Refund, (refund) => refund.payment, {
cascade: [Cascade.REMOVE],
})
refunds = new Collection<Rel<Refund>>(this)
@OneToMany(() => Capture, (capture) => capture.payment, {
cascade: [Cascade.REMOVE],
})
captures = new Collection<Rel<Capture>>(this)
@ManyToOne({
entity: () => PaymentCollection,
persist: false,
})
payment_collection: Rel<PaymentCollection>
@ManyToOne({
entity: () => PaymentCollection,
columnType: "text",
index: "IDX_payment_payment_collection_id",
fieldName: "payment_collection_id",
mapToPk: true,
})
payment_collection_id: string
@OneToOne({
entity: () => PaymentSession,
owner: true,
fieldName: "payment_session_id",
index: "IDX_payment_payment_session_id",
})
payment_session: Rel<PaymentSession>
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "pay")
this.payment_collection_id ??=
this.payment_collection_id ?? this.payment_collection?.id
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "pay")
this.payment_collection_id ??=
this.payment_collection_id ?? this.payment_collection?.id
}
}
export default Payment

View File

@@ -1,58 +1,14 @@
import {
DALUtils,
generateEntityId,
Searchable,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import Refund from "./refund"
@Entity({ tableName: "refund_reason" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class RefundReason {
@PrimaryKey({ columnType: "text" })
id: string
const RefundReason = model.define("RefundReason", {
id: model.id({ prefix: "refr" }).primaryKey(),
label: model.text().searchable(),
description: model.text().nullable(),
metadata: model.json().nullable(),
refunds: model.hasMany(() => Refund, {
mappedBy: "refund_reason",
}),
})
@Searchable()
@Property({ columnType: "text" })
label: string
@Property({ columnType: "text", nullable: true })
description: string | null = 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 })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "refr")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "refr")
}
}
export default RefundReason

View File

@@ -1,101 +1,28 @@
import { BigNumberRawValue, DAL } from "@medusajs/framework/types"
import {
BigNumber,
MikroOrmBigNumberProperty,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
OptionalProps,
PrimaryKey,
Property,
Rel,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import Payment from "./payment"
import RefundReason from "./refund-reason"
type OptionalProps =
| "note"
| "refund_reason_id"
| "refund_reason"
| DAL.ModelDateColumns
@Entity({ tableName: "refund" })
export default class Refund {
[OptionalProps]?: OptionalProps
@PrimaryKey({ columnType: "text" })
id: string
@MikroOrmBigNumberProperty()
amount: BigNumber | number
@Property({ columnType: "jsonb" })
raw_amount: BigNumberRawValue
@ManyToOne(() => Payment, {
onDelete: "cascade",
index: "IDX_refund_payment_id",
fieldName: "payment_id",
const Refund = model
.define("Refund", {
id: model.id({ prefix: "ref" }).primaryKey(),
amount: model.bigNumber(),
payment: model.belongsTo(() => Payment, {
mappedBy: "refunds",
}),
refund_reason: model
.belongsTo(() => RefundReason, {
mappedBy: "refunds",
})
.nullable(),
note: model.text().nullable(),
created_by: model.text().nullable(),
metadata: model.json().nullable(),
})
payment!: Rel<Payment>
.indexes([
{
name: "IDX_refund_payment_id",
on: ["payment_id"],
},
])
@Property({ columnType: "text", nullable: true })
payment_id: string
@ManyToOne(() => RefundReason, {
columnType: "text",
mapToPk: true,
fieldName: "refund_reason_id",
nullable: true,
})
refund_reason_id: string | null = null
@ManyToOne(() => RefundReason, { persist: false, nullable: true })
refund_reason: Rel<RefundReason> | null = null
@Property({ columnType: "text", nullable: true })
note: string | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Property({
columnType: "timestamptz",
nullable: true,
index: "IDX_refund_deleted_at",
})
deleted_at: Date | null = null
@Property({ columnType: "text", nullable: true })
created_by: string | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "ref")
this.refund_reason_id ??= this.refund_reason?.id || null
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "ref")
this.refund_reason_id ??= this.refund_reason?.id || null
}
}
export default Refund