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

Get all users endpoint #52

Merged
merged 11 commits into from
Aug 20, 2023
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cors from 'cors'
import { dataSource } from './configs/dbConfig'
import authRouter from './routes/auth.route'
import profileRouter from './routes/profile/profile.route'
import adminRouter from './routes/admin/admin.route'
import passport from 'passport'
import './configs/passport'
import { SERVER_PORT } from './configs/envConfig'
Expand All @@ -22,6 +23,7 @@ app.get('/', (req, res) => {

app.use('/api/auth', authRouter)
app.use('/api/me', profileRouter)
app.use('/api/admin', adminRouter)

export const startServer = async (): Promise<Express> => {
try {
Expand Down
19 changes: 19 additions & 0 deletions src/controllers/admin.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Request, Response } from 'express'
import { getAllUsers } from '../services/admin.service'

export const getAllUsersHandler = async (
req: Request,
res: Response
): Promise<void> => {
try {
const users = await getAllUsers(req)
if (users && users.length != 0) {

Check failure on line 10 in src/controllers/admin.controller.ts

View workflow job for this annotation

GitHub Actions / build

Expected '!==' and instead saw '!='
res.status(404).json({ message: 'No users available' })
}

res.status(200).json(users)
} catch (err) {
console.error('Error executing query', err)
res.status(500).json({ error: err })
}
}
34 changes: 34 additions & 0 deletions src/routes/admin/admin.route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Express } from 'express'
import supertest from 'supertest'

let server: Express
let accessToken: string

describe('Get all users route', () => {
it('should return a 401 when a valid access token is not provided', async () => {
await supertest(server).get('/api/admin/users').expect(401)
})

it('should return a 200 with all users from the DB', async () => {
const response = await supertest(server)
.get('/api/me/profile')
.set('Authorization', `Bearer ${accessToken}`)
.expect(200)

const userProfiles = response.body

userProfiles.forEach((userProfile: Object) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
userProfiles.forEach((userProfile: Object) => {
userProfiles.forEach((userProfile: Partial<Profile>) => {

expect(userProfile).toHaveProperty('created_at')
expect(userProfile).toHaveProperty('updated_at')
expect(userProfile).toHaveProperty('primary_email')
expect(userProfile).toHaveProperty('contact_email')
expect(userProfile).toHaveProperty('first_name')
expect(userProfile).toHaveProperty('last_name')
expect(userProfile).toHaveProperty('image_url')
expect(userProfile).toHaveProperty('linkedin_url')
expect(userProfile).toHaveProperty('type')
expect(userProfile).toHaveProperty('uuid')
expect(userProfile).not.toHaveProperty('password')
})
})
})
9 changes: 9 additions & 0 deletions src/routes/admin/admin.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from 'express'
import { getAllUsersHandler } from '../../controllers/admin.controller'
import { requireAuth } from '../../controllers/auth.controller'

const adminRouter = express.Router()

adminRouter.get('/users', requireAuth, getAllUsersHandler)

export default adminRouter
11 changes: 11 additions & 0 deletions src/services/admin.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Request } from 'express'
import { dataSource } from '../configs/dbConfig'
import Profile from '../entities/profile.entity'

export const getAllUsers = async (
req: Request
): Promise<Profile[] | undefined> => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const getAllUsers = async (
req: Request
): Promise<Profile[] | undefined> => {
export const getAllUsers = async ( ): Promise<Profile[] | undefined> => {

const profileRepository = dataSource.getRepository(Profile)
const allUsers = await profileRepository.find()
return allUsers as Profile[]

Check failure on line 10 in src/services/admin.service.ts

View workflow job for this annotation

GitHub Actions / build

This assertion is unnecessary since it does not change the type of the expression
}
Loading