Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User deletion #619

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -631,6 +631,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 @@ -19,6 +19,7 @@ export interface UsersWebAuthnModule {
extensionOptions?: any,
) => Promise<any>;
verifyCredentialRequest: (userPublicKeys: any[], username: string, credentials: any) => Promise<any>;
deleteUserWebAuthnCredentials: (username: string) => Promise<number>;
}

const logger = createLogger('unchained:core-users');
Expand Down Expand Up @@ -231,5 +232,11 @@ export const configureUsersWebAuthnModule = async ({
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,
});
}
1 change: 0 additions & 1 deletion tests/products.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,6 @@ describe('Products', () => {
it('return minimum and maximum simulated price range of a configurable product', async () => {
const {
data: { product = {} },
...rest
} = await graphqlFetchAsAdmin({
query: /* GraphQL */ `
query simulatedPriceRange($productId: ID!) {
Expand Down
Loading