Skip to content

Commit

Permalink
Added getAllMembers Endpoint (#9)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kimharr24 and ojn03 authored Oct 4, 2023
1 parent db6e1a5 commit 20c541a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
7 changes: 7 additions & 0 deletions apps/backend/src/users/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum Status {
MEMBER = 'MEMBER',
RECRUITER = 'RECRUITER',
ADMIN = 'ADMIN',
ALUMNI = 'ALUMNI',
APPLICANT = 'APPLICANT',
}
26 changes: 22 additions & 4 deletions apps/backend/src/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

import { Entity, Column, ObjectIdColumn, ObjectId } from 'typeorm';
import { Status } from './types';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@ObjectIdColumn() // https://github.com/typeorm/typeorm/issues/1584
userId: ObjectId;

@Column()
status: Status;

@Column()
firstName: string;
Expand All @@ -13,4 +16,19 @@ export class User {

@Column()
email: string;

@Column()
profilePicture: string;

@Column()
linkedin: string | null;

@Column()
github: string | null;

@Column()
team: string | null;

@Column()
role: string | null;
}
15 changes: 12 additions & 3 deletions apps/backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Controller, Get } from '@nestjs/common';
import {
DefaultValuePipe,
ParseBoolPipe,
Query,
Controller,
Get,
} from '@nestjs/common';

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

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

@Get()
getAllUsers() {
return this.usersService.findAll();
getAllMembers(
@Query('getAllMembers', new DefaultValuePipe(false), ParseBoolPipe)
getAllMembers: boolean,
) {
return this.usersService.findAll(getAllMembers);
}
}
37 changes: 32 additions & 5 deletions apps/backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { Injectable } from '@nestjs/common';
import { UnauthorizedException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { MongoRepository } from 'typeorm';

import { User } from './user.entity';
import { Status } from './types';
import { ObjectId } from 'mongodb';

@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
private usersRepository: MongoRepository<User>,
) {}

findAll(): Promise<User[]> {
return this.usersRepository.find();
async findAll(getAllMembers: boolean): Promise<User[]> {
if (!getAllMembers) return [];

const exampleUser: User = {
userId: new ObjectId('a0f3efa0f3efa0f3efa0f3ef'),
status: Status.ADMIN,
firstName: 'jimmy',
lastName: 'jimmy2',
email: 'jimmy.jimmy2@mail.com',
profilePicture: null,
linkedin: null,
github: null,
team: null,
role: null,
};

if (exampleUser.status == Status.APPLICANT) {
throw new UnauthorizedException();
}

const users: User[] = await this.usersRepository.find({
where: {
status: { $not: { $eq: Status.APPLICANT } },
},
});

return users;
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.0.0",
"license": "MIT",
"scripts": {
"format:check": "prettier --check apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
"format": "prettier --write apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
"format:check": "prettier --no-error-on-unmatched-pattern --check apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
"format": "prettier --no-error-on-unmatched-pattern --write apps/{frontend,backend}/src/**/*.{js,ts,tsx}",
"lint:check": "eslint apps/frontend --ext .ts,.tsx && eslint apps/backend --ext .ts,.tsx",
"lint": "eslint apps/frontend --ext .ts,.tsx --fix && eslint apps/backend --ext .ts,.tsx --fix",
"prepush": "yarn run format:check && yarn run lint:check",
Expand Down

0 comments on commit 20c541a

Please sign in to comment.