fix(quality request): bug fixes
lots of bug fixes plug 2 new counters, quality inspect time and warehouse return time
This commit is contained in:
@@ -1,171 +1,180 @@
|
||||
import { differenceInMinutes } from "date-fns";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { qualityRequest } from "../../../../database/schema/qualityRequest.js";
|
||||
import { timeZoneFix } from "../../../globalUtils/timeZoneFix.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { query } from "../../sqlServer/prodSqlServer.js";
|
||||
import { qrequestQuery } from "../../sqlServer/querys/quality/request.js";
|
||||
import { timeZoneFix } from "../../../globalUtils/timeZoneFix.js";
|
||||
|
||||
export const addNewPallet = async (data: any, user: string) => {
|
||||
/**
|
||||
* Post new pallets
|
||||
*/
|
||||
export const addNewPallet = async (data: any) => {
|
||||
/**
|
||||
* Post new pallets
|
||||
*/
|
||||
|
||||
if (parseInt(data.runningNr) <= 0) {
|
||||
return {
|
||||
sucess: false,
|
||||
message: "Please add a valid running number.",
|
||||
};
|
||||
}
|
||||
const updateQuery = qrequestQuery.replaceAll(
|
||||
"[runningNumber]",
|
||||
data.runningNr
|
||||
);
|
||||
const { data: c, error: ce } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(qualityRequest)
|
||||
.where(eq(qualityRequest.runningNr, data.runningNr))
|
||||
);
|
||||
if (ce) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the quality request",
|
||||
data: ce,
|
||||
};
|
||||
}
|
||||
if (parseInt(data.runningNr) <= 0) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Please add a valid running number.",
|
||||
};
|
||||
}
|
||||
const updateQuery = qrequestQuery.replaceAll(
|
||||
"[runningNumber]",
|
||||
data.runningNr,
|
||||
);
|
||||
const { data: c, error: ce } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(qualityRequest)
|
||||
.where(eq(qualityRequest.runningNr, data.runningNr)),
|
||||
);
|
||||
if (ce) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the quality request",
|
||||
data: ce,
|
||||
};
|
||||
}
|
||||
|
||||
const palletData: any = c;
|
||||
// if the pallet exist then tell the user to check on it
|
||||
if (
|
||||
(palletData && palletData[0]?.palletStatus === 1) ||
|
||||
palletData[0]?.palletStatus === 4
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running number ${data.runningNr} is already pending or reactivated please follow up with the warehouse team on status to be moved.`,
|
||||
};
|
||||
}
|
||||
const palletData: any = c;
|
||||
// if the pallet exist then tell the user to check on it
|
||||
const pStatus = [1, 4, 6];
|
||||
if (palletData && pStatus.includes(palletData[0]?.palletStatus)) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running number ${data.runningNr} is already pending or reactivated please follow up with the warehouse team on status to be moved.`,
|
||||
};
|
||||
}
|
||||
|
||||
if (palletData.length > 0) {
|
||||
try {
|
||||
// get the pallet info from stock
|
||||
const { data: pa, error: pe } = await tryCatch(
|
||||
query(updateQuery, "quality request")
|
||||
);
|
||||
const p: any = pa;
|
||||
if (pe) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the pallet from stock",
|
||||
data: pe,
|
||||
};
|
||||
}
|
||||
const pData = {
|
||||
warehouseAtRequest: p[0].warehouseAtRequest,
|
||||
locationAtRequest: p[0].locationAtRequest,
|
||||
warehouseMovedTo: null,
|
||||
locationMovedTo: null,
|
||||
palletStatus: 4,
|
||||
durationToMove: 0,
|
||||
palletStatusText: "reactivated",
|
||||
palletRequest: palletData[0].palletStatus + 1,
|
||||
upd_user: user,
|
||||
upd_date: new Date(timeZoneFix()),
|
||||
};
|
||||
// update the existing pallet if already in the system
|
||||
if (palletData.length > 0) {
|
||||
try {
|
||||
// get the pallet info from stock
|
||||
const { data: pa, error: pe } = await tryCatch(
|
||||
query(updateQuery, "quality request"),
|
||||
);
|
||||
const p: any = pa ? pa.data : [];
|
||||
if (pe) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the pallet from stock",
|
||||
data: pe,
|
||||
};
|
||||
}
|
||||
const pData = {
|
||||
warehouseAtRequest: p[0].warehouseAtRequest,
|
||||
locationAtRequest: p[0].locationAtRequest,
|
||||
warehouseMovedTo: null,
|
||||
locationMovedTo: null,
|
||||
palletStatus: data.palletStatusText === "return" ? 6 : 4,
|
||||
//durationToMove: 0,
|
||||
palletStatusText:
|
||||
data.palletStatusText === "return" ? "return" : "reactivated",
|
||||
qualityDurationToInspect:
|
||||
data.palletStatusText === "return"
|
||||
? differenceInMinutes(new Date(Date.now()), p[0].lastMove)
|
||||
: 0,
|
||||
locationDropOff:
|
||||
data.palletStatusText === "return" ? "Return to warhouse" : "",
|
||||
palletRequest: palletData[0].palletStatus + 1,
|
||||
upd_user: data.user,
|
||||
upd_date: sql`NOW()`,
|
||||
};
|
||||
|
||||
const { data: u, error } = await tryCatch(
|
||||
db
|
||||
.update(qualityRequest)
|
||||
.set(pData)
|
||||
.where(eq(qualityRequest.runningNr, data.runningNr))
|
||||
);
|
||||
const { data: u, error } = await tryCatch(
|
||||
db
|
||||
.update(qualityRequest)
|
||||
.set(pData)
|
||||
.where(eq(qualityRequest.runningNr, data.runningNr)),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running number: ${data.runningNr} encountered and error reactivated.`,
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running number: ${data.runningNr} encountered and error reactivated.`,
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
|
||||
if (data) {
|
||||
return {
|
||||
success: true,
|
||||
message: `Running number: ${data.runningNr} was just reactivated.`,
|
||||
data: u,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message:
|
||||
"There was an error updating the pallet in quality request",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (data) {
|
||||
return {
|
||||
success: true,
|
||||
message: `Running number: ${data.runningNr} was just reactivated.`,
|
||||
data: u,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error updating the pallet in quality request",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// add new pallet
|
||||
try {
|
||||
const { data: px, error: pe } = await tryCatch(
|
||||
query(updateQuery, "quality request")
|
||||
);
|
||||
const p: any = px;
|
||||
if (p.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running Number ${data.runningNr} dose not exist in stock.`,
|
||||
};
|
||||
}
|
||||
// add new pallet
|
||||
try {
|
||||
const { data: px, error: pe } = await tryCatch(
|
||||
query(updateQuery, "quality request"),
|
||||
);
|
||||
|
||||
if (pe) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the pallet from stock",
|
||||
data: pe,
|
||||
};
|
||||
}
|
||||
const p: any = px ? px.data : [];
|
||||
if (p.length === 0) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running Number ${data.runningNr} dose not exist in stock, please check the running number and try again.`,
|
||||
};
|
||||
}
|
||||
|
||||
const nData = {
|
||||
article: p[0].article,
|
||||
description: p[0].description,
|
||||
runningNr: p[0].runningNr,
|
||||
lotNr: p[0].lotNr,
|
||||
warehouseAtRequest: p[0].warehouseAtRequest,
|
||||
locationAtRequest: p[0].locationAtRequest,
|
||||
locationDropOff: data.moveTo,
|
||||
palletStatus: 1,
|
||||
palletStatusText: "pending",
|
||||
palletRequest: 1,
|
||||
add_user: user,
|
||||
upd_user: user,
|
||||
};
|
||||
if (pe) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the pallet from stock",
|
||||
data: pe,
|
||||
};
|
||||
}
|
||||
console.log(p);
|
||||
const nData = {
|
||||
article: p[0].article,
|
||||
description: p[0].description,
|
||||
runningNr: p[0].runningNr,
|
||||
lotNr: p[0].lotNr,
|
||||
warehouseAtRequest: p[0].warehouseAtRequest,
|
||||
locationAtRequest: p[0].locationAtRequest,
|
||||
locationDropOff: data.moveTo,
|
||||
palletStatus: 1,
|
||||
palletStatusText: "pending",
|
||||
palletRequest: 1,
|
||||
add_user: data.user,
|
||||
upd_user: data.user,
|
||||
};
|
||||
|
||||
const { data: u, error } = await tryCatch(
|
||||
db.insert(qualityRequest).values(nData)
|
||||
);
|
||||
const { data: u, error } = await tryCatch(
|
||||
db.insert(qualityRequest).values(nData).onConflictDoNothing(),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running number: ${data.runningNr} encountered and error reactivated.`,
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: `Running number: ${data.runningNr} encountered and error reactivated.`,
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
|
||||
if (data) {
|
||||
return {
|
||||
success: true,
|
||||
message: `Running number: ${data.runningNr} was just added.`,
|
||||
data: u,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error adding the pallet in quality request",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
if (data) {
|
||||
return {
|
||||
success: true,
|
||||
message: `Running number: ${data.runningNr} was just added.`,
|
||||
data: u,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error adding the pallet in quality request",
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,110 +1,127 @@
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
import { differenceInMinutes } from "date-fns";
|
||||
import { eq, inArray, sql } from "drizzle-orm";
|
||||
import { db } from "../../../../database/dbclient.js";
|
||||
import { qualityRequest } from "../../../../database/schema/qualityRequest.js";
|
||||
import { delay } from "../../../globalUtils/delay.js";
|
||||
import { timeZoneFix } from "../../../globalUtils/timeZoneFix.js";
|
||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
import { qrequestQuery } from "../../sqlServer/querys/quality/request.js";
|
||||
import { query } from "../../sqlServer/prodSqlServer.js";
|
||||
import { differenceInMinutes } from "date-fns";
|
||||
import { timeZoneFix } from "../../../globalUtils/timeZoneFix.js";
|
||||
import { qrequestQuery } from "../../sqlServer/querys/quality/request.js";
|
||||
|
||||
export const qualityCycle = async () => {
|
||||
/**
|
||||
* Cycles the pallets in the quality request to see whats been moved or changed.
|
||||
*/
|
||||
/**
|
||||
* Cycles the pallets in the quality request to see whats been moved or changed.
|
||||
*/
|
||||
const warehouse = [1, 4, 5];
|
||||
|
||||
// pallet request check interval 5min check to start
|
||||
//setInterval(async () => {
|
||||
// create the date stuff
|
||||
const currentTime = new Date(Date.now());
|
||||
// pallet request check interval 5min check to start
|
||||
setInterval(
|
||||
async () => {
|
||||
// create the date stuff
|
||||
const currentTime = new Date(Date.now());
|
||||
|
||||
// pull in all current pallets from our db
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(qualityRequest)
|
||||
.where(inArray(qualityRequest.palletStatus, [1, 4, 5]))
|
||||
);
|
||||
// pull in all current pallets from our db
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(qualityRequest)
|
||||
.where(inArray(qualityRequest.palletStatus, [1, 4, 5, 6])),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"lst",
|
||||
"quality",
|
||||
`There was an error getting quality request data: ${error}`
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting quality request data",
|
||||
};
|
||||
}
|
||||
const lstQData: any = data;
|
||||
// get the pallets that currentStat is moved
|
||||
// const res = await runQuery(palletMoveCheck, "palletCheck");
|
||||
if (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"lst",
|
||||
"quality",
|
||||
`There was an error getting quality request data: ${error}`,
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting quality request data",
|
||||
};
|
||||
}
|
||||
const lstQData: any = data;
|
||||
// get the pallets that currentStat is moved
|
||||
// const res = await runQuery(palletMoveCheck, "palletCheck");
|
||||
|
||||
if (lstQData.length != 0) {
|
||||
for (let i = 0; i < lstQData.length; i++) {
|
||||
// run the pallet query we will compare the data.
|
||||
// console.log(lstQData[i]);
|
||||
//update query with plant token
|
||||
if (lstQData.length != 0) {
|
||||
for (let i = 0; i < lstQData.length; i++) {
|
||||
// run the pallet query we will compare the data.
|
||||
// console.log(lstQData[i]);
|
||||
//update query with plant token
|
||||
|
||||
// change the update the pallet number
|
||||
const qPalletNumber = qrequestQuery.replaceAll(
|
||||
"[runningNumber]",
|
||||
lstQData[i].runningNr
|
||||
);
|
||||
// update the the pallet number
|
||||
const qPalletNumber = qrequestQuery.replaceAll(
|
||||
"[runningNumber]",
|
||||
lstQData[i].runningNr,
|
||||
);
|
||||
|
||||
const queryData: any = await query(
|
||||
qPalletNumber,
|
||||
"Quality update check"
|
||||
);
|
||||
let prodData: any =
|
||||
queryData?.data.length === 0 ? [] : queryData.data;
|
||||
const queryData: any = await query(
|
||||
qPalletNumber,
|
||||
"Quality update check",
|
||||
);
|
||||
let prodData: any =
|
||||
queryData?.data.length === 0 ? [] : queryData.data;
|
||||
|
||||
if (
|
||||
lstQData[i]?.locationAtRequest != prodData[0]?.locationAtRequest
|
||||
) {
|
||||
// time to do the pallet update stuff
|
||||
const qDataPost = {
|
||||
warehouseMovedTo: prodData[0]?.warehouseAtRequest,
|
||||
locationMovedTo: prodData[0]?.locationAtRequest,
|
||||
durationToMove: differenceInMinutes(
|
||||
timeZoneFix(),
|
||||
lstQData[i].upd_date
|
||||
),
|
||||
palletStatus: 2,
|
||||
palletStatusText: "moved",
|
||||
upd_date: new Date(timeZoneFix()),
|
||||
upd_user: "LST_System",
|
||||
};
|
||||
if (
|
||||
lstQData[i]?.locationAtRequest != prodData[0]?.locationAtRequest
|
||||
) {
|
||||
// time to do the pallet update stuff
|
||||
const qDataPost = {
|
||||
warehouseMovedTo: prodData[0]?.warehouseAtRequest,
|
||||
locationMovedTo: prodData[0]?.locationAtRequest,
|
||||
// how ling did it take the warhouse to originally move the pallet
|
||||
durationToMove: warehouse.includes(lstQData[i].palletStatus)
|
||||
? differenceInMinutes(
|
||||
new Date(Date.now()),
|
||||
lstQData[i].upd_date,
|
||||
)
|
||||
: lstQData[i].durationToMove,
|
||||
// how long did it take warehouse to move the pallet back agian
|
||||
returnDurationToInspect:
|
||||
lstQData[i].palletStatus === 7
|
||||
? differenceInMinutes(
|
||||
new Date(Date.now()),
|
||||
lstQData[i].upd_date,
|
||||
)
|
||||
: lstQData[i].qualityDurationToInspect,
|
||||
palletStatus: 2,
|
||||
palletStatusText: "moved",
|
||||
upd_date: sql`NOW()`,
|
||||
upd_user: "LST_System",
|
||||
};
|
||||
|
||||
const updatePallet = await db
|
||||
.update(qualityRequest)
|
||||
.set(qDataPost)
|
||||
.where(eq(qualityRequest.runningNr, lstQData[i].runningNr));
|
||||
const updatePallet = await db
|
||||
.update(qualityRequest)
|
||||
.set(qDataPost)
|
||||
.where(eq(qualityRequest.runningNr, lstQData[i].runningNr));
|
||||
|
||||
createLog(
|
||||
"info",
|
||||
"lst",
|
||||
"quality",
|
||||
`Pallet ${lstQData[i].runningNr} was updated`
|
||||
);
|
||||
} else {
|
||||
createLog(
|
||||
"debug",
|
||||
"lst",
|
||||
"quality",
|
||||
`Pallet ${
|
||||
lstQData[i].runningNr
|
||||
} has not been moved yet it has been pending for ${differenceInMinutes(
|
||||
timeZoneFix(),
|
||||
lstQData[i].upd_date
|
||||
)} min(s)`
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
createLog("debug", "lst", "quality", "nothing to update");
|
||||
}
|
||||
//}, 5 * 60 * 1000); // every 5 min
|
||||
createLog(
|
||||
"info",
|
||||
"lst",
|
||||
"quality",
|
||||
`Pallet ${lstQData[i].runningNr} was updated`,
|
||||
);
|
||||
} else {
|
||||
createLog(
|
||||
"info",
|
||||
"lst",
|
||||
"quality",
|
||||
`Pallet ${
|
||||
lstQData[i].runningNr
|
||||
} has not been moved yet it has been pending for ${differenceInMinutes(
|
||||
new Date(Date.now()),
|
||||
lstQData[i].upd_date,
|
||||
)} min(s)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
await delay(150);
|
||||
} else {
|
||||
createLog("info", "lst", "quality", "nothing to update");
|
||||
}
|
||||
},
|
||||
5 * 60 * 1000,
|
||||
); // every 5 min
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user