Skip to content

Commit

Permalink
Log event to event_log when there are changes to graders list
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksTeresh committed Sep 27, 2024
1 parent 6578459 commit 3c2df3a
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/server/db/models/Grader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
InferCreationAttributes,
DataTypes,
UUIDV4,
NonAttribute,
} from 'sequelize'

import { sequelize } from '../connection'
import User from './User'

class Grader extends Model<
InferAttributes<Grader>,
Expand All @@ -19,6 +21,8 @@ class Grader extends Model<
declare userId: string

declare isPrimaryGrader: boolean

declare user: NonAttribute<User>
}

Grader.init(
Expand Down
4 changes: 4 additions & 0 deletions src/server/db/models/Thesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
InferCreationAttributes,
DataTypes,
UUIDV4,
NonAttribute,
} from 'sequelize'
import type { ThesisStatus } from '@backend/types'

import { sequelize } from '../connection'
import Grader from './Grader'

class Thesis extends Model<
InferAttributes<Thesis>,
Expand All @@ -26,6 +28,8 @@ class Thesis extends Model<
declare startDate: string

declare targetDate: string | undefined

declare graders: NonAttribute<Grader[]>
}

Thesis.init(
Expand Down
294 changes: 288 additions & 6 deletions src/server/routes/thesis.integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ describe('thesis router', () => {
)
.field('json', JSON.stringify(newThesis))
expect(response.status).toEqual(201)

const eventLog = await EventLog.findOne({
where: { type: 'THESIS_CREATED', thesisId: response.body.id },
})
Expand Down Expand Up @@ -2777,7 +2777,7 @@ describe('thesis router', () => {
})

thesis1.status = 'CANCELLED'
thesis1.save()
await thesis1.save()

const updatedThesis = {
programId: 'Testing program',
Expand Down Expand Up @@ -2906,9 +2906,9 @@ describe('thesis router', () => {
})

describe('when the thesis already has IN_PROGRESS status', () => {
beforeEach(() => {
beforeEach(async () => {
thesis1.status = 'IN_PROGRESS'
thesis1.save()
await thesis1.save()
})

it('should return 200, update the thesis and not log status change event', async () => {
Expand Down Expand Up @@ -2969,9 +2969,9 @@ describe('thesis router', () => {
})

describe('when the thesis has status other than PLANING or IN_PROGRESS', () => {
beforeEach(() => {
beforeEach(async () => {
thesis1.status = 'CANCELLED'
thesis1.save()
await thesis1.save()
})

it('should return 200, update the thesis and log status change event', async () => {
Expand Down Expand Up @@ -3032,6 +3032,288 @@ describe('thesis router', () => {
})
})
})

describe('logic for adding THESIS_GRADERS_CHANGED event to the event_log table', () => {
describe('when a new grader is added to the thesis', () => {
beforeEach(async () => {
await Grader.destroy({ where: { thesisId: thesis1.id } })
await Grader.create({
userId: user4.id,
thesisId: thesis1.id,
isPrimaryGrader: true,
isExternal: false,
})
})

it('adds THESIS_GRADERS_CHANGED event to the event_log table', async () => {
const updatedThesis = {
programId: 'Updated program',
studyTrackId: 'new-test-study-track-id',
topic: 'Updated topic',
status: 'PLANNING',
startDate: '1970-01-01T00:00:00.000Z',
targetDate: '2070-01-01T00:00:00.000Z',
supervisions: [
{
user: user1,
percentage: 100,
isExternal: false,
isPrimarySupervisor: true,
},
],
graders: [
{
user: user4,
isPrimaryGrader: true,
isExternal: false,
},
{
user: user5,
isPrimaryGrader: false,
isExternal: false,
},
],
authors: [user2],
}
const response = await request
.put(`/api/theses/${thesis1.id}`)
.set({ uid: user1.id, hygroupcn: 'hy-employees' })
.attach(
'waysOfWorking',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.attach(
'researchPlan',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.field('json', JSON.stringify(updatedThesis))
expect(response.status).toEqual(200)

const eventLog = await EventLog.findOne({
where: { type: 'THESIS_GRADERS_CHANGED', thesisId: thesis1.id },
})
expect(eventLog).not.toBeNull()
})
})

describe('when a grader is removed from the thesis', () => {
beforeEach(async () => {
await Grader.destroy({ where: { thesisId: thesis1.id } })
await Grader.bulkCreate([
{
userId: user4.id,
thesisId: thesis1.id,
isPrimaryGrader: true,
isExternal: false,
},
{
userId: user5.id,
thesisId: thesis1.id,
isPrimaryGrader: false,
isExternal: false,
},
])
})

it('adds THESIS_GRADERS_CHANGED event to the event_log table', async () => {
const updatedThesis = {
programId: 'Updated program',
studyTrackId: 'new-test-study-track-id',
topic: 'Updated topic',
status: 'PLANNING',
startDate: '1970-01-01T00:00:00.000Z',
targetDate: '2070-01-01T00:00:00.000Z',
supervisions: [
{
user: user1,
percentage: 100,
isExternal: false,
isPrimarySupervisor: true,
},
],
graders: [
{
user: user4,
isPrimaryGrader: true,
isExternal: false,
},
],
authors: [user2],
}
const response = await request
.put(`/api/theses/${thesis1.id}`)
.set({ uid: user1.id, hygroupcn: 'hy-employees' })
.attach(
'waysOfWorking',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.attach(
'researchPlan',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.field('json', JSON.stringify(updatedThesis))
expect(response.status).toEqual(200)

const eventLog = await EventLog.findOne({
where: { type: 'THESIS_GRADERS_CHANGED', thesisId: thesis1.id },
})
expect(eventLog).not.toBeNull()
})
})

describe('when a primary grader changes when', () => {
beforeEach(async () => {
await Grader.destroy({ where: { thesisId: thesis1.id } })
await Grader.bulkCreate([
{
userId: user4.id,
thesisId: thesis1.id,
isPrimaryGrader: true,
isExternal: false,
},
{
userId: user5.id,
thesisId: thesis1.id,
isPrimaryGrader: false,
isExternal: false,
},
])
})

it('adds THESIS_GRADERS_CHANGED event to the event_log table', async () => {
const updatedThesis = {
programId: 'Updated program',
studyTrackId: 'new-test-study-track-id',
topic: 'Updated topic',
status: 'PLANNING',
startDate: '1970-01-01T00:00:00.000Z',
targetDate: '2070-01-01T00:00:00.000Z',
supervisions: [
{
user: user1,
percentage: 100,
isExternal: false,
isPrimarySupervisor: true,
},
],
graders: [
{
user: user4,
isPrimaryGrader: false,
isExternal: false,
},
{
user: user5,
isPrimaryGrader: true,
isExternal: false,
},
],
authors: [user2],
}
const response = await request
.put(`/api/theses/${thesis1.id}`)
.set({ uid: user1.id, hygroupcn: 'hy-employees' })
.attach(
'waysOfWorking',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.attach(
'researchPlan',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.field('json', JSON.stringify(updatedThesis))
expect(response.status).toEqual(200)

const eventLog = await EventLog.findOne({
where: { type: 'THESIS_GRADERS_CHANGED', thesisId: thesis1.id },
})
expect(eventLog).not.toBeNull()
})
})

describe('when graders are unchanged', () => {
beforeEach(async () => {
await Grader.destroy({ where: { thesisId: thesis1.id } })
await Grader.bulkCreate([
{
userId: user4.id,
thesisId: thesis1.id,
isPrimaryGrader: true,
isExternal: false,
},
])
})

it('does not add THESIS_GRADERS_CHANGED event to the event_log table', async () => {
const updatedThesis = {
programId: 'Updated program',
studyTrackId: 'new-test-study-track-id',
topic: 'Updated topic',
status: 'PLANNING',
startDate: '1970-01-01T00:00:00.000Z',
targetDate: '2070-01-01T00:00:00.000Z',
supervisions: [
{
user: user1,
percentage: 100,
isExternal: false,
isPrimarySupervisor: true,
},
],
graders: [
{
user: user4,
isPrimaryGrader: true,
isExternal: false,
},
],
authors: [user2],
}
const response = await request
.put(`/api/theses/${thesis1.id}`)
.set({ uid: user1.id, hygroupcn: 'hy-employees' })
.attach(
'waysOfWorking',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.attach(
'researchPlan',
path.resolve(
dirname(fileURLToPath(import.meta.url)),
'./index.ts'
)
)
.field('json', JSON.stringify(updatedThesis))
expect(response.status).toEqual(200)

const eventLog = await EventLog.findOne({
where: { type: 'THESIS_GRADERS_CHANGED' },
})
expect(eventLog).toBeNull()
})
})
})
})
})
})
2 changes: 2 additions & 0 deletions src/server/routes/thesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { authorizeStatusChange } from '../middleware/authorizeStatusChange'
import {
getAndCreateExtUsers,
getFindThesesOptions,
handleGradersChangeEventLog,
handleStatusChangeEventLog,
} from './thesisHelpers'
import {
Expand Down Expand Up @@ -260,6 +261,7 @@ thesisRouter.put(
await handleAttachmentByLabel(req, id, 'waysOfWorking', t)

await handleStatusChangeEventLog(originalThesis, thesisData, req.user, t)
await handleGradersChangeEventLog(originalThesis, thesisData, req.user, t)
})

const updatedThesis = await fetchThesisById(id, req.user)
Expand Down
Loading

0 comments on commit 3c2df3a

Please sign in to comment.