Skip to content

Commit

Permalink
Replace typeorm subscriber with controller code
Browse files Browse the repository at this point in the history
In order to expidite the patching of the bug, the subscriber has been replaced
with code in the Nest controller. This is due to the fact that changes to how
records are created would have knock-on impacts on the population service and
ETL script which would potentially take time to patch and review.

Therefore, #625 has been opened to track this tech debt and will be revisited
at a later time
  • Loading branch information
Robert Main committed Jan 12, 2023
1 parent 01b6c70 commit 6962b1f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 152 deletions.
4 changes: 0 additions & 4 deletions src/server/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { NonClassEventView } from 'server/nonClassEvent/NonClassEvent.view.entit
import { NonClassParentView } from 'server/nonClassEvent/NonClassParentView.entity';
import { ClientOpts } from 'redis';
import { RoomScheduleBlockView } from 'server/courseInstance/RoomScheduleBlockView.entity';
import { FacultySubscriber } from 'server/faculty/faculty.subscriber';
import LOG_LEVEL from '../../common/constants/logLevels';
import { ScheduleBlockView } from '../courseInstance/ScheduleBlockView.entity';
import { ScheduleEntryView } from '../courseInstance/ScheduleEntryView.entity';
Expand Down Expand Up @@ -205,9 +204,6 @@ class ConfigService {
View,
RoomScheduleBlockView,
],
subscribers: [
FacultySubscriber,
],
};
}

