Skip to content

Commit

Permalink
Merge branch 'main' into feature/CE-107
Browse files Browse the repository at this point in the history
  • Loading branch information
afwilcox authored Nov 2, 2023
2 parents 055e271 + cfb9dc8 commit 7e1efeb
Show file tree
Hide file tree
Showing 15 changed files with 748 additions and 221 deletions.
36 changes: 0 additions & 36 deletions backend/src/v1/office/office.controller.spec.ts

This file was deleted.

31 changes: 17 additions & 14 deletions backend/src/v1/office/office.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, HttpCode } from '@nestjs/common';
import { OfficeService } from './office.service';
import { CreateOfficeDto } from './dto/create-office.dto';
import { UpdateOfficeDto } from './dto/update-office.dto';
import { JwtRoleGuard } from '../../auth/jwtrole.guard';
import { ApiTags } from '@nestjs/swagger';
import { Role } from '../../enum/role.enum';
import { Roles } from '../../auth/decorators/roles.decorator';
import { UUID } from 'crypto';

@ApiTags("office")
@UseGuards(JwtRoleGuard)
Expand All @@ -15,16 +16,10 @@ import { Roles } from '../../auth/decorators/roles.decorator';
export class OfficeController {
constructor(private readonly officeService: OfficeService) {}

@Post()
@Roles(Role.COS_OFFICER)
create(@Body() createOfficeDto: CreateOfficeDto) {
return this.officeService.create(createOfficeDto);
}

@Get()
@Get(':id')
@Roles(Role.COS_OFFICER)
findByGeoOrgCode(@Param('geo_organization_code') geo_organization_code: string) {
return this.officeService.findByGeoOrgCode(+geo_organization_code);
findOne(@Param('id') id: UUID) {
return this.officeService.findOne(id);
}

@Get("/by-zone/:zone_code")
Expand All @@ -34,18 +29,26 @@ export class OfficeController {
return this.officeService.findOfficesByZone(zone_code);
}

@Get(':id')
@Get("/by-geo-code/:code")
@Roles(Role.COS_OFFICER)
findOne(@Param('office_guid') id: string) {
return this.officeService.findOne(+id);
findByGeoOrgCode(@Param('code') code: string) {
return this.officeService.findByGeoOrgCode(code);
}

@Post()
@Roles(Role.COS_OFFICER)
create(@Body() createOfficeDto: CreateOfficeDto) {
return this.officeService.create(createOfficeDto);
}

@HttpCode(501)
@Patch(':id')
@Roles(Role.COS_OFFICER)
update(@Param('id') id: string, @Body() updateOfficeDto: UpdateOfficeDto) {
return this.officeService.update(+id, updateOfficeDto);
}


@HttpCode(501)
@Delete(':id')
@Roles(Role.COS_OFFICER)
remove(@Param('id') id: string) {
Expand Down
144 changes: 144 additions & 0 deletions backend/src/v1/office/office.controller.v2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { Test, TestingModule } from "@nestjs/testing";
import { getRepositoryToken } from "@nestjs/typeorm";
import { DataSource } from "typeorm";
import { INestApplication } from "@nestjs/common";

import { OfficeController } from "./office.controller";
import { OfficeService } from "./office.service";
import { Office } from "./entities/office.entity";

import { authGuardMock } from "../../../test/mocks/authGuardMock";
import { roleGuardMock } from "../../../test/mocks/roleGuardMock";
import { JwtAuthGuard } from "../../auth/jwtauth.guard";
import { JwtRoleGuard } from "../../auth/jwtrole.guard";

import { MockOfficeRepository } from "../../../test/mocks/mock-office-repository";
import { dataSourceMockFactory } from "../../../test/mocks/datasource";

import * as request from "supertest";
import { UUID } from "crypto";
import { CreateOfficeDto } from "./dto/create-office.dto";
import { AgencyCodeDto } from "../agency_code/dto/agency_code.dto";
import { UpdateOfficeDto } from "./dto/update-office.dto";

describe("Testing: Office Controller", () => {
let app: INestApplication;
let controller: OfficeController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [OfficeController],
providers: [
OfficeService,
{
provide: getRepositoryToken(Office),
useFactory: MockOfficeRepository,
},
{
provide: DataSource,
useFactory: dataSourceMockFactory,
},
],
})
.overrideGuard(JwtAuthGuard)
.useValue({ authGuardMock })
.overrideGuard(JwtRoleGuard)
.useValue({ roleGuardMock })
.compile();

app = module.createNestApplication();
await app.init();

controller = module.get<OfficeController>(OfficeController);
});

it("should be defined", () => {
expect(controller).toBeDefined();
});

it("should return 200 when a GET is called successfully", async () => {
const _officeId: UUID = "2044f08d-b53c-489a-8584-dd867b63514a";
const _unit = "CRSTN";
const _zone = "NCHKOLKS";

let response = await request(app.getHttpServer()).get(
`/office/${_officeId}`
);
expect(response.statusCode).toBe(200);

response = await request(app.getHttpServer()).get(
`/office/by-zone/${_zone}`
);
expect(response.statusCode).toBe(200);

response = await request(app.getHttpServer()).get(
`/office/by-geo-code/${_unit}`
);
expect(response.statusCode).toBe(200);
});

it("should return 201 when a POST is called successfully", () => {
const agency: AgencyCodeDto = {
agency_code: "",
short_description: "",
long_description: "",
display_order: 1,
active_ind: true,
create_user_id: "",
update_user_id: "",
create_utc_timestamp: new Date(),
update_utc_timestamp: new Date(),
};

const payload: CreateOfficeDto = {
geo_organization_unit_code: "TEST",
agency_code: agency,
create_user_id: "",
create_utc_timestamp: new Date(),
update_user_id: "",
update_utc_timestamp: new Date(),
};

return request(app.getHttpServer())
.post("/office/")
.send({ payload })
.expect(201);
});

it("should return 501 when a PATCH is is not implemented", () => {
const agency: AgencyCodeDto = {
agency_code: "",
short_description: "",
long_description: "",
display_order: 1,
active_ind: true,
create_user_id: "",
update_user_id: "",
create_utc_timestamp: new Date(),
update_utc_timestamp: new Date(),
};

const payload: UpdateOfficeDto = {
geo_organization_unit_code: "TEST",
agency_code: agency,
create_user_id: "JEST",
create_utc_timestamp: new Date(),
update_user_id: null,
update_utc_timestamp: null,
};

return request(app.getHttpServer())
.patch("/office/81c5d19b-b188-4b52-8ca3-f00fa987ed88/")
.send({ payload })
.expect(501);
});

it("should return 501 when a DELETE is is not implemented", async () => {
const _officeId: UUID = "2044f08d-b53c-489a-8584-dd867b63514a";

let response = await request(app.getHttpServer()).delete(
`/office/${_officeId}`
);
expect(response.statusCode).toBe(501);
});
});
34 changes: 0 additions & 34 deletions backend/src/v1/office/office.service.spec.ts

