feat(payment): payment and session methods (#6138)

This commit is contained in:
Frane Polić
2024-02-06 13:40:22 +01:00
committed by GitHub
parent 5cabe9585f
commit 2104843826
13 changed files with 1239 additions and 319 deletions
@@ -0,0 +1,67 @@
export const defaultPaymentCollectionData = [
{
id: "pay-col-id-1",
amount: 100,
region_id: "region-id-1",
currency_code: "usd",
},
{
id: "pay-col-id-2",
amount: 200,
region_id: "region-id-1",
currency_code: "usd",
},
{
id: "pay-col-id-3",
amount: 300,
region_id: "region-id-2",
currency_code: "usd",
},
]
export const defaultPaymentSessionData = [
{
id: "pay-sess-id-1",
amount: 100,
currency_code: "usd",
provider_id: "manual",
payment_collection: "pay-col-id-1",
},
{
id: "pay-sess-id-2",
amount: 100,
currency_code: "usd",
provider_id: "manual",
payment_collection: "pay-col-id-2",
},
{
id: "pay-sess-id-3",
amount: 100,
currency_code: "usd",
provider_id: "manual",
payment_collection: "pay-col-id-2",
},
]
export const defaultPaymentData = [
{
id: "pay-id-1",
amount: 100,
currency_code: "usd",
payment_collection: "pay-col-id-1",
payment_session: "pay-sess-id-1",
provider_id: "manual",
authorized_amount: 100,
data: {},
},
{
id: "pay-id-2",
amount: 100,
authorized_amount: 100,
currency_code: "usd",
payment_collection: "pay-col-id-2",
payment_session: "pay-sess-id-2",
provider_id: "manual",
data: {},
},
]
@@ -0,0 +1,53 @@
import { EntityName } from "@mikro-orm/core"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { Payment, PaymentSession, PaymentCollection } from "@models"
import {
defaultPaymentCollectionData,
defaultPaymentData,
defaultPaymentSessionData,
} from "./data"
export * from "./data"
async function createEntities<
T extends EntityName<Payment | PaymentCollection | PaymentSession>
>(manager: SqlEntityManager, entity: T, data: any[]) {
const created: T[] = []
for (let record of data) {
created.push(manager.create(entity, record))
}
await manager.persistAndFlush(created)
return created
}
export async function createPaymentCollections(
manager: SqlEntityManager,
paymentCollectionData = defaultPaymentCollectionData
): Promise<PaymentCollection[]> {
return await createEntities<PaymentCollection>(
manager,
PaymentCollection,
paymentCollectionData
)
}
export async function createPaymentSessions(
manager: SqlEntityManager,
paymentSessionData = defaultPaymentSessionData
): Promise<PaymentCollection[]> {
return await createEntities<PaymentSession>(
manager,
PaymentSession,
paymentSessionData
)
}
export async function createPayments(
manager: SqlEntityManager,
paymentData = defaultPaymentData
): Promise<PaymentCollection[]> {
return await createEntities<Payment>(manager, Payment, paymentData)
}
@@ -1,20 +0,0 @@
export const defaultPaymentCollectionData = [
{
id: "pay-col-id-1",
amount: 100,
region_id: "region-id-1",
currency_code: "usd",
},
{
id: "pay-col-id-2",
amount: 200,
region_id: "region-id-1",
currency_code: "usd",
},
{
id: "pay-col-id-3",
amount: 300,
region_id: "region-id-2",
currency_code: "usd",
},
]
@@ -1,22 +0,0 @@
import { CreatePaymentCollectionDTO } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { PaymentCollection } from "../../../src/models"
import { defaultPaymentCollectionData } from "./data"
export * from "./data"
export async function createPaymentCollections(
manager: SqlEntityManager,
paymentCollectionData: CreatePaymentCollectionDTO[] = defaultPaymentCollectionData
): Promise<PaymentCollection[]> {
const collections: PaymentCollection[] = []
for (let data of paymentCollectionData) {
let collection = manager.create(PaymentCollection, data)
await manager.persistAndFlush(collection)
}
return collections
}