-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from logion-network/feature/workload-api
Expose workload of legal officer
- Loading branch information
Showing
8 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { injectable } from "inversify"; | ||
import { Controller, ApiController, Async, HttpGet } from "dinoloop"; | ||
import { components } from "./components.js"; | ||
import { OpenAPIV3 } from "express-oas-generator"; | ||
import { | ||
addTag, | ||
setControllerTag, | ||
AuthenticationService, | ||
getDefaultResponses, | ||
} from "@logion/rest-api-core"; | ||
import { WorkloadService } from "../services/workload.service.js"; | ||
|
||
export function fillInSpec(spec: OpenAPIV3.Document): void { | ||
const tagName = 'Workload'; | ||
addTag(spec, { | ||
name: tagName, | ||
description: "Legal-officer workload" | ||
}); | ||
setControllerTag(spec, /^\/api\/workload.*/, tagName); | ||
|
||
WorkloadController.getWorkload(spec); | ||
} | ||
|
||
type WorkloadView = components["schemas"]["WorkloadView"]; | ||
|
||
@injectable() | ||
@Controller('/workload') | ||
export class WorkloadController extends ApiController { | ||
|
||
constructor( | ||
private authenticationService: AuthenticationService, | ||
private workloadService: WorkloadService, | ||
) { | ||
super(); | ||
} | ||
|
||
static getWorkload(spec: OpenAPIV3.Document) { | ||
const operationObject = spec.paths["/api/workload/{legalOfficerAddress}"].get!; | ||
operationObject.summary = "Provides the workload of a given legal officer"; | ||
operationObject.description = "Requires authentication."; | ||
operationObject.responses = getDefaultResponses("WorkloadView"); | ||
} | ||
|
||
@HttpGet('/:legalOfficerAddress') | ||
@Async() | ||
async getWorkload(legalOfficerAddress: string): Promise<WorkloadView> { | ||
await this.authenticationService.authenticatedUser(this.request); | ||
return { | ||
workload: await this.workloadService.workloadOf(legalOfficerAddress), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { injectable } from "inversify"; | ||
import { LocRequestRepository } from "../model/locrequest.model.js"; | ||
import { FetchVaultTransferRequestsSpecification, VaultTransferRequestRepository } from "../model/vaulttransferrequest.model.js"; | ||
import { FetchProtectionRequestsSpecification, ProtectionRequestRepository } from "../model/protectionrequest.model.js"; | ||
|
||
@injectable() | ||
export class WorkloadService { | ||
|
||
constructor( | ||
private locRequestRepository: LocRequestRepository, | ||
private vaultTransferRequestRepository: VaultTransferRequestRepository, | ||
private protectionRequestRepository: ProtectionRequestRepository, | ||
) { | ||
} | ||
|
||
async workloadOf(address: string): Promise<number> { | ||
const pendingLocRequests = await this.locRequestRepository.findBy({ | ||
expectedOwnerAddress: address, | ||
expectedStatuses: [ "REVIEW_PENDING" ], | ||
}); | ||
const pendingVaultTransferRequests = await this.vaultTransferRequestRepository.findBy(new FetchVaultTransferRequestsSpecification({ | ||
expectedLegalOfficerAddress: address, | ||
expectedStatuses: [ "PENDING" ], | ||
})); | ||
const pendingProtectionRequests = await this.protectionRequestRepository.findBy(new FetchProtectionRequestsSpecification({ | ||
expectedLegalOfficerAddress: address, | ||
expectedStatuses: [ "PENDING" ], | ||
})); | ||
return pendingLocRequests.length + pendingVaultTransferRequests.length + pendingProtectionRequests.length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { TestApp } from "@logion/rest-api-core"; | ||
import { Container } from "inversify"; | ||
import { Mock } from "moq.ts"; | ||
import request from "supertest"; | ||
import { ALICE } from "../../helpers/addresses.js"; | ||
import { WorkloadController } from "../../../src/logion/controllers/workload.controller.js"; | ||
import { WorkloadService } from "../../../src/logion/services/workload.service.js"; | ||
|
||
const { setupApp } = TestApp; | ||
|
||
describe("WorkloadController", () => { | ||
|
||
it("provides expected workload", async () => { | ||
const app = setupApp(WorkloadController, mockForFetch) | ||
await request(app) | ||
.get(`/api/workload/${ ALICE }`) | ||
.send() | ||
.expect(200) | ||
.expect('Content-Type', /application\/json/) | ||
.expect(response => { | ||
expect(response.body.workload).toEqual(42); | ||
}); | ||
}); | ||
}); | ||
|
||
function mockForFetch(container: Container) { | ||
const service = new Mock<WorkloadService>(); | ||
service.setup(instance => instance.workloadOf(ALICE)).returnsAsync(42); | ||
container.bind(WorkloadService).toConstantValue(service.object()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { It, Mock } from "moq.ts"; | ||
import { FetchLocRequestsSpecification, LocRequestAggregateRoot, LocRequestRepository } from "../../../src/logion/model/locrequest.model.js"; | ||
import { FetchProtectionRequestsSpecification, ProtectionRequestAggregateRoot, ProtectionRequestRepository } from "../../../src/logion/model/protectionrequest.model.js"; | ||
import { FetchVaultTransferRequestsSpecification, VaultTransferRequestAggregateRoot, VaultTransferRequestRepository } from "../../../src/logion/model/vaulttransferrequest.model.js"; | ||
import { WorkloadService } from "../../../src/logion/services/workload.service.js"; | ||
import { ALICE } from "../../helpers/addresses.js"; | ||
|
||
describe("WorkloadService", () => { | ||
|
||
it("provides expected workload", async () => { | ||
const legalOfficerAddress = ALICE; | ||
const service = buildService({ | ||
legalOfficerAddress, | ||
locRequests: 10, | ||
vaultTransferRequests: 20, | ||
protectionRequests: 12, | ||
}); | ||
|
||
const workload = await service.workloadOf(legalOfficerAddress); | ||
|
||
expect(workload).toBe(42); | ||
}); | ||
|
||
it("provides 0 workload if nothing pending", async () => { | ||
const legalOfficerAddress = ALICE; | ||
const service = buildService({ | ||
legalOfficerAddress, | ||
locRequests: 0, | ||
vaultTransferRequests: 0, | ||
protectionRequests: 0, | ||
}); | ||
|
||
const workload = await service.workloadOf(legalOfficerAddress); | ||
|
||
expect(workload).toBe(0); | ||
}); | ||
}); | ||
|
||
function buildService(args: { | ||
legalOfficerAddress: string, | ||
locRequests: number, | ||
vaultTransferRequests: number, | ||
protectionRequests: number, | ||
}): WorkloadService { | ||
const locRequestRepository = new Mock<LocRequestRepository>(); | ||
locRequestRepository.setup(instance => instance.findBy( | ||
It.Is<FetchLocRequestsSpecification>(spec => spec.expectedOwnerAddress === args.legalOfficerAddress) | ||
)).returnsAsync(pendingLocRequests(args.locRequests)); | ||
|
||
const vaultTransferRequestRepository = new Mock<VaultTransferRequestRepository>(); | ||
vaultTransferRequestRepository.setup(instance => instance.findBy( | ||
It.Is<FetchVaultTransferRequestsSpecification>(spec => spec.expectedLegalOfficerAddress === args.legalOfficerAddress) | ||
)).returnsAsync(pendingVaultTransferRequests(args.vaultTransferRequests)); | ||
|
||
const protectionRequestRepository = new Mock<ProtectionRequestRepository>(); | ||
protectionRequestRepository.setup(instance => instance.findBy( | ||
It.Is<FetchProtectionRequestsSpecification>(spec => spec.expectedLegalOfficerAddress === args.legalOfficerAddress) | ||
)).returnsAsync(pendingProtectionRequests(args.protectionRequests)); | ||
|
||
return new WorkloadService( | ||
locRequestRepository.object(), | ||
vaultTransferRequestRepository.object(), | ||
protectionRequestRepository.object(), | ||
); | ||
} | ||
|
||
function pendingLocRequests(length: number): LocRequestAggregateRoot[] { | ||
return new Array(length); | ||
} | ||
|
||
function pendingVaultTransferRequests(length: number): VaultTransferRequestAggregateRoot[] { | ||
return new Array(length); | ||
} | ||
|
||
function pendingProtectionRequests(length: number): ProtectionRequestAggregateRoot[] { | ||
return new Array(length); | ||
} |