This file was deleted.

69 changes: 32 additions & 37 deletions backend/src/v1/office/office.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Injectable, Logger } from '@nestjs/common';
import { CreateOfficeDto } from './dto/create-office.dto';
import { UpdateOfficeDto } from './dto/update-office.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { DataSource, Repository } from 'typeorm';
import { Office } from './entities/office.entity';
import { Injectable, Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { DataSource, Repository } from "typeorm";
import { UUID } from "crypto";

import { CreateOfficeDto } from "./dto/create-office.dto";
import { UpdateOfficeDto } from "./dto/update-office.dto";
import { Office } from "./entities/office.entity";

@Injectable()
export class OfficeService {

private readonly logger = new Logger(OfficeService.name);

constructor(private dataSource: DataSource) {
}
constructor(private dataSource: DataSource) {}
@InjectRepository(Office)
private officeRepository: Repository<Office>;

Expand All @@ -21,13 +21,13 @@ export class OfficeService {
await queryRunner.connect();
await queryRunner.startTransaction();
let newOfficeString;
try
{
newOfficeString = await this.officeRepository.create(<CreateOfficeDto>office);
try {
newOfficeString = await this.officeRepository.create(
<CreateOfficeDto>office
);
await queryRunner.manager.save(newOfficeString);
await queryRunner.commitTransaction();
}
catch (err) {
} catch (err) {
this.logger.error(err);
await queryRunner.rollbackTransaction();
newOfficeString = "Error Occured";
Expand All @@ -37,33 +37,28 @@ export class OfficeService {
return newOfficeString;
}

findByGeoOrgCode (geo_org_code: any) {
findByGeoOrgCode = (geo_org_code: any) => {
return this.officeRepository.find({
where: {cos_geo_org_unit: geo_org_code},
relations: {
where: { cos_geo_org_unit: geo_org_code },
relations: {},
});
};

} ,
});
}

findOne(office_guid: any) {
return this.officeRepository.findOneOrFail({
where: {office_guid: office_guid},
})
}
findOne = async (id: UUID): Promise<Office> => {
return this.officeRepository.findOneByOrFail({ office_guid: id });
};

findOfficesByZone (zone_code: string)
{
const queryBuilder = this.officeRepository.createQueryBuilder('office')
.leftJoinAndSelect('office.cos_geo_org_unit', 'cos_geo_org_unit')
.leftJoinAndSelect('office.officers', 'officer')
.leftJoinAndSelect('officer.person_guid','person')
.where('cos_geo_org_unit.zone_code = :Zone', { Zone: zone_code })
.distinctOn(['cos_geo_org_unit.offloc_code']);
findOfficesByZone = (zone_code: string) => {
const queryBuilder = this.officeRepository
.createQueryBuilder("office")
.leftJoinAndSelect("office.cos_geo_org_unit", "cos_geo_org_unit")
.leftJoinAndSelect("office.officers", "officer")
.leftJoinAndSelect("officer.person_guid", "person")
.where("cos_geo_org_unit.zone_code = :Zone", { Zone: zone_code })
.distinctOn(["cos_geo_org_unit.offloc_code"]);

process.stdout.write("backend call" + queryBuilder.getQueryAndParameters().toLocaleString());
return queryBuilder.getMany();
}
};

update(id: number, updateOfficeDto: UpdateOfficeDto) {
return `This action updates a #${id} office`;
Expand All @@ -72,4 +67,4 @@ export class OfficeService {
remove(id: number) {
return `This action removes a #${id} office`;
}
}
}
Loading

0 comments on commit 7e1efeb

Please sign in to comment.