Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add findAllUsers #9

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/repository/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { PrismaClient, User } from '@prisma/client';

Check warning on line 2 in src/repository/user.repository.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'PrismaClient' is defined but never used
import { PrismaService } from '../prisma/service/prisma.service';
import { Prisma } from '@prisma/client';

@Injectable()
export class UserRepository {
constructor(private db: PrismaService) {}
constructor(private db: PrismaService) { }

async getUserByEmail(email: string): Promise<User> {
return this.db.user.findUnique({ where: { email: email } });
Expand Down Expand Up @@ -34,4 +34,26 @@
},
});
}

async getAllUsers(): Promise<object[]> {
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<Key extends keyof User>(user: User, keys: Key[]): Omit<User, Key> {
return Object.fromEntries(
Object.entries(user).filter(([key]) => !keys.includes(key as Key)),
) as Omit<User, Key>;
}
}
8 changes: 7 additions & 1 deletion src/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(UserController);
Expand Down
4 changes: 2 additions & 2 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 5 additions & 2 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -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 { }
11 changes: 10 additions & 1 deletion src/user/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserService } from './user.service';
import { UserRepository } from '../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>(UserService);
Expand Down
16 changes: 13 additions & 3 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -17,3 +23,7 @@
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; }[]>) {

Check warning on line 26 in src/user/user.service.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'exclude' is defined but never used
throw new Error('Function not implemented.');

Check warning on line 27 in src/user/user.service.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'allUsers' is defined but never used
}

Loading