feat(payment): PaymentCollection CRUD (#6124)

This commit is contained in:
Frane Polić
2024-01-22 11:04:46 +01:00
committed by GitHub
parent 3f41d818b6
commit d47e946496
11 changed files with 606 additions and 10 deletions
+6 -1
View File
@@ -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>({
+2 -1
View File
@@ -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 = {}
+1
View File
@@ -1 +1,2 @@
export { default as PaymentModuleService } from "./payment-module"
export { default as PaymentCollectionService } from "./payment-collection"
@@ -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)
}
}
+249 -4
View File
@@ -1,29 +1,274 @@
import {
Context,
CreatePaymentCollectionDTO,
CreatePaymentDTO,
CreatePaymentSessionDTO,
DAL,
FilterablePaymentCollectionProps,
FindConfig,
InternalModuleDeclaration,
IPaymentModuleService,
ModuleJoinerConfig,
PaymentCollectionDTO,
PaymentDTO,
SetPaymentSessionsDTO,
UpdatePaymentCollectionDTO,
UpdatePaymentDTO,
} from "@medusajs/types"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
} from "@medusajs/utils"
import { Payment } from "@models"
import * as services from "@services"
import { joinerConfig } from "../joiner-config"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
paymentCollectionService: services.PaymentCollectionService
}
// TODO: implement IPaymentModule
export default class PaymentModule<TPayment extends Payment = Payment> {
export default class PaymentModuleService implements IPaymentModuleService {
protected baseRepository_: DAL.RepositoryService
protected paymentCollectionService_: services.PaymentCollectionService
constructor(
{ baseRepository }: InjectedDependencies,
{ baseRepository, paymentCollectionService }: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
) {
this.baseRepository_ = baseRepository
this.paymentCollectionService_ = paymentCollectionService
}
__joinerConfig(): ModuleJoinerConfig {
return joinerConfig
}
createPaymentCollection(
data: CreatePaymentCollectionDTO,
sharedContext?: Context
): Promise<PaymentCollectionDTO>
createPaymentCollection(
data: CreatePaymentCollectionDTO[],
sharedContext?: Context
): Promise<PaymentCollectionDTO[]>
@InjectTransactionManager("baseRepository_")
async createPaymentCollection(
data: CreatePaymentCollectionDTO | CreatePaymentCollectionDTO[],
@MedusaContext() sharedContext?: Context
): Promise<PaymentCollectionDTO | PaymentCollectionDTO[]> {
const input = Array.isArray(data) ? data : [data]
const collections = await this.paymentCollectionService_.create(
input,
sharedContext
)
return await this.baseRepository_.serialize<PaymentCollectionDTO[]>(
Array.isArray(data) ? collections : collections[0],
{
populate: true,
}
)
}
updatePaymentCollection(
data: UpdatePaymentCollectionDTO[],
sharedContext?: Context
): Promise<PaymentCollectionDTO[]>
updatePaymentCollection(
data: UpdatePaymentCollectionDTO,
sharedContext?: Context
): Promise<PaymentCollectionDTO>
@InjectTransactionManager("baseRepository_")
async updatePaymentCollection(
data: UpdatePaymentCollectionDTO | UpdatePaymentCollectionDTO[],
sharedContext?: Context
): Promise<PaymentCollectionDTO | PaymentCollectionDTO[]> {
const input = Array.isArray(data) ? data : [data]
const result = await this.paymentCollectionService_.update(
input,
sharedContext
)
return await this.baseRepository_.serialize<PaymentCollectionDTO[]>(
Array.isArray(data) ? result : result[0],
{
populate: true,
}
)
}
deletePaymentCollection(
paymentCollectionId: string[],
sharedContext?: Context
): Promise<void>
deletePaymentCollection(
paymentCollectionId: string,
sharedContext?: Context
): Promise<void>
@InjectTransactionManager("baseRepository_")
async deletePaymentCollection(
ids: string | string[],
@MedusaContext() sharedContext?: Context
): Promise<void> {
const paymentCollectionIds = Array.isArray(ids) ? ids : [ids]
await this.paymentCollectionService_.delete(
paymentCollectionIds,
sharedContext
)
}
@InjectManager("baseRepository_")
async retrievePaymentCollection(
paymentCollectionId: string,
config: FindConfig<PaymentCollectionDTO> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<PaymentCollectionDTO> {
const paymentCollection = await this.paymentCollectionService_.retrieve(
paymentCollectionId,
config,
sharedContext
)
return await this.baseRepository_.serialize<PaymentCollectionDTO>(
paymentCollection,
{ populate: true }
)
}
@InjectManager("baseRepository_")
async listPaymentCollections(
filters: FilterablePaymentCollectionProps = {},
config: FindConfig<PaymentCollectionDTO> = {},
@MedusaContext() sharedContext?: Context
): Promise<PaymentCollectionDTO[]> {
const paymentCollections = await this.paymentCollectionService_.list(
filters,
config,
sharedContext
)
return await this.baseRepository_.serialize<PaymentCollectionDTO[]>(
paymentCollections,
{ populate: true }
)
}
@InjectManager("baseRepository_")
async listAndCountPaymentCollections(
filters: FilterablePaymentCollectionProps = {},
config: FindConfig<PaymentCollectionDTO> = {},
@MedusaContext() sharedContext?: Context
): Promise<[PaymentCollectionDTO[], number]> {
const [paymentCollections, count] =
await this.paymentCollectionService_.listAndCount(
filters,
config,
sharedContext
)
return [
await this.baseRepository_.serialize<PaymentCollectionDTO[]>(
paymentCollections,
{ populate: true }
),
count,
]
}
/**
* TODO
*/
authorizePaymentCollection(
paymentCollectionId: string,
sharedContext?: Context | undefined
): Promise<PaymentCollectionDTO> {
throw new Error("Method not implemented.")
}
completePaymentCollection(
paymentCollectionId: string,
sharedContext?: Context | undefined
): Promise<PaymentCollectionDTO> {
throw new Error("Method not implemented.")
}
createPayment(data: CreatePaymentDTO): Promise<PaymentDTO>
createPayment(data: CreatePaymentDTO[]): Promise<PaymentDTO[]>
createPayment(data: unknown): Promise<PaymentDTO | PaymentDTO[]> {
throw new Error("Method not implemented.")
}
capturePayment(
paymentId: string,
amount: number,
sharedContext?: Context | undefined
): Promise<PaymentDTO> {
throw new Error("Method not implemented.")
}
refundPayment(
paymentId: string,
amount: number,
sharedContext?: Context | undefined
): Promise<PaymentDTO> {
throw new Error("Method not implemented.")
}
updatePayment(
data: UpdatePaymentDTO,
sharedContext?: Context | undefined
): Promise<PaymentDTO>
updatePayment(
data: UpdatePaymentDTO[],
sharedContext?: Context | undefined
): Promise<PaymentDTO[]>
updatePayment(
data: unknown,
sharedContext?: unknown
): Promise<PaymentDTO | PaymentDTO[]> {
throw new Error("Method not implemented.")
}
createPaymentSession(
paymentCollectionId: string,
data: CreatePaymentSessionDTO,
sharedContext?: Context | undefined
): Promise<PaymentCollectionDTO>
createPaymentSession(
paymentCollectionId: string,
data: CreatePaymentSessionDTO[],
sharedContext?: Context | undefined
): Promise<PaymentCollectionDTO>
createPaymentSession(
paymentCollectionId: unknown,
data: unknown,
sharedContext?: unknown
): Promise<PaymentCollectionDTO> {
throw new Error("Method not implemented.")
}
authorizePaymentSessions(
paymentCollectionId: string,
sessionIds: string[],
sharedContext?: Context | undefined
): Promise<PaymentCollectionDTO> {
throw new Error("Method not implemented.")
}
completePaymentSessions(
paymentCollectionId: string,
sessionIds: string[],
sharedContext?: Context | undefined
): Promise<PaymentCollectionDTO> {
throw new Error("Method not implemented.")
}
setPaymentSessions(
paymentCollectionId: string,
data: SetPaymentSessionsDTO[],
sharedContext?: Context | undefined
): Promise<PaymentCollectionDTO> {
throw new Error("Method not implemented.")
}
}
@@ -0,0 +1,18 @@
import {
DAL,
CreatePaymentCollectionDTO,
UpdatePaymentCollectionDTO,
} from "@medusajs/types"
import { PaymentCollection } from "@models"
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IPaymentCollectionRepository<
TEntity extends PaymentCollection = PaymentCollection
> extends DAL.RepositoryService<
TEntity,
{
create: CreatePaymentCollectionDTO
update: UpdatePaymentCollectionDTO
}
> {}