Skip to content

Commit

Permalink
feat: CE-1045 Add getLeadsByActionTaken query (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevespi authored Oct 16, 2024
1 parent 3ddcbc2 commit 3a6bb8b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { SectorCodeModule } from "./code-tables/sector_code/sector_code.module";
import { CEEBDecisionActionModule } from "./ceeb_decision_action/ceeb_decision_action.module";
import { AgencyCodeModule } from "./agency_code/agency_code.module";
import { ScheduleSectorXrefModule } from "./schedule_sector_xref/schedule_sector_xref.module";
import { LeadModule } from "./lead/lead.module";

@Module({
imports: [
Expand Down Expand Up @@ -63,6 +64,7 @@ import { ScheduleSectorXrefModule } from "./schedule_sector_xref/schedule_sector
SectorCodeModule,
CEEBDecisionActionModule,
ScheduleSectorXrefModule,
LeadModule,
],
controllers: [AppController],
providers: [AppService, DateScalar],
Expand Down
3 changes: 3 additions & 0 deletions backend/src/lead/lead.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
getLeadsByActionTaken(actionCode: String!): [String]
}
10 changes: 10 additions & 0 deletions backend/src/lead/lead.module.ts
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 {}
18 changes: 18 additions & 0 deletions backend/src/lead/lead.resolver.ts
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);
}
}
20 changes: 20 additions & 0 deletions backend/src/lead/lead.service.spec.ts
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();
});
});
59 changes: 59 additions & 0 deletions backend/src/lead/lead.service.ts
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;
}
}

0 comments on commit 3a6bb8b

Please sign in to comment.