Expand Down
29 changes: 21 additions & 8 deletions src/server/faculty/__tests__/faculty.controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getRepositoryToken } from '@nestjs/typeorm';
import { Test, TestingModule } from '@nestjs/testing';
import { stub, SinonStub } from 'sinon';
import {
stub, SinonStub, createStubInstance, SinonStubbedInstance,
} from 'sinon';
import { strictEqual, deepStrictEqual, rejects } from 'assert';
import { FACULTY_TYPE } from 'common/constants';
import { Authentication } from 'server/auth/authentication.guard';
Expand All @@ -11,12 +13,15 @@ import {
appliedMathFacultyMember,
appliedMathFacultyMemberResponse,
newAreaFacultyMemberRequest,
fall,
spring,
} from 'testData';
import { Semester } from 'server/semester/semester.entity';
import { SemesterService } from 'server/semester/semester.service';
import { NotFoundException } from '@nestjs/common';
import { Absence } from 'server/absence/absence.entity';
import { ConfigModule } from 'server/config/config.module';
import { Repository } from 'typeorm';
import { FacultyController } from '../faculty.controller';
import { Faculty } from '../faculty.entity';
import { Area } from '../../area/area.entity';
Expand All @@ -29,8 +34,8 @@ describe('Faculty controller', function () {
let mockSemesterService : Record<string, SinonStub>;
let mockFacultyRepository : Record<string, SinonStub>;
let mockAreaRepository : Record<string, SinonStub>;
let mockSemesterRepository : Record<string, SinonStub>;
let mockAbsenceRepository: Record<string, SinonStub>;
let mockSemesterRepository: SinonStubbedInstance<Repository<Semester>>;
let mockAbsenceRepository: SinonStubbedInstance<Repository<Absence>>;
let controller: FacultyController;

beforeEach(async function () {
Expand Down Expand Up @@ -62,12 +67,13 @@ describe('Faculty controller', function () {
findOneOrFail: stub(),
};

mockSemesterRepository = {};
mockSemesterRepository = createStubInstance<Repository<Semester>>(
Repository
);

mockAbsenceRepository = {
findOneOrFail: stub(),
save: stub(),
};
mockAbsenceRepository = createStubInstance<Repository<Absence>>(
Repository
);

const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule],
Expand Down Expand Up @@ -100,6 +106,10 @@ describe('Faculty controller', function () {
provide: getRepositoryToken(Absence),
useValue: mockAbsenceRepository,
},
{
provide: getRepositoryToken(Semester),
useValue: mockSemesterRepository,
},
],
controllers: [FacultyController],
}).overrideGuard(Authentication).useValue(true).compile();
Expand All @@ -126,6 +136,9 @@ describe('Faculty controller', function () {
.findOne
.resolves(appliedMathFacultyMember.area);
mockAreaRepository.save.resolves(appliedMathFacultyMember.area);
mockSemesterRepository
.find
.resolves([fall, spring]);
});
it('creates a single faculty member', async function () {
mockFacultyRepository.save.resolves(appliedMathFacultyMember);
Expand Down
12 changes: 12 additions & 0 deletions src/server/faculty/__tests__/facultySchedule.controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
SinonStub,
stub,
createStubInstance,
SinonStubbedInstance,
} from 'sinon';
import { Test } from '@nestjs/testing';
import { Authentication } from 'server/auth/authentication.guard';
Expand All @@ -18,6 +20,8 @@ import { ABSENCE_TYPE } from 'common/constants';
import { InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { AbsenceResponseDTO } from 'common/dto/faculty/AbsenceResponse.dto';
import { ConfigModule } from 'server/config/config.module';
import { Repository } from 'typeorm';
import { Semester } from 'server/semester/semester.entity';
import { FacultyController } from '../faculty.controller';
import { FacultyScheduleService } from '../facultySchedule.service';
import { Faculty } from '../faculty.entity';
Expand All @@ -31,6 +35,7 @@ describe('Faculty Schedule Controller', function () {
let updateFacultyAbsencesStub: SinonStub;
const mockRepository: Record<string, SinonStub> = {};
let mockAbsenceRepository: Record<string, SinonStub>;
let mockSemesterRepository: SinonStubbedInstance<Repository<Semester>>;
const fakeYearList = [
2018,
2019,
Expand All @@ -45,6 +50,9 @@ describe('Faculty Schedule Controller', function () {
findOne: stub(),
save: stub(),
};
mockSemesterRepository = createStubInstance<Repository<Semester>>(
Repository
);
const testModule = await Test.createTestingModule({
imports: [ConfigModule],
controllers: [FacultyController],
Expand Down Expand Up @@ -80,6 +88,10 @@ describe('Faculty Schedule Controller', function () {
provide: getRepositoryToken(Absence),
useValue: mockAbsenceRepository,
},
{
provide: getRepositoryToken(Semester),
useValue: mockSemesterRepository,
},
],
})
.overrideGuard(Authentication)
Expand Down
12 changes: 10 additions & 2 deletions src/server/faculty/faculty.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { AbsenceResponseDTO } from 'common/dto/faculty/AbsenceResponse.dto';
import { AbsenceRequestDTO } from 'common/dto/faculty/AbsenceRequest.dto';
import { ConfigService } from 'server/config/config.service';
import { resolveAcademicYear } from 'common/utils/termHelperFunctions';
import { Semester } from 'server/semester/semester.entity';
import { Faculty } from './faculty.entity';
import { FacultyService } from './faculty.service';
import { FacultyScheduleService } from './facultySchedule.service';
Expand All @@ -55,6 +56,9 @@ export class FacultyController {
@InjectRepository(Absence)
private absenceRepository: Repository<Absence>;

@InjectRepository(Semester)
private semesterRepository: Repository<Semester>;

@Inject(FacultyService)
private facultyService: FacultyService;

Expand Down Expand Up @@ -196,7 +200,7 @@ export class FacultyController {
throw new NotFoundException('The entered Area does not exist');
}
}
const facultyToCreate: Faculty = Object.assign(new Faculty(), {
const faculty = await this.facultyRepository.save({
HUID: facultyDto.HUID,
firstName: facultyDto.firstName,
lastName: facultyDto.lastName,
Expand All @@ -205,7 +209,11 @@ export class FacultyController {
jointWith: facultyDto.jointWith,
notes: facultyDto.notes,
});
const faculty = await this.facultyRepository.save(facultyToCreate);

const allSemesters = await this.semesterRepository.find();
const facultyAbsences = allSemesters
.map((semester) => this.absenceRepository.create({ faculty, semester }));
await this.absenceRepository.save(facultyAbsences);
return {
id: faculty.id,
HUID: faculty.HUID,
Expand Down
32 changes: 0 additions & 32 deletions src/server/faculty/faculty.subscriber.ts

This file was deleted.

99 changes: 0 additions & 99 deletions tests/integration/server/faculty/facultysubscriber.test.ts

This file was deleted.

8 changes: 1 addition & 7 deletions tests/mocks/database/population/absence.population.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ export class AbsencePopulationService extends BasePopulationService<Absence> {
});
});

return this.repository.save(allAbsences, {
// Turn listeners off for this operation(but ONLY for this operation - we
// want them to run normally) since we're creating the
// absence records ourselves for testing so we don't need the subscriber
// to do it for us!
listeners: false,
});
return this.repository.save(allAbsences);
}

public async drop(): Promise<void> {
Expand Down

0 comments on commit 6962b1f

Please sign in to comment.