diff --git a/packages/payment/src/migrations/.snapshot-medusa-payment.json b/packages/payment/src/migrations/.snapshot-medusa-payment.json index df741b1f06..9a30897f1a 100644 --- a/packages/payment/src/migrations/.snapshot-medusa-payment.json +++ b/packages/payment/src/migrations/.snapshot-medusa-payment.json @@ -156,6 +156,15 @@ "nullable": false, "mappedType": "text" }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, "data": { "name": "data", "type": "jsonb", @@ -167,30 +176,30 @@ }, "name": { "name": "name", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "string" + "mappedType": "text" }, "type_detail": { "name": "type_detail", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, - "mappedType": "string" + "nullable": true, + "mappedType": "text" }, "description_detail": { "name": "description_detail", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, - "mappedType": "string" + "nullable": true, + "mappedType": "text" }, "metadata": { "name": "metadata", @@ -200,15 +209,6 @@ "primary": false, "nullable": true, "mappedType": "json" - }, - "provider_id": { - "name": "provider_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" } }, "name": "payment-method-token", @@ -395,12 +395,12 @@ }, "is_selected": { "name": "is_selected", - "type": "varchar(255)", + "type": "boolean", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "string" + "mappedType": "boolean" }, "authorised_at": { "name": "authorised_at", @@ -480,50 +480,50 @@ "nullable": true, "mappedType": "decimal" }, - "provider_id": { - "name": "provider_id", - "type": "varchar(255)", + "currency_code": { + "name": "currency_code", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "string" + "mappedType": "text" }, - "currency_code": { - "name": "currency_code", - "type": "varchar(255)", + "provider_id": { + "name": "provider_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": true, - "mappedType": "string" + "nullable": false, + "mappedType": "text" }, "cart_id": { "name": "cart_id", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "string" + "mappedType": "text" }, "order_id": { "name": "order_id", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "string" + "mappedType": "text" }, "customer_id": { "name": "customer_id", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "string" + "mappedType": "text" }, "data": { "name": "data", @@ -706,12 +706,12 @@ }, "created_by": { "name": "created_by", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "string" + "mappedType": "text" } }, "name": "capture", @@ -786,12 +786,12 @@ }, "created_by": { "name": "created_by", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "string" + "mappedType": "text" } }, "name": "refund", diff --git a/packages/payment/src/migrations/Migration20240111173302.ts b/packages/payment/src/migrations/Migration20240112104455.ts similarity index 74% rename from packages/payment/src/migrations/Migration20240111173302.ts rename to packages/payment/src/migrations/Migration20240112104455.ts index 14c2f7b6f1..0f42c7fe69 100644 --- a/packages/payment/src/migrations/Migration20240111173302.ts +++ b/packages/payment/src/migrations/Migration20240112104455.ts @@ -1,6 +1,6 @@ import { Migration } from "@mikro-orm/migrations" -export class Migration20240111173302 extends Migration { +export class Migration20240112104455 extends Migration { async up(): Promise { this.addSql( 'create table if not exists "payment-collection" ("id" text not null, "currency_code" text null, "amount" numeric not null, "authorized_amount" numeric null, "refunded_amount" numeric null, "region_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "completed_at" timestamptz null, "status" text check ("status" in (\'not_paid\', \'awaiting\', \'authorized\', \'partially_authorized\', \'canceled\')) not null default \'not_paid\', constraint "payment-collection_pkey" primary key ("id"));' @@ -10,7 +10,7 @@ export class Migration20240111173302 extends Migration { ) this.addSql( - 'create table if not exists "payment-method-token" ("id" text not null, "data" jsonb null, "name" varchar(255) not null, "type_detail" varchar(255) not null, "description_detail" varchar(255) not null, "metadata" jsonb null, "provider_id" text not null, constraint "payment-method-token_pkey" primary key ("id"));' + 'create table if not exists "payment-method-token" ("id" text not null, "provider_id" text not null, "data" jsonb null, "name" text not null, "type_detail" text null, "description_detail" text null, "metadata" jsonb null, constraint "payment-method-token_pkey" primary key ("id"));' ) this.addSql( @@ -22,11 +22,11 @@ export class Migration20240111173302 extends Migration { ) this.addSql( - 'create table if not exists "payment-session" ("id" text not null, "currency_code" text null, "amount" numeric not null, "provider_id" text not null, "data" jsonb null, "status" text check ("status" in (\'authorized\', \'pending\', \'requires_more\', \'error\', \'canceled\')) not null, "is_selected" varchar(255) null, "authorised_at" timestamptz null, "payment_collection_id" text not null, constraint "payment-session_pkey" primary key ("id"));' + 'create table if not exists "payment-session" ("id" text not null, "currency_code" text null, "amount" numeric not null, "provider_id" text not null, "data" jsonb null, "status" text check ("status" in (\'authorized\', \'pending\', \'requires_more\', \'error\', \'canceled\')) not null, "is_selected" boolean null, "authorised_at" timestamptz null, "payment_collection_id" text not null, constraint "payment-session_pkey" primary key ("id"));' ) this.addSql( - 'create table if not exists "payment" ("id" text not null, "amount" numeric not null, "authorized_amount" numeric null, "provider_id" varchar(255) not null, "currency_code" varchar(255) null, "cart_id" varchar(255) null, "order_id" varchar(255) null, "customer_id" varchar(255) null, "data" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "captured_at" timestamptz null, "canceled_at" timestamptz null, "payment_collection_id" text not null, "session_id" text not null, constraint "payment_pkey" primary key ("id"));' + 'create table if not exists "payment" ("id" text not null, "amount" numeric not null, "authorized_amount" numeric null, "currency_code" text not null, "provider_id" text not null, "cart_id" text null, "order_id" text null, "customer_id" text null, "data" jsonb null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, "captured_at" timestamptz null, "canceled_at" timestamptz null, "payment_collection_id" text not null, "session_id" text not null, constraint "payment_pkey" primary key ("id"));' ) this.addSql( 'create index "IDX_payment_deleted_at" on "payment" ("deleted_at");' @@ -36,11 +36,11 @@ export class Migration20240111173302 extends Migration { ) this.addSql( - 'create table if not exists "capture" ("id" text not null, "amount" numeric not null, "payment_id" text not null, "created_at" timestamptz not null default now(), "created_by" varchar(255) null, constraint "capture_pkey" primary key ("id"));' + 'create table if not exists "capture" ("id" text not null, "amount" numeric not null, "payment_id" text not null, "created_at" timestamptz not null default now(), "created_by" text null, constraint "capture_pkey" primary key ("id"));' ) this.addSql( - 'create table if not exists "refund" ("id" text not null, "amount" numeric not null, "payment_id" text not null, "created_at" timestamptz not null default now(), "created_by" varchar(255) null, constraint "refund_pkey" primary key ("id"));' + 'create table if not exists "refund" ("id" text not null, "amount" numeric not null, "payment_id" text not null, "created_at" timestamptz not null default now(), "created_by" text null, constraint "refund_pkey" primary key ("id"));' ) this.addSql( diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts index f1e2e7ba27..eec5105c16 100644 --- a/packages/payment/src/models/capture.ts +++ b/packages/payment/src/models/capture.ts @@ -3,6 +3,7 @@ import { Entity, ManyToOne, OnInit, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" @@ -10,8 +11,12 @@ import { import { generateEntityId } from "@medusajs/utils" import Payment from "./payment" +type OptionalCaptureProps = "created_by" | "created_at" | "completed_at" + @Entity({ tableName: "capture" }) export default class Capture { + [OptionalProps]?: OptionalCaptureProps + @PrimaryKey({ columnType: "text" }) id: string @@ -34,16 +39,16 @@ export default class Capture { }) created_at: Date - @Property({ nullable: true }) + @Property({ columnType: "text", nullable: true }) created_by: string | null @BeforeCreate() onCreate() { - this.id = generateEntityId(this.id, "capture") + this.id = generateEntityId(this.id, "cap") } @OnInit() onInit() { - this.id = generateEntityId(this.id, "capture") + this.id = generateEntityId(this.id, "cap") } } diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index eed769a76f..2cf082418b 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -7,9 +7,11 @@ import { ManyToMany, OneToMany, OnInit, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" +import { DAL } from "@medusajs/types" import { DALUtils, generateEntityId } from "@medusajs/utils" import PaymentProvider from "./payment-provider" @@ -44,9 +46,19 @@ export enum PaymentCollectionStatus { CANCELED = "canceled", } +type OptionalPaymentCollectionProps = + | "currency_code" + | "authorized_amount" + | "refunded_amount" + | "region_id" + | "completed_at" + | DAL.SoftDeletableEntityDateColumns + @Entity({ tableName: "payment-collection" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PaymentCollection { + [OptionalProps]?: OptionalPaymentCollectionProps + @PrimaryKey({ columnType: "text" }) id: string diff --git a/packages/payment/src/models/payment-method-token.ts b/packages/payment/src/models/payment-method-token.ts index 1f7a1314a0..9756d93040 100644 --- a/packages/payment/src/models/payment-method-token.ts +++ b/packages/payment/src/models/payment-method-token.ts @@ -2,42 +2,51 @@ 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 - @Property({ columnType: "jsonb", nullable: true }) - data?: Record | null - - @Property() - name: string - - @Property() - type_detail: string - - @Property() - description_detail: string - - @Property({ columnType: "jsonb", nullable: true }) - metadata?: Record | null - @Property({ columnType: "text" }) provider_id: string + @Property({ columnType: "jsonb", nullable: true }) + data?: Record | null + + @Property({ columnType: "text" }) + name: string + + @Property({ columnType: "text", nullable: true }) + type_detail: string | null + + @Property({ columnType: "text", nullable: true }) + description_detail: string | null + + @Property({ columnType: "jsonb", nullable: true }) + metadata?: Record | null + @BeforeCreate() onCreate() { - this.id = generateEntityId(this.id, "pmt") + this.id = generateEntityId(this.id, "paymt") } @OnInit() onInit() { - this.id = generateEntityId(this.id, "pmt") + this.id = generateEntityId(this.id, "paymt") } } diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index dcfdb317d5..3ad850a1c6 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -5,11 +5,14 @@ import { ManyToOne, OneToOne, OnInit, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" -import PaymentCollection from "./payment-collection" +import { DAL } from "@medusajs/types" import { generateEntityId } from "@medusajs/utils" + +import PaymentCollection from "./payment-collection" import Payment from "./payment" /** @@ -40,8 +43,17 @@ export enum PaymentSessionStatus { CANCELED = "canceled", } +type OptionalPaymentSessionProps = + | "currency_code" + | "data" + | "is_selected" + | "authorised_at" + | DAL.EntityDateColumns + @Entity({ tableName: "payment-session" }) export default class PaymentSession { + [OptionalProps]?: OptionalPaymentSessionProps + @PrimaryKey({ columnType: "text" }) id: string @@ -65,7 +77,7 @@ export default class PaymentSession { }) status: PaymentSessionStatus - @Property({ nullable: true }) + @Property({ columnType: "boolean", nullable: true }) is_selected: boolean | null @Property({ @@ -84,7 +96,7 @@ export default class PaymentSession { mappedBy: (payment) => payment.session, cascade: ["soft-remove"] as any, }) - payment: Payment + payment!: Payment @BeforeCreate() onCreate() { diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index f3fe6581ae..8fa2009378 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -7,9 +7,11 @@ import { OneToMany, OneToOne, OnInit, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" +import { DAL } from "@medusajs/types" import { DALUtils, generateEntityId } from "@medusajs/utils" import Refund from "./refund" @@ -17,9 +19,21 @@ 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 + @Entity({ tableName: "payment" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class Payment { + [OptionalProps]?: OptionalPaymentProps + @PrimaryKey({ columnType: "text" }) id: string @@ -36,19 +50,19 @@ export default class Payment { }) authorized_amount: number | null - @Property() + @Property({ columnType: "text" }) + currency_code: string + + @Property({ columnType: "text" }) provider_id: string - @Property({ nullable: true }) - currency_code: string | null - - @Property({ nullable: true }) + @Property({ columnType: "text", nullable: true }) cart_id: string | null - @Property({ nullable: true }) + @Property({ columnType: "text", nullable: true }) order_id: string | null - @Property({ nullable: true }) + @Property({ columnType: "text", nullable: true }) customer_id: string | null @Property({ columnType: "jsonb", nullable: true }) diff --git a/packages/payment/src/models/refund.ts b/packages/payment/src/models/refund.ts index a580f371c9..bf78eb7a22 100644 --- a/packages/payment/src/models/refund.ts +++ b/packages/payment/src/models/refund.ts @@ -3,6 +3,7 @@ import { Entity, ManyToOne, OnInit, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" @@ -10,8 +11,12 @@ import { import { generateEntityId } from "@medusajs/utils" import Payment from "./payment" +type OptionalRefundProps = "created_by" | "completed_at" + @Entity({ tableName: "refund" }) export default class Refund { + [OptionalProps]?: OptionalRefundProps + @PrimaryKey({ columnType: "text" }) id: string @@ -34,16 +39,16 @@ export default class Refund { }) created_at: Date - @Property({ nullable: true }) + @Property({ columnType: "text", nullable: true }) created_by: string | null @BeforeCreate() onCreate() { - this.id = generateEntityId(this.id, "refund") + this.id = generateEntityId(this.id, "ref") } @OnInit() onInit() { - this.id = generateEntityId(this.id, "refund") + this.id = generateEntityId(this.id, "ref") } }