Skip to content

Commit ea0d9d2

Browse files
committed
refactor(backend): use utility function for email normalization (SCRUM-307)
1 parent 7ca3bb7 commit ea0d9d2

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Utility function to normalize email addresses to lowercase
3+
* Used in DTOs to ensure email case consistency
4+
*/
5+
export function normalizeEmail(value: unknown): string {
6+
if (typeof value === 'string') {
7+
return value.toLowerCase();
8+
}
9+
// If not a string, convert to string and lowercase
10+
return String(value).toLowerCase();
11+
}

backend/src/modules/admin/dto/create-admin.dto.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { IsEmail, IsString, IsNotEmpty } from 'class-validator';
33
import { Transform } from 'class-transformer';
4+
import { normalizeEmail } from '../../../common/utils/email-utils';
45
import { IsAllowedEmailDomain } from '../../../common/validators/allowed-email-domains.validator';
56

67
export class CreateAdminDto {
@@ -11,9 +12,7 @@ export class CreateAdminDto {
1112
})
1213
@IsEmail()
1314
@IsNotEmpty()
14-
@Transform(({ value }: { value: unknown }) =>
15-
typeof value === 'string' ? value.toLowerCase() : (value as string),
16-
)
15+
@Transform(({ value }) => normalizeEmail(value))
1716
@IsAllowedEmailDomain({
1817
message: 'Email must be from an allowed domain',
1918
})

backend/src/modules/admin/dto/supervisors-bulk-import.dto.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
IsArray,
1111
} from 'class-validator';
1212
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
13+
import { normalizeEmail } from '../../../common/utils/email-utils';
1314

1415
export class SupervisorDto {
1516
@ApiProperty({
@@ -19,9 +20,7 @@ export class SupervisorDto {
1920
})
2021
@IsEmail()
2122
@IsNotEmpty()
22-
@Transform(({ value }: { value: unknown }) =>
23-
typeof value === 'string' ? value.toLowerCase() : (value as string),
24-
)
23+
@Transform(({ value }) => normalizeEmail(value))
2524
email: string;
2625

2726
@ApiPropertyOptional({

backend/src/modules/requests/supervision/dto/create-supervision-request.dto.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiPropertyOptional } from '@nestjs/swagger';
22
import { IsEmail, IsOptional, IsUUID } from 'class-validator';
33
import { Transform } from 'class-transformer';
4+
import { normalizeEmail } from '../../../../common/utils/email-utils';
45
import { IsAllowedEmailDomain } from '../../../../common/validators/allowed-email-domains.validator';
56

67
export class CreateSupervisionRequestDto {
@@ -19,9 +20,7 @@ export class CreateSupervisionRequestDto {
1920
})
2021
@IsOptional()
2122
@IsEmail()
22-
@Transform(({ value }: { value: unknown }) =>
23-
typeof value === 'string' ? value.toLowerCase() : (value as string),
24-
)
23+
@Transform(({ value }) => normalizeEmail(value))
2524
@IsAllowedEmailDomain({
2625
message: 'Email must be from an allowed domain',
2726
})

backend/src/modules/users/dto/create-user.dto.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
22
import { IsEmail, IsOptional, IsString, IsNotEmpty } from 'class-validator';
33
import { Transform } from 'class-transformer';
4+
import { normalizeEmail } from '../../../common/utils/email-utils';
45

56
export class CreateUserDto {
67
@ApiProperty({
@@ -10,9 +11,7 @@ export class CreateUserDto {
1011
})
1112
@IsEmail()
1213
@IsNotEmpty()
13-
@Transform(({ value }: { value: unknown }) =>
14-
typeof value === 'string' ? value.toLowerCase() : (value as string),
15-
)
14+
@Transform(({ value }) => normalizeEmail(value))
1615
email: string;
1716

1817
@ApiPropertyOptional({

backend/src/modules/users/dto/update-user.dto.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiPropertyOptional } from '@nestjs/swagger';
22
import { IsEmail, IsOptional, IsString } from 'class-validator';
33
import { Transform } from 'class-transformer';
4+
import { normalizeEmail } from '../../../common/utils/email-utils';
45

56
export class UpdateUserDto {
67
@ApiPropertyOptional({
@@ -9,9 +10,7 @@ export class UpdateUserDto {
910
})
1011
@IsEmail()
1112
@IsOptional()
12-
@Transform(({ value }: { value: unknown }) =>
13-
typeof value === 'string' ? value.toLowerCase() : (value as string),
14-
)
13+
@Transform(({ value }) => normalizeEmail(value))
1514
email?: string;
1615

1716
@ApiPropertyOptional({

0 commit comments

Comments
 (0)