Skip to content
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test-login": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/login.spec.js",
"test-cohorts": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/cohort.spec.js",
"test-logs": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/deliveryLog.spec.js",
"test-users": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/user.spec.js"
"test-users": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/user.spec.js",
"test-auth": "node --experimental-vm-modules node_modules/.bin/jest -- -i ./tests/api/routes/auth.spec.js"
},
"jest": {
"transform": {
Expand Down
16 changes: 8 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ model User {
role UserRole @default(STUDENT)
profile Profile?
cohortId Int?
cohort Cohort? @relation(fields: [cohortId], references: [id])
cohort Cohort? @relation(fields: [cohortId], references: [id], onDelete: Cascade)
posts Post[]
deliveryLogs DeliveryLog[]
notesCreated Note[] @relation("TeacherNotes")
Expand All @@ -32,7 +32,7 @@ model User {
model Profile {
id Int @id @default(autoincrement())
userId Int @unique
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
firstName String?
lastName String?
username String?
Expand All @@ -51,7 +51,7 @@ model Post {
id Int @id @default(autoincrement())
content String
userId Int
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
}
Expand All @@ -60,26 +60,26 @@ model DeliveryLog {
id Int @id @default(autoincrement())
date DateTime @default(now())
userId Int
user User @relation(fields: [userId], references: [id])
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
cohortId Int
cohort Cohort @relation(fields: [cohortId], references: [id])
cohort Cohort @relation(fields: [cohortId], references: [id], onDelete: Cascade)
lines DeliveryLogLine[]
}

model DeliveryLogLine {
id Int @id @default(autoincrement())
content String
logId Int
log DeliveryLog @relation(fields: [logId], references: [id])
log DeliveryLog @relation(fields: [logId], references: [id], onDelete: Cascade)
}

model Note {
id Int @id @default(autoincrement())
content String
teacherId Int
teacher User @relation("TeacherNotes", fields: [teacherId], references: [id])
teacher User @relation("TeacherNotes", fields: [teacherId], references: [id], onDelete: Cascade)
studentId Int
student User @relation("StudentNotes", fields: [studentId], references: [id])
student User @relation("StudentNotes", fields: [studentId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
}
1 change: 1 addition & 0 deletions src/controllers/deliveryLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const createDeliveryLog = async (req, res) => {
if (!cohortExists) {
return sendDataResponse(res, 404, { error: ERR.COHORT_NOT_FOUND })
}

const log = await createDeliveryLogDb(cohortId, req.user.id, lines)

const logLines = log.lines.map((line) => ({
Expand Down
2 changes: 1 addition & 1 deletion tests/api/routes/cohort.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Cohort Endpoint', () => {
it('will allow a user with the role of teacher to create a new cohort', async () => {
const uniqueEmail = `testuser${Date.now()}@gmail.com`
const teacher = await createUser(uniqueEmail, 'password', 'TEACHER')
const token = jwt.sign(teacher.id, process.env.JWT_SECRET)
const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET)
const request = {}
const response = await supertest(app)
.post('/cohorts')
Expand Down
21 changes: 13 additions & 8 deletions tests/api/routes/deliveryLog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import { createCohort } from '../../helpers/createCohort.js'
describe('DeliveryLog Endpoint', () => {
describe('POST/logs', () => {
it('will allow teachers to create delivery logs for each cohort', async () => {
const uniqueEmail = `testuser${Date.now()}@gmail.com`
const uniqueEmail = `testuser@gmail.com`
const teacher = await createUser(uniqueEmail, 'password', 'TEACHER')
const token = jwt.sign(teacher.id, process.env.JWT_SECRET)
const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET)
const cohort = await createCohort()

const request = {
cohort_id: cohort.id,
userId: teacher.id,
lines: [{ id: 1, content: 'today in the class we covered abstraction' }]
}
const response = await supertest(app)
Expand All @@ -30,9 +29,9 @@ describe('DeliveryLog Endpoint', () => {
)
})
it('will send a status code 400 if the cohortId or lines is missing from the request body', async () => {
const uniqueEmail = `testuser${Date.now()}@gmail.com`
const uniqueEmail = `testuser@gmail.com`
const teacher = await createUser(uniqueEmail, 'password', 'TEACHER')
const token = jwt.sign(teacher.id, process.env.JWT_SECRET)
const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET)

const request = {}
const response = await supertest(app)
Expand All @@ -47,15 +46,21 @@ describe('DeliveryLog Endpoint', () => {
)
})
it('will send a status code 404 if the the cohort does not exist', async () => {
const uniqueEmail = `testuser${Date.now()}@gmail.com`
const uniqueEmail = `testuser@gmail.com`
const teacher = await createUser(uniqueEmail, 'password', 'TEACHER')
const token = jwt.sign(teacher.id, process.env.JWT_SECRET)
const token = jwt.sign({ userId: teacher.id }, process.env.JWT_SECRET)
const cohort = await createCohort()

let cohortId = 1
while (cohort.id === cohortId) {
cohortId++
}

const request = {
cohort_id: 100,
cohort_id: cohortId,
lines: 'today in the class we covered abstraction'
}

const response = await supertest(app)
.post('/logs')
.set('Authorization', `Bearer ${token}`)
Expand Down
4 changes: 2 additions & 2 deletions tests/api/routes/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ describe('Users Endpoint', () => {
.get(`/users/${user.id}`)
.set('Authorization', `Bearer ${token}`)

expect(response.status).toEqual(200)
expect(response.body.data.user.email).toEqual(user.email)
expect(response.status).toEqual(201)
expect(response.body.data.user.user.email).toEqual(user.email)
})
it('should throw an error if no user exists with that ID', async () => {
const user1 = await createUser(
Expand Down
6 changes: 1 addition & 5 deletions tests/helpers/createCohort.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import dbClient from '../../src/utils/dbClient'

export const createCohort = async () => {
return await dbClient.cohort.create({
data: {
id: 1
}
})
return await dbClient.cohort.create()
}
7 changes: 4 additions & 3 deletions tests/setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import dbClient from '../src/utils/dbClient.js'

const deleteTables = () => {
const deleteTables = [
dbClient.user.deleteMany(),
dbClient.deliveryLogLine.deleteMany(),
dbClient.deliveryLog.deleteMany(),
dbClient.cohort.deleteMany(),
dbClient.post.deleteMany(),
dbClient.profile.deleteMany(),
dbClient.user.deleteMany()
dbClient.note.deleteMany(),
dbClient.profile.deleteMany()
]
return dbClient.$transaction(deleteTables)
}
Expand All @@ -21,5 +22,5 @@ global.afterEach(() => {
})

global.afterAll(() => {
return dbClient.$disconnect
return dbClient.$disconnect()
})