refactor: remove providerId from User

This commit is contained in:
M1000fr 2024-12-13 13:32:12 +01:00
parent e52c9acf2d
commit 41a9bfad98
5 changed files with 19 additions and 27 deletions

View File

@ -8,11 +8,10 @@ datasource db {
}
model User {
id String @id @default(cuid())
username String? @unique
id String @id
username String
role Role @default(STUDENT)
createdAt DateTime @default(now())
providerId String @unique
Class Class[]
SentMessages UserMessage[] @relation("SentMessages")

View File

@ -27,9 +27,10 @@ export class JwtAuthGuard implements CanActivate {
try {
const jwtPayload = await this.authService.checkToken(token);
let user = await this.userService.findOrCreateByProviderId({
providerId: jwtPayload.sub.toString(),
username: jwtPayload[this.configService.get("auth.usernameField")],
let user = await this.userService.findOrCreate({
id: jwtPayload.sub.toString(),
username:
jwtPayload[this.configService.get("auth.usernameField")],
});
request.user = user;

View File

@ -1,12 +1,17 @@
import { ApiProperty } from "@nestjs/swagger";
import { Role } from "@prisma/client";
import { IsString } from "class-validator";
export class CreateUserDTO {
@IsString()
@ApiProperty()
id: string;
@IsString()
@ApiProperty()
username: string;
@IsString()
@ApiProperty()
providerId: string;
role: Role;
}

View File

@ -35,8 +35,8 @@ export class UserGateway implements OnGatewayConnection, OnGatewayDisconnect {
return client.disconnect();
}
const user = await this.userService.findOrCreateByProviderId({
providerId: jwtPayload.sub.toString(),
const user = await this.userService.findOrCreate({
id: jwtPayload.sub.toString(),
username: jwtPayload[this.configService.get("auth.usernameField")],
});

View File

@ -37,24 +37,11 @@ export class UserService {
});
}
async findByProviderId(providerId: string) {
return await this.prisma.user.findUniqueOrThrow({
where: {
providerId,
},
});
}
async findOrCreateByProviderId({
providerId,
username,
}: {
providerId: string;
username: string;
}) {
async findOrCreate({ id, username }: { id: string; username: string }) {
let user = await this.prisma.user.findFirst({
where: {
OR: [{ providerId }, { username }],
id,
username,
},
});
@ -63,8 +50,8 @@ export class UserService {
user = await this.prisma.user.create({
data: {
id,
username,
providerId,
role: isFirstUser ? "ADMIN" : "STUDENT",
},
});
@ -76,8 +63,8 @@ export class UserService {
async create(createUserDto: CreateUserDTO) {
return await this.prisma.user.create({
data: {
id: createUserDto.id,
username: createUserDto.username,
providerId: createUserDto.providerId,
},
});
}