Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🐛 Combine specific keys of users, fix response
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 16, 2020
1 parent 70c5328 commit ed3a210
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class AuthController {
duration: 60,
errorMessage: 'Wait for 60 seconds before trying to merge accounts again',
})
async merge(@Body('token') token: string): Promise<void> {
async merge(@Body('token') token: string): Promise<{ success: true }> {
return this.authService.mergeUsers(token);
}
}
34 changes: 29 additions & 5 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ export class AuthService {
return ids;
}

async mergeUsers(token: string): Promise<void> {
async mergeUsers(token: string): Promise<{ success: true }> {
let baseUserId: number | undefined = undefined;
let mergeUserId: number | undefined = undefined;
try {
Expand All @@ -718,7 +718,10 @@ export class AuthService {
return this.merge(baseUserId, mergeUserId);
}

private async merge(baseUserId: number, mergeUserId: number): Promise<void> {
private async merge(
baseUserId: number,
mergeUserId: number,
): Promise<{ success: true }> {
const baseUser = await this.prisma.users.findOne({
where: { id: baseUserId },
});
Expand All @@ -727,9 +730,26 @@ export class AuthService {
});
if (!baseUser || !mergeUser) throw new NotFoundException(USER_NOT_FOUND);

const combinedUser = { ...baseUser };
Object.keys(mergeUser).forEach((key) => {
if (mergeUser[key]) combinedUser[key] = mergeUser[key];
const combinedUser: Record<string, any> = {};
[
'checkLocationOnLogin',
'countryCode',
'gender',
'name',
'notificationEmails',
'active',
'prefersLanguage',
'prefersColorScheme',
'prefersReducedMotion',
'profilePictureUrl',
'role',
'timezone',
'twoFactorMethod',
'twoFactorPhone',
'twoFactorSecret',
'attributes',
].forEach((key) => {
if (mergeUser[key] != null) combinedUser[key] = mergeUser[key];
});
await this.prisma.users.update({
where: { id: baseUserId },
Expand All @@ -744,6 +764,7 @@ export class AuthService {
this.prisma.backupCodes,
this.prisma.identities,
this.prisma.auditLogs,
this.prisma.apiKeys,
]) {
for await (const item of await (dataType as emailsDelegate).findMany({
where: { user: { id: mergeUserId } },
Expand All @@ -754,5 +775,8 @@ export class AuthService {
data: { user: { connect: { id: baseUserId } } },
});
}

await this.prisma.users.delete({ where: { id: mergeUser.id } });
return { success: true };
}
}
2 changes: 1 addition & 1 deletion src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class UserController {
async mergeRequest(
@Param('userId', ParseIntPipe) id: number,
@Body('email') email: string,
): Promise<void> {
): Promise<{ queued: true }> {
return this.usersService.requestMerge(Number(id), email);
}
}
5 changes: 3 additions & 2 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class UsersService {
return this.prisma.expose<users>(user);
}

async requestMerge(userId: number, email: string): Promise<void> {
async requestMerge(userId: number, email: string): Promise<{ queued: true }> {
const emailSafe = safeEmail(email);
const user = await this.prisma.users.findFirst({
where: { emails: { some: { emailSafe } } },
Expand All @@ -122,7 +122,7 @@ export class UsersService {
const minutes = parseInt(
this.configService.get<string>('security.mergeUsersTokenExpiry') ?? '',
);
return this.email.send({
this.email.send({
to: `"${user.name}" <${user.prefersEmail.email}>`,
template: 'auth/mfa-code',
data: {
Expand All @@ -137,5 +137,6 @@ export class UsersService {
)}`,
},
});
return { queued: true };
}
}

0 comments on commit ed3a210

Please sign in to comment.