From da42db7467dababb876005f0d121674f79576de9 Mon Sep 17 00:00:00 2001 From: Mikael Araya Date: Thu, 19 Dec 2024 15:43:19 +0300 Subject: [PATCH] Add payment credential count helper --- .../module/configurePaymentCredentialsModule.ts | 13 ++++++++----- packages/core/src/services/deleteUser.ts | 14 +++++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/core-payment/src/module/configurePaymentCredentialsModule.ts b/packages/core-payment/src/module/configurePaymentCredentialsModule.ts index f7dd408bd..f0e9a1081 100644 --- a/packages/core-payment/src/module/configurePaymentCredentialsModule.ts +++ b/packages/core-payment/src/module/configurePaymentCredentialsModule.ts @@ -36,7 +36,10 @@ export const configurePaymentCredentialsModule = ( return { markPreferred, - + async count(query: mongodb.Filter): Promise { + const credentials = await PaymentCredentials.countDocuments(query); + return credentials; + }, credentialsExists: async ({ paymentCredentialsId, }: { @@ -91,9 +94,9 @@ export const configurePaymentCredentialsModule = ( _id ? generateDbFilterById(_id) : { - userId, - paymentProviderId, - }, + userId, + paymentProviderId, + }, { $setOnInsert: { _id: insertedId, @@ -128,7 +131,7 @@ export const configurePaymentCredentialsModule = ( const paymentCredentials = await PaymentCredentials.findOneAndDelete(selector, {}); return paymentCredentials; }, - removeUserCredentials: async (userId: string): Promise => { + deleteUserPaymentCredentials: async (userId: string): Promise => { const { deletedCount } = await PaymentCredentials.deleteMany({ userId }, {}); return deletedCount; }, diff --git a/packages/core/src/services/deleteUser.ts b/packages/core/src/services/deleteUser.ts index f6fb8a5f5..07de4eb5f 100644 --- a/packages/core/src/services/deleteUser.ts +++ b/packages/core/src/services/deleteUser.ts @@ -8,7 +8,7 @@ export async function deleteUserService(this: Modules, { userId }: { userId: str await this.bookmarks.deleteByUserId(userId); await this.quotations.deleteRequestedUserQuotations(userId); await this.enrollments.deleteInactiveUserEnrollments(userId); - await this.payment.paymentCredentials.removeUserCredentials(userId); + await this.payment.paymentCredentials.deleteUserPaymentCredentials(userId); await this.users.webAuthn.deleteUserCredentials(user.username); const carts = await this.orders.findOrders({ userId, status: null }); @@ -26,8 +26,16 @@ export async function deleteUserService(this: Modules, { userId }: { userId: str const reviewsCount = await this.products.reviews.count({ authorId: userId }); const enrollmentsCount = await this.enrollments.count({ userId }); const tokens = await this.warehousing.findTokensForUser({ userId }); - - if (!ordersCount && !reviewsCount && !enrollmentsCount && !quotationsCount && !tokens?.length) { + const paymentCredentials = await this.payment.paymentCredentials.count({ userId }); + + if ( + !ordersCount && + !reviewsCount && + !enrollmentsCount && + !quotationsCount && + !tokens?.length && + !paymentCredentials + ) { await this.users.deletePermanently({ userId }); }