diff --git a/.changeset/mighty-cups-cheer.md b/.changeset/mighty-cups-cheer.md new file mode 100644 index 0000000000..9df1e88254 --- /dev/null +++ b/.changeset/mighty-cups-cheer.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +chore: refactor payment collection setPaymentSession diff --git a/packages/medusa/src/services/__tests__/payment-collection.ts b/packages/medusa/src/services/__tests__/payment-collection.ts index 9ec0cedf6f..09cf300de8 100644 --- a/packages/medusa/src/services/__tests__/payment-collection.ts +++ b/packages/medusa/src/services/__tests__/payment-collection.ts @@ -66,6 +66,13 @@ describe("PaymentCollectionService", () => { region_id: IdMap.getId("region1"), amount: 35000, status: PaymentCollectionStatus.AUTHORIZED, + region: { + payment_providers: [ + { + id: IdMap.getId("region1_provider1"), + }, + ], + }, } const zeroSample = { diff --git a/packages/medusa/src/services/payment-collection.ts b/packages/medusa/src/services/payment-collection.ts index fc4434a852..9b82f11420 100644 --- a/packages/medusa/src/services/payment-collection.ts +++ b/packages/medusa/src/services/payment-collection.ts @@ -2,7 +2,7 @@ import { DeepPartial, EntityManager } from "typeorm" import { isDefined, MedusaError } from "medusa-core-utils" import { FindConfig } from "../types/common" -import { buildQuery, setMetadata } from "../utils" +import { buildQuery, isString, setMetadata } from "../utils" import { PaymentCollectionRepository } from "../repositories/payment-collection" import { PaymentCollection, @@ -225,13 +225,13 @@ export default class PaymentCollectionService extends TransactionBaseService { /** * Manages multiple payment sessions of a payment collection. - * @param paymentCollectionId - the id of the payment collection + * @param paymentCollectionOrId - the id of the payment collection * @param sessionsInput - array containing payment session info * @param customerId - the id of the customer * @return the payment collection and its payment sessions. */ async setPaymentSessionsBatch( - paymentCollectionId: string, + paymentCollectionOrId: string | PaymentCollection, sessionsInput: PaymentCollectionsSessionsBatchInput[], customerId: string ): Promise { @@ -240,9 +240,15 @@ export default class PaymentCollectionService extends TransactionBaseService { this.paymentCollectionRepository_ ) - const payCol = await this.retrieve(paymentCollectionId, { - relations: ["region", "region.payment_providers", "payment_sessions"], - }) + const payCol: PaymentCollection = isString(paymentCollectionOrId) + ? await this.retrieve(paymentCollectionOrId, { + relations: [ + "region", + "region.payment_providers", + "payment_sessions", + ], + }) + : paymentCollectionOrId if (payCol.status !== PaymentCollectionStatus.NOT_PAID) { throw new MedusaError( @@ -362,25 +368,14 @@ export default class PaymentCollectionService extends TransactionBaseService { sessionInput: PaymentCollectionsSessionsInput, customerId: string ): Promise { - return await this.atomicPhase_(async (manager: EntityManager) => { - const paymentCollectionRepository = manager.getCustomRepository( - this.paymentCollectionRepository_ - ) - + return await this.atomicPhase_(async () => { const payCol = await this.retrieve(paymentCollectionId, { relations: ["region", "region.payment_providers", "payment_sessions"], }) - if (payCol.status !== PaymentCollectionStatus.NOT_PAID) { - throw new MedusaError( - MedusaError.Types.NOT_ALLOWED, - `Cannot set payment sessions for a payment collection with status ${payCol.status}` - ) - } - - const hasProvider = payCol?.region?.payment_providers - .map((p) => p.id) - .includes(sessionInput.provider_id) + const hasProvider = payCol?.region?.payment_providers.find( + (p) => p.id === sessionInput.provider_id + ) if (!hasProvider) { throw new MedusaError( @@ -389,72 +384,21 @@ export default class PaymentCollectionService extends TransactionBaseService { ) } - const customer = !isDefined(customerId) - ? null - : await this.customerService_ - .withTransaction(manager) - .retrieve(customerId, { - select: ["id", "email", "metadata"], - }) - .catch(() => null) - - const paymentSessions: PaymentSession[] = [] - - const inputData: PaymentSessionInput = { - cart: { - email: customer?.email || "", - context: {}, - shipping_methods: [], - shipping_address: null, - id: "", - }, - resource_id: payCol.id, - currency_code: payCol.currency_code, - amount: payCol.amount, - provider_id: sessionInput.provider_id, - customer, - } - const existingSession = payCol.payment_sessions?.find( (sess) => sessionInput.provider_id === sess?.provider_id ) - if (existingSession) { - const paymentSession = await this.paymentProviderService_ - .withTransaction(manager) - .updateSession(existingSession, inputData) - - paymentSessions.push(paymentSession) - } else { - const paymentSession = await this.paymentProviderService_ - .withTransaction(manager) - .createSession(inputData) - - paymentSessions.push(paymentSession) - - const removeSessions: PaymentSession[] = payCol.payment_sessions.filter( - ({ id }) => id != paymentSession.id - ) - - if (removeSessions.length) { - await paymentCollectionRepository.delete( - removeSessions.map((sess) => sess.id) - ) - - const paymentProviderTx = - this.paymentProviderService_.withTransaction(manager) - - Promise.all( - removeSessions.map(async (sess) => - paymentProviderTx.deleteSession(sess) - ) - ).catch(() => void 0) - } - } - - payCol.payment_sessions = paymentSessions - - return await paymentCollectionRepository.save(payCol) + return await this.setPaymentSessionsBatch( + payCol, + [ + { + ...sessionInput, + amount: payCol.amount, + session_id: existingSession?.id, + }, + ], + customerId + ) }) }