IO-3166-Global-Notifications-Part-2 - Checkpoint
This commit is contained in:
@@ -326,7 +326,6 @@ const newNoteAddedBuilder = (data) => {
|
||||
* Builds notification data for new time tickets posted.
|
||||
*/
|
||||
const newTimeTicketPostedBuilder = (data) => {
|
||||
consoleDir(data);
|
||||
const type = data?.data?.cost_center;
|
||||
const body = `An ${type} time ticket has been posted${data?.data?.flat_rate ? " (Flat Rate)" : ""}.`.trim();
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ const {
|
||||
supplementImportedBuilder,
|
||||
partMarkedBackOrderedBuilder
|
||||
} = require("./scenarioBuilders");
|
||||
const { isFunction } = require("lodash");
|
||||
|
||||
/**
|
||||
* An array of notification scenario definitions.
|
||||
@@ -25,9 +26,9 @@ const {
|
||||
* - fields {Array<string>}: Fields to check for changes.
|
||||
* - matchToUserFields {Array<string>}: Fields used to match scenarios to user data.
|
||||
* - onNew {boolean|Array<boolean>}: Indicates whether the scenario should be triggered on new data.
|
||||
* - onlyTrue {Array<string>}: Specifies fields that must be true for the scenario to match.
|
||||
* - builder {Function}: A function to handle the scenario.
|
||||
*/
|
||||
* - onlyTruthyValues {boolean|Array<string>}: Specifies fields that must have truthy values for the scenario to match.
|
||||
* */
|
||||
const notificationScenarios = [
|
||||
{
|
||||
key: "job-assigned-to-me",
|
||||
@@ -86,7 +87,6 @@ const notificationScenarios = [
|
||||
builder: newTimeTicketPostedBuilder
|
||||
},
|
||||
{
|
||||
// Good test for batching as this will hit multiple scenarios
|
||||
key: "intake-delivery-checklist-completed",
|
||||
table: "jobs",
|
||||
fields: ["intakechecklist", "deliverchecklist"],
|
||||
@@ -109,24 +109,21 @@ const notificationScenarios = [
|
||||
key: "critical-parts-status-changed",
|
||||
table: "joblines",
|
||||
fields: ["critical"],
|
||||
onlyTrue: ["critical"],
|
||||
onlyTruthyValues: ["critical"],
|
||||
builder: criticalPartsStatusChangedBuilder
|
||||
},
|
||||
{
|
||||
key: "part-marked-back-ordered",
|
||||
table: "joblines",
|
||||
fields: ["status"],
|
||||
builder: partMarkedBackOrderedBuilder
|
||||
},
|
||||
// -------------- Difficult ---------------
|
||||
// Holding off on this one for now
|
||||
{
|
||||
key: "supplement-imported",
|
||||
builder: supplementImportedBuilder
|
||||
// spans multiple tables,
|
||||
},
|
||||
// This one may be tricky as the jobid is not directly in the event data (this is probably wrong)
|
||||
// (should otherwise)
|
||||
// Status needs to mark meta data 'md_backorderd' for example
|
||||
// Double check Jobid
|
||||
{
|
||||
key: "part-marked-back-ordered",
|
||||
table: "joblines",
|
||||
builder: partMarkedBackOrderedBuilder
|
||||
}
|
||||
];
|
||||
|
||||
@@ -183,18 +180,6 @@ const getMatchingScenarios = (eventData) =>
|
||||
}
|
||||
}
|
||||
|
||||
// OnlyTrue logic:
|
||||
// If a scenario defines an onlyTrue array, then at least one of those fields must have changed
|
||||
// and its new value (from eventData.data) must be non-falsey.
|
||||
if (scenario.onlyTrue && Array.isArray(scenario.onlyTrue) && scenario.onlyTrue.length > 0) {
|
||||
const hasTruthyChange = scenario.onlyTrue.some(
|
||||
(field) => eventData.changedFieldNames.includes(field) && Boolean(eventData.data[field])
|
||||
);
|
||||
if (!hasTruthyChange) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// OnlyTruthyValues logic:
|
||||
// If onlyTruthyValues is defined, check that the new values of specified fields (or all changed fields if true)
|
||||
// are truthy. If an array, only check the listed fields, which must be in scenario.fields.
|
||||
@@ -225,6 +210,14 @@ const getMatchingScenarios = (eventData) =>
|
||||
}
|
||||
}
|
||||
|
||||
// Execute the callback if defined, passing eventData, and filter based on its return value
|
||||
if (isFunction(scenario?.callback)) {
|
||||
const shouldInclude = scenario.callback(eventData);
|
||||
if (!shouldInclude) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user