Skip to content

Commit

Permalink
feat: CE-1014 update web eoc filter to include complaints with a viol…
Browse files Browse the repository at this point in the history
…ation type of waste or pesticide in any region (#649)

Co-authored-by: dmitri-korin-bcps <[email protected]>
Co-authored-by: gregorylavery <[email protected]>
Co-authored-by: Scarlett <[email protected]>
Co-authored-by: Mike <[email protected]>
Co-authored-by: Mike Sears <[email protected]>
Co-authored-by: jeznorth <[email protected]>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: afwilcox <[email protected]>
Co-authored-by: Scarlett <[email protected]>
  • Loading branch information
10 people authored Sep 24, 2024
1 parent 78673aa commit 3f96ea0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ OR REPLACE FUNCTION public.insert_complaint_from_staging (_complaint_identifier
_webeoc_cos_area_community := complaint_data ->> 'cos_area_community';
_webeoc_cos_reffered_by_lst := complaint_data ->> 'cos_reffered_by_lst';
_cos_reffered_by_txt := left(complaint_data ->> '_cos_reffered_by_txt',120);
_webeoc_identifier := complaint_data ->> 'webeoc_identifier';

SELECT *
FROM PUBLIC.insert_and_return_code( _webeoc_cos_reffered_by_lst, 'reprtdbycd' )
INTO _cos_reffered_by_lst;
Expand Down
3 changes: 3 additions & 0 deletions webeoc/src/types/complaint-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ export interface Complaint {
back_number_of_days: string;
back_number_of_hours: string;
back_number_of_minutes: string;
flag_COS: string;
flag_AT: string;
flag_UAT: string;
}
1 change: 1 addition & 0 deletions webeoc/src/types/complaint-update-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ComplaintUpdate {
parent_species: string;
update_created_by_username: string;
update_created_by_position: string;
flag_UPD: string;
back_number_of_days: string;
back_number_of_hours: string;
back_number_of_minutes: string;
Expand Down
126 changes: 51 additions & 75 deletions webeoc/src/webeoc-scheduler/webeoc-scheduler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ export class WebEocScheduler {
this.publishComplaintUpdate.bind(this),
);

await this._handleActionTaken(WEBEOC_API_PATHS.ACTIONS_TAKEN, this._publishAction.bind(this));
await this._handleActionTakenUpdate(WEBEOC_API_PATHS.ACTIONS_TAKEN_UPDATES, this._publishActionUpdate.bind(this));
// handle actions taken
await this._handleAction(
() => this._fetchDataFromWebEOC(WEBEOC_API_PATHS.ACTIONS_TAKEN),
this._publishAction.bind(this),
);

// handle actions taken updates
await this._handleAction(
() => this._fetchDataFromWebEOC(WEBEOC_API_PATHS.ACTIONS_TAKEN_UPDATES),
this._publishActionUpdate.bind(this),
);
});

this.cronJob.start();
Expand Down Expand Up @@ -96,6 +105,26 @@ export class WebEocScheduler {
}
}

// Filter the complaints to only include complaints with a given flag (indicating the type of data: complaint, update, action taken, etc), or complaints where the violation type is Waste or Pesticide (which
// are complaints needed for CEEB (where the violation type is waste or pesticide, accross all regions)
private _filterComplaints(complaints: any[], flagName: string) {
return complaints.filter((complaint: any) => {
if (flagName === WEBEOC_FLAGS.COMPLAINTS) {
return (
complaint.flag_COS === "Yes" ||
complaint.violation_type === "Waste" ||
complaint.violation_type === "Pesticide"
);
} else if (flagName === WEBEOC_FLAGS.COMPLAINT_UPDATES) {
return (
complaint.flag_UPD === "Yes" ||
complaint.update_violation_type === "Waste" ||
complaint.update_violation_type === "Pesticide"
);
}
});
}

private async fetchDataFromWebEOC(urlPath: string, flagName: string): Promise<any[]> {
const dateFilter = this.getDateFilter();
const url = `${process.env.WEBEOC_URL}/${urlPath}`;
Expand All @@ -105,23 +134,24 @@ export class WebEocScheduler {
},
};

// default filter grabs complaints that are newer than a specific date
const filterItems = [dateFilter];

// construct the body of the request
const body = {
customFilter: {
boolean: "and",
items: [
dateFilter,
{
fieldname: flagName,
operator: "Equals",
fieldvalue: "Yes",
},
],
items: filterItems,
},
};

try {
const response = await axios.post(url, body, config);
return response.data as Complaint[];
const complaints = response.data;

const filteredComplaints = this._filterComplaints(complaints, flagName);

return filteredComplaints;
} catch (error) {
this.logger.error(`Error fetching data from WebEOC at ${urlPath}:`, error);
throw error;
Expand Down Expand Up @@ -165,8 +195,7 @@ export class WebEocScheduler {
return format(date, "yyyy-MM-dd HH:mm:ss");
}

//-- actions taken
private _fetchActions = async (path: string) => {
private _fetchDataFromWebEOC = async (path: string): Promise<Complaint[]> => {
const dateFilter = this.getDateFilter();
const url = `${process.env.WEBEOC_URL}/${path}`;
const config: AxiosRequestConfig = {
Expand All @@ -178,85 +207,32 @@ export class WebEocScheduler {
const body = {
customFilter: {
boolean: "and",
items: [
dateFilter,
{
fieldname: WEBEOC_FLAGS.ACTIONS_TAKEN,
operator: "Equals",
fieldvalue: "Yes",
},
],
items: [dateFilter],
},
};

try {
const response = await axios.post(url, body, config);
return response?.data;
} catch (error) {
this.logger.error(`Error fetching data from WebEOC at ${path}:`, error);
throw error;
}
};

private _fetchActionUpdates = async (path: string) => {
const dateFilter = this.getDateFilter();
const url = `${process.env.WEBEOC_URL}/${path}`;
const config: AxiosRequestConfig = {
headers: {
Cookie: this.cookie,
},
};

const body = {
customFilter: {
boolean: "and",
items: [
dateFilter,
{
fieldname: WEBEOC_FLAGS.ACTIONS_TAKEN_UPDATES,
operator: "Equals",
fieldvalue: "Yes",
},
],
},
};

try {
const response = await axios.post(url, body, config);
return response?.data;
return response.data as Complaint[];
} catch (error) {
this.logger.error(`Error fetching data from WebEOC at ${path}:`, error);
throw error;
}
};

private _handleActionTaken = async (path: string, publish: any) => {
try {
await this.authenticateWithWebEOC();
const data = await this._fetchActions(path);

if (data) {
this.logger.debug(`Found ${data?.length} actions_taken from WebEOC`);

for (const item of data) {
await publish(item);
}
}
} catch (error) {
this.logger.error(`Unable to fetch data from WebEOC`, error);
}
};

private _handleActionTakenUpdate = async (path: string, publish: any) => {
private _handleAction = async (
fetchMethod: () => Promise<any>,
publishMethod: (item: ActionTaken) => Promise<void>,
) => {
try {
await this.authenticateWithWebEOC();
const data = await this._fetchActionUpdates(path);
const data = await fetchMethod();

if (data) {
this.logger.debug(`Found ${data?.length} action_taken_updates from WebEOC`);
this.logger.debug(`Found ${data.length} action taken from WebEOC`);

for (const item of data) {
await publish(item);
await publishMethod(item);
}
}
} catch (error) {
Expand Down

0 comments on commit 3f96ea0

Please sign in to comment.