Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,16 @@ import { newErr, newOk } from '@tech-matters/types';

export type PendingIncident = {
id: number;
case_id: number;
contact_id: string;
description: string;
address: string;
category_id: number;
incident_class_id: number;
status: string;
caller_name: string;
caller_number: string;
created_at: string;
updated_at: string;
};

type CreateIncidentResponse = {
pending_incident: PendingIncident;
incident: any;
incident: { id: number };
} & ({ status: 'success' } | { status: 'exists'; message: string });

export type CreateIncidentParams = {
contact_id: string;
case_id: number;
caller_name: string;
caller_number: string;
description: string;
address: string;
category: string;
priority: string;
requestor_call_back: boolean;
person_demographics: {
first_name: string;
last_name: string;
nick_name: string;
age: string;
gender: string;
race: string;
};
is_officer_on_standby: boolean;
};

export const createIncident = async ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import { isErr, newErr, newOk, TResult } from '@tech-matters/types';
import { validatePayload } from './validation';
import * as hrmService from './hrm-service';
import * as beaconService from './beacon-service';
import * as mapping from './mapping';
import { getSsmParameter } from '@tech-matters/ssm-cache';
import {
handleAlbEvent,
AlbHandlerEvent,
AlbHandlerResult,
} from '@tech-matters/alb-handler';
import { twilioTokenValidator } from '@tech-matters/twilio-worker-auth';
import { newCreateIncidentMapper } from './mapping';

type DispatcherError =
| 'ValidationError'
Expand Down Expand Up @@ -152,7 +152,7 @@ const postHandler = async (
});
}

