Skip to content

Commit

Permalink
Add payment credential count helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikearaya committed Dec 20, 2024
1 parent 5e0768b commit c30cf57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const configurePaymentCredentialsModule = (

return {
markPreferred,

async count(query: mongodb.Filter<PaymentCredentialsType>): Promise<number> {
const credentials = await PaymentCredentials.countDocuments(query);
return credentials;
},
credentialsExists: async ({
paymentCredentialsId,
}: {
Expand Down Expand Up @@ -91,9 +94,9 @@ export const configurePaymentCredentialsModule = (
_id
? generateDbFilterById(_id)
: {
userId,
paymentProviderId,
},
userId,
paymentProviderId,
},
{
$setOnInsert: {
_id: insertedId,
Expand Down Expand Up @@ -128,7 +131,7 @@ export const configurePaymentCredentialsModule = (
const paymentCredentials = await PaymentCredentials.findOneAndDelete(selector, {});
return paymentCredentials;
},
removeUserCredentials: async (userId: string): Promise<number> => {
deleteUserPaymentCredentials: async (userId: string): Promise<number> => {
const { deletedCount } = await PaymentCredentials.deleteMany({ userId }, {});
return deletedCount;
},
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/services/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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 });
}

Expand Down

0 comments on commit c30cf57

Please sign in to comment.