Skip to content

Commit

Permalink
🐛 Fix update user API bug
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPLukaas committed May 17, 2024
1 parent 0321494 commit 30b82ee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
8 changes: 7 additions & 1 deletion backend-nest/src/api/users/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateUserDto } from './create-user.dto';
import { IsNotEmpty } from 'class-validator';

export class UpdateUserDto extends PartialType(CreateUserDto) {}
export class UpdateUserBaseDto extends CreateUserDto {
@IsNotEmpty()
id: number;
}

export class UpdateUserDto extends PartialType(UpdateUserBaseDto) {}
4 changes: 2 additions & 2 deletions backend-nest/src/api/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class UsersController {
}

@Get(':id')
async findOne(@Param('id') id: string) {
return this.usersService.findOne({ id: Number(id) });
async findOne(@Param('id') id: number) {
return this.usersService.findOne(id);
}

@Post(':email')
Expand Down
44 changes: 35 additions & 9 deletions backend-nest/src/api/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ import { UpdateUserDto } from './dto/update-user.dto';
export class UsersService {
constructor(private prisma: PrismaService) {}

async findOne(
userWhereUniqueInput: Prisma.userWhereUniqueInput,
): Promise<user | null> {
async findOne(id: number): Promise<user | null> {
const existingUser = await this.prisma.user.findUnique({
where: userWhereUniqueInput,
where: { id: id },
});
if (!existingUser) {
throw new NotFoundException('User not found');
}
try {
return await this.prisma.user.findUnique({
where: userWhereUniqueInput,
where: { id: id },
});
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -70,7 +68,15 @@ export class UsersService {
}

async create(data: CreateUserDto): Promise<user> {
let existingUser;
let existingUser: {
id: number;
email: string;
password: string;
firstname: string;
lastname: string;
role: $Enums.user_role;
classGroupId: number;
};
try {
existingUser = await this.findOneByEmail(data.email);
} catch (error) {
Expand Down Expand Up @@ -111,7 +117,7 @@ export class UsersService {
classGroupId: number;
};
try {
existingUser = await this.findOneByEmail(params.data.email);
existingUser = await this.findOne(params.data.id);
} catch (error) {
if (error instanceof NotFoundException) {
existingUser = null;
Expand All @@ -126,6 +132,13 @@ export class UsersService {
throw new NotFoundException(errorMessage);
}

const emailExists = await this.emailExists(params.data.email);
if (emailExists && params.data.email !== existingUser.email) {
const errorMessage = 'An account with this email already exists';
console.error(errorMessage);
throw new ConflictException(errorMessage);
}

try {
return await this.prisma.user.update(params);
} catch (error) {
Expand All @@ -135,7 +148,7 @@ export class UsersService {
}

async delete(where: Prisma.userWhereUniqueInput): Promise<user> {
const existingUser = await this.findOne(where);
const existingUser = await this.findOne(where.id);
if (!existingUser) {
throw new NotFoundException('User not found');
}
Expand All @@ -152,7 +165,7 @@ export class UsersService {
}

async updateClassGroup(userId: number, classGroupId: number): Promise<user> {
const existingUser = await this.findOne({ id: userId });
const existingUser = await this.findOne(userId);
if (!existingUser) {
throw new NotFoundException('User not found');
}
Expand All @@ -171,4 +184,17 @@ export class UsersService {
);
}
}

async emailExists(email: string): Promise<boolean> {
try {
const existingUser = await this.findOneByEmail(email);
return !!existingUser;
} catch (error) {
if (error instanceof NotFoundException) {
return false;
} else {
throw error;
}
}
}
}

0 comments on commit 30b82ee

Please sign in to comment.