From 824bcce3dd9916ede1cd83875b747bac60ca1fa8 Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 11 Jan 2024 11:26:41 +0100 Subject: [PATCH 01/30] feat: Payment module package setup --- packages/modules-sdk/src/definitions.ts | 17 ++++++ packages/payment/.gitignore | 6 ++ packages/payment/CHANGELOG.md | 0 packages/payment/README.md | 3 + .../integration-tests/__tests__/index.spec.ts | 5 ++ .../payment/integration-tests/setup-env.js | 6 ++ packages/payment/integration-tests/setup.js | 3 + .../payment/integration-tests/utils/config.ts | 6 ++ .../integration-tests/utils/database.ts | 18 ++++++ .../payment/integration-tests/utils/index.ts | 2 + packages/payment/jest.config.js | 21 +++++++ packages/payment/mikro-orm.config.dev.ts | 8 +++ packages/payment/package.json | 61 +++++++++++++++++++ packages/payment/src/index.ts | 23 +++++++ packages/payment/src/initialize/index.ts | 27 ++++++++ packages/payment/src/joiner-config.ts | 31 ++++++++++ packages/payment/src/loaders/connection.ts | 34 +++++++++++ packages/payment/src/loaders/container.ts | 39 ++++++++++++ packages/payment/src/loaders/index.ts | 2 + packages/payment/src/models/index.ts | 1 + packages/payment/src/module-definition.ts | 14 +++++ packages/payment/src/repositories/index.ts | 1 + packages/payment/src/scripts/bin/run-seed.ts | 33 ++++++++++ packages/payment/src/scripts/seed-utils.ts | 8 +++ .../src/services/__tests__/index.spec.ts | 5 ++ packages/payment/src/services/index.ts | 1 + .../payment/src/services/payment-module.ts | 29 +++++++++ packages/payment/src/types/index.ts | 5 ++ packages/payment/tsconfig.json | 36 +++++++++++ packages/payment/tsconfig.spec.json | 8 +++ packages/types/src/index.ts | 2 +- packages/types/src/payment/index.ts | 1 + packages/types/src/payment/service.ts | 3 + yarn.lock | 27 ++++++++ 34 files changed, 485 insertions(+), 1 deletion(-) create mode 100644 packages/payment/.gitignore create mode 100644 packages/payment/CHANGELOG.md create mode 100644 packages/payment/README.md create mode 100644 packages/payment/integration-tests/__tests__/index.spec.ts create mode 100644 packages/payment/integration-tests/setup-env.js create mode 100644 packages/payment/integration-tests/setup.js create mode 100644 packages/payment/integration-tests/utils/config.ts create mode 100644 packages/payment/integration-tests/utils/database.ts create mode 100644 packages/payment/integration-tests/utils/index.ts create mode 100644 packages/payment/jest.config.js create mode 100644 packages/payment/mikro-orm.config.dev.ts create mode 100644 packages/payment/package.json create mode 100644 packages/payment/src/index.ts create mode 100644 packages/payment/src/initialize/index.ts create mode 100644 packages/payment/src/joiner-config.ts create mode 100644 packages/payment/src/loaders/connection.ts create mode 100644 packages/payment/src/loaders/container.ts create mode 100644 packages/payment/src/loaders/index.ts create mode 100644 packages/payment/src/models/index.ts create mode 100644 packages/payment/src/module-definition.ts create mode 100644 packages/payment/src/repositories/index.ts create mode 100644 packages/payment/src/scripts/bin/run-seed.ts create mode 100644 packages/payment/src/scripts/seed-utils.ts create mode 100644 packages/payment/src/services/__tests__/index.spec.ts create mode 100644 packages/payment/src/services/index.ts create mode 100644 packages/payment/src/services/payment-module.ts create mode 100644 packages/payment/src/types/index.ts create mode 100644 packages/payment/tsconfig.json create mode 100644 packages/payment/tsconfig.spec.json create mode 100644 packages/types/src/payment/index.ts create mode 100644 packages/types/src/payment/service.ts diff --git a/packages/modules-sdk/src/definitions.ts b/packages/modules-sdk/src/definitions.ts index 48cbe2eb16..a70b6f1b93 100644 --- a/packages/modules-sdk/src/definitions.ts +++ b/packages/modules-sdk/src/definitions.ts @@ -16,6 +16,7 @@ export enum Modules { PROMOTION = "promotion", AUTHENTICATION = "authentication", CART = "cart", + PAYMENT = "payment", } export enum ModuleRegistrationName { @@ -28,6 +29,7 @@ export enum ModuleRegistrationName { PROMOTION = "promotionModuleService", AUTHENTICATION = "authenticationModuleService", CART = "cartModuleService", + PAYMENT = "paymentModuleService", } export const MODULE_PACKAGE_NAMES = { @@ -40,6 +42,7 @@ export const MODULE_PACKAGE_NAMES = { [Modules.PROMOTION]: "@medusajs/promotion", [Modules.AUTHENTICATION]: "@medusajs/authentication", [Modules.CART]: "@medusajs/cart", + [Modules.PAYMENT]: "@medusajs/payment", } export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } = @@ -171,6 +174,20 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } = resources: MODULE_RESOURCE_TYPE.SHARED, }, }, + [Modules.PAYMENT]: { + key: Modules.PAYMENT, + registrationName: ModuleRegistrationName.PAYMENT, + defaultPackage: false, + label: upperCaseFirst(ModuleRegistrationName.PAYMENT), + isRequired: false, + canOverride: true, + isQueryable: true, + dependencies: ["logger"], + defaultModuleDeclaration: { + scope: MODULE_SCOPE.INTERNAL, + resources: MODULE_RESOURCE_TYPE.SHARED, + }, + }, } export const MODULE_DEFINITIONS: ModuleDefinition[] = diff --git a/packages/payment/.gitignore b/packages/payment/.gitignore new file mode 100644 index 0000000000..874c6c69d3 --- /dev/null +++ b/packages/payment/.gitignore @@ -0,0 +1,6 @@ +/dist +node_modules +.DS_store +.env* +.env +*.sql diff --git a/packages/payment/CHANGELOG.md b/packages/payment/CHANGELOG.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/payment/README.md b/packages/payment/README.md new file mode 100644 index 0000000000..5f233bc704 --- /dev/null +++ b/packages/payment/README.md @@ -0,0 +1,3 @@ +# Payment Module + +A Payment is the result of a series of financial transactions performed as part of collecting a payment of a purchase. The Payment holds information about the payment provider as well as the state of it. You can associate a Payment with a Cart, an Order, an OrderEdit, or any other resource you’d like to have your customers pay for. diff --git a/packages/payment/integration-tests/__tests__/index.spec.ts b/packages/payment/integration-tests/__tests__/index.spec.ts new file mode 100644 index 0000000000..728f6245c6 --- /dev/null +++ b/packages/payment/integration-tests/__tests__/index.spec.ts @@ -0,0 +1,5 @@ +describe("Noop test", () => { + it("noop check", async () => { + expect(true).toBe(true) + }) +}) diff --git a/packages/payment/integration-tests/setup-env.js b/packages/payment/integration-tests/setup-env.js new file mode 100644 index 0000000000..a6084bae66 --- /dev/null +++ b/packages/payment/integration-tests/setup-env.js @@ -0,0 +1,6 @@ +if (typeof process.env.DB_TEMP_NAME === "undefined") { + const tempName = parseInt(process.env.JEST_WORKER_ID || "1") + process.env.DB_TEMP_NAME = `medusa-payment-integration-${tempName}` +} + +process.env.MEDUSA_CART_DB_SCHEMA = "public" diff --git a/packages/payment/integration-tests/setup.js b/packages/payment/integration-tests/setup.js new file mode 100644 index 0000000000..43f99aab4a --- /dev/null +++ b/packages/payment/integration-tests/setup.js @@ -0,0 +1,3 @@ +import { JestUtils } from "medusa-test-utils" + +JestUtils.afterAllHookDropDatabase() diff --git a/packages/payment/integration-tests/utils/config.ts b/packages/payment/integration-tests/utils/config.ts new file mode 100644 index 0000000000..5017bc79f0 --- /dev/null +++ b/packages/payment/integration-tests/utils/config.ts @@ -0,0 +1,6 @@ +import { ModuleServiceInitializeOptions } from "@medusajs/types" + +export const databaseOptions: ModuleServiceInitializeOptions["database"] = { + schema: "public", + clientUrl: "medusa-payment-test", +} diff --git a/packages/payment/integration-tests/utils/database.ts b/packages/payment/integration-tests/utils/database.ts new file mode 100644 index 0000000000..0fbf6bdd1c --- /dev/null +++ b/packages/payment/integration-tests/utils/database.ts @@ -0,0 +1,18 @@ +import { TestDatabaseUtils } from "medusa-test-utils" + +import * as PaymentModules from "@models" + +const pathToMigrations = "../../src/migrations" +const mikroOrmEntities = PaymentModules as unknown as any[] + +export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper( + mikroOrmEntities, + pathToMigrations +) + +export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig( + mikroOrmEntities, + pathToMigrations +) + +export const DB_URL = TestDatabaseUtils.getDatabaseURL() diff --git a/packages/payment/integration-tests/utils/index.ts b/packages/payment/integration-tests/utils/index.ts new file mode 100644 index 0000000000..5ca5d1bdc0 --- /dev/null +++ b/packages/payment/integration-tests/utils/index.ts @@ -0,0 +1,2 @@ +export * from "./config" +export * from "./database" diff --git a/packages/payment/jest.config.js b/packages/payment/jest.config.js new file mode 100644 index 0000000000..860ba90a49 --- /dev/null +++ b/packages/payment/jest.config.js @@ -0,0 +1,21 @@ +module.exports = { + moduleNameMapper: { + "^@models": "/src/models", + "^@services": "/src/services", + "^@repositories": "/src/repositories", + }, + transform: { + "^.+\\.[jt]s?$": [ + "ts-jest", + { + tsConfig: "tsconfig.spec.json", + isolatedModules: true, + }, + ], + }, + testEnvironment: `node`, + moduleFileExtensions: [`js`, `ts`], + modulePathIgnorePatterns: ["dist/"], + setupFiles: ["/integration-tests/setup-env.js"], + setupFilesAfterEnv: ["/integration-tests/setup.js"], +} diff --git a/packages/payment/mikro-orm.config.dev.ts b/packages/payment/mikro-orm.config.dev.ts new file mode 100644 index 0000000000..9e03481768 --- /dev/null +++ b/packages/payment/mikro-orm.config.dev.ts @@ -0,0 +1,8 @@ +import * as entities from "./src/models" + +module.exports = { + entities: Object.values(entities), + schema: "public", + clientUrl: "postgres://postgres@localhost/medusa-payment", + type: "postgresql", +} diff --git a/packages/payment/package.json b/packages/payment/package.json new file mode 100644 index 0000000000..cfc0e8a564 --- /dev/null +++ b/packages/payment/package.json @@ -0,0 +1,61 @@ +{ + "name": "@medusajs/payment", + "version": "0.0.1", + "description": "Medusa Payment module", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "files": [ + "dist" + ], + "engines": { + "node": ">=16" + }, + "bin": { + "medusa-payment-seed": "dist/scripts/bin/run-seed.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/medusajs/medusa", + "directory": "packages/payment" + }, + "publishConfig": { + "access": "public" + }, + "author": "Medusa", + "license": "MIT", + "scripts": { + "watch": "tsc --build --watch", + "watch:test": "tsc --build tsconfig.spec.json --watch", + "prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json", + "build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json", + "test": "jest --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts", + "test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.ts", + "migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate", + "migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial", + "migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create", + "migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:up", + "orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear" + }, + "devDependencies": { + "@mikro-orm/cli": "5.9.7", + "cross-env": "^5.2.1", + "jest": "^29.6.3", + "medusa-test-utils": "^1.1.40", + "rimraf": "^3.0.2", + "ts-jest": "^29.1.1", + "ts-node": "^10.9.1", + "tsc-alias": "^1.8.6", + "typescript": "^5.1.6" + }, + "dependencies": { + "@medusajs/modules-sdk": "^1.12.5", + "@medusajs/types": "^1.11.9", + "@medusajs/utils": "^1.11.2", + "@mikro-orm/core": "5.9.7", + "@mikro-orm/migrations": "5.9.7", + "@mikro-orm/postgresql": "5.9.7", + "awilix": "^8.0.0", + "dotenv": "^16.1.4", + "knex": "2.4.2" + } +} diff --git a/packages/payment/src/index.ts b/packages/payment/src/index.ts new file mode 100644 index 0000000000..5ec1f1b289 --- /dev/null +++ b/packages/payment/src/index.ts @@ -0,0 +1,23 @@ +import { moduleDefinition } from "./module-definition" +import { Modules } from "@medusajs/modules-sdk" +import { ModulesSdkUtils } from "@medusajs/utils" + +import * as PaymentModels from "@models" + +export default moduleDefinition + +const migrationScriptOptions = { + moduleName: Modules.PAYMENT, + models: PaymentModels, + pathToMigrations: __dirname + "/migrations", +} + +export const runMigrations = ModulesSdkUtils.buildMigrationScript( + migrationScriptOptions +) +export const revertMigration = ModulesSdkUtils.buildRevertMigrationScript( + migrationScriptOptions +) + +export * from "./initialize" +export * from "./loaders" diff --git a/packages/payment/src/initialize/index.ts b/packages/payment/src/initialize/index.ts new file mode 100644 index 0000000000..37830dcf8a --- /dev/null +++ b/packages/payment/src/initialize/index.ts @@ -0,0 +1,27 @@ +import { + ExternalModuleDeclaration, + InternalModuleDeclaration, + MedusaModule, + MODULE_PACKAGE_NAMES, + Modules, +} from "@medusajs/modules-sdk" +import { IPaymentModuleService, ModulesSdkTypes } from "@medusajs/types" +import { moduleDefinition } from "../module-definition" +import { InitializeModuleInjectableDependencies } from "../types" + +export const initialize = async ( + options?: ModulesSdkTypes.ModuleBootstrapDeclaration, + injectedDependencies?: InitializeModuleInjectableDependencies +): Promise => { + const loaded = await MedusaModule.bootstrap({ + moduleKey: Modules.PAYMENT, + defaultPath: MODULE_PACKAGE_NAMES[Modules.PAYMENT], + declaration: options as + | InternalModuleDeclaration + | ExternalModuleDeclaration, + injectedDependencies, + moduleExports: moduleDefinition, + }) + + return loaded[Modules.PAYMENT] +} diff --git a/packages/payment/src/joiner-config.ts b/packages/payment/src/joiner-config.ts new file mode 100644 index 0000000000..a04381a6ef --- /dev/null +++ b/packages/payment/src/joiner-config.ts @@ -0,0 +1,31 @@ +import { Modules } from "@medusajs/modules-sdk" +import { ModuleJoinerConfig } from "@medusajs/types" +import { MapToConfig } from "@medusajs/utils" +import { Payment } from "@models" + +export const LinkableKeys = { + payment_id: Payment.name, +} + +const entityLinkableKeysMap: MapToConfig = {} +Object.entries(LinkableKeys).forEach(([key, value]) => { + entityLinkableKeysMap[value] ??= [] + entityLinkableKeysMap[value].push({ + mapTo: key, + valueFrom: key.split("_").pop()!, + }) +}) + +export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap + +export const joinerConfig: ModuleJoinerConfig = { + serviceName: Modules.PAYMENT, + primaryKeys: ["id"], + linkableKeys: LinkableKeys, + alias: { + name: ["payment", "payments"], + args: { + entity: Payment.name, + }, + }, +} diff --git a/packages/payment/src/loaders/connection.ts b/packages/payment/src/loaders/connection.ts new file mode 100644 index 0000000000..97eec362af --- /dev/null +++ b/packages/payment/src/loaders/connection.ts @@ -0,0 +1,34 @@ +import { + InternalModuleDeclaration, + LoaderOptions, + Modules, +} from "@medusajs/modules-sdk" +import { ModulesSdkTypes } from "@medusajs/types" +import { ModulesSdkUtils } from "@medusajs/utils" +import { EntitySchema } from "@mikro-orm/core" +import * as PaymentModels from "../models" + +export default async ( + { + options, + container, + logger, + }: LoaderOptions< + | ModulesSdkTypes.ModuleServiceInitializeOptions + | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions + >, + moduleDeclaration?: InternalModuleDeclaration +): Promise => { + const entities = Object.values(PaymentModels) as unknown as EntitySchema[] + const pathToMigrations = __dirname + "/../migrations" + + await ModulesSdkUtils.mikroOrmConnectionLoader({ + moduleName: Modules.PAYMENT, + entities, + container, + options, + moduleDeclaration, + logger, + pathToMigrations, + }) +} diff --git a/packages/payment/src/loaders/container.ts b/packages/payment/src/loaders/container.ts new file mode 100644 index 0000000000..ba1b855344 --- /dev/null +++ b/packages/payment/src/loaders/container.ts @@ -0,0 +1,39 @@ +import { LoaderOptions } from "@medusajs/modules-sdk" +import { ModulesSdkTypes } from "@medusajs/types" +import { loadCustomRepositories } from "@medusajs/utils" +import { asClass } from "awilix" + +import * as defaultRepositories from "@repositories" +import * as defaultServices from "@services" + +export default async ({ + container, + options, +}: LoaderOptions< + | ModulesSdkTypes.ModuleServiceInitializeOptions + | ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions +>): Promise => { + const customRepositories = ( + options as ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions + )?.repositories + + container.register({ + // paymentService: asClass(defaultServices.PaymentService).singleton(), + }) + + if (customRepositories) { + loadCustomRepositories({ + defaultRepositories, + customRepositories, + container, + }) + } else { + loadDefaultRepositories({ container }) + } +} + +function loadDefaultRepositories({ container }) { + container.register({ + baseRepository: asClass(defaultRepositories.BaseRepository).singleton(), + }) +} diff --git a/packages/payment/src/loaders/index.ts b/packages/payment/src/loaders/index.ts new file mode 100644 index 0000000000..3614963d8c --- /dev/null +++ b/packages/payment/src/loaders/index.ts @@ -0,0 +1,2 @@ +export * from "./connection" +export * from "./container" diff --git a/packages/payment/src/models/index.ts b/packages/payment/src/models/index.ts new file mode 100644 index 0000000000..6810edbc6f --- /dev/null +++ b/packages/payment/src/models/index.ts @@ -0,0 +1 @@ +export { default as Payment } from "./payment" diff --git a/packages/payment/src/module-definition.ts b/packages/payment/src/module-definition.ts new file mode 100644 index 0000000000..543a6ef254 --- /dev/null +++ b/packages/payment/src/module-definition.ts @@ -0,0 +1,14 @@ +import { ModuleExports } from "@medusajs/types" + +import { PaymentModuleService } from "@services" + +import loadConnection from "./loaders/connection" +import loadContainer from "./loaders/container" + +const service = PaymentModuleService +const loaders = [loadContainer, loadConnection] as any + +export const moduleDefinition: ModuleExports = { + service, + loaders, +} diff --git a/packages/payment/src/repositories/index.ts b/packages/payment/src/repositories/index.ts new file mode 100644 index 0000000000..147c9cc259 --- /dev/null +++ b/packages/payment/src/repositories/index.ts @@ -0,0 +1 @@ +export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils" diff --git a/packages/payment/src/scripts/bin/run-seed.ts b/packages/payment/src/scripts/bin/run-seed.ts new file mode 100644 index 0000000000..ab7eacb3f3 --- /dev/null +++ b/packages/payment/src/scripts/bin/run-seed.ts @@ -0,0 +1,33 @@ +#!/usr/bin/env node + +import { EOL } from "os" +import { ModulesSdkUtils } from "@medusajs/utils" +import { Modules } from "@medusajs/modules-sdk" + +import * as PaymentModels from "@models" + +import { createPayments } from "../seed-utils" + +const args = process.argv +const path = args.pop() as string + +export default (async () => { + const { config } = await import("dotenv") + config() + if (!path) { + throw new Error( + `filePath is required.${EOL}Example: medusa-payment-seed ` + ) + } + + const run = ModulesSdkUtils.buildSeedScript({ + moduleName: Modules.PAYMENT, + models: PaymentModels, + pathToMigrations: __dirname + "/../../migrations", + seedHandler: async ({ manager, data }) => { + const { paymentsData } = data + await createPayments(manager, paymentsData) + }, + }) + await run({ path }) +})() diff --git a/packages/payment/src/scripts/seed-utils.ts b/packages/payment/src/scripts/seed-utils.ts new file mode 100644 index 0000000000..a507d0b6bc --- /dev/null +++ b/packages/payment/src/scripts/seed-utils.ts @@ -0,0 +1,8 @@ +import { SqlEntityManager } from "@mikro-orm/postgresql" + +export async function createPayments( + manager: SqlEntityManager, + paymentsData: any[] +): Promise { + return [] +} diff --git a/packages/payment/src/services/__tests__/index.spec.ts b/packages/payment/src/services/__tests__/index.spec.ts new file mode 100644 index 0000000000..728f6245c6 --- /dev/null +++ b/packages/payment/src/services/__tests__/index.spec.ts @@ -0,0 +1,5 @@ +describe("Noop test", () => { + it("noop check", async () => { + expect(true).toBe(true) + }) +}) diff --git a/packages/payment/src/services/index.ts b/packages/payment/src/services/index.ts new file mode 100644 index 0000000000..a4f5ea37ae --- /dev/null +++ b/packages/payment/src/services/index.ts @@ -0,0 +1 @@ +export { default as PaymentModuleService } from "./payment-module" diff --git a/packages/payment/src/services/payment-module.ts b/packages/payment/src/services/payment-module.ts new file mode 100644 index 0000000000..9dc1a7a406 --- /dev/null +++ b/packages/payment/src/services/payment-module.ts @@ -0,0 +1,29 @@ +import { + DAL, + InternalModuleDeclaration, + ModuleJoinerConfig, +} from "@medusajs/types" + +import { Payment } from "@models" + +import { joinerConfig } from "../joiner-config" + +type InjectedDependencies = { + baseRepository: DAL.RepositoryService +} + +// TODO: implement IPaymentModule +export default class PaymentModule { + protected baseRepository_: DAL.RepositoryService + + constructor( + { baseRepository }: InjectedDependencies, + protected readonly moduleDeclaration: InternalModuleDeclaration + ) { + this.baseRepository_ = baseRepository + } + + __joinerConfig(): ModuleJoinerConfig { + return joinerConfig + } +} diff --git a/packages/payment/src/types/index.ts b/packages/payment/src/types/index.ts new file mode 100644 index 0000000000..0f252977b0 --- /dev/null +++ b/packages/payment/src/types/index.ts @@ -0,0 +1,5 @@ +import { Logger } from "@medusajs/types" + +export type InitializeModuleInjectableDependencies = { + logger?: Logger +} diff --git a/packages/payment/tsconfig.json b/packages/payment/tsconfig.json new file mode 100644 index 0000000000..213e38fc55 --- /dev/null +++ b/packages/payment/tsconfig.json @@ -0,0 +1,36 @@ +{ + "compilerOptions": { + "lib": ["es2020"], + "target": "es2020", + "outDir": "./dist", + "esModuleInterop": true, + "declaration": true, + "module": "commonjs", + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "sourceMap": false, + "noImplicitReturns": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noImplicitThis": true, + "allowJs": true, + "skipLibCheck": true, + "downlevelIteration": true, // to use ES5 specific tooling + "baseUrl": ".", + "resolveJsonModule": true, + "paths": { + "@models": ["./src/models"], + "@services": ["./src/services"], + "@repositories": ["./src/repositories"] + } + }, + "include": ["src"], + "exclude": [ + "dist", + "./src/**/__tests__", + "./src/**/__mocks__", + "./src/**/__fixtures__", + "node_modules" + ] +} diff --git a/packages/payment/tsconfig.spec.json b/packages/payment/tsconfig.spec.json new file mode 100644 index 0000000000..48e47e8cbb --- /dev/null +++ b/packages/payment/tsconfig.spec.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "include": ["src", "integration-tests"], + "exclude": ["node_modules", "dist"], + "compilerOptions": { + "sourceMap": true + } +} diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index e9ed92dcbd..823b2eada2 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -16,6 +16,7 @@ export * from "./logger" export * from "./modules-sdk" export * from "./pricing" export * from "./product" +export * from "./payment" export * from "./product-category" export * from "./promotion" export * from "./region" @@ -25,4 +26,3 @@ export * from "./shared-context" export * from "./stock-location" export * from "./transaction-base" export * from "./workflow" - diff --git a/packages/types/src/payment/index.ts b/packages/types/src/payment/index.ts new file mode 100644 index 0000000000..9376fea807 --- /dev/null +++ b/packages/types/src/payment/index.ts @@ -0,0 +1 @@ +export * from "./service" diff --git a/packages/types/src/payment/service.ts b/packages/types/src/payment/service.ts new file mode 100644 index 0000000000..80c70e30da --- /dev/null +++ b/packages/types/src/payment/service.ts @@ -0,0 +1,3 @@ +import { IModuleService } from "../modules-sdk" + +export interface IPaymentModuleService extends IModuleService {} diff --git a/yarn.lock b/yarn.lock index 1a9b7e1666..3edcaca246 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8358,6 +8358,33 @@ __metadata: languageName: unknown linkType: soft +"@medusajs/payment@workspace:packages/payment": + version: 0.0.0-use.local + resolution: "@medusajs/payment@workspace:packages/payment" + dependencies: + "@medusajs/modules-sdk": ^1.12.5 + "@medusajs/types": ^1.11.9 + "@medusajs/utils": ^1.11.2 + "@mikro-orm/cli": 5.9.7 + "@mikro-orm/core": 5.9.7 + "@mikro-orm/migrations": 5.9.7 + "@mikro-orm/postgresql": 5.9.7 + awilix: ^8.0.0 + cross-env: ^5.2.1 + dotenv: ^16.1.4 + jest: ^29.6.3 + knex: 2.4.2 + medusa-test-utils: ^1.1.40 + rimraf: ^3.0.2 + ts-jest: ^29.1.1 + ts-node: ^10.9.1 + tsc-alias: ^1.8.6 + typescript: ^5.1.6 + bin: + medusa-payment-seed: dist/scripts/bin/run-seed.js + languageName: unknown + linkType: soft + "@medusajs/pricing@workspace:^, @medusajs/pricing@workspace:packages/pricing": version: 0.0.0-use.local resolution: "@medusajs/pricing@workspace:packages/pricing" From c9f185864966cdd7c2c02250510f37befd5fbbc1 Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 11 Jan 2024 11:35:01 +0100 Subject: [PATCH 02/30] fix: missing files --- .changeset/wicked-months-greet.md | 7 ++++ .../payment/integration-tests/setup-env.js | 2 +- packages/payment/src/models/payment.ts | 40 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 .changeset/wicked-months-greet.md create mode 100644 packages/payment/src/models/payment.ts diff --git a/.changeset/wicked-months-greet.md b/.changeset/wicked-months-greet.md new file mode 100644 index 0000000000..54775726e2 --- /dev/null +++ b/.changeset/wicked-months-greet.md @@ -0,0 +1,7 @@ +--- +"@medusajs/modules-sdk": patch +"@medusajs/payment": patch +"@medusajs/types": patch +--- + +feat: add Payment module package diff --git a/packages/payment/integration-tests/setup-env.js b/packages/payment/integration-tests/setup-env.js index a6084bae66..ca80a7dace 100644 --- a/packages/payment/integration-tests/setup-env.js +++ b/packages/payment/integration-tests/setup-env.js @@ -3,4 +3,4 @@ if (typeof process.env.DB_TEMP_NAME === "undefined") { process.env.DB_TEMP_NAME = `medusa-payment-integration-${tempName}` } -process.env.MEDUSA_CART_DB_SCHEMA = "public" +process.env.MEDUSA_PAYMENT_DB_SCHEMA = "public" diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts new file mode 100644 index 0000000000..f4fee2bd64 --- /dev/null +++ b/packages/payment/src/models/payment.ts @@ -0,0 +1,40 @@ +import { + BeforeCreate, + Entity, + OnInit, + PrimaryKey, + Property, +} from "@mikro-orm/core" + +import { generateEntityId } from "@medusajs/utils" + +@Entity({ tableName: "payment" }) +export default class Payment { + @PrimaryKey({ columnType: "text" }) + id: string + + @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 + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "pay") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "pay") + } +} From 85e4e6ce4bd1c7cb2d65216a7d38ff2e6c4a555f Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 11 Jan 2024 11:50:06 +0100 Subject: [PATCH 03/30] fix: changesets --- .changeset/wicked-months-greet.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.changeset/wicked-months-greet.md b/.changeset/wicked-months-greet.md index 54775726e2..e0a8e026af 100644 --- a/.changeset/wicked-months-greet.md +++ b/.changeset/wicked-months-greet.md @@ -1,6 +1,5 @@ --- "@medusajs/modules-sdk": patch -"@medusajs/payment": patch "@medusajs/types": patch --- From 8a5888aeac5d0eb3b7dec3d547bea58f13e16f79 Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 11 Jan 2024 18:29:33 +0100 Subject: [PATCH 04/30] feat: Payment models --- packages/payment/src/models/capture.ts | 49 +++++++ packages/payment/src/models/index.ts | 6 + .../payment/src/models/payment-collection.ts | 135 ++++++++++++++++++ .../src/models/payment-method-token.ts | 43 ++++++ .../payment/src/models/payment-provider.ts | 12 ++ .../payment/src/models/payment-session.ts | 98 +++++++++++++ packages/payment/src/models/payment.ts | 86 ++++++++++- packages/payment/src/models/refund.ts | 49 +++++++ 8 files changed, 477 insertions(+), 1 deletion(-) create mode 100644 packages/payment/src/models/capture.ts create mode 100644 packages/payment/src/models/payment-collection.ts create mode 100644 packages/payment/src/models/payment-method-token.ts create mode 100644 packages/payment/src/models/payment-provider.ts create mode 100644 packages/payment/src/models/payment-session.ts create mode 100644 packages/payment/src/models/refund.ts diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts new file mode 100644 index 0000000000..f1e2e7ba27 --- /dev/null +++ b/packages/payment/src/models/capture.ts @@ -0,0 +1,49 @@ +import { + BeforeCreate, + Entity, + ManyToOne, + OnInit, + PrimaryKey, + Property, +} from "@mikro-orm/core" + +import { generateEntityId } from "@medusajs/utils" +import Payment from "./payment" + +@Entity({ tableName: "capture" }) +export default class Capture { + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ + columnType: "numeric", + serializer: Number, + }) + amount: number + + @ManyToOne(() => Payment, { + onDelete: "cascade", + fieldName: "payment_id", + }) + payment: Payment + + @Property({ + onCreate: () => new Date(), + columnType: "timestamptz", + defaultRaw: "now()", + }) + created_at: Date + + @Property({ nullable: true }) + created_by: string | null + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "capture") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "capture") + } +} diff --git a/packages/payment/src/models/index.ts b/packages/payment/src/models/index.ts index 6810edbc6f..6588a97b76 100644 --- a/packages/payment/src/models/index.ts +++ b/packages/payment/src/models/index.ts @@ -1 +1,7 @@ +export { default as Capture } from "./capture" export { default as Payment } from "./payment" +export { default as PaymentCollection } from "./payment-collection" +export { default as PaymentMethodToken } from "./payment-method-token" +export { default as PaymentProvider } from "./payment-provider" +export { default as PaymentSession } from "./payment-session" +export { default as Refund } from "./refund" diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts new file mode 100644 index 0000000000..eed769a76f --- /dev/null +++ b/packages/payment/src/models/payment-collection.ts @@ -0,0 +1,135 @@ +import { + BeforeCreate, + Collection, + Entity, + Enum, + Filter, + ManyToMany, + OneToMany, + OnInit, + PrimaryKey, + Property, +} from "@mikro-orm/core" + +import { DALUtils, generateEntityId } 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", +} + +@Entity({ tableName: "payment-collection" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) +export default class PaymentCollection { + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ columnType: "text", nullable: true }) + currency_code: string | null + + @Property({ + columnType: "numeric", + serializer: Number, + }) + amount: number + + @Property({ + columnType: "numeric", + nullable: true, + serializer: Number, + }) + authorized_amount: number | null + + @Property({ + columnType: "numeric", + nullable: true, + serializer: Number, + }) + refunded_amount: number | null + + @Property({ columnType: "text", nullable: true }) + region_id?: string | 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_collection_deleted_at", + }) + deleted_at: Date | null + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + completed_at: Date | null + + @Enum({ + items: () => PaymentCollectionStatus, + default: PaymentCollectionStatus.NOT_PAID, + }) + status: PaymentCollectionStatus = PaymentCollectionStatus.NOT_PAID + + @ManyToMany(() => PaymentProvider) + payment_providers = new Collection(this) + + @OneToMany(() => PaymentSession, (ps) => ps.payment_collection, { + orphanRemoval: true, + }) + payment_sessions = new Collection(this) + + @OneToMany(() => Payment, (payment) => payment.payment_collection, { + orphanRemoval: true, + }) + payments = new Collection(this) + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "paycol") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "paycol") + } +} diff --git a/packages/payment/src/models/payment-method-token.ts b/packages/payment/src/models/payment-method-token.ts new file mode 100644 index 0000000000..1f7a1314a0 --- /dev/null +++ b/packages/payment/src/models/payment-method-token.ts @@ -0,0 +1,43 @@ +import { + BeforeCreate, + Entity, + OnInit, + PrimaryKey, + Property, +} from "@mikro-orm/core" + +import { generateEntityId } from "@medusajs/utils" + +@Entity({ tableName: "payment-method-token" }) +export default class PaymentMethodToken { + @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 + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "pmt") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "pmt") + } +} diff --git a/packages/payment/src/models/payment-provider.ts b/packages/payment/src/models/payment-provider.ts new file mode 100644 index 0000000000..db5f53ae99 --- /dev/null +++ b/packages/payment/src/models/payment-provider.ts @@ -0,0 +1,12 @@ +import { Entity, PrimaryKey, Property } from "@mikro-orm/core" + +@Entity({ tableName: "payment-provider" }) +export default class PaymentProvider { + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ + default: true, + }) + is_enabled: boolean = true +} diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts new file mode 100644 index 0000000000..dcfdb317d5 --- /dev/null +++ b/packages/payment/src/models/payment-session.ts @@ -0,0 +1,98 @@ +import { + BeforeCreate, + Entity, + Enum, + ManyToOne, + OneToOne, + OnInit, + PrimaryKey, + Property, +} from "@mikro-orm/core" +import PaymentCollection from "./payment-collection" +import { generateEntityId } from "@medusajs/utils" +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", +} + +@Entity({ tableName: "payment-session" }) +export default class PaymentSession { + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ columnType: "text", nullable: true }) + currency_code: string | null + + @Property({ + columnType: "numeric", + serializer: Number, + }) + amount: number + + @Property({ columnType: "text" }) + provider_id: string + + @Property({ columnType: "jsonb", nullable: true }) + data?: Record | null + + @Enum({ + items: () => PaymentSessionStatus, + }) + status: PaymentSessionStatus + + @Property({ nullable: true }) + is_selected: boolean | null + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + authorised_at: Date | null + + @ManyToOne({ + fieldName: "payment_collection_id", + }) + payment_collection!: PaymentCollection + + @OneToOne({ + entity: () => Payment, + mappedBy: (payment) => payment.session, + cascade: ["soft-remove"] as any, + }) + payment: Payment + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "ps") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "ps") + } +} diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index f4fee2bd64..366a5a542c 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -1,18 +1,58 @@ import { BeforeCreate, + Collection, Entity, + Filter, + ManyToOne, + OneToMany, + OneToOne, OnInit, PrimaryKey, Property, } from "@mikro-orm/core" -import { generateEntityId } from "@medusajs/utils" +import { DALUtils, generateEntityId } from "@medusajs/utils" +import Refund from "./refund" +import Capture from "./capture" +import PaymentSession from "./payment-session" +import PaymentCollection from "./payment-collection" @Entity({ tableName: "payment" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class Payment { @PrimaryKey({ columnType: "text" }) id: string + @Property({ + columnType: "numeric", + serializer: Number, + }) + amount: number + + @Property({ + columnType: "numeric", + serializer: Number, + }) + authorized_amount: number + + @Property() + provider_id: string + + @Property({ nullable: true }) + currency_code: string | null + + @Property({ nullable: true }) + cart_id: string | null + + @Property({ nullable: true }) + order_id: string | null + + @Property({ nullable: true }) + customer_id: string | null + + @Property({ columnType: "jsonb", nullable: true }) + data?: Record | null + @Property({ onCreate: () => new Date(), columnType: "timestamptz", @@ -28,6 +68,50 @@ export default class Payment { }) updated_at: Date + @Property({ + columnType: "timestamptz", + nullable: true, + index: "IDX_payment_deleted_at", + }) + deleted_at: Date | null + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + captured_at: Date | null + + @Property({ + columnType: "timestamptz", + nullable: true, + }) + canceled_at: Date | null + + @OneToMany(() => Refund, (refund) => refund.payment, { + orphanRemoval: true, + }) + refunds = new Collection(this) + + @OneToMany(() => Capture, (capture) => capture.payment, { + orphanRemoval: true, + }) + captures = new Collection(this) + + @ManyToOne({ + fieldName: "payment_collection_id", + }) + payment_collection!: PaymentCollection + + @OneToOne({ owner: true, fieldName: "session_id" }) + session: PaymentSession + + /** COMPUTED PROPERTIES START **/ + + // captured_amount: number // sum of the associated captures + // refunded_amount: number // sum of the associated refunds + + /** COMPUTED PROPERTIES END **/ + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "pay") diff --git a/packages/payment/src/models/refund.ts b/packages/payment/src/models/refund.ts new file mode 100644 index 0000000000..a580f371c9 --- /dev/null +++ b/packages/payment/src/models/refund.ts @@ -0,0 +1,49 @@ +import { + BeforeCreate, + Entity, + ManyToOne, + OnInit, + PrimaryKey, + Property, +} from "@mikro-orm/core" + +import { generateEntityId } from "@medusajs/utils" +import Payment from "./payment" + +@Entity({ tableName: "refund" }) +export default class Refund { + @PrimaryKey({ columnType: "text" }) + id: string + + @Property({ + columnType: "numeric", + serializer: Number, + }) + amount: number + + @ManyToOne(() => Payment, { + onDelete: "cascade", + fieldName: "payment_id", + }) + payment: Payment + + @Property({ + onCreate: () => new Date(), + columnType: "timestamptz", + defaultRaw: "now()", + }) + created_at: Date + + @Property({ nullable: true }) + created_by: string | null + + @BeforeCreate() + onCreate() { + this.id = generateEntityId(this.id, "refund") + } + + @OnInit() + onInit() { + this.id = generateEntityId(this.id, "refund") + } +} From 9e72d25203996532f953ce9618cf26893915899a Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 11 Jan 2024 18:33:43 +0100 Subject: [PATCH 05/30] feat: migration --- .../migrations/.snapshot-medusa-payment.json | 828 ++++++++++++++++++ .../src/migrations/Migration20240111173302.ts | 72 ++ packages/payment/src/models/payment.ts | 3 +- 3 files changed, 902 insertions(+), 1 deletion(-) create mode 100644 packages/payment/src/migrations/.snapshot-medusa-payment.json create mode 100644 packages/payment/src/migrations/Migration20240111173302.ts diff --git a/packages/payment/src/migrations/.snapshot-medusa-payment.json b/packages/payment/src/migrations/.snapshot-medusa-payment.json new file mode 100644 index 0000000000..df741b1f06 --- /dev/null +++ b/packages/payment/src/migrations/.snapshot-medusa-payment.json @@ -0,0 +1,828 @@ +{ + "namespaces": [ + "public" + ], + "name": "public", + "tables": [ + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "currency_code": { + "name": "currency_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "authorized_amount": { + "name": "authorized_amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "refunded_amount": { + "name": "refunded_amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "region_id": { + "name": "region_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "completed_at": { + "name": "completed_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "status": { + "name": "status", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "'not_paid'", + "enumItems": [ + "not_paid", + "awaiting", + "authorized", + "partially_authorized", + "canceled" + ], + "mappedType": "enum" + } + }, + "name": "payment-collection", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "deleted_at" + ], + "composite": false, + "keyName": "IDX_payment_collection_deleted_at", + "primary": false, + "unique": false + }, + { + "keyName": "payment-collection_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "name": { + "name": "name", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "type_detail": { + "name": "type_detail", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "description_detail": { + "name": "description_detail", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "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", + "schema": "public", + "indexes": [ + { + "keyName": "payment-method-token_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "is_enabled": { + "name": "is_enabled", + "type": "boolean", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "true", + "mappedType": "boolean" + } + }, + "name": "payment-provider", + "schema": "public", + "indexes": [ + { + "keyName": "payment-provider_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": {} + }, + { + "columns": { + "payment_collection_id": { + "name": "payment_collection_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "payment_provider_id": { + "name": "payment_provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "payment-collection_payment_providers", + "schema": "public", + "indexes": [ + { + "keyName": "payment-collection_payment_providers_pkey", + "columnNames": [ + "payment_collection_id", + "payment_provider_id" + ], + "composite": true, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "payment-collection_payment_providers_payment_coll_6b015_foreign": { + "constraintName": "payment-collection_payment_providers_payment_coll_6b015_foreign", + "columnNames": [ + "payment_collection_id" + ], + "localTableName": "public.payment-collection_payment_providers", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-collection", + "deleteRule": "cascade", + "updateRule": "cascade" + }, + "payment-collection_payment_providers_payment_provider_id_foreign": { + "constraintName": "payment-collection_payment_providers_payment_provider_id_foreign", + "columnNames": [ + "payment_provider_id" + ], + "localTableName": "public.payment-collection_payment_providers", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-provider", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "currency_code": { + "name": "currency_code", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "status": { + "name": "status", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "enumItems": [ + "authorized", + "pending", + "requires_more", + "error", + "canceled" + ], + "mappedType": "enum" + }, + "is_selected": { + "name": "is_selected", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "authorised_at": { + "name": "authorised_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "payment_collection_id": { + "name": "payment_collection_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "payment-session", + "schema": "public", + "indexes": [ + { + "keyName": "payment-session_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "payment-session_payment_collection_id_foreign": { + "constraintName": "payment-session_payment_collection_id_foreign", + "columnNames": [ + "payment_collection_id" + ], + "localTableName": "public.payment-session", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-collection", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "authorized_amount": { + "name": "authorized_amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "decimal" + }, + "provider_id": { + "name": "provider_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "string" + }, + "currency_code": { + "name": "currency_code", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "cart_id": { + "name": "cart_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "order_id": { + "name": "order_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "customer_id": { + "name": "customer_id", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + }, + "data": { + "name": "data", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "captured_at": { + "name": "captured_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "canceled_at": { + "name": "canceled_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" + }, + "payment_collection_id": { + "name": "payment_collection_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "session_id": { + "name": "session_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + } + }, + "name": "payment", + "schema": "public", + "indexes": [ + { + "columnNames": [ + "deleted_at" + ], + "composite": false, + "keyName": "IDX_payment_deleted_at", + "primary": false, + "unique": false + }, + { + "columnNames": [ + "session_id" + ], + "composite": false, + "keyName": "payment_session_id_unique", + "primary": false, + "unique": true + }, + { + "keyName": "payment_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "payment_payment_collection_id_foreign": { + "constraintName": "payment_payment_collection_id_foreign", + "columnNames": [ + "payment_collection_id" + ], + "localTableName": "public.payment", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-collection", + "updateRule": "cascade" + }, + "payment_session_id_foreign": { + "constraintName": "payment_session_id_foreign", + "columnNames": [ + "session_id" + ], + "localTableName": "public.payment", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment-session", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "payment_id": { + "name": "payment_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "created_by": { + "name": "created_by", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + } + }, + "name": "capture", + "schema": "public", + "indexes": [ + { + "keyName": "capture_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "capture_payment_id_foreign": { + "constraintName": "capture_payment_id_foreign", + "columnNames": [ + "payment_id" + ], + "localTableName": "public.capture", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + }, + { + "columns": { + "id": { + "name": "id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "amount": { + "name": "amount", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "payment_id": { + "name": "payment_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "created_at": { + "name": "created_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "length": 6, + "default": "now()", + "mappedType": "datetime" + }, + "created_by": { + "name": "created_by", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "string" + } + }, + "name": "refund", + "schema": "public", + "indexes": [ + { + "keyName": "refund_pkey", + "columnNames": [ + "id" + ], + "composite": false, + "primary": true, + "unique": true + } + ], + "checks": [], + "foreignKeys": { + "refund_payment_id_foreign": { + "constraintName": "refund_payment_id_foreign", + "columnNames": [ + "payment_id" + ], + "localTableName": "public.refund", + "referencedColumnNames": [ + "id" + ], + "referencedTableName": "public.payment", + "deleteRule": "cascade", + "updateRule": "cascade" + } + } + } + ] +} diff --git a/packages/payment/src/migrations/Migration20240111173302.ts b/packages/payment/src/migrations/Migration20240111173302.ts new file mode 100644 index 0000000000..14c2f7b6f1 --- /dev/null +++ b/packages/payment/src/migrations/Migration20240111173302.ts @@ -0,0 +1,72 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20240111173302 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"));' + ) + this.addSql( + 'create index "IDX_payment_collection_deleted_at" on "payment-collection" ("deleted_at");' + ) + + 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"));' + ) + + this.addSql( + 'create table if not exists "payment-provider" ("id" text not null, "is_enabled" boolean not null default true, constraint "payment-provider_pkey" primary key ("id"));' + ) + + this.addSql( + 'create table if not exists "payment-collection_payment_providers" ("payment_collection_id" text not null, "payment_provider_id" text not null, constraint "payment-collection_payment_providers_pkey" primary key ("payment_collection_id", "payment_provider_id"));' + ) + + 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"));' + ) + + 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"));' + ) + this.addSql( + 'create index "IDX_payment_deleted_at" on "payment" ("deleted_at");' + ) + this.addSql( + 'alter table "payment" add constraint "payment_session_id_unique" unique ("session_id");' + ) + + 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"));' + ) + + 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"));' + ) + + this.addSql( + 'alter table "payment-collection_payment_providers" add constraint "payment-collection_payment_providers_payment_coll_6b015_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'alter table "payment-collection_payment_providers" add constraint "payment-collection_payment_providers_payment_provider_id_foreign" foreign key ("payment_provider_id") references "payment-provider" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "payment-session" add constraint "payment-session_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade;' + ) + + this.addSql( + 'alter table "payment" add constraint "payment_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade;' + ) + this.addSql( + 'alter table "payment" add constraint "payment_session_id_foreign" foreign key ("session_id") references "payment-session" ("id") on update cascade;' + ) + + this.addSql( + 'alter table "capture" add constraint "capture_payment_id_foreign" foreign key ("payment_id") references "payment" ("id") on update cascade on delete cascade;' + ) + + this.addSql( + 'alter table "refund" add constraint "refund_payment_id_foreign" foreign key ("payment_id") references "payment" ("id") on update cascade on delete cascade;' + ) + } +} diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index 366a5a542c..f3fe6581ae 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -31,9 +31,10 @@ export default class Payment { @Property({ columnType: "numeric", + nullable: true, serializer: Number, }) - authorized_amount: number + authorized_amount: number | null @Property() provider_id: string From 1b882a90ed91072b3032777ea525c88eb488e44c Mon Sep 17 00:00:00 2001 From: fPolic Date: Fri, 12 Jan 2024 11:46:08 +0100 Subject: [PATCH 06/30] feat: optional props, update fields --- .../migrations/.snapshot-medusa-payment.json | 76 +++++++++---------- ...11173302.ts => Migration20240112104455.ts} | 12 +-- packages/payment/src/models/capture.ts | 11 ++- .../payment/src/models/payment-collection.ts | 12 +++ .../src/models/payment-method-token.ts | 43 ++++++----- .../payment/src/models/payment-session.ts | 18 ++++- packages/payment/src/models/payment.ts | 28 +++++-- packages/payment/src/models/refund.ts | 11 ++- 8 files changed, 134 insertions(+), 77 deletions(-) rename packages/payment/src/migrations/{Migration20240111173302.ts => Migration20240112104455.ts} (74%) 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") } } From f2a4f454fe6eaf6b06da2484aec43bf6577a9b74 Mon Sep 17 00:00:00 2001 From: fPolic Date: Fri, 12 Jan 2024 12:03:17 +0100 Subject: [PATCH 07/30] fix: Refund/Capture delete, add indexes --- packages/payment/src/models/capture.ts | 2 +- packages/payment/src/models/refund.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts index eec5105c16..1c8b399744 100644 --- a/packages/payment/src/models/capture.ts +++ b/packages/payment/src/models/capture.ts @@ -27,7 +27,7 @@ export default class Capture { amount: number @ManyToOne(() => Payment, { - onDelete: "cascade", + index: "IDX_capture_payment_id", fieldName: "payment_id", }) payment: Payment diff --git a/packages/payment/src/models/refund.ts b/packages/payment/src/models/refund.ts index bf78eb7a22..52528f25a9 100644 --- a/packages/payment/src/models/refund.ts +++ b/packages/payment/src/models/refund.ts @@ -27,7 +27,7 @@ export default class Refund { amount: number @ManyToOne(() => Payment, { - onDelete: "cascade", + index: "IDX_refund_payment_id", fieldName: "payment_id", }) payment: Payment From 00ee67c22841530ec5969c80a96acb9bc5dcbf9a Mon Sep 17 00:00:00 2001 From: fPolic Date: Fri, 12 Jan 2024 14:02:45 +0100 Subject: [PATCH 08/30] chore: refactor --- .../migrations/.snapshot-medusa-payment.json | 18 ++++++++++++++++++ ...112104455.ts => Migration20240112130209.ts} | 8 +++++++- packages/payment/src/models/capture.ts | 1 + packages/payment/src/models/refund.ts | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) rename packages/payment/src/migrations/{Migration20240112104455.ts => Migration20240112130209.ts} (95%) diff --git a/packages/payment/src/migrations/.snapshot-medusa-payment.json b/packages/payment/src/migrations/.snapshot-medusa-payment.json index 9a30897f1a..4b2793cfca 100644 --- a/packages/payment/src/migrations/.snapshot-medusa-payment.json +++ b/packages/payment/src/migrations/.snapshot-medusa-payment.json @@ -717,6 +717,15 @@ "name": "capture", "schema": "public", "indexes": [ + { + "columnNames": [ + "payment_id" + ], + "composite": false, + "keyName": "IDX_capture_payment_id", + "primary": false, + "unique": false + }, { "keyName": "capture_pkey", "columnNames": [ @@ -797,6 +806,15 @@ "name": "refund", "schema": "public", "indexes": [ + { + "columnNames": [ + "payment_id" + ], + "composite": false, + "keyName": "IDX_refund_payment_id", + "primary": false, + "unique": false + }, { "keyName": "refund_pkey", "columnNames": [ diff --git a/packages/payment/src/migrations/Migration20240112104455.ts b/packages/payment/src/migrations/Migration20240112130209.ts similarity index 95% rename from packages/payment/src/migrations/Migration20240112104455.ts rename to packages/payment/src/migrations/Migration20240112130209.ts index 0f42c7fe69..54c4e1933a 100644 --- a/packages/payment/src/migrations/Migration20240112104455.ts +++ b/packages/payment/src/migrations/Migration20240112130209.ts @@ -1,6 +1,6 @@ import { Migration } from "@mikro-orm/migrations" -export class Migration20240112104455 extends Migration { +export class Migration20240112130209 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"));' @@ -38,10 +38,16 @@ export class Migration20240112104455 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" text null, constraint "capture_pkey" primary key ("id"));' ) + this.addSql( + 'create index "IDX_capture_payment_id" on "capture" ("payment_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" text null, constraint "refund_pkey" primary key ("id"));' ) + this.addSql( + 'create index "IDX_refund_payment_id" on "refund" ("payment_id");' + ) this.addSql( 'alter table "payment-collection_payment_providers" add constraint "payment-collection_payment_providers_payment_coll_6b015_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade on delete cascade;' diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts index 1c8b399744..8f6de9e116 100644 --- a/packages/payment/src/models/capture.ts +++ b/packages/payment/src/models/capture.ts @@ -27,6 +27,7 @@ export default class Capture { amount: number @ManyToOne(() => Payment, { + onDelete: "cascade", index: "IDX_capture_payment_id", fieldName: "payment_id", }) diff --git a/packages/payment/src/models/refund.ts b/packages/payment/src/models/refund.ts index 52528f25a9..6da06d2c16 100644 --- a/packages/payment/src/models/refund.ts +++ b/packages/payment/src/models/refund.ts @@ -27,6 +27,7 @@ export default class Refund { amount: number @ManyToOne(() => Payment, { + onDelete: "cascade", index: "IDX_refund_payment_id", fieldName: "payment_id", }) From a7f74bdfb8a369168f7d3221a7ae2e2244ac8816 Mon Sep 17 00:00:00 2001 From: fPolic Date: Fri, 12 Jan 2024 14:10:56 +0100 Subject: [PATCH 09/30] fix: add missing indexes --- .../migrations/.snapshot-medusa-payment.json | 18 ++++++++++++++++++ ...112130209.ts => Migration20240112131027.ts} | 8 +++++++- packages/payment/src/models/payment-session.ts | 1 + packages/payment/src/models/payment.ts | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) rename packages/payment/src/migrations/{Migration20240112130209.ts => Migration20240112131027.ts} (94%) diff --git a/packages/payment/src/migrations/.snapshot-medusa-payment.json b/packages/payment/src/migrations/.snapshot-medusa-payment.json index 4b2793cfca..2522b71b85 100644 --- a/packages/payment/src/migrations/.snapshot-medusa-payment.json +++ b/packages/payment/src/migrations/.snapshot-medusa-payment.json @@ -425,6 +425,15 @@ "name": "payment-session", "schema": "public", "indexes": [ + { + "columnNames": [ + "payment_collection_id" + ], + "composite": false, + "keyName": "IDX_payment_session_payment_collection_id", + "primary": false, + "unique": false + }, { "keyName": "payment-session_pkey", "columnNames": [ @@ -617,6 +626,15 @@ "primary": false, "unique": false }, + { + "columnNames": [ + "payment_collection_id" + ], + "composite": false, + "keyName": "IDX_payment_payment_collection_id", + "primary": false, + "unique": false + }, { "columnNames": [ "session_id" diff --git a/packages/payment/src/migrations/Migration20240112130209.ts b/packages/payment/src/migrations/Migration20240112131027.ts similarity index 94% rename from packages/payment/src/migrations/Migration20240112130209.ts rename to packages/payment/src/migrations/Migration20240112131027.ts index 54c4e1933a..cc588e5207 100644 --- a/packages/payment/src/migrations/Migration20240112130209.ts +++ b/packages/payment/src/migrations/Migration20240112131027.ts @@ -1,6 +1,6 @@ import { Migration } from "@mikro-orm/migrations" -export class Migration20240112130209 extends Migration { +export class Migration20240112131027 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"));' @@ -24,6 +24,9 @@ export class Migration20240112130209 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" boolean null, "authorised_at" timestamptz null, "payment_collection_id" text not null, constraint "payment-session_pkey" primary key ("id"));' ) + this.addSql( + 'create index "IDX_payment_session_payment_collection_id" on "payment-session" ("payment_collection_id");' + ) this.addSql( '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"));' @@ -31,6 +34,9 @@ export class Migration20240112130209 extends Migration { this.addSql( 'create index "IDX_payment_deleted_at" on "payment" ("deleted_at");' ) + this.addSql( + 'create index "IDX_payment_payment_collection_id" on "payment" ("payment_collection_id");' + ) this.addSql( 'alter table "payment" add constraint "payment_session_id_unique" unique ("session_id");' ) diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index 3ad850a1c6..e86c02dc91 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -87,6 +87,7 @@ export default class PaymentSession { authorised_at: Date | null @ManyToOne({ + index: "IDX_payment_session_payment_collection_id", fieldName: "payment_collection_id", }) payment_collection!: PaymentCollection diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index 8fa2009378..05e3c64f22 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -113,6 +113,7 @@ export default class Payment { captures = new Collection(this) @ManyToOne({ + index: "IDX_payment_payment_collection_id", fieldName: "payment_collection_id", }) payment_collection!: PaymentCollection From aabe1647936374e19656c59126a5e988adf68c46 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 15 Jan 2024 10:34:45 +0100 Subject: [PATCH 10/30] fix: partial feedback address --- .../migrations/.snapshot-medusa-payment.json | 864 ------------------ .../src/migrations/Migration20240112131027.ts | 84 -- packages/payment/src/models/capture.ts | 4 +- .../payment/src/models/payment-collection.ts | 11 +- .../src/models/payment-method-token.ts | 2 +- .../payment/src/models/payment-provider.ts | 2 +- .../payment/src/models/payment-session.ts | 10 +- packages/payment/src/models/payment.ts | 5 +- 8 files changed, 18 insertions(+), 964 deletions(-) delete mode 100644 packages/payment/src/migrations/.snapshot-medusa-payment.json delete mode 100644 packages/payment/src/migrations/Migration20240112131027.ts diff --git a/packages/payment/src/migrations/.snapshot-medusa-payment.json b/packages/payment/src/migrations/.snapshot-medusa-payment.json deleted file mode 100644 index 2522b71b85..0000000000 --- a/packages/payment/src/migrations/.snapshot-medusa-payment.json +++ /dev/null @@ -1,864 +0,0 @@ -{ - "namespaces": [ - "public" - ], - "name": "public", - "tables": [ - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "currency_code": { - "name": "currency_code", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "amount": { - "name": "amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "decimal" - }, - "authorized_amount": { - "name": "authorized_amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "decimal" - }, - "refunded_amount": { - "name": "refunded_amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "decimal" - }, - "region_id": { - "name": "region_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "created_at": { - "name": "created_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "length": 6, - "mappedType": "datetime" - }, - "completed_at": { - "name": "completed_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "length": 6, - "mappedType": "datetime" - }, - "status": { - "name": "status", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "default": "'not_paid'", - "enumItems": [ - "not_paid", - "awaiting", - "authorized", - "partially_authorized", - "canceled" - ], - "mappedType": "enum" - } - }, - "name": "payment-collection", - "schema": "public", - "indexes": [ - { - "columnNames": [ - "deleted_at" - ], - "composite": false, - "keyName": "IDX_payment_collection_deleted_at", - "primary": false, - "unique": false - }, - { - "keyName": "payment-collection_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": {} - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "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", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "json" - }, - "name": { - "name": "name", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "type_detail": { - "name": "type_detail", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "description_detail": { - "name": "description_detail", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "metadata": { - "name": "metadata", - "type": "jsonb", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "json" - } - }, - "name": "payment-method-token", - "schema": "public", - "indexes": [ - { - "keyName": "payment-method-token_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": {} - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "is_enabled": { - "name": "is_enabled", - "type": "boolean", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "default": "true", - "mappedType": "boolean" - } - }, - "name": "payment-provider", - "schema": "public", - "indexes": [ - { - "keyName": "payment-provider_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": {} - }, - { - "columns": { - "payment_collection_id": { - "name": "payment_collection_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "payment_provider_id": { - "name": "payment_provider_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - } - }, - "name": "payment-collection_payment_providers", - "schema": "public", - "indexes": [ - { - "keyName": "payment-collection_payment_providers_pkey", - "columnNames": [ - "payment_collection_id", - "payment_provider_id" - ], - "composite": true, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "payment-collection_payment_providers_payment_coll_6b015_foreign": { - "constraintName": "payment-collection_payment_providers_payment_coll_6b015_foreign", - "columnNames": [ - "payment_collection_id" - ], - "localTableName": "public.payment-collection_payment_providers", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.payment-collection", - "deleteRule": "cascade", - "updateRule": "cascade" - }, - "payment-collection_payment_providers_payment_provider_id_foreign": { - "constraintName": "payment-collection_payment_providers_payment_provider_id_foreign", - "columnNames": [ - "payment_provider_id" - ], - "localTableName": "public.payment-collection_payment_providers", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.payment-provider", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "currency_code": { - "name": "currency_code", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "amount": { - "name": "amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "decimal" - }, - "provider_id": { - "name": "provider_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "data": { - "name": "data", - "type": "jsonb", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "json" - }, - "status": { - "name": "status", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "enumItems": [ - "authorized", - "pending", - "requires_more", - "error", - "canceled" - ], - "mappedType": "enum" - }, - "is_selected": { - "name": "is_selected", - "type": "boolean", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "boolean" - }, - "authorised_at": { - "name": "authorised_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "length": 6, - "mappedType": "datetime" - }, - "payment_collection_id": { - "name": "payment_collection_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - } - }, - "name": "payment-session", - "schema": "public", - "indexes": [ - { - "columnNames": [ - "payment_collection_id" - ], - "composite": false, - "keyName": "IDX_payment_session_payment_collection_id", - "primary": false, - "unique": false - }, - { - "keyName": "payment-session_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "payment-session_payment_collection_id_foreign": { - "constraintName": "payment-session_payment_collection_id_foreign", - "columnNames": [ - "payment_collection_id" - ], - "localTableName": "public.payment-session", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.payment-collection", - "updateRule": "cascade" - } - } - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "amount": { - "name": "amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "decimal" - }, - "authorized_amount": { - "name": "authorized_amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "decimal" - }, - "currency_code": { - "name": "currency_code", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "provider_id": { - "name": "provider_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "cart_id": { - "name": "cart_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "order_id": { - "name": "order_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "customer_id": { - "name": "customer_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, - "data": { - "name": "data", - "type": "jsonb", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "json" - }, - "created_at": { - "name": "created_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "updated_at": { - "name": "updated_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "deleted_at": { - "name": "deleted_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "length": 6, - "mappedType": "datetime" - }, - "captured_at": { - "name": "captured_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "length": 6, - "mappedType": "datetime" - }, - "canceled_at": { - "name": "canceled_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "length": 6, - "mappedType": "datetime" - }, - "payment_collection_id": { - "name": "payment_collection_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "session_id": { - "name": "session_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - } - }, - "name": "payment", - "schema": "public", - "indexes": [ - { - "columnNames": [ - "deleted_at" - ], - "composite": false, - "keyName": "IDX_payment_deleted_at", - "primary": false, - "unique": false - }, - { - "columnNames": [ - "payment_collection_id" - ], - "composite": false, - "keyName": "IDX_payment_payment_collection_id", - "primary": false, - "unique": false - }, - { - "columnNames": [ - "session_id" - ], - "composite": false, - "keyName": "payment_session_id_unique", - "primary": false, - "unique": true - }, - { - "keyName": "payment_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "payment_payment_collection_id_foreign": { - "constraintName": "payment_payment_collection_id_foreign", - "columnNames": [ - "payment_collection_id" - ], - "localTableName": "public.payment", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.payment-collection", - "updateRule": "cascade" - }, - "payment_session_id_foreign": { - "constraintName": "payment_session_id_foreign", - "columnNames": [ - "session_id" - ], - "localTableName": "public.payment", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.payment-session", - "updateRule": "cascade" - } - } - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "amount": { - "name": "amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "decimal" - }, - "payment_id": { - "name": "payment_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "created_at": { - "name": "created_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "created_by": { - "name": "created_by", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - } - }, - "name": "capture", - "schema": "public", - "indexes": [ - { - "columnNames": [ - "payment_id" - ], - "composite": false, - "keyName": "IDX_capture_payment_id", - "primary": false, - "unique": false - }, - { - "keyName": "capture_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "capture_payment_id_foreign": { - "constraintName": "capture_payment_id_foreign", - "columnNames": [ - "payment_id" - ], - "localTableName": "public.capture", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.payment", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } - }, - { - "columns": { - "id": { - "name": "id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "amount": { - "name": "amount", - "type": "numeric", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "decimal" - }, - "payment_id": { - "name": "payment_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, - "created_at": { - "name": "created_at", - "type": "timestamptz", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "length": 6, - "default": "now()", - "mappedType": "datetime" - }, - "created_by": { - "name": "created_by", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - } - }, - "name": "refund", - "schema": "public", - "indexes": [ - { - "columnNames": [ - "payment_id" - ], - "composite": false, - "keyName": "IDX_refund_payment_id", - "primary": false, - "unique": false - }, - { - "keyName": "refund_pkey", - "columnNames": [ - "id" - ], - "composite": false, - "primary": true, - "unique": true - } - ], - "checks": [], - "foreignKeys": { - "refund_payment_id_foreign": { - "constraintName": "refund_payment_id_foreign", - "columnNames": [ - "payment_id" - ], - "localTableName": "public.refund", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.payment", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } - } - ] -} diff --git a/packages/payment/src/migrations/Migration20240112131027.ts b/packages/payment/src/migrations/Migration20240112131027.ts deleted file mode 100644 index cc588e5207..0000000000 --- a/packages/payment/src/migrations/Migration20240112131027.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { Migration } from "@mikro-orm/migrations" - -export class Migration20240112131027 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"));' - ) - this.addSql( - 'create index "IDX_payment_collection_deleted_at" on "payment-collection" ("deleted_at");' - ) - - this.addSql( - '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( - 'create table if not exists "payment-provider" ("id" text not null, "is_enabled" boolean not null default true, constraint "payment-provider_pkey" primary key ("id"));' - ) - - this.addSql( - 'create table if not exists "payment-collection_payment_providers" ("payment_collection_id" text not null, "payment_provider_id" text not null, constraint "payment-collection_payment_providers_pkey" primary key ("payment_collection_id", "payment_provider_id"));' - ) - - 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" boolean null, "authorised_at" timestamptz null, "payment_collection_id" text not null, constraint "payment-session_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_payment_session_payment_collection_id" on "payment-session" ("payment_collection_id");' - ) - - this.addSql( - '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");' - ) - this.addSql( - 'create index "IDX_payment_payment_collection_id" on "payment" ("payment_collection_id");' - ) - this.addSql( - 'alter table "payment" add constraint "payment_session_id_unique" unique ("session_id");' - ) - - 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" text null, constraint "capture_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_capture_payment_id" on "capture" ("payment_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" text null, constraint "refund_pkey" primary key ("id"));' - ) - this.addSql( - 'create index "IDX_refund_payment_id" on "refund" ("payment_id");' - ) - - this.addSql( - 'alter table "payment-collection_payment_providers" add constraint "payment-collection_payment_providers_payment_coll_6b015_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade on delete cascade;' - ) - this.addSql( - 'alter table "payment-collection_payment_providers" add constraint "payment-collection_payment_providers_payment_provider_id_foreign" foreign key ("payment_provider_id") references "payment-provider" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "payment-session" add constraint "payment-session_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade;' - ) - - this.addSql( - 'alter table "payment" add constraint "payment_payment_collection_id_foreign" foreign key ("payment_collection_id") references "payment-collection" ("id") on update cascade;' - ) - this.addSql( - 'alter table "payment" add constraint "payment_session_id_foreign" foreign key ("session_id") references "payment-session" ("id") on update cascade;' - ) - - this.addSql( - 'alter table "capture" add constraint "capture_payment_id_foreign" foreign key ("payment_id") references "payment" ("id") on update cascade on delete cascade;' - ) - - this.addSql( - 'alter table "refund" add constraint "refund_payment_id_foreign" foreign key ("payment_id") references "payment" ("id") on update cascade on delete cascade;' - ) - } -} diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts index 8f6de9e116..9952342db1 100644 --- a/packages/payment/src/models/capture.ts +++ b/packages/payment/src/models/capture.ts @@ -45,11 +45,11 @@ export default class Capture { @BeforeCreate() onCreate() { - this.id = generateEntityId(this.id, "cap") + this.id = generateEntityId(this.id, "capt") } @OnInit() onInit() { - this.id = generateEntityId(this.id, "cap") + this.id = generateEntityId(this.id, "capt") } } diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index 2cf082418b..80f41c0151 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -1,5 +1,6 @@ import { BeforeCreate, + Cascade, Collection, Entity, Enum, @@ -54,7 +55,7 @@ type OptionalPaymentCollectionProps = | "completed_at" | DAL.SoftDeletableEntityDateColumns -@Entity({ tableName: "payment-collection" }) +@Entity({ tableName: "payment_collection" }) @Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class PaymentCollection { [OptionalProps]?: OptionalPaymentCollectionProps @@ -126,22 +127,22 @@ export default class PaymentCollection { payment_providers = new Collection(this) @OneToMany(() => PaymentSession, (ps) => ps.payment_collection, { - orphanRemoval: true, + cascade: [Cascade.REMOVE], }) payment_sessions = new Collection(this) @OneToMany(() => Payment, (payment) => payment.payment_collection, { - orphanRemoval: true, + cascade: [Cascade.REMOVE], }) payments = new Collection(this) @BeforeCreate() onCreate() { - this.id = generateEntityId(this.id, "paycol") + this.id = generateEntityId(this.id, "pay_col") } @OnInit() onInit() { - this.id = generateEntityId(this.id, "paycol") + this.id = generateEntityId(this.id, "pay_col") } } diff --git a/packages/payment/src/models/payment-method-token.ts b/packages/payment/src/models/payment-method-token.ts index 9756d93040..43dd4bbec5 100644 --- a/packages/payment/src/models/payment-method-token.ts +++ b/packages/payment/src/models/payment-method-token.ts @@ -15,7 +15,7 @@ type OptionalPaymentMethodTokenProps = | "description_detail" | "metadata" -@Entity({ tableName: "payment-method-token" }) +@Entity({ tableName: "payment_method_token" }) export default class PaymentMethodToken { [OptionalProps]?: OptionalPaymentMethodTokenProps diff --git a/packages/payment/src/models/payment-provider.ts b/packages/payment/src/models/payment-provider.ts index db5f53ae99..d5ff3c03f7 100644 --- a/packages/payment/src/models/payment-provider.ts +++ b/packages/payment/src/models/payment-provider.ts @@ -1,6 +1,6 @@ import { Entity, PrimaryKey, Property } from "@mikro-orm/core" -@Entity({ tableName: "payment-provider" }) +@Entity({ tableName: "payment_provider" }) export default class PaymentProvider { @PrimaryKey({ columnType: "text" }) id: string diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index e86c02dc91..c96d651722 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -47,10 +47,10 @@ type OptionalPaymentSessionProps = | "currency_code" | "data" | "is_selected" - | "authorised_at" + | "authorized_at" | DAL.EntityDateColumns -@Entity({ tableName: "payment-session" }) +@Entity({ tableName: "payment_session" }) export default class PaymentSession { [OptionalProps]?: OptionalPaymentSessionProps @@ -84,7 +84,7 @@ export default class PaymentSession { columnType: "timestamptz", nullable: true, }) - authorised_at: Date | null + authorized_at: Date | null @ManyToOne({ index: "IDX_payment_session_payment_collection_id", @@ -101,11 +101,11 @@ export default class PaymentSession { @BeforeCreate() onCreate() { - this.id = generateEntityId(this.id, "ps") + this.id = generateEntityId(this.id, "payses") } @OnInit() onInit() { - this.id = generateEntityId(this.id, "ps") + this.id = generateEntityId(this.id, "payses") } } diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index 05e3c64f22..15941df136 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -1,5 +1,6 @@ import { BeforeCreate, + Cascade, Collection, Entity, Filter, @@ -103,12 +104,12 @@ export default class Payment { canceled_at: Date | null @OneToMany(() => Refund, (refund) => refund.payment, { - orphanRemoval: true, + cascade: [Cascade.REMOVE], }) refunds = new Collection(this) @OneToMany(() => Capture, (capture) => capture.payment, { - orphanRemoval: true, + cascade: [Cascade.REMOVE], }) captures = new Collection(this) From 1d82294ba48813cc53e2f0c20426ab366877a289 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 15 Jan 2024 10:58:31 +0100 Subject: [PATCH 11/30] fix: currency code --- packages/payment/src/models/payment-collection.ts | 5 ++--- packages/payment/src/models/payment-session.ts | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index 80f41c0151..e844224381 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -48,7 +48,6 @@ export enum PaymentCollectionStatus { } type OptionalPaymentCollectionProps = - | "currency_code" | "authorized_amount" | "refunded_amount" | "region_id" @@ -63,8 +62,8 @@ export default class PaymentCollection { @PrimaryKey({ columnType: "text" }) id: string - @Property({ columnType: "text", nullable: true }) - currency_code: string | null + @Property({ columnType: "text" }) + currency_code: string @Property({ columnType: "numeric", diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index c96d651722..ca0a4e8f5f 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -44,7 +44,6 @@ export enum PaymentSessionStatus { } type OptionalPaymentSessionProps = - | "currency_code" | "data" | "is_selected" | "authorized_at" @@ -57,8 +56,8 @@ export default class PaymentSession { @PrimaryKey({ columnType: "text" }) id: string - @Property({ columnType: "text", nullable: true }) - currency_code: string | null + @Property({ columnType: "text" }) + currency_code: string @Property({ columnType: "numeric", From 56752039cfb8df126c69434d90907e89d0a186c6 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 15 Jan 2024 15:18:02 +0100 Subject: [PATCH 12/30] wip: interface and types --- packages/types/src/payment/common.ts | 30 ++++++++++ packages/types/src/payment/index.ts | 3 + packages/types/src/payment/mutations.ts | 3 + packages/types/src/payment/service.ts | 73 ++++++++++++++++++++++++- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 packages/types/src/payment/common.ts create mode 100644 packages/types/src/payment/mutations.ts diff --git a/packages/types/src/payment/common.ts b/packages/types/src/payment/common.ts new file mode 100644 index 0000000000..01788395ed --- /dev/null +++ b/packages/types/src/payment/common.ts @@ -0,0 +1,30 @@ +import { BaseFilterable } from "../dal" +import { OperatorMap } from "../dal/utils" + +/* ********** PAYMENT COLLECTION ********** */ + +export interface PaymentCollectionDTO { + /** + * The ID of the Payment Collection + */ + id: string +} + +export interface FilterablePaymentCollectionProps + extends BaseFilterable { + id?: string | string[] + + region_id?: string | string[] | OperatorMap + + created_at?: OperatorMap + updated_at?: OperatorMap +} + +/* ********** PAYMENT ********** */ + +export interface PaymentDTO { + /** + * The ID of the Payment Collection + */ + id: string +} diff --git a/packages/types/src/payment/index.ts b/packages/types/src/payment/index.ts index 9376fea807..a83c8a61ef 100644 --- a/packages/types/src/payment/index.ts +++ b/packages/types/src/payment/index.ts @@ -1 +1,4 @@ +export * from "./common" +export * from "./mutations" export * from "./service" + diff --git a/packages/types/src/payment/mutations.ts b/packages/types/src/payment/mutations.ts new file mode 100644 index 0000000000..2785309646 --- /dev/null +++ b/packages/types/src/payment/mutations.ts @@ -0,0 +1,3 @@ +export interface CreatePaymentCollectionDTO {} + +export interface UpdatePaymentCollectionDTO {} diff --git a/packages/types/src/payment/service.ts b/packages/types/src/payment/service.ts index 80c70e30da..63d40b1680 100644 --- a/packages/types/src/payment/service.ts +++ b/packages/types/src/payment/service.ts @@ -1,3 +1,74 @@ import { IModuleService } from "../modules-sdk" +import { Context } from "../shared-context" +import { + CreatePaymentCollectionDTO, + UpdatePaymentCollectionDTO, +} from "./mutations" +import { + FilterablePaymentCollectionProps, + PaymentCollectionDTO, + PaymentDTO, +} from "./common" +import { FindConfig } from "../common" -export interface IPaymentModuleService extends IModuleService {} +export interface IPaymentModuleService extends IModuleService { + createPaymentCollection( + data: CreatePaymentCollectionDTO[], + sharedContext?: Context + ): Promise + createPaymentCollection( + data: CreatePaymentCollectionDTO, + sharedContext?: Context + ): Promise + + retrievePaymentCollection( + paymentCollectionId: string, + config?: FindConfig, + sharedContext?: Context + ): Promise + + listPaymentCollections( + filters?: FilterablePaymentCollectionProps, + config?: FindConfig, + sharedContext?: Context + ): Promise<[PaymentCollectionDTO[], number]> + + listAndCountPaymentCollections( + filters?: FilterablePaymentCollectionProps, + config?: FindConfig, + sharedContext?: Context + ): Promise<[PaymentCollectionDTO[], number]> + + updatePaymentCollection( + data: UpdatePaymentCollectionDTO[], + sharedContext?: Context + ): Promise + updatePaymentCollection( + data: UpdatePaymentCollectionDTO, + sharedContext?: Context + ): Promise + + deletePaymentCollection( + paymentCollectionId: string[], + sharedContext?: Context + ): Promise + deletePaymentCollection( + paymentCollectionId: string, + sharedContext?: Context + ): Promise + + authorizePaymentCollection( + paymentCollectionId: string, + sharedContext?: Context + ): Promise + + completePaymentCollection( + paymentCollectionId: string, + sharedContext?: Context + ): Promise + + capturePayment(paymentId: string): Promise + refundPayment(paymentId: string): Promise + + createPayment() +} From fbd3fedb68838928b92b974ac6ff91a12457485f Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 15 Jan 2024 17:15:49 +0100 Subject: [PATCH 13/30] wip: service --- packages/types/src/payment/mutations.ts | 21 +++++++++++++++++++++ packages/types/src/payment/service.ts | 12 +++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/types/src/payment/mutations.ts b/packages/types/src/payment/mutations.ts index 2785309646..e5535aafbb 100644 --- a/packages/types/src/payment/mutations.ts +++ b/packages/types/src/payment/mutations.ts @@ -1,3 +1,24 @@ +/** + * TODO + */ + export interface CreatePaymentCollectionDTO {} export interface UpdatePaymentCollectionDTO {} + +export interface CreatePaymentDTO { + amount: number + currency_code: string + provider_id: string + data: Record + + cart_id?: string + order_id?: string + customer_id?: string +} + +export interface UpdatePaymentDTO { + cart_id?: string + order_id?: string + customer_id?: string +} diff --git a/packages/types/src/payment/service.ts b/packages/types/src/payment/service.ts index 63d40b1680..4980c4a7a4 100644 --- a/packages/types/src/payment/service.ts +++ b/packages/types/src/payment/service.ts @@ -2,7 +2,9 @@ import { IModuleService } from "../modules-sdk" import { Context } from "../shared-context" import { CreatePaymentCollectionDTO, + CreatePaymentDTO, UpdatePaymentCollectionDTO, + UpdatePaymentDTO, } from "./mutations" import { FilterablePaymentCollectionProps, @@ -67,8 +69,12 @@ export interface IPaymentModuleService extends IModuleService { sharedContext?: Context ): Promise - capturePayment(paymentId: string): Promise - refundPayment(paymentId: string): Promise + createPayment(data: CreatePaymentDTO): Promise - createPayment() + capturePayment(paymentId: string, amount: number): Promise + refundPayment(paymentId: string, amount: number): Promise + + updatePayment(data: UpdatePaymentDTO): Promise + + // TODO: PaymentSession methods } From 89d6d7e846e4958976443d77ab5e52703365c7a7 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 15 Jan 2024 18:04:39 +0100 Subject: [PATCH 14/30] 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 66ec3d0931b1398b51c70362c8fd897e20186711 Mon Sep 17 00:00:00 2001 From: fPolic Date: Mon, 15 Jan 2024 18:40:05 +0100 Subject: [PATCH 15/30] wip: session methods --- packages/payment/src/models/payment.ts | 3 +++ packages/types/src/payment/mutations.ts | 4 ++++ packages/types/src/payment/service.ts | 22 +++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) 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 diff --git a/packages/types/src/payment/mutations.ts b/packages/types/src/payment/mutations.ts index e5535aafbb..1c2e6ac925 100644 --- a/packages/types/src/payment/mutations.ts +++ b/packages/types/src/payment/mutations.ts @@ -14,11 +14,15 @@ export interface CreatePaymentDTO { cart_id?: string order_id?: string + order_edit_id?: string customer_id?: string } export interface UpdatePaymentDTO { cart_id?: string order_id?: string + order_edit_id?: string customer_id?: string } + +export interface CreatePaymentSessionDTO {} diff --git a/packages/types/src/payment/service.ts b/packages/types/src/payment/service.ts index 4980c4a7a4..2b8709cbc8 100644 --- a/packages/types/src/payment/service.ts +++ b/packages/types/src/payment/service.ts @@ -3,6 +3,7 @@ import { Context } from "../shared-context" import { CreatePaymentCollectionDTO, CreatePaymentDTO, + CreatePaymentSessionDTO, UpdatePaymentCollectionDTO, UpdatePaymentDTO, } from "./mutations" @@ -69,6 +70,8 @@ export interface IPaymentModuleService extends IModuleService { sharedContext?: Context ): Promise + /* ********** PAYMENTS ********** */ + createPayment(data: CreatePaymentDTO): Promise capturePayment(paymentId: string, amount: number): Promise @@ -76,5 +79,22 @@ export interface IPaymentModuleService extends IModuleService { updatePayment(data: UpdatePaymentDTO): Promise - // TODO: PaymentSession methods + /* ********** PAYMENT SESSIONS ********** */ + + createPaymentSession( + paymentCollectionId: string, + data: CreatePaymentSessionDTO + ): Promise + + authorizePaymentSessions( + paymentCollectionId: string, + sessionIds: string[] + ): Promise + + completePaymentSessions( + paymentCollectionId: string, + sessionIds: string[] + ): Promise + + // TODO: PaymentSession set session } From 1cc888df80dbaf71ec7e0e988b725b01f480c504 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 09:56:54 +0100 Subject: [PATCH 16/30] feat: Sessions API update --- packages/types/src/payment/mutations.ts | 16 +++++++++- packages/types/src/payment/service.ts | 39 +++++++++++++++++++------ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/packages/types/src/payment/mutations.ts b/packages/types/src/payment/mutations.ts index 1c2e6ac925..aa17791fa8 100644 --- a/packages/types/src/payment/mutations.ts +++ b/packages/types/src/payment/mutations.ts @@ -25,4 +25,18 @@ export interface UpdatePaymentDTO { customer_id?: string } -export interface CreatePaymentSessionDTO {} +export interface CreatePaymentSessionDTO { + amount: number + currency_code: string + provider_id: string + + cart_id?: string + resource_id?: string + customer_id?: string +} + +export interface SetPaymentSessionsDTO { + provider_id: string + amount: number + session_id?: string +} diff --git a/packages/types/src/payment/service.ts b/packages/types/src/payment/service.ts index 2b8709cbc8..cb6adf8199 100644 --- a/packages/types/src/payment/service.ts +++ b/packages/types/src/payment/service.ts @@ -4,6 +4,7 @@ import { CreatePaymentCollectionDTO, CreatePaymentDTO, CreatePaymentSessionDTO, + SetPaymentSessionsDTO, UpdatePaymentCollectionDTO, UpdatePaymentDTO, } from "./mutations" @@ -15,6 +16,8 @@ import { import { FindConfig } from "../common" export interface IPaymentModuleService extends IModuleService { + /* ********** PAYMENT COLLECTION ********** */ + createPaymentCollection( data: CreatePaymentCollectionDTO[], sharedContext?: Context @@ -70,31 +73,49 @@ export interface IPaymentModuleService extends IModuleService { sharedContext?: Context ): Promise - /* ********** PAYMENTS ********** */ + /* ********** PAYMENT ********** */ createPayment(data: CreatePaymentDTO): Promise - capturePayment(paymentId: string, amount: number): Promise - refundPayment(paymentId: string, amount: number): Promise + capturePayment( + paymentId: string, + amount: number, + sharedContext?: Context + ): Promise + refundPayment( + paymentId: string, + amount: number, + sharedContext?: Context + ): Promise - updatePayment(data: UpdatePaymentDTO): Promise + updatePayment( + data: UpdatePaymentDTO, + sharedContext?: Context + ): Promise - /* ********** PAYMENT SESSIONS ********** */ + /* ********** PAYMENT SESSION ********** */ createPaymentSession( paymentCollectionId: string, - data: CreatePaymentSessionDTO + data: CreatePaymentSessionDTO, + sharedContext?: Context ): Promise authorizePaymentSessions( paymentCollectionId: string, - sessionIds: string[] + sessionIds: string[], + sharedContext?: Context ): Promise completePaymentSessions( paymentCollectionId: string, - sessionIds: string[] + sessionIds: string[], + sharedContext?: Context ): Promise - // TODO: PaymentSession set session + setPaymentSessions( + paymentCollectionId: string, + data: SetPaymentSessionsDTO[], + sharedContext?: Context + ): Promise } From fc6b1772a71582bb48602c5cac7b2297e9d267a9 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 10:02:58 +0100 Subject: [PATCH 17/30] chore: changeset --- .changeset/forty-moons-wash.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/forty-moons-wash.md diff --git a/.changeset/forty-moons-wash.md b/.changeset/forty-moons-wash.md new file mode 100644 index 0000000000..0c943d4fda --- /dev/null +++ b/.changeset/forty-moons-wash.md @@ -0,0 +1,5 @@ +--- +"@medusajs/types": patch +--- + +feat: Payment Module service interface From 22254a22c04330ccb6b7623ee986221ae97f3de8 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 10:30:52 +0100 Subject: [PATCH 18/30] 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 19/30] 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 20/30] 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 From 33e5b7f72fbe889e4a84e27d41ab2b0cbf013c90 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 12:25:24 +0100 Subject: [PATCH 21/30] fix: Payment merge --- packages/payment/src/models/payment.ts | 36 ++++++++++++-------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/packages/payment/src/models/payment.ts b/packages/payment/src/models/payment.ts index 0f1b5071a0..15b6e5185a 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -14,21 +14,17 @@ 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" 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) @@ -47,9 +43,9 @@ export default class Payment { @Property({ columnType: "numeric", nullable: true, - serializer: Number, + serializer: optionalNumericSerializer, }) - authorized_amount: number | null + authorized_amount?: number | null @Property({ columnType: "text" }) currency_code: string @@ -58,16 +54,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 +88,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 +116,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 From dcdb6df6f82e916d5a787ba9f9ba8a0afd38da80 Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 12:26:07 +0100 Subject: [PATCH 22/30] fix: `listPaymentCollections` shape --- packages/types/src/payment/service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/payment/service.ts b/packages/types/src/payment/service.ts index cb6adf8199..a0a58cd0f2 100644 --- a/packages/types/src/payment/service.ts +++ b/packages/types/src/payment/service.ts @@ -37,7 +37,7 @@ export interface IPaymentModuleService extends IModuleService { filters?: FilterablePaymentCollectionProps, config?: FindConfig, sharedContext?: Context - ): Promise<[PaymentCollectionDTO[], number]> + ): Promise listAndCountPaymentCollections( filters?: FilterablePaymentCollectionProps, From ca0d6f5a9f15418bd73341e1dce04033e60b714d Mon Sep 17 00:00:00 2001 From: fPolic Date: Tue, 16 Jan 2024 14:13:48 +0100 Subject: [PATCH 23/30] fix: method overloads --- packages/types/src/payment/service.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/types/src/payment/service.ts b/packages/types/src/payment/service.ts index a0a58cd0f2..94951f5545 100644 --- a/packages/types/src/payment/service.ts +++ b/packages/types/src/payment/service.ts @@ -76,6 +76,7 @@ export interface IPaymentModuleService extends IModuleService { /* ********** PAYMENT ********** */ createPayment(data: CreatePaymentDTO): Promise + createPayment(data: CreatePaymentDTO[]): Promise capturePayment( paymentId: string, @@ -92,6 +93,10 @@ export interface IPaymentModuleService extends IModuleService { data: UpdatePaymentDTO, sharedContext?: Context ): Promise + updatePayment( + data: UpdatePaymentDTO[], + sharedContext?: Context + ): Promise /* ********** PAYMENT SESSION ********** */ @@ -100,6 +105,11 @@ export interface IPaymentModuleService extends IModuleService { data: CreatePaymentSessionDTO, sharedContext?: Context ): Promise + createPaymentSession( + paymentCollectionId: string, + data: CreatePaymentSessionDTO[], + sharedContext?: Context + ): Promise authorizePaymentSessions( paymentCollectionId: string, From ef7f51afe675efc0cf5015d54e788829838bf8b6 Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 17 Jan 2024 09:43:00 +0100 Subject: [PATCH 24/30] fix: remove is selected --- packages/payment/src/models/payment-session.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index 44940dc695..868290fdc9 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -38,9 +38,6 @@ export default class PaymentSession { }) status: PaymentSessionStatus - @Property({ columnType: "boolean", nullable: true }) - is_selected?: boolean | null - @Property({ columnType: "timestamptz", nullable: true, From 6341533eb0bd23e14c19385886ad3a61db1b3a30 Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 17 Jan 2024 10:55:03 +0100 Subject: [PATCH 25/30] fix: region id --- packages/payment/src/models/payment-collection.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index 347912a9c7..f19a50249b 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -57,8 +57,8 @@ export default class PaymentCollection { }) refunded_amount?: number | null - @Property({ columnType: "text", nullable: true }) - region_id?: string | null + @Property({ columnType: "text" }) + region_id: string @Property({ onCreate: () => new Date(), From 57b88968a714c43aae499353afff4d99e4e7e42f Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 17 Jan 2024 10:59:59 +0100 Subject: [PATCH 26/30] fix: update DTO --- packages/types/src/payment/mutations.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/types/src/payment/mutations.ts b/packages/types/src/payment/mutations.ts index aa17791fa8..0359f7ba00 100644 --- a/packages/types/src/payment/mutations.ts +++ b/packages/types/src/payment/mutations.ts @@ -2,9 +2,14 @@ * TODO */ -export interface CreatePaymentCollectionDTO {} +export interface CreatePaymentCollectionDTO { + region_id: string + currency_code: string + amount: number +} -export interface UpdatePaymentCollectionDTO {} +export interface UpdatePaymentCollectionDTO + extends CreatePaymentCollectionDTO {} export interface CreatePaymentDTO { amount: number From aa1585b52cdbf7cff94fee979452aa46e9985076 Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 17 Jan 2024 12:14:30 +0100 Subject: [PATCH 27/30] feat: add region index --- packages/payment/src/models/payment-collection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index f19a50249b..5ebb5fa841 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -57,7 +57,7 @@ export default class PaymentCollection { }) refunded_amount?: number | null - @Property({ columnType: "text" }) + @Property({ columnType: "text", index: "IDX_payment_collection_region_id" }) region_id: string @Property({ From d5af7cc5f9c911e9b5edc9e78ea81eaa0148e4b5 Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 18 Jan 2024 10:50:46 +0100 Subject: [PATCH 28/30] fix: add column type --- packages/payment/src/models/payment-provider.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/payment/src/models/payment-provider.ts b/packages/payment/src/models/payment-provider.ts index b4ed62003b..42fe25d256 100644 --- a/packages/payment/src/models/payment-provider.ts +++ b/packages/payment/src/models/payment-provider.ts @@ -9,6 +9,7 @@ export default class PaymentProvider { @Property({ default: true, + columnType: "boolean", }) is_enabled: boolean = true } From 81544088d36ae556f10697228222e19c96bce3d4 Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 18 Jan 2024 10:52:14 +0100 Subject: [PATCH 29/30] fix: change type --- packages/types/src/payment/mutations.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/src/payment/mutations.ts b/packages/types/src/payment/mutations.ts index 0359f7ba00..35f72d81df 100644 --- a/packages/types/src/payment/mutations.ts +++ b/packages/types/src/payment/mutations.ts @@ -9,7 +9,7 @@ export interface CreatePaymentCollectionDTO { } export interface UpdatePaymentCollectionDTO - extends CreatePaymentCollectionDTO {} + extends Partial {} export interface CreatePaymentDTO { amount: number From c332375ff2af653314a4d68b2f6b75607e48b09d Mon Sep 17 00:00:00 2001 From: fPolic Date: Thu, 18 Jan 2024 11:56:10 +0100 Subject: [PATCH 30/30] chore: update fields to new convention --- packages/payment/src/models/capture.ts | 2 +- .../payment/src/models/payment-collection.ts | 8 ++++---- .../payment/src/models/payment-method-token.ts | 8 ++++---- packages/payment/src/models/payment-session.ts | 4 ++-- packages/payment/src/models/payment.ts | 18 +++++++++--------- packages/payment/src/models/refund.ts | 2 +- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/packages/payment/src/models/capture.ts b/packages/payment/src/models/capture.ts index 259a7dbec3..5c8d1eb53a 100644 --- a/packages/payment/src/models/capture.ts +++ b/packages/payment/src/models/capture.ts @@ -41,7 +41,7 @@ export default class Capture { created_at: Date @Property({ columnType: "text", nullable: true }) - created_by?: string | null + created_by: string | null = null @BeforeCreate() onCreate() { diff --git a/packages/payment/src/models/payment-collection.ts b/packages/payment/src/models/payment-collection.ts index 5ebb5fa841..3cc690fea1 100644 --- a/packages/payment/src/models/payment-collection.ts +++ b/packages/payment/src/models/payment-collection.ts @@ -48,14 +48,14 @@ export default class PaymentCollection { nullable: true, serializer: optionalNumericSerializer, }) - authorized_amount?: number | null + authorized_amount: number | null = null @Property({ columnType: "numeric", nullable: true, serializer: optionalNumericSerializer, }) - refunded_amount?: number | null + refunded_amount: number | null = null @Property({ columnType: "text", index: "IDX_payment_collection_region_id" }) region_id: string @@ -80,13 +80,13 @@ export default class PaymentCollection { nullable: true, index: "IDX_payment_collection_deleted_at", }) - deleted_at?: Date | null + deleted_at: Date | null = null @Property({ columnType: "timestamptz", nullable: true, }) - completed_at?: Date | null + completed_at: Date | null = null @Enum({ items: () => PaymentCollectionStatus, diff --git a/packages/payment/src/models/payment-method-token.ts b/packages/payment/src/models/payment-method-token.ts index a1f284524d..e7af8af115 100644 --- a/packages/payment/src/models/payment-method-token.ts +++ b/packages/payment/src/models/payment-method-token.ts @@ -17,19 +17,19 @@ export default class PaymentMethodToken { provider_id: string @Property({ columnType: "jsonb", nullable: true }) - data?: Record | null + data: Record | null = null @Property({ columnType: "text" }) name: string @Property({ columnType: "text", nullable: true }) - type_detail?: string | null + type_detail: string | null = null @Property({ columnType: "text", nullable: true }) - description_detail?: string | null + description_detail: string | null = null @Property({ columnType: "jsonb", nullable: true }) - metadata?: Record | null + metadata: Record | null = null @BeforeCreate() onCreate() { diff --git a/packages/payment/src/models/payment-session.ts b/packages/payment/src/models/payment-session.ts index 868290fdc9..f001852af5 100644 --- a/packages/payment/src/models/payment-session.ts +++ b/packages/payment/src/models/payment-session.ts @@ -31,7 +31,7 @@ export default class PaymentSession { provider_id: string @Property({ columnType: "jsonb", nullable: true }) - data?: Record | null + data: Record | null = null @Enum({ items: () => PaymentSessionStatus, @@ -42,7 +42,7 @@ export default class PaymentSession { columnType: "timestamptz", nullable: true, }) - authorized_at?: Date | null + authorized_at: Date | null = 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 15b6e5185a..4647be64ac 100644 --- a/packages/payment/src/models/payment.ts +++ b/packages/payment/src/models/payment.ts @@ -45,7 +45,7 @@ export default class Payment { nullable: true, serializer: optionalNumericSerializer, }) - authorized_amount?: number | null + authorized_amount: number | null = null @Property({ columnType: "text" }) currency_code: string @@ -54,19 +54,19 @@ export default class Payment { provider_id: string @Property({ columnType: "text", nullable: true }) - cart_id?: string | null + cart_id: string | null = null @Property({ columnType: "text", nullable: true }) - order_id?: string | null + order_id: string | null = null @Property({ columnType: "text", nullable: true }) - order_edit_id?: string | null + order_edit_id: string | null = null @Property({ columnType: "text", nullable: true }) - customer_id?: string | null + customer_id: string | null = null @Property({ columnType: "jsonb", nullable: true }) - data?: Record | null + data: Record | null = null @Property({ onCreate: () => new Date(), @@ -88,19 +88,19 @@ export default class Payment { nullable: true, index: "IDX_payment_deleted_at", }) - deleted_at?: Date | null + deleted_at: Date | null = null @Property({ columnType: "timestamptz", nullable: true, }) - captured_at?: Date | null + captured_at: Date | null = null @Property({ columnType: "timestamptz", nullable: true, }) - canceled_at?: Date | null + canceled_at: Date | null = null @OneToMany(() => Refund, (refund) => refund.payment, { cascade: [Cascade.REMOVE], diff --git a/packages/payment/src/models/refund.ts b/packages/payment/src/models/refund.ts index ca071ac514..4f99da250f 100644 --- a/packages/payment/src/models/refund.ts +++ b/packages/payment/src/models/refund.ts @@ -36,7 +36,7 @@ export default class Refund { created_at: Date @Property({ columnType: "text", nullable: true }) - created_by?: string | null + created_by: string | null = null @BeforeCreate() onCreate() {