Skip to content

Commit

Permalink
Merge pull request #619 from unchainedshop/user-deletion-improvements
Browse files Browse the repository at this point in the history
User deletion
  • Loading branch information
pozylon authored Dec 27, 2024
2 parents d292de7 + 3d7fac9 commit 1c8cbab
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 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 @@ -128,6 +131,10 @@ export const configurePaymentCredentialsModule = (
const paymentCredentials = await PaymentCredentials.findOneAndDelete(selector, {});
return paymentCredentials;
},
deleteUserPaymentCredentials: async (userId: string): Promise<number> => {
const { deletedCount } = await PaymentCredentials.deleteMany({ userId }, {});
return deletedCount;
},
};
};

Expand Down
3 changes: 3 additions & 0 deletions packages/core-users/src/module/configureUsersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ export const configureUsersModule = async ({
},

markDeleted: async (userId: string): Promise<User> => {
await db.collection('sessions').deleteMany({
session: { $regex: `"user":"${userId}"` },
});
const user = await Users.findOneAndUpdate(
{ _id: userId },
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,11 @@ export const configureUsersWebAuthnModule = async ({ db }: ModuleInput<any>) =>
const loginResult = await f2l.assertionResult(assertionResponse, assertionExpectations);
return { userHandle: loginResult?.authnrData?.get('userHandle') };
},
deleteUserWebAuthnCredentials: async (username: string) => {
const { deletedCount } = await WebAuthnCredentialsCreationRequests.deleteMany({
username,
});
return deletedCount;
},
};
};
14 changes: 12 additions & 2 deletions packages/core/src/services/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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.deleteUserPaymentCredentials(userId);
await this.users.webAuthn.deleteUserWebAuthnCredentials(user.username);

const carts = await this.orders.findOrders({ userId, status: null });

Expand All @@ -24,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
4 changes: 2 additions & 2 deletions packages/core/src/services/migrateUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export async function migrateUserDataService(userIdBeforeLogin: string, userId:
fromUserId: userIdBeforeLogin,
toUserId: userId,
shouldMerge: userSettings.mergeUserCartsOnLogin,
countryContext: userBeforeLogin.lastLogin?.countryCode || user.lastLogin?.countryCode,
countryContext: userBeforeLogin?.lastLogin?.countryCode || user?.lastLogin?.countryCode,
});

await migrateBookmarksService.bind(this)({
fromUserId: userIdBeforeLogin,
toUserId: userId,
shouldMerge: userSettings.mergeUserCartsOnLogin,
countryContext: userBeforeLogin.lastLogin?.countryCode || user.lastLogin?.countryCode,
countryContext: userBeforeLogin?.lastLogin?.countryCode || user?.lastLogin?.countryCode,
});
}

0 comments on commit 1c8cbab

Please sign in to comment.