Skip to content
Closed
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
5 changes: 0 additions & 5 deletions .env.example

This file was deleted.

79 changes: 79 additions & 0 deletions docs/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,85 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
/cohorts/{id}/members:
get:
tags:
- cohort
summary: Get members of a specific cohort
description: Returns all members (teachers and students) of a specific cohort. Teachers can access any cohort, students can only access their own cohort.
operationId: getCohortMembers
security:
- bearerAuth: []
parameters:
- name: id
in: path
description: ID of the cohort to get members from
required: true
schema:
type: integer
responses:
'200':
description: Successful operation
content:
application/json:
schema:
type: object
properties:
status:
type: string
data:
type: object
properties:
cohort:
type: object
properties:
id:
type: integer
type:
type: string
members:
type: object
properties:
students:
type: array
items:
type: object
properties:
id:
type: integer
firstName:
type: string
lastName:
type: string
teachers:
type: array
items:
type: object
properties:
id:
type: integer
firstName:
type: string
lastName:
type: string
'403':
description: Unauthorized access to cohort
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: No members found in cohort
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

components:
securitySchemes:
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1",
"swagger-ui-express": "^5.0.0",
"validator": "^13.12.0",
"yaml": "^2.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- CreateEnum
CREATE TYPE "CohortType" AS ENUM ('SOFTWARE_DEVELOPMENT', 'FRONTEND_DEVELOPMENT', 'DATA_ANALYTICS');

-- AlterTable
ALTER TABLE "Cohort" ADD COLUMN "type" "CohortType" NOT NULL DEFAULT E'SOFTWARE_DEVELOPMENT';
7 changes: 7 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ model Profile {
githubUrl String?
}

enum CohortType {
SOFTWARE_DEVELOPMENT
FRONTEND_DEVELOPMENT
DATA_ANALYTICS
}

model Cohort {
id Int @id @default(autoincrement())
type CohortType @default(SOFTWARE_DEVELOPMENT)
users User[]
deliveryLogs DeliveryLog[]
}
Expand Down
4 changes: 3 additions & 1 deletion prisma/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ async function createPost(userId, content) {

async function createCohort() {
const cohort = await prisma.cohort.create({
data: {}
data: {
type: 'SOFTWARE_DEVELOPMENT'
}
})

console.info('Cohort created', cohort)
Expand Down
74 changes: 74 additions & 0 deletions src/controllers/cohort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createCohort } from '../domain/cohort.js'
import { sendDataResponse, sendMessageResponse } from '../utils/responses.js'
import dbClient from '../utils/dbClient.js'

export const cohortTypeDisplay = {
SOFTWARE_DEVELOPMENT: 'Software Development',
FRONTEND_DEVELOPMENT: 'Front-End Development',
DATA_ANALYTICS: 'Data Analytics'
}

export const create = async (req, res) => {
try {
Expand All @@ -10,3 +17,70 @@ export const create = async (req, res) => {
return sendMessageResponse(res, 500, 'Unable to create cohort')
}
}

export const getCohortMembers = async (req, res) => {
const cohortId = parseInt(req.params.id)

try {
const cohortMembers = await dbClient.user.findMany({
where: {
cohortId: cohortId
},
select: {
id: true,
role: true,
profile: {
select: {
firstName: true,
lastName: true
}
}
}
})

const cohort = await dbClient.cohort.findUnique({
where: {
id: cohortId
},
select: {
id: true,
type: true
}
})

if (!cohortMembers.length) {
return sendDataResponse(res, 404, {
cohort: 'No members found in this cohort'
})
}

const formattedMembers = {
students: [],
teachers: []
}

cohortMembers.forEach((member) => {
const formattedMember = {
id: member.id,
firstName: member.profile.firstName,
lastName: member.profile.lastName
}

if (member.role === 'TEACHER') {
formattedMembers.teachers.push(formattedMember)
} else {
formattedMembers.students.push(formattedMember)
}
})

return sendDataResponse(res, 200, {
cohort: {
id: cohort.id,
type: cohortTypeDisplay[cohort.type]
},
members: formattedMembers
})
} catch (error) {
return sendMessageResponse(res, 500, 'Unable to fetch cohort members')
}
}
Loading
Loading