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", id: "1",
role: "ADMIN", role: "ADMIN",
username: "admin", username: "admin",
Class: [
{
id: "1",
name: "Class 1",
createdAt: new Date(),
},
],
} as UserEntity, } as UserEntity,
}, },
}, },

View File

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

View File

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

View File

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

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

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