From 89d6d7e846e4958976443d77ab5e52703365c7a7 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 15 Jan 2024 18:04:39 +0100 Subject: [PATCH 1/4] fix: add order_edit_id to Payment --- packages/payment/src/models/payment.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index 15941df136..0f1b5071a0 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -63,6 +63,9 @@ export default class Payment { @Property({ columnType: "text", nullable: true }) order_id: string | null + @Property({ columnType: "text", nullable: true }) + order_edit_id: string | null + @Property({ columnType: "text", nullable: true }) customer_id: string | null From 22254a22c04330ccb6b7623ee986221ae97f3de8 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 10:30:52 +0100 Subject: [PATCH 2/4] fix: address feedback partial --- packages/payment/src/models/capture.ts | 2 +- .../payment/src/models/payment-collection.ts | 37 ++++--------------- .../payment/src/models/payment-session.ts | 30 +-------------- packages/payment/src/models/refund.ts | 2 +- packages/utils/src/index.ts | 1 + packages/utils/src/payment/index.ts | 2 + .../utils/src/payment/payment-collection.ts | 27 ++++++++++++++ packages/utils/src/payment/payment-session.ts | 27 ++++++++++++++ 8 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 packages/utils/src/payment/index.ts create mode 100644 packages/utils/src/payment/payment-collection.ts create mode 100644 packages/utils/src/payment/payment-session.ts diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts index 9952342db1..f180fa21d5 100644 --- a/packages/payment/src/models/capture.ts +++ b/packages/payment/src/models/capture.ts @@ -11,7 +11,7 @@ import { import { generateEntityId } from "@medusajs/utils" import Payment from "./payment" -type OptionalCaptureProps = "created_by" | "created_at" | "completed_at" +type OptionalCaptureProps = "created_by" | "created_at" @Entity({ tableName: "capture" }) export default class Capture { diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index e844224381..9b5985fdbf 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -14,44 +14,21 @@ import { } from "@mikro-orm/core" import { DAL } from "@medusajs/types" -import { DALUtils, generateEntityId } from "@medusajs/utils" +import { + DALUtils, + generateEntityId, + PaymentCollectionStatus, +} from "@medusajs/utils" import PaymentProvider from "./payment-provider" import PaymentSession from "./payment-session" import Payment from "./payment" -/** - * @enum - * - * The payment collection's status. - */ -export enum PaymentCollectionStatus { - /** - * The payment collection isn't paid. - */ - NOT_PAID = "not_paid", - /** - * The payment collection is awaiting payment. - */ - AWAITING = "awaiting", - /** - * The payment collection is authorized. - */ - AUTHORIZED = "authorized", - /** - * Some of the payments in the payment collection are authorized. - */ - PARTIALLY_AUTHORIZED = "partially_authorized", - /** - * The payment collection is canceled. - */ - CANCELED = "canceled", -} - type OptionalPaymentCollectionProps = | "authorized_amount" | "refunded_amount" | "region_id" | "completed_at" + | "status" | DAL.SoftDeletableEntityDateColumns @Entity({ tableName: "payment_collection" }) @@ -120,7 +97,7 @@ export default class PaymentCollection { items: () => PaymentCollectionStatus, default: PaymentCollectionStatus.NOT_PAID, }) - status: PaymentCollectionStatus = PaymentCollectionStatus.NOT_PAID + status?: PaymentCollectionStatus = PaymentCollectionStatus.NOT_PAID @ManyToMany(() => PaymentProvider) payment_providers = new Collection(this) diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index ca0a4e8f5f..387a702490 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -10,39 +10,11 @@ import { Property, } from "@mikro-orm/core" import { DAL } from "@medusajs/types" -import { generateEntityId } from "@medusajs/utils" +import { generateEntityId, PaymentSessionStatus } from "@medusajs/utils" import PaymentCollection from "./payment-collection" import Payment from "./payment" -/** - * @enum - * - * The status of a payment session. - */ -export enum PaymentSessionStatus { - /** - * The payment is authorized. - */ - AUTHORIZED = "authorized", - /** - * The payment is pending. - */ - PENDING = "pending", - /** - * The payment requires an action. - */ - REQUIRES_MORE = "requires_more", - /** - * An error occurred while processing the payment. - */ - ERROR = "error", - /** - * The payment is canceled. - */ - CANCELED = "canceled", -} - type OptionalPaymentSessionProps = | "data" | "is_selected" diff --git a/packages/payment/src/models/refund.ts b/packages/payment/src/models/refund.ts index 6da06d2c16..59cda86f3a 100644 --- a/packages/payment/src/models/refund.ts +++ b/packages/payment/src/models/refund.ts @@ -11,7 +11,7 @@ import { import { generateEntityId } from "@medusajs/utils" import Payment from "./payment" -type OptionalRefundProps = "created_by" | "completed_at" +type OptionalRefundProps = "created_by" @Entity({ tableName: "refund" }) export default class Refund { diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 7674050a26..5f96dcb3dd 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -5,6 +5,7 @@ export * from "./decorators" export * from "./event-bus" export * from "./feature-flags" export * from "./modules-sdk" +export * from "./payment" export * from "./pricing" export * from "./product" export * from "./promotion" diff --git a/packages/utils/src/payment/index.ts b/packages/utils/src/payment/index.ts new file mode 100644 index 0000000000..e5e483a8d5 --- /dev/null +++ b/packages/utils/src/payment/index.ts @@ -0,0 +1,2 @@ +export * from "./payment-collection" +export * from "./payment-session" diff --git a/packages/utils/src/payment/payment-collection.ts b/packages/utils/src/payment/payment-collection.ts new file mode 100644 index 0000000000..600445705f --- /dev/null +++ b/packages/utils/src/payment/payment-collection.ts @@ -0,0 +1,27 @@ +/** + * @enum + * + * The payment collection's status. + */ +export enum PaymentCollectionStatus { + /** + * The payment collection isn't paid. + */ + NOT_PAID = "not_paid", + /** + * The payment collection is awaiting payment. + */ + AWAITING = "awaiting", + /** + * The payment collection is authorized. + */ + AUTHORIZED = "authorized", + /** + * Some of the payments in the payment collection are authorized. + */ + PARTIALLY_AUTHORIZED = "partially_authorized", + /** + * The payment collection is canceled. + */ + CANCELED = "canceled", +} diff --git a/packages/utils/src/payment/payment-session.ts b/packages/utils/src/payment/payment-session.ts new file mode 100644 index 0000000000..d65005fc92 --- /dev/null +++ b/packages/utils/src/payment/payment-session.ts @@ -0,0 +1,27 @@ +/** + * @enum + * + * The status of a payment session. + */ +export enum PaymentSessionStatus { + /** + * The payment is authorized. + */ + AUTHORIZED = "authorized", + /** + * The payment is pending. + */ + PENDING = "pending", + /** + * The payment requires an action. + */ + REQUIRES_MORE = "requires_more", + /** + * An error occurred while processing the payment. + */ + ERROR = "error", + /** + * The payment is canceled. + */ + CANCELED = "canceled", +} From bc7829db3c1f289622d2120cc9eb1e372c6902d8 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 11:58:14 +0100 Subject: [PATCH 3/4] fix: update models --- packages/payment/src/models/capture.ts | 4 +-- .../payment/src/models/payment-collection.ts | 18 ++++-------- .../src/models/payment-method-token.ts | 13 ++------- .../payment/src/models/payment-provider.ts | 4 ++- .../payment/src/models/payment-session.ts | 14 ++-------- packages/payment/src/models/payment.ts | 28 +++++++------------ packages/payment/src/models/refund.ts | 7 +---- 7 files changed, 26 insertions(+), 62 deletions(-) diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts index f180fa21d5..259a7dbec3 100644 --- a/packages/payment/src/models/capture.ts +++ b/packages/payment/src/models/capture.ts @@ -11,7 +11,7 @@ import { import { generateEntityId } from "@medusajs/utils" import Payment from "./payment" -type OptionalCaptureProps = "created_by" | "created_at" +type OptionalCaptureProps = "created_at" @Entity({ tableName: "capture" }) export default class Capture { @@ -41,7 +41,7 @@ export default class Capture { created_at: Date @Property({ columnType: "text", nullable: true }) - created_by: string | null + created_by?: string | null @BeforeCreate() onCreate() { diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index 9b5985fdbf..411ba03220 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -23,13 +23,7 @@ import PaymentProvider from "./payment-provider" import PaymentSession from "./payment-session" import Payment from "./payment" -type OptionalPaymentCollectionProps = - | "authorized_amount" - | "refunded_amount" - | "region_id" - | "completed_at" - | "status" - | DAL.SoftDeletableEntityDateColumns +type OptionalPaymentCollectionProps = "status" | DAL.EntityDateColumns @Entity({ tableName: "payment_collection" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) @@ -53,14 +47,14 @@ export default class PaymentCollection { nullable: true, serializer: Number, }) - authorized_amount: number | null + authorized_amount?: number | null @Property({ columnType: "numeric", nullable: true, serializer: Number, }) - refunded_amount: number | null + refunded_amount?: number | null @Property({ columnType: "text", nullable: true }) region_id?: string | null @@ -85,19 +79,19 @@ export default class PaymentCollection { nullable: true, index: "IDX_payment_collection_deleted_at", }) - deleted_at: Date | null + deleted_at?: Date | null @Property({ columnType: "timestamptz", nullable: true, }) - completed_at: Date | null + completed_at?: Date | null @Enum({ items: () => PaymentCollectionStatus, default: PaymentCollectionStatus.NOT_PAID, }) - status?: PaymentCollectionStatus = PaymentCollectionStatus.NOT_PAID + status: PaymentCollectionStatus = PaymentCollectionStatus.NOT_PAID @ManyToMany(() => PaymentProvider) payment_providers = new Collection(this) diff --git a/packages/payment/src/models/payment-method-token.ts b/packages/payment/src/models/payment-method-token.ts index 43dd4bbec5..69956f8855 100644 --- a/packages/payment/src/models/payment-method-token.ts +++ b/packages/payment/src/models/payment-method-token.ts @@ -2,23 +2,14 @@ import { BeforeCreate, Entity, OnInit, - OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" import { generateEntityId } from "@medusajs/utils" -type OptionalPaymentMethodTokenProps = - | "data" - | "type_detail" - | "description_detail" - | "metadata" - @Entity({ tableName: "payment_method_token" }) export default class PaymentMethodToken { - [OptionalProps]?: OptionalPaymentMethodTokenProps - @PrimaryKey({ columnType: "text" }) id: string @@ -32,10 +23,10 @@ export default class PaymentMethodToken { name: string @Property({ columnType: "text", nullable: true }) - type_detail: string | null + type_detail?: string | null @Property({ columnType: "text", nullable: true }) - description_detail: string | null + description_detail?: string | null @Property({ columnType: "jsonb", nullable: true }) metadata?: Record | null diff --git a/packages/payment/src/models/payment-provider.ts b/packages/payment/src/models/payment-provider.ts index d5ff3c03f7..b4ed62003b 100644 --- a/packages/payment/src/models/payment-provider.ts +++ b/packages/payment/src/models/payment-provider.ts @@ -1,7 +1,9 @@ -import { Entity, PrimaryKey, Property } from "@mikro-orm/core" +import { Entity, OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" @Entity({ tableName: "payment_provider" }) export default class PaymentProvider { + [OptionalProps]?: "is_enabled" + @PrimaryKey({ columnType: "text" }) id: string diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index 387a702490..44940dc695 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -5,26 +5,16 @@ import { ManyToOne, OneToOne, OnInit, - OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" -import { DAL } from "@medusajs/types" import { generateEntityId, PaymentSessionStatus } from "@medusajs/utils" import PaymentCollection from "./payment-collection" import Payment from "./payment" -type OptionalPaymentSessionProps = - | "data" - | "is_selected" - | "authorized_at" - | DAL.EntityDateColumns - @Entity({ tableName: "payment_session" }) export default class PaymentSession { - [OptionalProps]?: OptionalPaymentSessionProps - @PrimaryKey({ columnType: "text" }) id: string @@ -49,13 +39,13 @@ export default class PaymentSession { status: PaymentSessionStatus @Property({ columnType: "boolean", nullable: true }) - is_selected: boolean | null + is_selected?: boolean | null @Property({ columnType: "timestamptz", nullable: true, }) - authorized_at: Date | null + authorized_at?: Date | null @ManyToOne({ index: "IDX_payment_session_payment_collection_id", diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index 0f1b5071a0..c5af36d9c2 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -20,15 +20,7 @@ import Capture from "./capture" import PaymentSession from "./payment-session" import PaymentCollection from "./payment-collection" -type OptionalPaymentProps = - | "authorized_amount" - | "cart_id" - | "order_id" - | "customer_id" - | "data" - | "captured_at" - | "canceled_at" - | DAL.SoftDeletableEntityDateColumns +type OptionalPaymentProps = DAL.EntityDateColumns @Entity({ tableName: "payment" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) @@ -49,7 +41,7 @@ export default class Payment { nullable: true, serializer: Number, }) - authorized_amount: number | null + authorized_amount?: number | null @Property({ columnType: "text" }) currency_code: string @@ -58,16 +50,16 @@ export default class Payment { provider_id: string @Property({ columnType: "text", nullable: true }) - cart_id: string | null + cart_id?: string | null @Property({ columnType: "text", nullable: true }) - order_id: string | null + order_id?: string | null @Property({ columnType: "text", nullable: true }) - order_edit_id: string | null + order_edit_id?: string | null @Property({ columnType: "text", nullable: true }) - customer_id: string | null + customer_id?: string | null @Property({ columnType: "jsonb", nullable: true }) data?: Record | null @@ -92,19 +84,19 @@ export default class Payment { nullable: true, index: "IDX_payment_deleted_at", }) - deleted_at: Date | null + deleted_at?: Date | null @Property({ columnType: "timestamptz", nullable: true, }) - captured_at: Date | null + captured_at?: Date | null @Property({ columnType: "timestamptz", nullable: true, }) - canceled_at: Date | null + canceled_at?: Date | null @OneToMany(() => Refund, (refund) => refund.payment, { cascade: [Cascade.REMOVE], @@ -120,7 +112,7 @@ export default class Payment { index: "IDX_payment_payment_collection_id", fieldName: "payment_collection_id", }) - payment_collection!: PaymentCollection + payment_collection: PaymentCollection @OneToOne({ owner: true, fieldName: "session_id" }) session: PaymentSession diff --git a/packages/payment/src/models/refund.ts b/packages/payment/src/models/refund.ts index 59cda86f3a..ca071ac514 100644 --- a/packages/payment/src/models/refund.ts +++ b/packages/payment/src/models/refund.ts @@ -3,7 +3,6 @@ import { Entity, ManyToOne, OnInit, - OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" @@ -11,12 +10,8 @@ import { import { generateEntityId } from "@medusajs/utils" import Payment from "./payment" -type OptionalRefundProps = "created_by" - @Entity({ tableName: "refund" }) export default class Refund { - [OptionalProps]?: OptionalRefundProps - @PrimaryKey({ columnType: "text" }) id: string @@ -41,7 +36,7 @@ export default class Refund { created_at: Date @Property({ columnType: "text", nullable: true }) - created_by: string | null + created_by?: string | null @BeforeCreate() onCreate() { From eab0ca4b9f98b40602d345be6a71de9f17867289 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 12:19:03 +0100 Subject: [PATCH 4/4] fix: serializer --- packages/payment/src/models/payment-collection.ts | 5 +++-- packages/payment/src/models/payment-method-token.ts | 4 ++-- packages/payment/src/models/payment.ts | 8 ++++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index 411ba03220..347912a9c7 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -17,6 +17,7 @@ import { DAL } from "@medusajs/types" import { DALUtils, generateEntityId, + optionalNumericSerializer, PaymentCollectionStatus, } from "@medusajs/utils" import PaymentProvider from "./payment-provider" @@ -45,14 +46,14 @@ export default class PaymentCollection { @Property({ columnType: "numeric", nullable: true, - serializer: Number, + serializer: optionalNumericSerializer, }) authorized_amount?: number | null @Property({ columnType: "numeric", nullable: true, - serializer: Number, + serializer: optionalNumericSerializer, }) refunded_amount?: number | null diff --git a/packages/payment/src/models/payment-method-token.ts b/packages/payment/src/models/payment-method-token.ts index 69956f8855..a1f284524d 100644 --- a/packages/payment/src/models/payment-method-token.ts +++ b/packages/payment/src/models/payment-method-token.ts @@ -33,11 +33,11 @@ export default class PaymentMethodToken { @BeforeCreate() onCreate() { - this.id = generateEntityId(this.id, "paymt") + this.id = generateEntityId(this.id, "paymttok") } @OnInit() onInit() { - this.id = generateEntityId(this.id, "paymt") + this.id = generateEntityId(this.id, "paymttok") } } diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index c5af36d9c2..15b6e5185a 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -14,7 +14,11 @@ import { } from "@mikro-orm/core" import { DAL } from "@medusajs/types" -import { DALUtils, generateEntityId } from "@medusajs/utils" +import { + DALUtils, + generateEntityId, + optionalNumericSerializer, +} from "@medusajs/utils" import Refund from "./refund" import Capture from "./capture" import PaymentSession from "./payment-session" @@ -39,7 +43,7 @@ export default class Payment { @Property({ columnType: "numeric", nullable: true, - serializer: Number, + serializer: optionalNumericSerializer, }) authorized_amount?: number | null