Skip to content

Commit

Permalink
fix: 232 - allow user to update phone number on registration screen (#…
Browse files Browse the repository at this point in the history
…361)

Co-authored-by: Birloi Florian <[email protected]>
  • Loading branch information
birloiflorian and Birloi Florian authored Aug 14, 2024
1 parent a382525 commit bd2b8cf
Show file tree
Hide file tree
Showing 8 changed files with 1,555 additions and 1,494 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CognitoModuleOptions } from './cognito.interfaces';
import {
AdminDeleteUserCommand,
AdminUserGlobalSignOutCommand,
AdminUpdateUserAttributesCommand,
CognitoIdentityProviderClient,
} from '@aws-sdk/client-cognito-identity-provider';

Expand All @@ -24,6 +25,21 @@ export class CognitoService {
});
}

async updateUser(cognitoId: string, phoneNumber: string): Promise<unknown> {
const command = new AdminUpdateUserAttributesCommand({
UserPoolId: this.options.defaultUserPoolId,
Username: cognitoId,
UserAttributes: [
{
Name: 'phone_number',
Value: phoneNumber,
},
],
});

return await this.cognitoProvider.send(command);
}

async deleteUser(cognitoId: string): Promise<unknown> {
const deleteUserCommand = new AdminDeleteUserCommand({
UserPoolId: this.options.defaultUserPoolId,
Expand Down
5 changes: 5 additions & 0 deletions backend/src/modules/user/exceptions/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum UserExceptionCodes {
USER_005 = 'USER_005',
USER_006 = 'USER_006',
USER_007 = 'USER_007',
USER_008 = 'USER_008',
}

type UserExceptionCodeType = keyof typeof UserExceptionCodes;
Expand Down Expand Up @@ -45,4 +46,8 @@ export const UserExceptionMessages: Record<
code_error: UserExceptionCodes.USER_007,
message: 'Error while trying to delete user account',
},
[UserExceptionCodes.USER_008]: {
code_error: UserExceptionCodes.USER_008,
message: 'Error while trying to update the user data',
},
};
19 changes: 19 additions & 0 deletions backend/src/usecases/user/create-regular-user.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { ExceptionsService } from 'src/infrastructure/exceptions/exceptions.serv
import { UserExceptionMessages } from 'src/modules/user/exceptions/exceptions';
import { NotificationsSettingsFacade } from 'src/modules/notifications/notifications-settings.facade';
import { GetOneRegularUserProfileUseCase } from './get-regule-user-profile.usecase';
import { CognitoService } from 'src/infrastructure/providers/cognito/module/cognito.service';
import { JSONStringifyError } from 'src/common/helpers/utils';

@Injectable()
export class CreateRegularUsereUseCaseService
Expand All @@ -21,6 +23,7 @@ export class CreateRegularUsereUseCaseService
private exceptionService: ExceptionsService,
private readonly notificationSettingsFacade: NotificationsSettingsFacade,
private readonly getRegularUserProfileUsecase: GetOneRegularUserProfileUseCase,
private readonly cognitoService: CognitoService,
) {}

async execute(
Expand All @@ -40,11 +43,27 @@ export class CreateRegularUsereUseCaseService
const notificationsSettings =
await this.notificationSettingsFacade.create();

// 1. update cognito phone number
// OBS: This needs to be done as cognito accepts duplicates in regards to phone numbers and this will keep the data in sync
try {
await this.cognitoService.updateUser(newUser.cognitoId, newUser.phone);
} catch (error) {
this.logger.error({
...UserExceptionMessages.USER_008,
error: JSONStringifyError(error),
});
this.exceptionService.internalServerErrorException(
UserExceptionMessages.USER_008,
);
}

// 2. create new user
const user = await this.userService.createRegularUser({
...newUser,
notificationsSettingsId: notificationsSettings.id,
});

// 3. get user profile
return this.getRegularUserProfileUsecase.execute({ id: user.id });
}
}
23 changes: 20 additions & 3 deletions backend/src/usecases/user/update-regular-user.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { S3Service } from 'src/infrastructure/providers/s3/module/s3.service';
import { S3_FILE_PATHS } from 'src/common/constants/constants';
import { JSONStringifyError } from 'src/common/helpers/utils';
import { GetOneRegularUserProfileUseCase } from './get-regule-user-profile.usecase';
import { CognitoService } from 'src/infrastructure/providers/cognito/module/cognito.service';

@Injectable()
export class UpdateRegularUserUsecase
Expand All @@ -23,6 +24,7 @@ export class UpdateRegularUserUsecase
private readonly getRegularUserProfileUsecase: GetOneRegularUserProfileUseCase,
private readonly exceptionService: ExceptionsService,
private readonly s3Service: S3Service,
private readonly cognitoService: CognitoService,
) {}

async execute(
Expand All @@ -38,7 +40,22 @@ export class UpdateRegularUserUsecase
this.exceptionService.notFoundException(UserExceptionMessages.USER_001);
}

// 3. check if new profilePicture is provided
// 3. Check if the user phone number is the same as the one from the request
if (user.phone !== userUpdates.phone) {
try {
await this.cognitoService.updateUser(user.cognitoId, userUpdates.phone);
} catch (error) {
this.logger.error({
...UserExceptionMessages.USER_008,
error: JSONStringifyError(error),
});
this.exceptionService.internalServerErrorException(
UserExceptionMessages.USER_008,
);
}
}

// 4. check if new profilePicture is provided
if (profilePicture) {
try {
// 3.1 upload File and get path
Expand Down Expand Up @@ -67,13 +84,13 @@ export class UpdateRegularUserUsecase
}
}

// 4 update db structure
// 5 update db structure
const updatedUser = await this.userService.updateRegularUser(
id,
userUpdates,
);

// 5. return user profile with presigned url
// 6. return user profile with presigned url
return this.getRegularUserProfileUsecase.execute({ id: updatedUser.id });
}
}
Loading

0 comments on commit bd2b8cf

Please sign in to comment.