Skip to content

Commit

Permalink
Merge pull request #623 from code4romania/fix/344
Browse files Browse the repository at this point in the history
fix: [344] make user phone not mandatory
  • Loading branch information
dragos1195 committed Aug 29, 2024
2 parents ea72ccb + 4f3b4f6 commit ff8eb83
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
23 changes: 23 additions & 0 deletions backend/src/migrations/1724932441086-UserPhoneNullable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class UserPhoneNullable1724932441086 implements MigrationInterface {
name = 'UserPhoneNullable1724932441086';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "user" ALTER COLUMN "phone" DROP NOT NULL`,
);
await queryRunner.query(
`ALTER TABLE "user_history" ALTER COLUMN "phone" DROP NOT NULL`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "user" ALTER COLUMN "phone" SET NOT NULL`,
);
await queryRunner.query(
`ALTER TABLE "user_history" ALTER COLUMN "phone" SET NOT NULL`,
);
}
}
3 changes: 1 addition & 2 deletions backend/src/modules/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export class CreateUserDto {
@MaxLength(50)
email: string;

@IsString()
@IsNotEmpty()
@IsOptional()
@IsPhoneValid()
phone: string;

Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/user/entities/user-history.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class UserHistory extends BaseEntity implements HistoryEntityInterface {
@Column({ type: 'varchar', name: 'email' })
email: string;

@Column({ type: 'varchar', name: 'phone' })
@Column({ type: 'varchar', name: 'phone', nullable: true })
phone: string;

@Column({ type: 'enum', enum: Role, name: 'role', default: Role.EMPLOYEE })
Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class User extends BaseEntity {
@Column({ type: 'varchar', name: 'email', unique: true })
email: string;

@Column({ type: 'varchar', name: 'phone' })
@Column({ type: 'varchar', name: 'phone', nullable: true })
phone: string;

@Column({ type: 'enum', enum: Role, name: 'role', default: Role.EMPLOYEE })
Expand Down
10 changes: 3 additions & 7 deletions backend/src/modules/user/services/cognito.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export class CognitoUserService {
Username: email,
DesiredDeliveryMediums: [DeliveryMediumType.EMAIL],
UserAttributes: [
{
Name: 'phone_number',
Value: phone,
},
...(phone ? [{ Name: 'phone_number', Value: phone }] : []),
{
Name: 'name',
Value: name,
Expand All @@ -48,9 +45,8 @@ export class CognitoUserService {
],
});

const data: AdminCreateUserCommandOutput = await this.cognitoProvider.send(
createUserCommand,
);
const data: AdminCreateUserCommandOutput =
await this.cognitoProvider.send(createUserCommand);
return data.User.Username;
}

Expand Down
5 changes: 4 additions & 1 deletion backend/src/modules/user/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ export class UserService {
) {
throw new BadRequestException(USER_ERRORS.ALREADY_EXISTS_EMAIL);
} else if (
await this.userRepository.get({ where: { phone: createUserDto.phone } })
createUserDto.phone &&
(await this.userRepository.get({
where: { phone: createUserDto.phone },
}))
) {
throw new BadRequestException(USER_ERRORS.ALREADY_EXISTS_PHONE);
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/assets/locales/ro/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@
"max": "Numărul de telefon poate avea maxim 15 caractere",
"min": "Numărul de telefon poate avea minim 10 caractere",
"invalid": "Numărul de telefon are un format invalid",
"label": "Telefon*"
"label": "Telefon"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ export const UserCreateConfig: Record<string, any> = {
phone: {
key: 'phone',
rules: {
required: {
value: true,
message: translations.phone.required,
},
maxLength: {
value: 15,
message: translations.phone.max,
Expand All @@ -92,6 +88,9 @@ export const UserCreateConfig: Record<string, any> = {
message: translations.phone.min,
},
validate: (value: string) => {
if (!value) {
return true;
}
return isValidPhoneNumber(value) || translations.phone.invalid;
},
},
Expand Down

0 comments on commit ff8eb83

Please sign in to comment.