Skip to content

Commit

Permalink
chore: Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Jan 4, 2023
1 parent 7e1f90a commit bb07143
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions packages/medusa/src/services/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1907,11 +1907,13 @@ class CartService extends TransactionBaseService {
)

if (paymentSession) {
// Delete the session with the provider
await psRepo.delete(paymentSession)
/* await this.paymentProviderService_
.withTransaction(transactionManager)
.deleteSession(paymentSession)*/
if (paymentSession.is_selected) {
await this.paymentProviderService_
.withTransaction(transactionManager)
.deleteSession(paymentSession)
} else {
await psRepo.delete(paymentSession)
}
}
}

Expand All @@ -1930,12 +1932,12 @@ class CartService extends TransactionBaseService {
* @param cartId - the id of the cart to remove from
* @param providerId - the id of the provider whoose payment session
* should be removed.
* @return {Promise<Cart>} the resulting cart.
* @return {Promise<void>} the resulting cart.
*/
async refreshPaymentSession(
cartId: string,
providerId: string
): Promise<Cart> {
): Promise<void> {
return await this.atomicPhase_(
async (transactionManager: EntityManager) => {
const cart = await this.retrieveWithTotals(cartId, {
Expand All @@ -1948,25 +1950,31 @@ class CartService extends TransactionBaseService {
)

if (paymentSession) {
// Delete the session with the provider
await this.paymentProviderService_
.withTransaction(transactionManager)
.refreshSession(paymentSession, {
cart: cart as Cart,
customer: cart.customer,
if (paymentSession.is_selected) {
await this.paymentProviderService_
.withTransaction(transactionManager)
.refreshSession(paymentSession, {
cart: cart as Cart,
customer: cart.customer,
amount: cart.total,
currency_code: cart.region.currency_code,
provider_id: providerId,
})
} else {
const psRepo = transactionManager.getCustomRepository(
this.paymentSessionRepository_
)
await psRepo.update(paymentSession.id, {
amount: cart.total,
currency_code: cart.region.currency_code,
provider_id: providerId,
})
}
}
}

const updatedCart = await this.retrieve(cartId)

await this.eventBus_
.withTransaction(transactionManager)
.emit(CartService.Events.UPDATED, updatedCart)
return updatedCart
.emit(CartService.Events.UPDATED, { id: cartId })
}
)
}
Expand Down

0 comments on commit bb07143

Please sign in to comment.