ref: format with prettier
This commit is contained in:
parent
ccf496a5d9
commit
a6d24cee9d
@ -5,7 +5,7 @@
|
||||
"license": "Apache-2.0",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"format": "prettier --write \"src/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
|
@ -1,17 +1,17 @@
|
||||
export default () => ({
|
||||
JWT: {
|
||||
secret: process.env.JWT_SECRET,
|
||||
expiresIn: process.env.JWT_EXPIRES_IN,
|
||||
refresh: {
|
||||
secret: process.env.REFRESH_JWT_SECRET,
|
||||
expiresIn: process.env.REFRESH_JWT_EXPIRES_IN,
|
||||
},
|
||||
},
|
||||
oauth2: {
|
||||
discord: {
|
||||
clientId: process.env.DISCORD_CLIENT_ID,
|
||||
clientSecret: process.env.DISCORD_CLIENT_SECRET,
|
||||
callbackUrl: process.env.DISCORD_CALLBACK_URL,
|
||||
},
|
||||
},
|
||||
});
|
||||
export default () => ({
|
||||
JWT: {
|
||||
secret: process.env.JWT_SECRET,
|
||||
expiresIn: process.env.JWT_EXPIRES_IN,
|
||||
refresh: {
|
||||
secret: process.env.REFRESH_JWT_SECRET,
|
||||
expiresIn: process.env.REFRESH_JWT_EXPIRES_IN,
|
||||
},
|
||||
},
|
||||
oauth2: {
|
||||
discord: {
|
||||
clientId: process.env.DISCORD_CLIENT_ID,
|
||||
clientSecret: process.env.DISCORD_CLIENT_SECRET,
|
||||
callbackUrl: process.env.DISCORD_CALLBACK_URL,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
@ -27,7 +27,7 @@ async function bootstrap() {
|
||||
swaggerOptions: {
|
||||
tryItOutEnabled: true,
|
||||
persistAuthorization: true,
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
await app.listen(process.env.PORT ?? 3000, "0.0.0.0");
|
||||
|
@ -1,22 +1,12 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Req,
|
||||
Res,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import { Controller, Get, Req, Res, UseGuards } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { ApiBearerAuth, ApiBody, ApiOkResponse } from "@nestjs/swagger";
|
||||
import { ApiBearerAuth, ApiOkResponse } from "@nestjs/swagger";
|
||||
|
||||
import { URLSearchParams } from "node:url";
|
||||
|
||||
import { ResfreshDTO } from "./dto/refresh.dto";
|
||||
|
||||
import { AuthService } from "./auth.service";
|
||||
import { DiscordAuthGuard } from "./guards/discord.guard";
|
||||
import { RefreshJwtAuthGuard } from "./guards/refresh.guard";
|
||||
import { AuthService } from "./auth.service";
|
||||
|
||||
@Controller("auth")
|
||||
export class AuthController {
|
||||
@ -70,10 +60,9 @@ export class AuthController {
|
||||
@Get("refresh")
|
||||
@UseGuards(RefreshJwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiBody({
|
||||
type: ResfreshDTO,
|
||||
})
|
||||
refresh(@Req() req) {
|
||||
return this.authService.accessToken(req.user);
|
||||
return {
|
||||
accessToken: this.authService.accessToken(req.user),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -10,24 +10,24 @@ export class AuthService {
|
||||
) {}
|
||||
|
||||
accessToken(user: { id: string }) {
|
||||
const payload = { id: user.id };
|
||||
return {
|
||||
accessToken: this.jwtService.sign(payload, {
|
||||
return this.jwtService.sign(
|
||||
{ id: user.id },
|
||||
{
|
||||
secret: this.configService.get<string>("JWT.secret"),
|
||||
expiresIn: this.configService.get<string>("JWT.expiresIn"),
|
||||
}),
|
||||
};
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
refreshToken(user: { id: string }) {
|
||||
const payload = { id: user.id };
|
||||
return {
|
||||
refreshToken: this.jwtService.sign(payload, {
|
||||
return this.jwtService.sign(
|
||||
{ id: user.id },
|
||||
{
|
||||
secret: this.configService.get<string>("JWT.refresh.secret"),
|
||||
expiresIn: this.configService.get<string>(
|
||||
"JWT.refresh.expiresIn",
|
||||
),
|
||||
}),
|
||||
};
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { SetMetadata } from "@nestjs/common";
|
||||
import { $Enums } from "@prisma/client";
|
||||
|
||||
export const Role = (role: $Enums.Role) => SetMetadata("role", role);
|
||||
import { SetMetadata } from "@nestjs/common";
|
||||
import { $Enums } from "@prisma/client";
|
||||
|
||||
export const Role = (role: $Enums.Role) => SetMetadata("role", role);
|
||||
|
@ -1,8 +0,0 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsJWT } from "class-validator";
|
||||
|
||||
export class ResfreshDTO {
|
||||
@ApiProperty()
|
||||
@IsJWT()
|
||||
refreshToken: string;
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class DiscordAuthGuard extends AuthGuard("discord") {
|
||||
handleRequest(err, user, info, context) {
|
||||
if (err || !user) {
|
||||
const errorMessage = info?.message || "Authentication failed";
|
||||
throw new UnauthorizedException(errorMessage);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class DiscordAuthGuard extends AuthGuard("discord") {
|
||||
handleRequest(err, user, info, context) {
|
||||
if (err || !user) {
|
||||
const errorMessage = info?.message || "Authentication failed";
|
||||
throw new UnauthorizedException(errorMessage);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard("jwt") {}
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard("jwt") {}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class RefreshJwtAuthGuard extends AuthGuard("refresh") {}
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
|
||||
@Injectable()
|
||||
export class RefreshJwtAuthGuard extends AuthGuard("refresh") {}
|
||||
|
@ -1,39 +1,36 @@
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { Reflector } from "@nestjs/core";
|
||||
|
||||
@Injectable()
|
||||
export class RolesGuard implements CanActivate {
|
||||
constructor(private readonly reflector: Reflector) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const role = this.reflector.get<string>(
|
||||
"role",
|
||||
context.getHandler(),
|
||||
);
|
||||
if (!role) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const user = request.user;
|
||||
|
||||
if (!user) {
|
||||
throw new ForbiddenException("User not authenticated");
|
||||
}
|
||||
|
||||
const hasRole = role === user.role;
|
||||
if (!hasRole) {
|
||||
throw new UnauthorizedException(
|
||||
`You need to have the role ${role} to access this resource`,
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { Reflector } from "@nestjs/core";
|
||||
|
||||
@Injectable()
|
||||
export class RolesGuard implements CanActivate {
|
||||
constructor(private readonly reflector: Reflector) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const role = this.reflector.get<string>("role", context.getHandler());
|
||||
if (!role) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const user = request.user;
|
||||
|
||||
if (!user) {
|
||||
throw new ForbiddenException("User not authenticated");
|
||||
}
|
||||
|
||||
const hasRole = role === user.role;
|
||||
if (!hasRole) {
|
||||
throw new UnauthorizedException(
|
||||
`You need to have the role ${role} to access this resource`,
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,40 +1,40 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { Profile, Strategy } from "passport-discord";
|
||||
import { AuthService } from "../auth.service";
|
||||
|
||||
@Injectable()
|
||||
export class DiscordStrategy extends PassportStrategy(Strategy, "discord") {
|
||||
configService: ConfigService;
|
||||
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
clientID: configService.get<string>("oauth2.discord.clientId"),
|
||||
clientSecret: configService.get<string>(
|
||||
"oauth2.discord.clientSecret",
|
||||
),
|
||||
callbackURL: configService.get<string>(
|
||||
"oauth2.discord.callbackUrl",
|
||||
),
|
||||
scope: ["identify", "email"],
|
||||
});
|
||||
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
async validate(
|
||||
_accessToken: string,
|
||||
_refreshToken: string,
|
||||
profile: Profile,
|
||||
done: Function,
|
||||
) {
|
||||
const accessToken = this.authService.accessToken({ id: profile.id });
|
||||
const refreshToken = this.authService.refreshToken({ id: profile.id });
|
||||
|
||||
done(null, { accessToken, refreshToken });
|
||||
}
|
||||
}
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { Profile, Strategy } from "passport-discord";
|
||||
import { AuthService } from "../auth.service";
|
||||
|
||||
@Injectable()
|
||||
export class DiscordStrategy extends PassportStrategy(Strategy, "discord") {
|
||||
configService: ConfigService;
|
||||
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
clientID: configService.get<string>("oauth2.discord.clientId"),
|
||||
clientSecret: configService.get<string>(
|
||||
"oauth2.discord.clientSecret",
|
||||
),
|
||||
callbackURL: configService.get<string>(
|
||||
"oauth2.discord.callbackUrl",
|
||||
),
|
||||
scope: ["identify", "email"],
|
||||
});
|
||||
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
async validate(
|
||||
_accessToken: string,
|
||||
_refreshToken: string,
|
||||
profile: Profile,
|
||||
done: Function,
|
||||
) {
|
||||
const accessToken = this.authService.accessToken({ id: profile.id });
|
||||
const refreshToken = this.authService.refreshToken({ id: profile.id });
|
||||
|
||||
done(null, { accessToken, refreshToken });
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,30 @@
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { User } from "@prisma/client";
|
||||
import { Strategy, ExtractJwt } from "passport-jwt";
|
||||
import { UserService } from "@Modules/user/user.service";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
@Injectable()
|
||||
export class JWTStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExipration: false,
|
||||
secretOrKey: configService.get<string>("JWT.secret"),
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any): Promise<User> {
|
||||
const user = await this.userService.findById(payload.id);
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException("User not found");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { User } from "@prisma/client";
|
||||
import { Strategy, ExtractJwt } from "passport-jwt";
|
||||
import { UserService } from "@Modules/user/user.service";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
@Injectable()
|
||||
export class JWTStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExipration: false,
|
||||
secretOrKey: configService.get<string>("JWT.secret"),
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any): Promise<User> {
|
||||
const user = await this.userService.findById(payload.id);
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException("User not found");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,30 @@
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { User } from "@prisma/client";
|
||||
import { Strategy, ExtractJwt } from "passport-jwt";
|
||||
import { UserService } from "@Modules/user/user.service";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
@Injectable()
|
||||
export class RefreshJWTStrategy extends PassportStrategy(Strategy, "refresh") {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExipration: false,
|
||||
secretOrKey: configService.get<string>("JWT.refresh.secret"),
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any): Promise<User> {
|
||||
const user = await this.userService.findById(payload.id);
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException("User not found");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
import { Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { User } from "@prisma/client";
|
||||
import { Strategy, ExtractJwt } from "passport-jwt";
|
||||
import { UserService } from "@Modules/user/user.service";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
@Injectable()
|
||||
export class RefreshJWTStrategy extends PassportStrategy(Strategy, "refresh") {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
configService: ConfigService,
|
||||
) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExipration: false,
|
||||
secretOrKey: configService.get<string>("JWT.refresh.secret"),
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any): Promise<User> {
|
||||
const user = await this.userService.findById(payload.id);
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException("User not found");
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { Injectable, OnModuleInit } from "@nestjs/common";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
}
|
||||
import { Injectable, OnModuleInit } from "@nestjs/common";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { ArrayMaxSize, ArrayMinSize, IsArray, IsString } from "class-validator";
|
||||
|
||||
export class BulkDeleteUserDTO {
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
@ArrayMinSize(1)
|
||||
@ArrayMaxSize(10)
|
||||
ids: string[];
|
||||
}
|
||||
import { ArrayMaxSize, ArrayMinSize, IsArray, IsString } from "class-validator";
|
||||
|
||||
export class BulkDeleteUserDTO {
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
@ArrayMinSize(1)
|
||||
@ArrayMaxSize(10)
|
||||
ids: string[];
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
export class CreateUserDTO {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
username: string;
|
||||
}
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
export class CreateUserDTO {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
username: string;
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { PartialType } from "@nestjs/mapped-types";
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
import { CreateUserDTO } from "./create-user.dto";
|
||||
|
||||
export class UpdateUserDTO extends PartialType(CreateUserDTO) {
|
||||
@IsString()
|
||||
id: string;
|
||||
|
||||
@IsString()
|
||||
username: string;
|
||||
}
|
||||
import { PartialType } from "@nestjs/mapped-types";
|
||||
import { IsString } from "class-validator";
|
||||
|
||||
import { CreateUserDTO } from "./create-user.dto";
|
||||
|
||||
export class UpdateUserDTO extends PartialType(CreateUserDTO) {
|
||||
@IsString()
|
||||
id: string;
|
||||
|
||||
@IsString()
|
||||
username: string;
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
import { ApiProperty, ApiSchema } from "@nestjs/swagger";
|
||||
import { $Enums } from "@prisma/client";
|
||||
import { Expose } from "class-transformer";
|
||||
|
||||
@ApiSchema({ name: "User" })
|
||||
export class UserEntity {
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
id: string;
|
||||
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
username: string;
|
||||
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
role: $Enums.Role
|
||||
|
||||
constructor(partial: Partial<UserEntity>) {
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
import { ApiProperty, ApiSchema } from "@nestjs/swagger";
|
||||
import { $Enums } from "@prisma/client";
|
||||
import { Expose } from "class-transformer";
|
||||
|
||||
@ApiSchema({ name: "User" })
|
||||
export class UserEntity {
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
id: string;
|
||||
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
username: string;
|
||||
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
role: $Enums.Role;
|
||||
|
||||
constructor(partial: Partial<UserEntity>) {
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import { Module } from "@nestjs/common";
|
||||
import { UserService } from "./user.service";
|
||||
|
||||
import { PrismaService } from "@Modules/prisma/prisma.service";
|
||||
import { UserController } from './user.controller';
|
||||
import { UserController } from "./user.controller";
|
||||
|
||||
@Module({
|
||||
providers: [UserService, PrismaService],
|
||||
|
@ -1,69 +1,69 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { PrismaService } from "@Modules/prisma/prisma.service";
|
||||
|
||||
import { CreateUserDTO } from "./dto/create-user.dto";
|
||||
import { UpdateUserDTO } from "./dto/update-user.dto";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll({
|
||||
include,
|
||||
cursor,
|
||||
take,
|
||||
}: {
|
||||
include?: Prisma.UserInclude;
|
||||
cursor?: string;
|
||||
take?: number;
|
||||
} = {}) {
|
||||
return await this.prisma.user.findMany({
|
||||
include,
|
||||
cursor: cursor ? { id: cursor } : undefined,
|
||||
take: take || 10,
|
||||
});
|
||||
}
|
||||
|
||||
async findById(id: string) {
|
||||
return await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async create(createUserInput: CreateUserDTO) {
|
||||
return await this.prisma.user.create({
|
||||
data: {
|
||||
username: createUserInput.username,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(updateUserInput: UpdateUserDTO) {
|
||||
return await this.prisma.user.update({
|
||||
where: { id: updateUserInput.id },
|
||||
data: {
|
||||
username: updateUserInput.username,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
const exist = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (!exist) throw new NotFoundException("User not found");
|
||||
|
||||
return await this.prisma.user.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async bulkDelete(ids: string[]) {
|
||||
return await this.prisma.user.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: ids,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { PrismaService } from "@Modules/prisma/prisma.service";
|
||||
|
||||
import { CreateUserDTO } from "./dto/create-user.dto";
|
||||
import { UpdateUserDTO } from "./dto/update-user.dto";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll({
|
||||
include,
|
||||
cursor,
|
||||
take,
|
||||
}: {
|
||||
include?: Prisma.UserInclude;
|
||||
cursor?: string;
|
||||
take?: number;
|
||||
} = {}) {
|
||||
return await this.prisma.user.findMany({
|
||||
include,
|
||||
cursor: cursor ? { id: cursor } : undefined,
|
||||
take: take || 10,
|
||||
});
|
||||
}
|
||||
|
||||
async findById(id: string) {
|
||||
return await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async create(createUserInput: CreateUserDTO) {
|
||||
return await this.prisma.user.create({
|
||||
data: {
|
||||
username: createUserInput.username,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(updateUserInput: UpdateUserDTO) {
|
||||
return await this.prisma.user.update({
|
||||
where: { id: updateUserInput.id },
|
||||
data: {
|
||||
username: updateUserInput.username,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
const exist = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (!exist) throw new NotFoundException("User not found");
|
||||
|
||||
return await this.prisma.user.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async bulkDelete(ids: string[]) {
|
||||
return await this.prisma.user.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: ids,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
import * as Joi from 'joi';
|
||||
|
||||
export const envValidation = Joi.object({
|
||||
DATABASE_URL: Joi.string().required(),
|
||||
|
||||
JWT_SECRET: Joi.string().required(),
|
||||
JWT_EXPIRES_IN: Joi.string().required(),
|
||||
|
||||
REFRESH_JWT_SECRET: Joi.string().required(),
|
||||
REFRESH_JWT_EXPIRES_IN: Joi.string().required(),
|
||||
|
||||
DISCORD_CLIENT_ID: Joi.string().required(),
|
||||
DISCORD_CLIENT_SECRET: Joi.string().required(),
|
||||
DISCORD_CALLBACK_URL: Joi.string().required(),
|
||||
|
||||
PORT: Joi.number().optional(),
|
||||
});
|
||||
import * as Joi from "joi";
|
||||
|
||||
export const envValidation = Joi.object({
|
||||
DATABASE_URL: Joi.string().required(),
|
||||
|
||||
JWT_SECRET: Joi.string().required(),
|
||||
JWT_EXPIRES_IN: Joi.string().required(),
|
||||
|
||||
REFRESH_JWT_SECRET: Joi.string().required(),
|
||||
REFRESH_JWT_EXPIRES_IN: Joi.string().required(),
|
||||
|
||||
DISCORD_CLIENT_ID: Joi.string().required(),
|
||||
DISCORD_CLIENT_SECRET: Joi.string().required(),
|
||||
DISCORD_CALLBACK_URL: Joi.string().required(),
|
||||
|
||||
PORT: Joi.number().optional(),
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user