From 592b47b5d4b3335c246bb15404eea1b10f2096a1 Mon Sep 17 00:00:00 2001
From: Patrick Fic <>
Date: Mon, 21 Mar 2022 11:47:25 -0600
Subject: [PATCH] IO-1789 Incoming hours cap
---
bodyshop_translations.babel | 26 ++++++
.../shop-info.scheduling.component.jsx | 6 ++
client/src/graphql/bodyshop.queries.js | 3 +-
client/src/translations/en_us/common.json | 3 +
client/src/translations/es/common.json | 3 +
client/src/translations/fr/common.json | 3 +
hasura/metadata/tables.yaml | 2 +
.../down.sql | 4 +
.../up.sql | 2 +
server/graphql-client/queries.js | 1 +
server/scheduling/scheduling-job.js | 89 +++++++++++++++----
11 files changed, 124 insertions(+), 18 deletions(-)
create mode 100644 hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/down.sql
create mode 100644 hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/up.sql
diff --git a/bodyshop_translations.babel b/bodyshop_translations.babel
index d11e1ed2d..28764f991 100644
--- a/bodyshop_translations.babel
+++ b/bodyshop_translations.babel
@@ -7280,6 +7280,32 @@
+
+ ss_configuration
+
+
+ dailyhrslimit
+ false
+
+
+
+
+
+ en-US
+ false
+
+
+ es-MX
+ false
+
+
+ fr-CA
+ false
+
+
+
+
+
ssbuckets
diff --git a/client/src/components/shop-info/shop-info.scheduling.component.jsx b/client/src/components/shop-info/shop-info.scheduling.component.jsx
index d071cc9de..85e64c555 100644
--- a/client/src/components/shop-info/shop-info.scheduling.component.jsx
+++ b/client/src/components/shop-info/shop-info.scheduling.component.jsx
@@ -70,6 +70,12 @@ export default function ShopInfoSchedulingComponent({ form }) {
>
+
+
+
{t("bodyshop.labels.workingdays")}
diff --git a/client/src/graphql/bodyshop.queries.js b/client/src/graphql/bodyshop.queries.js
index f0bd54a21..88f9b7bec 100644
--- a/client/src/graphql/bodyshop.queries.js
+++ b/client/src/graphql/bodyshop.queries.js
@@ -101,6 +101,7 @@ export const QUERY_BODYSHOP = gql`
md_filehandlers
md_email_cc
timezone
+ ss_configuration
employees {
user_email
id
@@ -199,6 +200,7 @@ export const UPDATE_SHOP = gql`
md_filehandlers
md_email_cc
timezone
+ ss_configuration
employees {
id
first_name
@@ -266,7 +268,6 @@ export const QUERY_DELIVER_CHECKLIST = gql`
ro_number
actual_completion
actual_delivery
-
}
}
`;
diff --git a/client/src/translations/en_us/common.json b/client/src/translations/en_us/common.json
index 255a81a03..595bd958f 100644
--- a/client/src/translations/en_us/common.json
+++ b/client/src/translations/en_us/common.json
@@ -455,6 +455,9 @@
"label": "Label",
"templates": "Templates"
},
+ "ss_configuration": {
+ "dailyhrslimit": "Daily Incoming Hours Limit"
+ },
"ssbuckets": {
"gte": "Greater Than/Equal to (hrs)",
"id": "ID",
diff --git a/client/src/translations/es/common.json b/client/src/translations/es/common.json
index 336ba07d4..3d3c44a0d 100644
--- a/client/src/translations/es/common.json
+++ b/client/src/translations/es/common.json
@@ -455,6 +455,9 @@
"label": "",
"templates": ""
},
+ "ss_configuration": {
+ "dailyhrslimit": ""
+ },
"ssbuckets": {
"gte": "",
"id": "",
diff --git a/client/src/translations/fr/common.json b/client/src/translations/fr/common.json
index 67435a1fc..d44d11e95 100644
--- a/client/src/translations/fr/common.json
+++ b/client/src/translations/fr/common.json
@@ -455,6 +455,9 @@
"label": "",
"templates": ""
},
+ "ss_configuration": {
+ "dailyhrslimit": ""
+ },
"ssbuckets": {
"gte": "",
"id": "",
diff --git a/hasura/metadata/tables.yaml b/hasura/metadata/tables.yaml
index c91b048fc..8473f5887 100644
--- a/hasura/metadata/tables.yaml
+++ b/hasura/metadata/tables.yaml
@@ -860,6 +860,7 @@
- shopname
- shoprates
- speedprint
+ - ss_configuration
- ssbuckets
- state
- state_tax_id
@@ -938,6 +939,7 @@
- shopname
- shoprates
- speedprint
+ - ss_configuration
- ssbuckets
- state
- state_tax_id
diff --git a/hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/down.sql b/hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/down.sql
new file mode 100644
index 000000000..fbb2eef6c
--- /dev/null
+++ b/hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/down.sql
@@ -0,0 +1,4 @@
+-- Could not auto-generate a down migration.
+-- Please write an appropriate down migration for the SQL below:
+-- alter table "public"."bodyshops" add column "ss_configuration" jsonb
+-- null default jsonb_build_object();
diff --git a/hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/up.sql b/hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/up.sql
new file mode 100644
index 000000000..c693c6d4e
--- /dev/null
+++ b/hasura/migrations/1647876980174_alter_table_public_bodyshops_add_column_ss_configuration/up.sql
@@ -0,0 +1,2 @@
+alter table "public"."bodyshops" add column "ss_configuration" jsonb
+ null default jsonb_build_object();
diff --git a/server/graphql-client/queries.js b/server/graphql-client/queries.js
index 306cb06cb..641adbd82 100644
--- a/server/graphql-client/queries.js
+++ b/server/graphql-client/queries.js
@@ -464,6 +464,7 @@ exports.QUERY_UPCOMING_APPOINTMENTS = `query QUERY_UPCOMING_APPOINTMENTS($now: t
jobs_by_pk(id: $jobId) {
bodyshop {
ssbuckets
+ ss_configuration
target_touchtime
workingdays
timezone
diff --git a/server/scheduling/scheduling-job.js b/server/scheduling/scheduling-job.js
index a314022c9..316bc7974 100644
--- a/server/scheduling/scheduling-job.js
+++ b/server/scheduling/scheduling-job.js
@@ -5,6 +5,7 @@ const Dinero = require("dinero.js");
const moment = require("moment-timezone");
const logger = require("../utils/logger");
const _ = require("lodash");
+const { filter } = require("lodash");
require("dotenv").config({
path: path.resolve(
process.cwd(),
@@ -32,7 +33,8 @@ exports.job = async (req, res) => {
});
const { jobs_by_pk, blockedDays, prodJobs, arrJobs, compJobs } = result;
- const { ssbuckets, workingdays, timezone } = result.jobs_by_pk.bodyshop;
+ const { ssbuckets, workingdays, timezone, ss_configuration } =
+ result.jobs_by_pk.bodyshop;
const jobHrs = result.jobs_by_pk.jobhrs.aggregate.sum.mod_lb_hrs;
const JobBucket = ssbuckets.filter(
@@ -63,29 +65,68 @@ exports.job = async (req, res) => {
}
});
- const filteredArrJobs = arrJobs.filter(
- (j) => JobBucket.id === CheckJobBucket(ssbuckets, j)
- );
+ // const filteredArrJobs = arrJobs.filter(
+ // (j) => JobBucket.id === CheckJobBucket(ssbuckets, j)
+ // );
+ const filteredArrJobs = [];
+
+ // filteredArrJobs.forEach((item) => {
+ // const itemDate = moment(item.scheduled_in)
+ // .tz(timezone)
+ // .format("yyyy-MM-DD");
+ // if (!!load[itemDate]) {
+ // load[itemDate].hoursIn =
+ // (load[itemDate].hoursIn || 0) +
+ // item.labhrs.aggregate.sum.mod_lb_hrs +
+ // item.larhrs.aggregate.sum.mod_lb_hrs;
+ // load[itemDate].jobsIn.push(item);
+ // } else {
+ // load[itemDate] = {
+ // jobsIn: [item],
+ // jobsOut: [],
+ // hoursIn:
+ // item.labhrs.aggregate.sum.mod_lb_hrs +
+ // item.larhrs.aggregate.sum.mod_lb_hrs,
+ // };
+ // }
+ // });
+
+ arrJobs.forEach((item) => {
+ let isSameBucket = false;
+ if (JobBucket.id === CheckJobBucket(ssbuckets, item)) {
+ filteredArrJobs.push(item);
+ isSameBucket = true;
+ }
+
+ let jobHours =
+ item.labhrs.aggregate.sum.mod_lb_hrs +
+ item.larhrs.aggregate.sum.mod_lb_hrs;
- filteredArrJobs.forEach((item) => {
const itemDate = moment(item.scheduled_in)
.tz(timezone)
.format("yyyy-MM-DD");
- if (!!load[itemDate]) {
- load[itemDate].hoursIn =
- (load[itemDate].hoursIn || 0) +
- item.labhrs.aggregate.sum.mod_lb_hrs +
- item.larhrs.aggregate.sum.mod_lb_hrs;
- load[itemDate].jobsIn.push(item);
- } else {
+ if (isSameBucket) {
+ if (!!load[itemDate]) {
+ load[itemDate].hoursIn = (load[itemDate].hoursIn || 0) + jobHours;
+ load[itemDate].jobsIn.push(item);
+ } else {
+ load[itemDate] = {
+ jobsIn: [item],
+ jobsOut: [],
+ hoursIn: jobHours,
+ };
+ }
+ }
+ if (!load[itemDate]) {
load[itemDate] = {
- jobsIn: [item],
+ jobsIn: [],
jobsOut: [],
- hoursIn:
- item.labhrs.aggregate.sum.mod_lb_hrs +
- item.larhrs.aggregate.sum.mod_lb_hrs,
+ hoursIn: 0,
+ hoursInTotal: 0,
};
}
+ load[itemDate].hoursInTotal =
+ (load[itemDate].hoursInTotal || 0) + jobHours;
});
//Get the completing jobs.
@@ -214,11 +255,25 @@ exports.job = async (req, res) => {
(workingdays[dayOfWeekMapper(moment(loadKey).day())] || false) &&
!load[loadKey].blocked;
+ let isUnderDailyTotalLimit = true;
+
+ if (
+ ss_configuration &&
+ ss_configuration.dailyhrslimit &&
+ ss_configuration.dailyhrslimit > 0 &&
+ load[loadKey] &&
+ load[loadKey].hoursInTotal &&
+ load[loadKey].hoursInTotal > ss_configuration.dailyhrslimit
+ ) {
+ isUnderDailyTotalLimit = false;
+ }
+
if (
load[loadKey].expectedLoad &&
load[loadKey].expectedLoad[JobBucket.id] &&
JobBucket.target > load[loadKey].expectedLoad[JobBucket.id].count &&
- isShopOpen
+ isShopOpen &&
+ isUnderDailyTotalLimit
)
possibleDates.push(new Date(loadKey).toISOString().substr(0, 10));
});