From 1d87cbafef3fab8549db940558faaff33973200b Mon Sep 17 00:00:00 2001 From: richard483 Date: Sat, 13 Jan 2024 17:39:18 +0700 Subject: [PATCH 1/2] added bugfix 13 jan --- docs/auth-controller.md | 2 +- docs/user-controller.md | 8 +++--- package-lock.json | 4 +-- package.json | 2 +- .../migration.sql | 18 ++++++++++++ prisma/schema.prisma | 14 ++++++---- src/auth/auth.controller.ts | 8 +++--- src/auth/auth.service.ts | 10 +++---- .../authenticate.dto.ts} | 11 ++++++-- .../register.dto.ts} | 2 +- src/auth/test/auth.controller.spec.ts | 4 +-- src/interceptors/response.interceptor.ts | 2 +- src/job/interface/job.interface.ts | 8 ------ src/job/test/job.controller.spec.ts | 10 +++---- src/job/test/job.service.spec.ts | 4 +-- src/rating/dto/rating-create.dto.ts | 2 +- src/rating/dto/rating-update.dto.ts | 2 +- src/rating/interface/rating.interface.ts | 2 +- src/rating/test/rating.controller.spec.ts | 14 +++++----- src/rating/test/rating.service.spec.ts | 6 ++-- .../user-create.dto.ts} | 11 ++++++-- .../user-filter.dto.ts} | 2 +- .../user-update.dto.ts} | 28 +++++++++++++------ src/users/interface/user.interface.ts | 2 +- src/users/test/users.controller.spec.ts | 2 +- src/users/user.controller.ts | 18 ++++++------ src/users/user.repository.ts | 4 +-- src/users/users.service.ts | 11 ++++++-- 28 files changed, 127 insertions(+), 84 deletions(-) create mode 100644 prisma/migrations/20240113095800_updated_rating_and_user_field/migration.sql rename src/auth/{requests/authenticate.request.ts => dto/authenticate.dto.ts} (63%) rename src/auth/{requests/register.request.ts => dto/register.dto.ts} (96%) delete mode 100644 src/job/interface/job.interface.ts rename src/users/{requests/user-create.request.ts => dto/user-create.dto.ts} (81%) rename src/users/{requests/user-filter.request.ts => dto/user-filter.dto.ts} (88%) rename src/users/{requests/user-update.request.ts => dto/user-update.dto.ts} (82%) diff --git a/docs/auth-controller.md b/docs/auth-controller.md index 1fb7b57..a9af4a4 100644 --- a/docs/auth-controller.md +++ b/docs/auth-controller.md @@ -129,7 +129,7 @@ HTTP Code: 200 "previousWorkplaceCount": null, "ratingsAvg": null, "companyId": null, - "portofolio": [], + "portfolio": [], "profilePicture": null, "hasGoogleAccount": false } diff --git a/docs/user-controller.md b/docs/user-controller.md index 6f04f39..7024e4a 100644 --- a/docs/user-controller.md +++ b/docs/user-controller.md @@ -23,7 +23,7 @@ | `description` | String | | `previousWorkPlace` | String[] | | `companyId` | String | -| `portofolio` | String[] | +| `portfolio` | String[] | | `profilePicture` | String | ### Success Response @@ -54,7 +54,7 @@ HTTP Code: 200 "previousWorkplaceCount": 3, "ratingsAvg": null, "companyId": null, - "portofolio": [], + "portfolio": [], "profilePicture": "https://www.google.com/url?sa=i&url=https%3A%2F%2Fgithub.com%2Fnestjs%2Fswagger%2Fissues%2F1062&psig=AOvVaw3aa3sM4HwPBnbxa_JISyrs&ust=1704354787503000&source=images&cd=vfe&opi=89978449&ved=0CBEQjRxqFwoTCKDOkcfewIMDFQAAAAAdAAAAABAD", "hasGoogleAccount": false } @@ -164,7 +164,7 @@ HTTP Code: 200 "previousWorkplaceCount": 3, "ratingsAvg": null, "companyId": null, - "portofolio": [], + "portfolio": [], "profilePicture": "https://firebasestorage.googleapis.com/v0/b/tech-395620.appspot.com/o/profilePicture%2Fd29636a9-ecdd-4905-9568-27e0af35732f.jpg?alt=media&token=25968c38-961d-41f2-be7e-2262ee44e142", "hasGoogleAccount": false } @@ -220,7 +220,7 @@ HTTP Code: 200 "previousWorkplaceCount": 0, "ratingsAvg": null, "companyId": null, - "portofolio": [], + "portfolio": [], "profilePicture": null, "hasGoogleAccount": false } diff --git a/package-lock.json b/package-lock.json index f6a2dfe..c1bff0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "backend-8tech", - "version": "4.0.1", + "version": "4.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "backend-8tech", - "version": "4.0.1", + "version": "4.0.3", "license": "UNLICENSED", "dependencies": { "@nestjs/common": "^9.4.3", diff --git a/package.json b/package.json index 552b2e5..60fd935 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "backend-8tech", - "version": "4.0.2", + "version": "4.0.3", "description": "", "author": "8tech", "private": true, diff --git a/prisma/migrations/20240113095800_updated_rating_and_user_field/migration.sql b/prisma/migrations/20240113095800_updated_rating_and_user_field/migration.sql new file mode 100644 index 0000000..943cbdc --- /dev/null +++ b/prisma/migrations/20240113095800_updated_rating_and_user_field/migration.sql @@ -0,0 +1,18 @@ +/* + Warnings: + + - You are about to drop the column `givenByUserId` on the `Rating` table. All the data in the column will be lost. + - You are about to drop the column `portofolio` on the `User` table. All the data in the column will be lost. + - Added the required column `recruiterUserId` to the `Rating` table without a default value. This is not possible if the table is not empty. + - Added the required column `updatedAt` to the `Rating` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "Rating" DROP COLUMN "givenByUserId", +ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, +ADD COLUMN "recruiterUserId" TEXT NOT NULL, +ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL; + +-- AlterTable +ALTER TABLE "User" DROP COLUMN "portofolio", +ADD COLUMN "portfolio" TEXT[]; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 86a5e6c..4f308d2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -29,7 +29,7 @@ model User { ratings Rating[] ratingsAvg Float? companyId String? - portofolio String[] + portfolio String[] @map("portfolio") profilePicture String? hasGoogleAccount Boolean @default(false) company Company? @relation(fields: [companyId], references: [id]) @@ -88,11 +88,13 @@ model Payment { } model Rating { - id String @id @default(uuid()) - User User @relation(fields: [userId], references: [id]) - userId String - givenByUserId String - ratingOf10 Int + id String @id @default(uuid()) + User User @relation(fields: [userId], references: [id]) + userId String + recruiterUserId String + ratingOf10 Int + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt } enum Role { diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index e3f4513..c29f905 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -11,11 +11,11 @@ import { AuthService } from './auth.service'; import { ApiBearerAuth, ApiCookieAuth, ApiTags } from '@nestjs/swagger'; import { Roles } from './roles/role.decorator'; import { Role } from './roles/role.enum'; -import { AuthenticateRequest } from './requests/authenticate.request'; +import { AuthenticateRequestDto } from './dto/authenticate.dto'; import { JwtAuthGuard } from './jwt/jwt-auth.guard'; import { RoleGuard } from './roles/role.guard'; import { GoogleGuard } from './google/google.guard'; -import { RegisterRequest } from './requests/register.request'; +import { RegisterRequestDto } from './dto/register.dto'; @ApiTags('Auth') @Controller('auth') @@ -23,14 +23,14 @@ export class AuthController { constructor(private authService: AuthService) {} @Post('login') - async signIn(@Res() res, @Body() authenticateDto: AuthenticateRequest) { + async signIn(@Res() res, @Body() authenticateDto: AuthenticateRequestDto) { console.info('#AuthLogin request incoming with: ', authenticateDto); const response = await this.authService.login(authenticateDto, res); return response; } @Post('register') - async signUp(@Res() res, @Body() request: RegisterRequest) { + async signUp(@Res() res, @Body() request: RegisterRequestDto) { console.info('#AuthRegister request incoming with: ', request); const response = await this.authService.register(request); return response; diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index ca8c597..42ad971 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,11 +1,11 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { UsersService } from '../users/users.service'; -import { AuthenticateRequest } from './requests/authenticate.request'; +import { AuthenticateRequestDto } from './dto/authenticate.dto'; import { IAuthenticate, IGoogleUser } from './interface/auth.interface'; import { IUser } from '../users/interface/user.interface'; import { compare, genSaltSync, hashSync } from 'bcrypt'; -import { RegisterRequest } from './requests/register.request'; +import { RegisterRequestDto } from './dto/register.dto'; import { Role } from '@prisma/client'; @Injectable() @@ -16,7 +16,7 @@ export class AuthService { ) {} async validateUser( - authenticateRequest: AuthenticateRequest, + authenticateRequest: AuthenticateRequestDto, ): Promise { const user = await this.usersService.findOne(authenticateRequest.email); if (!user) { @@ -42,7 +42,7 @@ export class AuthService { } async login( - authenticateRequest: AuthenticateRequest, + authenticateRequest: AuthenticateRequestDto, res, ): Promise { const user = await this.validateUser(authenticateRequest); @@ -50,7 +50,7 @@ export class AuthService { return user; } - async register(request: RegisterRequest): Promise { + async register(request: RegisterRequestDto): Promise { let data; const { isRecruiter, ...userCreate } = request; if (isRecruiter) { diff --git a/src/auth/requests/authenticate.request.ts b/src/auth/dto/authenticate.dto.ts similarity index 63% rename from src/auth/requests/authenticate.request.ts rename to src/auth/dto/authenticate.dto.ts index 61e2ba2..d63501a 100644 --- a/src/auth/requests/authenticate.request.ts +++ b/src/auth/dto/authenticate.dto.ts @@ -1,10 +1,17 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsEmail, IsNotEmpty, IsString } from 'class-validator'; -export class AuthenticateRequest { +export class AuthenticateRequestDto { @ApiProperty() @IsNotEmpty() - @IsEmail() + @IsEmail( + { + allow_display_name: false, + }, + { + message: 'INVALID_EMAIL', + }, + ) readonly email: string; @ApiProperty() diff --git a/src/auth/requests/register.request.ts b/src/auth/dto/register.dto.ts similarity index 96% rename from src/auth/requests/register.request.ts rename to src/auth/dto/register.dto.ts index 25d2e44..64b3f31 100644 --- a/src/auth/requests/register.request.ts +++ b/src/auth/dto/register.dto.ts @@ -1,7 +1,7 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsEmail, IsNotEmpty, IsOptional, Matches } from 'class-validator'; -export class RegisterRequest { +export class RegisterRequestDto { @ApiProperty() @IsNotEmpty({ message: 'MUST_NOT_BE_EMPTY', diff --git a/src/auth/test/auth.controller.spec.ts b/src/auth/test/auth.controller.spec.ts index 2f8a08a..2090ad4 100644 --- a/src/auth/test/auth.controller.spec.ts +++ b/src/auth/test/auth.controller.spec.ts @@ -21,7 +21,7 @@ const mockIAuthSuccessResponse: IAuthenticate = { roles: ['MEMBER'], hasGoogleAccount: false, description: 'desc', - portofolio: [], + portfolio: [], }, token: 'token', }; @@ -42,7 +42,7 @@ const mockIUserResponse: IUser = { roles: ['MEMBER'], hasGoogleAccount: false, description: 'desc', - portofolio: [], + portfolio: [], }; describe('AuthController', () => { diff --git a/src/interceptors/response.interceptor.ts b/src/interceptors/response.interceptor.ts index 8a8c4fe..4a0b574 100644 --- a/src/interceptors/response.interceptor.ts +++ b/src/interceptors/response.interceptor.ts @@ -81,7 +81,7 @@ export class ResponseInterceptor implements NestInterceptor { '#responseHandler: trying to set cookie for this response: ', res, ); - if (res != undefined) { + if (res != undefined && res?.token != undefined) { response.cookie('EToken', res?.token); console.log( '#responseHandler: success to set cookie for this response: ', diff --git a/src/job/interface/job.interface.ts b/src/job/interface/job.interface.ts deleted file mode 100644 index d81f064..0000000 --- a/src/job/interface/job.interface.ts +++ /dev/null @@ -1,8 +0,0 @@ -export interface IJob { - id: string; - title: string; - description: string; - createdAt: Date; - updatedAt: Date; - companyId: string; -} diff --git a/src/job/test/job.controller.spec.ts b/src/job/test/job.controller.spec.ts index 9620c28..9ec5888 100644 --- a/src/job/test/job.controller.spec.ts +++ b/src/job/test/job.controller.spec.ts @@ -5,7 +5,7 @@ import { JwtAuthGuard } from '../../auth/jwt/jwt-auth.guard'; import { RoleGuard } from '../../auth/roles/role.guard'; import { JobController } from '../job.controller'; import { JobService } from '../job.service'; -import { IJob } from '../interface/job.interface'; +import { JobVacancy } from '@prisma/client'; describe('JobController', () => { let controller: JobController; @@ -43,7 +43,7 @@ describe('JobController', () => { companyId: 'test', }; - const mockJob: IJob = { + const mockJob: JobVacancy = { id: 'randomId', title: 'deez noot', description: 'this is description about job that is created for test', @@ -100,7 +100,7 @@ describe('JobController', () => { }); it('deleteJob success', async () => { - const mockJob: IJob = { + const mockJob: JobVacancy = { id: 'randomId', title: 'deez noot', description: 'this is description about job that is created for test', @@ -160,7 +160,7 @@ describe('JobController', () => { } }); it('updateJob success', async () => { - const mockJob: IJob = { + const mockJob: JobVacancy = { id: 'randomId', title: 'deez noot', description: 'this is description about job that is created for test', @@ -224,7 +224,7 @@ describe('JobController', () => { } }); it('getJob success', async () => { - const mockJob: IJob = { + const mockJob: JobVacancy = { id: 'randomId', title: 'deez noot', description: 'this is description about job that is created for test', diff --git a/src/job/test/job.service.spec.ts b/src/job/test/job.service.spec.ts index 6f53ed5..d7c9954 100644 --- a/src/job/test/job.service.spec.ts +++ b/src/job/test/job.service.spec.ts @@ -1,14 +1,14 @@ import { Test, TestingModule } from '@nestjs/testing'; import { DeepMocked, createMock } from '@golevelup/ts-jest'; import { JobService } from '../job.service'; -import { IJob } from '../interface/job.interface'; import { JobRepository } from '../job.repository'; import { JobUpdateDto } from '../dto/job-update.dto'; +import { JobVacancy } from '@prisma/client'; describe('JobService', () => { let service: JobService; let reposiotry: DeepMocked; - let jobMock: IJob; + let jobMock: JobVacancy; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ diff --git a/src/rating/dto/rating-create.dto.ts b/src/rating/dto/rating-create.dto.ts index 355736b..fd2cc77 100644 --- a/src/rating/dto/rating-create.dto.ts +++ b/src/rating/dto/rating-create.dto.ts @@ -10,7 +10,7 @@ export class RatingCreateDto { @ApiProperty() @IsNotEmpty() @IsString() - readonly givenByUserId: string; + readonly recruiterUserId: string; @ApiProperty() @IsNotEmpty() diff --git a/src/rating/dto/rating-update.dto.ts b/src/rating/dto/rating-update.dto.ts index d251ad6..5b8c226 100644 --- a/src/rating/dto/rating-update.dto.ts +++ b/src/rating/dto/rating-update.dto.ts @@ -15,7 +15,7 @@ export class RatingUpdateDto { @ApiProperty() @IsNotEmpty() @IsString() - readonly givenByUserId: string; + readonly recruiterUserId: string; @ApiProperty() @IsNotEmpty() diff --git a/src/rating/interface/rating.interface.ts b/src/rating/interface/rating.interface.ts index 8b58866..7e74aba 100644 --- a/src/rating/interface/rating.interface.ts +++ b/src/rating/interface/rating.interface.ts @@ -1,5 +1,5 @@ export interface IRating { id: string; - givenByUserId: string; + recruiterUserId: string; ratingOf10?: number; } diff --git a/src/rating/test/rating.controller.spec.ts b/src/rating/test/rating.controller.spec.ts index 7146bdd..a5371c0 100644 --- a/src/rating/test/rating.controller.spec.ts +++ b/src/rating/test/rating.controller.spec.ts @@ -41,13 +41,13 @@ describe('RatingController', () => { it('craeteRating success', async () => { const createRatingDTO: RatingCreateDto = { userId: 'test', - givenByUserId: 'test', + recruiterUserId: 'test', ratingOf10: 9, }; const mockJob: IRating = { id: 'ratingId', - givenByUserId: 'userId', + recruiterUserId: 'userId', ratingOf10: 9, }; @@ -101,12 +101,12 @@ describe('RatingController', () => { const ratingUpdateDto: RatingUpdateDto = { id: 'randomId', userId: 'test', - givenByUserId: 'test', + recruiterUserId: 'test', ratingOf10: 9, }; const mockJob: IRating = { id: 'ratingId', - givenByUserId: 'userId', + recruiterUserId: 'userId', ratingOf10: 9, }; @@ -126,7 +126,7 @@ describe('RatingController', () => { expect(updateSpy).toBeCalledWith({ id: 'randomId', - givenByUserId: 'test', + recruiterUserId: 'test', ratingOf10: 9, userId: 'test', }); @@ -138,7 +138,7 @@ describe('RatingController', () => { const ratingUpdateDto: RatingUpdateDto = { id: 'randomId', userId: 'test', - givenByUserId: 'test', + recruiterUserId: 'test', ratingOf10: 9, }; const mockResponse = { @@ -164,7 +164,7 @@ describe('RatingController', () => { expect(updateSpy).toBeCalledWith({ id: 'randomId', userId: 'test', - givenByUserId: 'test', + recruiterUserId: 'test', ratingOf10: 9, }); expect(e).toEqual(mockResponse); diff --git a/src/rating/test/rating.service.spec.ts b/src/rating/test/rating.service.spec.ts index 0f6a290..fe70721 100644 --- a/src/rating/test/rating.service.spec.ts +++ b/src/rating/test/rating.service.spec.ts @@ -22,7 +22,7 @@ describe('RatingService', () => { reposiotry = module.get(RatingRepository); ratingMock = { id: 'randomId', - givenByUserId: 'randomUserId', + recruiterUserId: 'randomUserId', ratingOf10: 9, }; }); @@ -34,7 +34,7 @@ describe('RatingService', () => { it('create success', async () => { const createRatingDto: RatingCreateDto = { userId: 'randomUserId', - givenByUserId: 'randomUserId', + recruiterUserId: 'randomUserId', ratingOf10: 9, }; const createSpy = jest @@ -53,7 +53,7 @@ describe('RatingService', () => { const ratingUpdate: RatingUpdateDto = { id: 'randomId', userId: 'randomUserId', - givenByUserId: 'randomUserId', + recruiterUserId: 'randomUserId', ratingOf10: 9, }; const updateSpy = jest diff --git a/src/users/requests/user-create.request.ts b/src/users/dto/user-create.dto.ts similarity index 81% rename from src/users/requests/user-create.request.ts rename to src/users/dto/user-create.dto.ts index c210691..5b31330 100644 --- a/src/users/requests/user-create.request.ts +++ b/src/users/dto/user-create.dto.ts @@ -2,10 +2,17 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsArray, IsEmail, IsNotEmpty, IsString } from 'class-validator'; import { Role } from '../../auth/roles/role.enum'; -export class UserCreateRequest { +export class UserCreateRequestDto { @ApiProperty() @IsNotEmpty() - @IsEmail() + @IsEmail( + { + allow_display_name: false, + }, + { + message: 'INVALID_EMAIL', + }, + ) readonly email: string; @ApiProperty() diff --git a/src/users/requests/user-filter.request.ts b/src/users/dto/user-filter.dto.ts similarity index 88% rename from src/users/requests/user-filter.request.ts rename to src/users/dto/user-filter.dto.ts index 4bbc6dd..c755ccc 100644 --- a/src/users/requests/user-filter.request.ts +++ b/src/users/dto/user-filter.dto.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; -export class UserFilterRequest { +export class UserFilterRequestDto { @ApiProperty() readonly field?: string; diff --git a/src/users/requests/user-update.request.ts b/src/users/dto/user-update.dto.ts similarity index 82% rename from src/users/requests/user-update.request.ts rename to src/users/dto/user-update.dto.ts index d0c2460..e950d9f 100644 --- a/src/users/requests/user-update.request.ts +++ b/src/users/dto/user-update.dto.ts @@ -2,7 +2,7 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator'; import { Role } from '../../auth/roles/role.enum'; -export class UserUpdateRequest { +export class UserUpdateRequestDto { @ApiProperty() @IsNotEmpty({ message: 'MUST_NOT_BE_EMPTY', @@ -10,7 +10,14 @@ export class UserUpdateRequest { readonly id: string; @ApiProperty() - @IsEmail() + @IsEmail( + { + allow_display_name: false, + }, + { + message: 'INVALID_EMAIL', + }, + ) @IsOptional() readonly email: string; @@ -46,9 +53,8 @@ export class UserUpdateRequest { readonly companyId: string; @ApiProperty() - @IsString() @IsOptional() - readonly portofolio: string; + readonly portfolio: string[]; @ApiProperty() @IsString() @@ -56,9 +62,16 @@ export class UserUpdateRequest { readonly profilePicture: string; } -export class UserUpdateRequestMe { +export class UserUpdateRequestMeDto { @ApiProperty() - @IsEmail() + @IsEmail( + { + allow_display_name: false, + }, + { + message: 'INVALID_EMAIL', + }, + ) @IsOptional() readonly email: string; @@ -94,9 +107,8 @@ export class UserUpdateRequestMe { readonly companyId: string; @ApiProperty() - @IsString() @IsOptional() - readonly portofolio: string; + readonly portfolio: string[]; @ApiProperty() @IsString() diff --git a/src/users/interface/user.interface.ts b/src/users/interface/user.interface.ts index 5e1fdcd..7b0da95 100644 --- a/src/users/interface/user.interface.ts +++ b/src/users/interface/user.interface.ts @@ -13,7 +13,7 @@ export interface IUser { previousWorkplaceId?: any; ratings?: any; companyId?: string; - portofolio: string[]; + portfolio: string[]; profilePicture?: string; hasGoogleAccount: boolean; } diff --git a/src/users/test/users.controller.spec.ts b/src/users/test/users.controller.spec.ts index a957496..b8b7891 100644 --- a/src/users/test/users.controller.spec.ts +++ b/src/users/test/users.controller.spec.ts @@ -60,7 +60,7 @@ describe('AuthController', () => { password: 'password', ratingsAvg: 0, companyId: '1', - portofolio: ['1', '2'], + portfolio: ['1', '2'], previousWorkplaceCount: 0, previousWorkplaceId: ['1', '2'], profilePicture: 'picture', diff --git a/src/users/user.controller.ts b/src/users/user.controller.ts index d5e0ac3..8a78db5 100644 --- a/src/users/user.controller.ts +++ b/src/users/user.controller.ts @@ -15,12 +15,12 @@ import { Roles } from '../auth/roles/role.decorator'; import { JwtAuthGuard } from '../auth/jwt/jwt-auth.guard'; import { RoleGuard } from '../auth/roles/role.guard'; import { Role } from '../auth/roles/role.enum'; -import { UserCreateRequest } from './requests/user-create.request'; -import { UserFilterRequest } from './requests/user-filter.request'; +import { UserCreateRequestDto } from './dto/user-create.dto'; +import { UserFilterRequestDto } from './dto/user-filter.dto'; import { - UserUpdateRequest, - UserUpdateRequestMe, -} from './requests/user-update.request'; + UserUpdateRequestDto, + UserUpdateRequestMeDto, +} from './dto/user-update.dto'; import { Express } from 'express'; import { FileInterceptor } from '@nestjs/platform-express'; @@ -47,7 +47,7 @@ export class UserController { @Roles(Role.ADMIN) @UseGuards(JwtAuthGuard, RoleGuard) @Post('create') - async createAdmin(@Res() res, @Body() user: UserCreateRequest) { + async createAdmin(@Res() res, @Body() user: UserCreateRequestDto) { const response = await this.userService.create(user); return response; } @@ -56,7 +56,7 @@ export class UserController { @Roles(Role.ADMIN) @UseGuards(JwtAuthGuard, RoleGuard) @Post('update') - async update(@Res() res, @Body() data: UserUpdateRequest) { + async update(@Res() res, @Body() data: UserUpdateRequestDto) { const response = await this.userService.update(data); return response; } @@ -65,7 +65,7 @@ export class UserController { @Roles(Role.ADMIN, Role.USER) @UseGuards(JwtAuthGuard, RoleGuard) @Post('update/me') - async updateMe(@Request() req, @Body() data: UserUpdateRequestMe) { + async updateMe(@Request() req, @Body() data: UserUpdateRequestMeDto) { const response = await this.userService.update({ ...data, id: req.user.id, @@ -77,7 +77,7 @@ export class UserController { @Roles(Role.USER) @UseGuards(JwtAuthGuard, RoleGuard) @Post('filter') - async filterUser(@Res() res, @Body() body: UserFilterRequest) { + async filterUser(@Res() res, @Body() body: UserFilterRequestDto) { const response = await this.userService.findManyByList(body); return response; } diff --git a/src/users/user.repository.ts b/src/users/user.repository.ts index 1ef0a7d..248f4e7 100644 --- a/src/users/user.repository.ts +++ b/src/users/user.repository.ts @@ -1,7 +1,7 @@ import { HttpException, Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { pagination, returnablePaginated } from '../prisma/prisma.util'; -import { UserFilterRequest } from './requests/user-filter.request'; +import { UserFilterRequestDto } from './dto/user-filter.dto'; import { User } from '@prisma/client'; @Injectable() @@ -109,7 +109,7 @@ export class UserRepository { }); } - async findManyByFieldAndSortBy(reqData: UserFilterRequest): Promise { + async findManyByFieldAndSortBy(reqData: UserFilterRequestDto): Promise { const result = this.prisma.user.findMany({ ...pagination(reqData.page, reqData.size), where: this.field[reqData.field] diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 766e2ac..71ef050 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -1,6 +1,6 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { UserRepository } from './user.repository'; -import { UserFilterRequest } from './requests/user-filter.request'; +import { UserFilterRequestDto } from './dto/user-filter.dto'; import { UserHelper } from './user.helper'; import { User } from '@prisma/client'; @@ -28,7 +28,12 @@ export class UsersService { user.username, ); if (existingUsername !== null) { - throw new HttpException('USERNAME_ALREADY_USED', HttpStatus.BAD_REQUEST); + throw new HttpException( + { + username: 'USERNAME_ALREADY_USED', + }, + HttpStatus.BAD_REQUEST, + ); } return this.userRepository.create(user); } @@ -52,7 +57,7 @@ export class UsersService { return user; } - async findManyByList(data: UserFilterRequest): Promise { + async findManyByList(data: UserFilterRequestDto): Promise { return this.userRepository.findManyByFieldAndSortBy(data); } From f451a1604822de469393129ec163a119a0aa8b6b Mon Sep 17 00:00:00 2001 From: richard483 Date: Sat, 13 Jan 2024 17:57:08 +0700 Subject: [PATCH 2/2] updated some styling --- src/contract/contract.controller.ts | 14 +++++++------- src/contract/contract.repository.ts | 4 ++-- src/contract/contract.service.ts | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/contract/contract.controller.ts b/src/contract/contract.controller.ts index c165151..4886679 100644 --- a/src/contract/contract.controller.ts +++ b/src/contract/contract.controller.ts @@ -41,7 +41,6 @@ export class ContractController { return response; } - //9432dae7-ba04-410a-a4ff-27d6da87ae63 @Get('generate/:contractId') @ApiParam({ name: 'contractId', type: String }) @Roles(Role.USER) @@ -72,8 +71,9 @@ export class ContractController { @ApiParam({ name: 'userId', type: String }) async getAllbyUserId(@Res() res, @Param('userId') userId: string) { console.info('#getAllbyUserId request incoming with: ', userId); - const response: Contract[] = - await this.contractService.getAllbyUserId(userId); + const response: Contract[] = await this.contractService.getAllbyUserId( + userId, + ); return response; } @@ -82,8 +82,9 @@ export class ContractController { @ApiParam({ name: 'jobId', type: String }) async getAllbyJobId(@Res() res, @Param('jobId') jobId: string) { console.info('#getAllbyJobId request incoming with: ', jobId); - const response: Contract[] = - await this.contractService.getAllbyJobId(jobId); + const response: Contract[] = await this.contractService.getAllbyJobId( + jobId, + ); return response; } @@ -92,8 +93,7 @@ export class ContractController { @ApiParam({ name: 'Id', type: String }) async getContractbyId(@Res() res, @Param('Id') Id: string) { console.info('#getContractbyId request incoming with: ', Id); - const response: Contract = - await this.contractService.getContractbyId(Id); + const response: Contract = await this.contractService.getContractbyId(Id); return response; } diff --git a/src/contract/contract.repository.ts b/src/contract/contract.repository.ts index 4aaddce..2fbf86c 100644 --- a/src/contract/contract.repository.ts +++ b/src/contract/contract.repository.ts @@ -16,7 +16,7 @@ export class ContractRepository { }); } - async getAllbyUserId(userId : string): Promise { + async getAllbyUserId(userId: string): Promise { return this.prisma.contract.findMany({ where: { userId, @@ -24,7 +24,7 @@ export class ContractRepository { }); } - async getAllbyJobId(jobId : string): Promise { + async getAllbyJobId(jobId: string): Promise { return this.prisma.contract.findMany({ where: { jobId, diff --git a/src/contract/contract.service.ts b/src/contract/contract.service.ts index b0da59c..830aacc 100644 --- a/src/contract/contract.service.ts +++ b/src/contract/contract.service.ts @@ -49,17 +49,17 @@ export class ContractService { } //GetAll Contract by userId - async getAllbyUserId(userId : string): Promise { + async getAllbyUserId(userId: string): Promise { return await this.contractRepository.getAllbyUserId(userId); } //GetAll Contract by jobId - async getAllbyJobId(jobId : string): Promise { + async getAllbyJobId(jobId: string): Promise { return await this.contractRepository.getAllbyJobId(jobId); } //Get Contract by contractId - async getContractbyId(id : string): Promise{ + async getContractbyId(id: string): Promise { return await this.contractRepository.get(id); }