feat: add Class property to UserEntity and update user retrieval to include classes

This commit is contained in:
Rémi 2025-01-08 00:20:55 +01:00
parent 7aab149bb2
commit 7fa7e4448d
5 changed files with 31 additions and 9 deletions

View File

@ -11,6 +11,13 @@ export const UserResponse = {
id: "1",
role: "ADMIN",
username: "admin",
Class: [
{
id: "1",
name: "Class 1",
createdAt: new Date(),
},
],
} as UserEntity,
},
},

View File

@ -1,3 +1,4 @@
import { ClassEntity } from "@/modules/class/entities/class.entity";
import { ApiProperty, ApiSchema } from "@nestjs/swagger";
import { Expose } from "class-transformer";
@ -11,6 +12,10 @@ export class UserEntity {
@ApiProperty()
username: string;
@Expose()
@ApiProperty()
Class?: ClassEntity[];
constructor(partial: Partial<UserEntity>) {
Object.assign(this, partial);
}

View File

@ -11,7 +11,7 @@ import {
Patch,
Query,
Req,
UseGuards
UseGuards,
} from "@nestjs/common";
import {
ApiBearerAuth,
@ -19,8 +19,9 @@ import {
ApiOperation,
ApiParam,
ApiQuery,
ApiUnauthorizedResponse
ApiUnauthorizedResponse,
} from "@nestjs/swagger";
import { Request } from "express";
import {
UserCountResponse,
UserResponse,
@ -29,7 +30,6 @@ import {
import { UpdateUserDTO } from "./dto/update-user.dto";
import { UserEntity } from "./entities/user.entity";
import { UserService } from "./user.service";
import { Request } from "express";
@Controller("user")
@UseGuards(RolesGuard)
@ -45,14 +45,21 @@ export class UserController {
@ApiOperation({ summary: "Get all users" })
async findAll(): Promise<UserEntity[]> {
return await this.userService
.findAll()
.findAll({
include: {
Class: true,
},
})
.then((users) => users.map((user) => new UserEntity(user)));
}
@Get(":id")
@ApiOkResponse(UserResponse)
@ApiOperation({ summary: "Get user by id" })
async findOne(@Param("id") id: string, @Req() req: Request): Promise<UserEntity> {
async findOne(
@Param("id") id: string,
@Req() req: Request,
): Promise<UserEntity> {
if (id === "@me") id = req.user.id;
return this.userService

View File

@ -7,7 +7,7 @@ import { UpdateUserDTO } from "./dto/update-user.dto";
@Injectable()
export class UserService {
constructor(private readonly prisma: PrismaService) { }
constructor(private readonly prisma: PrismaService) {}
async findAll({
skip,
@ -15,12 +15,14 @@ export class UserService {
cursor,
where,
orderBy,
include,
}: {
skip?: number;
take?: number;
cursor?: Prisma.UserWhereUniqueInput;
where?: Prisma.UserWhereInput;
orderBy?: Record<string, unknown>;
include?: Prisma.UserInclude;
} = {}) {
return await this.prisma.user.findMany({
skip,
@ -28,6 +30,7 @@ export class UserService {
cursor,
where,
orderBy,
include,
});
}
@ -48,7 +51,7 @@ export class UserService {
user = await this.prisma.user.create({
data: {
id,
username
username,
},
});
} else if (user.username !== username) {
@ -103,7 +106,7 @@ export class UserService {
select: {
id: true,
username: true,
}
},
});
}
}

2
src/types/http.d.ts vendored
View File

@ -4,6 +4,6 @@ import { IncomingMessage } from "http";
declare module "http" {
interface IncomingMessage {
user?: UserEntity;
user?: User;
}
}