generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CE-1045 Add getLeadsByActionTaken query (#95)
- Loading branch information
Showing
6 changed files
with
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type Query { | ||
getLeadsByActionTaken(actionCode: String!): [String] | ||
} |
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,10 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { LeadService } from "./lead.service"; | ||
import { LeadResolver } from "./lead.resolver"; | ||
import { PrismaModule } from "nestjs-prisma"; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
providers: [LeadResolver, LeadService], | ||
}) | ||
export class LeadModule {} |
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,18 @@ | ||
import { Resolver, Query, Args } from "@nestjs/graphql"; | ||
import { LeadService } from "./lead.service"; | ||
import { JwtRoleGuard } from "../auth/jwtrole.guard"; | ||
import { UseGuards } from "@nestjs/common"; | ||
import { Role } from "../enum/role.enum"; | ||
import { Roles } from "../auth/decorators/roles.decorator"; | ||
|
||
@UseGuards(JwtRoleGuard) | ||
@Resolver("Lead") | ||
export class LeadResolver { | ||
constructor(private readonly leadService: LeadService) {} | ||
|
||
@Query("getLeadsByActionTaken") | ||
@Roles(Role.CEEB) | ||
findOne(@Args("actionCode") actionCode: string) { | ||
return this.leadService.getLeadsByActionTaken(actionCode); | ||
} | ||
} |
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,20 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { LeadService } from "./lead.service"; | ||
import { PrismaModule } from "nestjs-prisma"; | ||
|
||
describe("LeadService", () => { | ||
let service: LeadService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [PrismaModule], | ||
providers: [LeadService], | ||
}).compile(); | ||
|
||
service = module.get<LeadService>(LeadService); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,59 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { PrismaService } from "nestjs-prisma"; | ||
import { GraphQLError } from "graphql"; | ||
import { ACTION_TYPE_CODES } from "../common/action_type_codes"; | ||
import { ACTION_CODES } from "../common/action_codes"; | ||
|
||
@Injectable() | ||
export class LeadService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async getLeadsByActionTaken(actionCode: string): Promise<string[]> { | ||
/** | ||
* Provided an action taken, returns the lead_identifier for each lead whose case_file has had that action taken | ||
* on it. | ||
* actionCode: the action_code to filter actions by | ||
*/ | ||
const actionCodeXrefContext = this.prisma.action_type_action_xref; | ||
const xrefResult = await actionCodeXrefContext.findFirst({ | ||
where: { | ||
action_code: actionCode, | ||
// Filtering by action taken is currently exclusive to CEEB which allows us to filter on CEEBACTION type | ||
action_type_code: ACTION_TYPE_CODES.CEEBACTION, | ||
}, | ||
select: { | ||
action_type_action_xref_guid: true, | ||
}, | ||
}); | ||
|
||
const actionResults = await this.prisma.action.findMany({ | ||
where: { | ||
action_type_action_xref_guid: xrefResult.action_type_action_xref_guid, | ||
}, | ||
select: { | ||
case_guid: true, | ||
}, | ||
}); | ||
|
||
const caseGuids: string[] = []; | ||
for (let action of actionResults) { | ||
caseGuids.push(action.case_guid); | ||
} | ||
|
||
const leadResults = await this.prisma.lead.findMany({ | ||
where: { | ||
case_identifier: { | ||
in: caseGuids, | ||
}, | ||
}, | ||
select: { | ||
lead_identifier: true, | ||
}, | ||
}); | ||
const leadIdentifiers: string[] = []; | ||
for (let leadId of leadResults) { | ||
leadIdentifiers.push(leadId.lead_identifier); | ||
} | ||
return leadIdentifiers; | ||
} | ||
} |