Skip to content

Commit 20c541a

Browse files
kimharr24ojn03
andauthored
Added getAllMembers Endpoint (#9)
* feat: getAllMembers endpoint completed resolves #3 * update linter * fix: updated user service to return all users except applicants * fix: changed userId field type to the expected ObjectId type * fix: removed redundant check in user service * style: removed unused imports --------- Co-authored-by: OJisMe <olivierjohnnn@gmail.com>
1 parent db6e1a5 commit 20c541a

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

apps/backend/src/users/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum Status {
2+
MEMBER = 'MEMBER',
3+
RECRUITER = 'RECRUITER',
4+
ADMIN = 'ADMIN',
5+
ALUMNI = 'ALUMNI',
6+
APPLICANT = 'APPLICANT',
7+
}
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
2-
1+
import { Entity, Column, ObjectIdColumn, ObjectId } from 'typeorm';
2+
import { Status } from './types';
33
@Entity()
44
export class User {
5-
@PrimaryGeneratedColumn()
6-
id: number;
5+
@ObjectIdColumn() // https://github.com/typeorm/typeorm/issues/1584
6+
userId: ObjectId;
7+
8+
@Column()
9+
status: Status;
710

811
@Column()
912
firstName: string;
@@ -13,4 +16,19 @@ export class User {
1316

1417
@Column()
1518
email: string;
19+
20+
@Column()
21+
profilePicture: string;
22+
23+
@Column()
24+
linkedin: string | null;
25+
26+
@Column()
27+
github: string | null;
28+
29+
@Column()
30+
team: string | null;
31+
32+
@Column()
33+
role: string | null;
1634
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Controller, Get } from '@nestjs/common';
1+
import {
2+
DefaultValuePipe,
3+
ParseBoolPipe,
4+
Query,
5+
Controller,
6+
Get,
7+
} from '@nestjs/common';
28

39
import { UsersService } from './users.service';
410

@@ -7,7 +13,10 @@ export class UsersController {
713
constructor(private readonly usersService: UsersService) {}
814

915
@Get()
10-
getAllUsers() {
11-
return this.usersService.findAll();
16+
getAllMembers(
17+
@Query('getAllMembers', new DefaultValuePipe(false), ParseBoolPipe)
18+
getAllMembers: boolean,
19+
) {
20+
return this.usersService.findAll(getAllMembers);
1221
}
1322
}
Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { UnauthorizedException, Injectable } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
3-
import { Repository } from 'typeorm';
3+
import { MongoRepository } from 'typeorm';
44

55
import { User } from './user.entity';
6+
import { Status } from './types';
7+
import { ObjectId } from 'mongodb';
68

79
@Injectable()
810
export class UsersService {
911
constructor(
1012
@InjectRepository(User)
11-
private usersRepository: Repository<User>,
13+
private usersRepository: MongoRepository<User>,
1214
) {}
1315

14-
findAll(): Promise<User[]> {
15-
return this.usersRepository.find();
16+
async findAll(getAllMembers: boolean): Promise<User[]> {
17+
if (!getAllMembers) return [];
18+
19+
const exampleUser: User = {
20+
userId: new ObjectId('a0f3efa0f3efa0f3efa0f3ef'),
21+
status: Status.ADMIN,
22+
firstName: 'jimmy',
23+
lastName: 'jimmy2',
24+
email: 'jimmy.jimmy2@mail.com',
25+
profilePicture: null,
26+
linkedin: null,
27+
github: null,
28+
team: null,
29+
role: null,
30+
};
31+
32+
if (exampleUser.status == Status.APPLICANT) {
33+
throw new UnauthorizedException();
34+
}
35+
36+
const users: User[] = await this.usersRepository.find({
37+
where: {
38+
status: { $not: { $eq: Status.APPLICANT } },
39+
},
40+
});
41+
42+
return users;
1643
}
1744
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "0.0.0",
44
"license": "MIT",
55
"scripts": {
6-
"format:check": "prettier --check apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
7-
"format": "prettier --write apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
6+
"format:check": "prettier --no-error-on-unmatched-pattern --check apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
7+
"format": "prettier --no-error-on-unmatched-pattern --write apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
88
"lint:check": "eslint apps/frontend --ext .ts,.tsx && eslint apps/backend --ext .ts,.tsx",
99
"lint": "eslint apps/frontend --ext .ts,.tsx --fix && eslint apps/backend --ext .ts,.tsx --fix",
1010
"prepush": "yarn run format:check && yarn run lint:check",

0 commit comments

Comments
 (0)