Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payment): PaymentCollection CRUD #6124

Merged
merged 19 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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",
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import { IPaymentModuleService } from "@medusajs/types"
import { SqlEntityManager } from "@mikro-orm/postgresql"

import { initialize } from "../../../../src/initialize"
import { DB_URL, MikroOrmWrapper } from "../../../utils"
import { createPaymentCollections } from "../../../__fixtures__/payment-collection"

jest.setTimeout(30000)

describe("Payment Module Service", () => {
let service: IPaymentModuleService
let repositoryManager: SqlEntityManager

beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()

service = await initialize({
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_PAYMNET_DB_SCHEMA,
},
})

await createPaymentCollections(repositoryManager)
})

afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})

describe("create", () => {
it("should throw an error when required params are not passed", async () => {
let error = await service
.createPaymentCollection([
{
amount: 200,
region_id: "req_123",
} as any,
])
.catch((e) => e)

expect(error.message).toContain(
"Value for PaymentCollection.currency_code is required, 'undefined' found"
)

error = await service
.createPaymentCollection([
{
currency_code: "USD",
region_id: "req_123",
} as any,
])
.catch((e) => e)

expect(error.message).toContain(
"Value for PaymentCollection.amount is required, 'undefined' found"
)

error = await service
.createPaymentCollection([
{
currency_code: "USD",
amount: 200,
} as any,
])
.catch((e) => e)

expect(error.message).toContain(
"Value for PaymentCollection.region_id is required, 'undefined' found"
)
})

it("should create a payment collection successfully", async () => {
const [createdPaymentCollection] = await service.createPaymentCollection([
{ currency_code: "USD", amount: 200, region_id: "reg_123" },
])

expect(createdPaymentCollection).toEqual(
expect.objectContaining({
id: expect.any(String),
status: "not_paid",
payment_providers: [],
payment_sessions: [],
payments: [],
currency_code: "USD",
amount: 200,
})
)
})
})

describe("delete", () => {
it("should delete a Payment Collection", async () => {
let collection = await service.listPaymentCollections({
id: ["pay-col-id-1"],
})

expect(collection.length).toEqual(1)

await service.deletePaymentCollection(["pay-col-id-1"])

collection = await service.listPaymentCollections({
id: ["pay-col-id-1"],
})

expect(collection.length).toEqual(0)
})
})

describe("retrieve", () => {
it("should retrieve a Payment Collection", async () => {
let collection = await service.retrievePaymentCollection("pay-col-id-2")

expect(collection).toEqual(
expect.objectContaining({
id: "pay-col-id-2",
amount: 200,
region_id: "region-id-1",
currency_code: "usd",
})
)
})

it("should fail to retrieve a non existent Payment Collection", async () => {
let error = await service
.retrievePaymentCollection("pay-col-id-not-exists")
.catch((e) => e)

expect(error.message).toContain(
"PaymentCollection with id: pay-col-id-not-exists was not found"
)
})
})

describe("list", () => {
it("should list and count Payment Collection", async () => {
let [collections, count] = await service.listAndCountPaymentCollections()

expect(count).toEqual(3)

expect(collections).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "pay-col-id-1",
amount: 100,
region_id: "region-id-1",
currency_code: "usd",
}),
expect.objectContaining({
id: "pay-col-id-2",
amount: 200,
region_id: "region-id-1",
currency_code: "usd",
}),
expect.objectContaining({
id: "pay-col-id-3",
amount: 300,
region_id: "region-id-2",
currency_code: "usd",
}),
])
)
})

it("should list Payment Collections by region_id", async () => {
let collections = await service.listPaymentCollections(
{
region_id: "region-id-1",
},
{ select: ["id", "amount", "region_id"] }
)

expect(collections.length).toEqual(2)

expect(collections).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "pay-col-id-1",
amount: 100,
region_id: "region-id-1",
}),
expect.objectContaining({
id: "pay-col-id-2",
amount: 200,
region_id: "region-id-1",
}),
])
)
})
})

describe("update", () => {
it("should update a Payment Collection", async () => {
await service.updatePaymentCollection({
id: "pay-col-id-2",
currency_code: "eur",
authorized_amount: 200,
})

const collection = await service.retrievePaymentCollection("pay-col-id-2")

expect(collection).toEqual(
expect.objectContaining({
id: "pay-col-id-2",
authorized_amount: 200,
currency_code: "eur",
})
)
})
})
})
7 changes: 6 additions & 1 deletion packages/payment/src/initialize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import {
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,
options?:
| ModulesSdkTypes.ModuleServiceInitializeOptions
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
| ExternalModuleDeclaration
| InternalModuleDeclaration,
injectedDependencies?: InitializeModuleInjectableDependencies
): Promise<IPaymentModuleService> => {
const loaded = await MedusaModule.bootstrap<IPaymentModuleService>({
Expand Down
3 changes: 2 additions & 1 deletion packages/payment/src/joiner-config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import { Payment } from "@models"
import { Payment, PaymentCollection } from "@models"

export const LinkableKeys = {
payment_id: Payment.name,
payment_collection_id: PaymentCollection.name,
}

const entityLinkableKeysMap: MapToConfig = {}
Expand Down
7 changes: 6 additions & 1 deletion packages/payment/src/loaders/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default async ({
)?.repositories
fPolic marked this conversation as resolved.
Show resolved Hide resolved

container.register({
// paymentService: asClass(defaultServices.PaymentService).singleton(),
paymentCollectionService: asClass(
defaultServices.PaymentCollection
).singleton(),
})

if (customRepositories) {
Expand All @@ -35,5 +37,8 @@ export default async ({
function loadDefaultRepositories({ container }) {
container.register({
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(),
paymentCollectionRepository: asClass(
defaultRepositories.PaymentCollectionRepository
).singleton(),
})
}
2 changes: 2 additions & 0 deletions packages/payment/src/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"

export { PaymentCollectionRepository } from "./payment-collection"
14 changes: 14 additions & 0 deletions packages/payment/src/repositories/payment-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
fPolic marked this conversation as resolved.
Show resolved Hide resolved
CreatePaymentCollectionDTO,
UpdatePaymentCollectionDTO,
} from "@medusajs/types"
import { DALUtils } from "@medusajs/utils"
import { PaymentCollection } from "@models"

export class PaymentCollectionRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
PaymentCollection,
{
create: CreatePaymentCollectionDTO
update: UpdatePaymentCollectionDTO
}
>(PaymentCollection) {}
1 change: 1 addition & 0 deletions packages/payment/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as PaymentModuleService } from "./payment-module"
export { default as PaymentCollection } from "./payment-collection"
fPolic marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 26 additions & 0 deletions packages/payment/src/services/payment-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PaymentCollection } from "@models"
import {
CreatePaymentCollectionDTO,
DAL,
UpdatePaymentCollectionDTO,
} from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"

type InjectedDependencies = {
paymentCollectionRepository: DAL.RepositoryService
}

export default class PaymentCollectionService<
TEntity extends PaymentCollection = PaymentCollection
> extends ModulesSdkUtils.abstractServiceFactory<
InjectedDependencies,
{
create: CreatePaymentCollectionDTO
update: UpdatePaymentCollectionDTO
}
>(PaymentCollection)<TEntity> {
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
}
}
Loading