From 30cd059f544a6feaf7a3023c4827e67f46200889 Mon Sep 17 00:00:00 2001 From: pawarisble Date: Thu, 21 Sep 2023 12:20:24 +0700 Subject: [PATCH 1/5] add findAllUsers --- src/auth/auth.service.ts | 12 ++++++------ src/repository/user.repository.ts | 24 +++++++++++++++++++++++- src/user/user.controller.ts | 4 ++-- src/user/user.module.ts | 7 +++++-- src/user/user.service.ts | 16 +++++++++++++--- 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 456be17..59efbd4 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -40,12 +40,12 @@ export class AuthService implements AuthServiceController { request.password, user.password, ); - if (!isPasswordMatch) { - throw new RpcException({ - code: status.PERMISSION_DENIED, - message: 'email or password is incorrect', - }); - } + // if (!isPasswordMatch) { + // throw new RpcException({ + // code: status.PERMISSION_DENIED, + // message: 'email or password is incorrect', + // }); + // } const { accessToken, refreshToken } = await this.getTokens(user.id); user = await this.userRepo.updateRefreshToken(user.id, refreshToken); diff --git a/src/repository/user.repository.ts b/src/repository/user.repository.ts index 747cdce..944e1b0 100644 --- a/src/repository/user.repository.ts +++ b/src/repository/user.repository.ts @@ -5,7 +5,7 @@ import { Prisma } from '@prisma/client'; @Injectable() export class UserRepository { - constructor(private db: PrismaService) {} + constructor(private db: PrismaService) { } async getUserByEmail(email: string): Promise { return this.db.user.findUnique({ where: { email: email } }); @@ -34,4 +34,26 @@ export class UserRepository { }, }); } + + async getAllUsers(): Promise { + return this.db.user.findMany({ + select: { + id: true, + firstName: true, + lastName: true, + email: true, + phoneNumber: true, + role: true, + password: false, + refreshToken: false, + status: true + } + }); + } + + exclude(user: User, keys: Key[]): Omit { + return Object.fromEntries( + Object.entries(user).filter(([key]) => !keys.includes(key as Key)), + ) as Omit; + } } diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 6f4e686..4830117 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -3,11 +3,11 @@ import { UserService } from './user.service'; @Controller('user') export class UserController { - constructor(private userService: UserService) {} + constructor(private userService: UserService) { } @Get() findAllUsers() { - return null; + return this.userService.findAllUsers(); } @Patch('ban/:userId') diff --git a/src/user/user.module.ts b/src/user/user.module.ts index b3801b5..e87ed82 100644 --- a/src/user/user.module.ts +++ b/src/user/user.module.ts @@ -1,9 +1,12 @@ import { Module } from '@nestjs/common'; import { UserController } from './user.controller'; import { UserService } from './user.service'; +import { UserRepository } from 'src/repository/user.repository'; +import { PrismaModule } from 'src/prisma/prisma.module'; @Module({ + imports: [PrismaModule], controllers: [UserController], - providers: [UserService] + providers: [UserService, UserRepository] }) -export class UserModule {} +export class UserModule { } diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 87a7b92..5f2286b 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -1,12 +1,18 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { UserRepository } from '../repository/user.repository'; @Injectable() export class UserService { - constructor(private userRepo: UserRepository) {} + constructor(private userRepo: UserRepository) { } findAllUsers() { - return null; + try { + const allUsers = this.userRepo.getAllUsers(); + return allUsers; + } catch (e) { + console.log(e); + throw InternalServerErrorException; + } } banUser() { @@ -17,3 +23,7 @@ export class UserService { return null; } } +function exclude(allUsers: Promise<{ id: string; createdAt: Date; updatedAt: Date; firstName: string; lastName: string; email: string; phoneNumber: string; password: string; role: import(".prisma/client").$Enums.Role; refreshToken: string; }[]>) { + throw new Error('Function not implemented.'); +} + From 3ec104640dd961496b4a6cda2ffab3b002907be4 Mon Sep 17 00:00:00 2001 From: pawarisble Date: Thu, 21 Sep 2023 12:26:27 +0700 Subject: [PATCH 2/5] update user spec --- src/user/user.service.spec.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/user/user.service.spec.ts b/src/user/user.service.spec.ts index 873de8a..609cf84 100644 --- a/src/user/user.service.spec.ts +++ b/src/user/user.service.spec.ts @@ -1,12 +1,21 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UserService } from './user.service'; +import { UserRepository } from 'src/repository/user.repository'; describe('UserService', () => { let service: UserService; + const mockUserRepository = { + findUserById: jest.fn(), + update: jest.fn(), + exclude: jest.fn(), + }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [UserService], + providers: [UserService, { + provide: UserRepository, + useValue: mockUserRepository, + },], }).compile(); service = module.get(UserService); From ce7bfb0256ab67b6ada5c95397d387bc2d558d2f Mon Sep 17 00:00:00 2001 From: pawarisble Date: Thu, 21 Sep 2023 12:29:56 +0700 Subject: [PATCH 3/5] Update user.controller.spec.ts --- src/user/user.controller.spec.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/user/user.controller.spec.ts b/src/user/user.controller.spec.ts index 7057a1a..f0c1beb 100644 --- a/src/user/user.controller.spec.ts +++ b/src/user/user.controller.spec.ts @@ -1,12 +1,18 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UserController } from './user.controller'; +import { UserService } from './user.service'; describe('UserController', () => { let controller: UserController; - + const mockUserService = { + findUserById: jest.fn(), + update: jest.fn(), + exclude: jest.fn(), + }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [UserController], + providers: [{ provide: UserService, useValue: mockUserService }] }).compile(); controller = module.get(UserController); From 62717928a7622534eb98a00ab2b17c8030abe9a7 Mon Sep 17 00:00:00 2001 From: pawarisble Date: Thu, 21 Sep 2023 12:34:22 +0700 Subject: [PATCH 4/5] Update user.service.spec.ts --- src/user/user.service.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user/user.service.spec.ts b/src/user/user.service.spec.ts index 609cf84..6908024 100644 --- a/src/user/user.service.spec.ts +++ b/src/user/user.service.spec.ts @@ -1,6 +1,6 @@ import { Test, TestingModule } from '@nestjs/testing'; import { UserService } from './user.service'; -import { UserRepository } from 'src/repository/user.repository'; +import { UserRepository } from '../repository/user.repository'; describe('UserService', () => { let service: UserService; From 15f92fa560e641507b9b156a95abfbdccae1c22b Mon Sep 17 00:00:00 2001 From: pawarisble Date: Thu, 21 Sep 2023 12:36:55 +0700 Subject: [PATCH 5/5] Update auth.service.ts --- src/auth/auth.service.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 59efbd4..456be17 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -40,12 +40,12 @@ export class AuthService implements AuthServiceController { request.password, user.password, ); - // if (!isPasswordMatch) { - // throw new RpcException({ - // code: status.PERMISSION_DENIED, - // message: 'email or password is incorrect', - // }); - // } + if (!isPasswordMatch) { + throw new RpcException({ + code: status.PERMISSION_DENIED, + message: 'email or password is incorrect', + }); + } const { accessToken, refreshToken } = await this.getTokens(user.id); user = await this.userRepo.updateRefreshToken(user.id, refreshToken);