feat(medusa): Migrate setPaymentSession to use new session flow (#2982)

**What**
When selecting a payment session, we do not necessarily run setPaymentSessions. manage the previously selected session.m accordingly.
This commit is contained in:
Adrien de Peretti authored and GitHub committed 2023-01-11 10:43:54 +00:00
1 parent 8e80e50638
commit 6239fa0704
2 files changed
+43 -13

No files matched your search

@@ -1400,7 +1400,7 @@ describe("CartService", () => {
{ {
id: IdMap.getId("test-session"), id: IdMap.getId("test-session"),
provider_id: "test-provider", provider_id: "test-provider",
is_selected: true, is_initiated: true,
}, },
], ],
}) })
@@ -1463,6 +1463,7 @@ describe("CartService", () => {
IdMap.getId("test-session"), IdMap.getId("test-session"),
{ {
is_selected: true, is_selected: true,
is_initiated: true,
} }
) )
}) })
@@ -1559,7 +1560,7 @@ describe("CartService", () => {
shipping_methods: [], shipping_methods: [],
payment_sessions: [ payment_sessions: [
{ provider_id: provider1Id, is_initiated: true }, { provider_id: provider1Id, is_initiated: true },
{ provider_id: provider2Id, is_selected: true }, { provider_id: provider2Id, is_selected: true, is_initiated: true },
], ],
region: { region: {
payment_providers: [{ id: provider1Id }, { id: provider2Id }], payment_providers: [{ id: provider1Id }, { id: provider2Id }],
+40 -11
View File
@@ -1663,11 +1663,37 @@ class CartService extends TransactionBaseService {
) )
} }
let currentlySelectedSession = cart.payment_sessions.find(
(s) => s.is_selected
)
if (
currentlySelectedSession &&
currentlySelectedSession.provider_id !== providerId
) {
const psRepo = transactionManager.getCustomRepository(
this.paymentSessionRepository_
)
if (currentlySelectedSession.is_initiated) {
await this.paymentProviderService_
.withTransaction(transactionManager)
.deleteSession(currentlySelectedSession)
currentlySelectedSession = psRepo.create(currentlySelectedSession)
}
currentlySelectedSession.is_initiated = false
currentlySelectedSession.is_selected = false
await psRepo.save(currentlySelectedSession)
}
const cartPaymentSessionIds = cart.payment_sessions.map((p) => p.id) const cartPaymentSessionIds = cart.payment_sessions.map((p) => p.id)
await psRepo.update( await psRepo.update(
{ id: In(cartPaymentSessionIds) }, { id: In(cartPaymentSessionIds) },
{ {
is_selected: null, is_selected: null,
is_initiated: false,
} }
) )
@@ -1691,21 +1717,22 @@ class CartService extends TransactionBaseService {
payment_session_id: paymentSession.id, payment_session_id: paymentSession.id,
} }
if (paymentSession.is_selected) { if (paymentSession.is_initiated) {
// update the session remotely // update the session remotely
await this.paymentProviderService_ await this.paymentProviderService_
.withTransaction(transactionManager) .withTransaction(transactionManager)
.updateSession(paymentSession, sessionInput) .updateSession(paymentSession, sessionInput)
} } else {
if (!paymentSession.is_initiated) {
// Create the session remotely // Create the session remotely
paymentSession = await this.paymentProviderService_ paymentSession = await this.paymentProviderService_
.withTransaction(transactionManager) .withTransaction(transactionManager)
.createSession(sessionInput) .createSession(sessionInput)
} }
await psRepo.update(paymentSession.id, { is_selected: true }) await psRepo.update(paymentSession.id, {
is_selected: true,
is_initiated: true,
})
await this.eventBus_ await this.eventBus_
.withTransaction(transactionManager) .withTransaction(transactionManager)
@@ -1763,7 +1790,7 @@ class CartService extends TransactionBaseService {
// Helpers that either delete a session locally or remotely. Will be used in multiple places below. // Helpers that either delete a session locally or remotely. Will be used in multiple places below.
const deleteSessionAppropriately = async (session) => { const deleteSessionAppropriately = async (session) => {
if (session.is_selected || session.is_initiated) { if (session.is_initiated) {
return paymentProviderServiceTx.deleteSession(session) return paymentProviderServiceTx.deleteSession(session)
} }
@@ -1802,7 +1829,7 @@ class CartService extends TransactionBaseService {
if (!providerSet.has(session.provider_id)) { if (!providerSet.has(session.provider_id)) {
/** /**
* if the provider does not belong to the region then delete the session. * if the provider does not belong to the region then delete the session.
* The deletion occurs locally if there is no external data or if it is not selected * The deletion occurs locally if the session is not initiated
* otherwise the deletion will also occur remotely through the external provider. * otherwise the deletion will also occur remotely through the external provider.
*/ */
@@ -1813,8 +1840,6 @@ class CartService extends TransactionBaseService {
* if the provider belongs to the region then update or delete the session. * if the provider belongs to the region then update or delete the session.
* The update occurs locally if it is not selected * The update occurs locally if it is not selected
* otherwise the update will also occur remotely through the external provider. * otherwise the update will also occur remotely through the external provider.
* In case the session is not selected but contains an external provider data, we delete the external provider
* session to be in a clean state.
*/ */
// We are saving the provider id on which the work below will be done. That way, // We are saving the provider id on which the work below will be done. That way,
@@ -1823,7 +1848,7 @@ class CartService extends TransactionBaseService {
alreadyConsumedProviderIds.add(session.provider_id) alreadyConsumedProviderIds.add(session.provider_id)
// Update remotely // Update remotely
if (session.is_selected) { if (session.is_selected && session.is_initiated) {
const paymentSessionInput = { const paymentSessionInput = {
...partialSessionInput, ...partialSessionInput,
provider_id: session.provider_id, provider_id: session.provider_id,
@@ -1843,6 +1868,7 @@ class CartService extends TransactionBaseService {
await paymentProviderServiceTx.deleteSession(session) await paymentProviderServiceTx.deleteSession(session)
updatedSession = psRepo.create({ updatedSession = psRepo.create({
...partialPaymentSessionData, ...partialPaymentSessionData,
is_initiated: false,
provider_id: session.provider_id, provider_id: session.provider_id,
}) })
} else { } else {
@@ -1872,7 +1898,10 @@ class CartService extends TransactionBaseService {
.withTransaction(transactionManager) .withTransaction(transactionManager)
.createSession(paymentSessionInput) .createSession(paymentSessionInput)
await psRepo.update(paymentSession.id, { is_selected: true }) await psRepo.update(paymentSession.id, {
is_selected: true,
is_initiated: true,
})
return return
} }