const incidentParams = mapping.toCreateIncident({
const incidentParams = newCreateIncidentMapper(helplineCode)({
caseObj: caseObj,
contact,
});
Expand Down Expand Up @@ -198,9 +198,7 @@ const postHandler = async (
console.info(
`${
status === 'success' ? 'new incident reported' : 'incident already exists'
}, incident id ${createIncidentResult.data.pending_incident.id}, case id ${
caseObj.id
}`,
}, incident id ${pendingIncidentId}, case id ${caseObj.id}`,
);
return newOk({
data: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (C) 2021-2023 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import type { CreateIncidentParams } from '../beacon-service';
import type { CaseService, Contact } from '@tech-matters/hrm-types';

export const toCreateIncident = ({
caseObj,
contact,
}: {
caseObj: CaseService;
contact: Contact;
}): CreateIncidentParams => {
return {
contact_id: contact.id.toString(),
case_id: parseInt(caseObj.id),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2021-2026 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import * as uscr from './uscrMapping';
import * as gy from './gyMapping';

export const newCreateIncidentMapper = (helplineCode: string | undefined) => {
switch (helplineCode) {
case 'gy':
return gy.toCreateIncident;
case 'as':
case 'uscr':
return uscr.toCreateIncident;
default:
throw new Error(`No mappings configured for for helpline code: ${helplineCode}`);
}
};
Comment on lines +20 to +30

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other places where we do this, we use helplineCode.toUpperCase(). Can we consolidate this one as well?

Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,35 @@
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import type { CreateIncidentParams } from './beacon-service';
import type { CreateIncidentParams } from '../beacon-service';
import type { CaseService, Contact } from '@tech-matters/hrm-types';

type UscrCreateIncidentParams = CreateIncidentParams & {
caller_name: string;
caller_number: string;
description: string;
address: string;
category: string;
priority: string;
requestor_call_back: boolean;
person_demographics: {
first_name: string;
last_name: string;
nick_name: string;
age: string;
gender: string;
race: string;
};
is_officer_on_standby: boolean;
};

export const toCreateIncident = ({
caseObj,
contact,
}: {
caseObj: CaseService;
contact: Contact;
}): CreateIncidentParams => {
}): UscrCreateIncidentParams => {
const { callerInformation, childInformation, caseInformation, categories } =
contact.rawJson || {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@

import { getSsmParameter, putSsmParameter } from '@tech-matters/ssm-cache';
import { isErr } from '@tech-matters/types';
import { ItemProcessor } from './types';
import type { BeaconDocumentSection, BeaconDocumentProcessor } from './types';

type ChunkReaderConfig<TItem> = {
type ChunkReaderConfig<TItem extends BeaconDocumentSection> = {
url: URL;
headers: Record<string, string>;
lastUpdateSeenSsmKey: string;
itemProcessor: ItemProcessor<TItem>;
itemProcessor: BeaconDocumentProcessor<TItem>;
itemExtractor: (responseBody: any) => TItem[];
maxItemsInChunk: number;
maxChunksToRead: number;
itemTypeName?: string; // Just for logging
};

const processChunk = async <TItem>(
const processChunk = async <TItem extends BeaconDocumentSection>(
items: TItem[],
lastSeen: string,
itemProcessor: ItemProcessor<TItem>,
itemProcessor: BeaconDocumentProcessor<TItem>,
itemTypeName: string = 'item',
): Promise<string> => {
let updatedLastSeen = lastSeen;
Expand All @@ -58,7 +58,7 @@ const processChunk = async <TItem>(
return updatedLastSeen;
};

export const readApiInChunks = async <TItem>({
export const readApiInChunks = async <TItem extends BeaconDocumentSection>({
url,
headers,
lastUpdateSeenSsmKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (C) 2021-2023 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

export type CaseReportContentNode = {
type:
| 'text'
| 'section'
| 'text_field'
| 'date_time_field'
| 'option'
| 'dropdown'
| 'date_time_field_calc'
| 'checkbox'
| 'documents';
value: string | null;
fields: CaseReportContentNode[] | null;
label: string;
} & Record<string, string | null | number | CaseReportContentNode[]>;

export type CaseReportContentValues = {
[key: string]: string | null | number | boolean | CaseReportContentValues;
};

export const extractContentNodeValues = ({
type,
value,
fields,
label,
}: CaseReportContentNode): CaseReportContentValues[keyof CaseReportContentValues] => {
switch (type) {
case 'section': {
const sectionEntries = (fields || []).map(node => [
node.label,
extractContentNodeValues(node),
]);
return Object.fromEntries(sectionEntries);
}
case 'checkbox': {
return value === label;
}
default: {
return value;
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* eslint-disable @typescript-eslint/naming-convention */
/**
* Copyright (C) 2021-2026 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import { BeaconDocumentProcessor, NewCaseSectionInfo } from '../../types';
import { addSectionToAseloCase, updateAseloCaseStatus } from '../../caseUpdater';
import { isErr, isOk, newErr } from '@tech-matters/types';
import { CaseReportContentNode } from '../extractContentNodeValues';

type RelevantRawCaseReportApiPayload = {
id: number;
case_id: string | null;
incident_id: number;
created_at: string;
updated_at: string;
content: {
fields: CaseReportContentNode[];
};
};

const caseReportToCaseReportCaseSection = ({
id,
case_id,
updated_at,
}: RelevantRawCaseReportApiPayload): NewCaseSectionInfo => {
return {
caseId: case_id as string,
lastUpdated: updated_at,
section: {
sectionId: id.toString(),
sectionTypeSpecificData: {},
},
};
};

export type RawCaseReportApiPayload = RelevantRawCaseReportApiPayload &
Omit<Record<string, any>, keyof RelevantRawCaseReportApiPayload>;

export const createCaseReportProcessor = (
accountSid: string,
): BeaconDocumentProcessor<RawCaseReportApiPayload> => {
const addCaseReportSectionToAseloCase = addSectionToAseloCase(
'caseReport',
caseReportToCaseReportCaseSection,
accountSid,
);

return async (caseReport: RawCaseReportApiPayload, lastSeen: string) => {
const caseReportResult = await addCaseReportSectionToAseloCase(caseReport, lastSeen);
if (isOk(caseReportResult)) {
const caseStatusUpdateResult = await updateAseloCaseStatus(
caseReport.case_id!,
'open',
accountSid,
);
if (isErr(caseStatusUpdateResult)) {
return newErr({
message: 'Failed to update status for Aselo case',
error: {
type: caseStatusUpdateResult.error.type,
level: caseStatusUpdateResult.error.level,
lastUpdated: caseReportResult.unwrap(),
},
});
}
}
return caseReportResult;
};
};
Loading
Loading