refactor(opendock): added some new goodies to the app to help manage releases
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m24s
All checks were successful
Build and Push LST Docker Image / docker (push) Successful in 1m24s
This commit is contained in:
@@ -16,6 +16,7 @@ export const opendockApt = pgTable(
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
release: integer("release").notNull().unique("opendock_apt_release_unique"),
|
||||
openDockAptId: text("open_dock_apt_id").notNull(),
|
||||
status: text("status").default("active"),
|
||||
appointment: jsonb("appointment").notNull().default([]),
|
||||
upd_date: timestamp("upd_date", { withTimezone: true })
|
||||
.notNull()
|
||||
|
||||
@@ -21,6 +21,7 @@ type Releases = {
|
||||
ReleaseNumber: number;
|
||||
DeliveryState: number;
|
||||
DeliveryDate: Date;
|
||||
ReleaseState: number;
|
||||
LineItemHumanReadableId: number;
|
||||
ArticleAlias: string;
|
||||
LoadingUnits: string;
|
||||
@@ -38,6 +39,8 @@ const actaulDocks = [
|
||||
{ name: "matrix", dockId: "3e32cbfc-49f4-4138-b491-9d5df9c94754" },
|
||||
{ name: "gerber", dockId: "9109e789-6c15-4cd9-87cb-de1b18627b6d" },
|
||||
{ name: "rb", dockId: "6be02526-6183-4789-a73f-e0aa155e6d1e" },
|
||||
//test server dock
|
||||
{ name: "second", dockId: "e87c92bd-13b4-4f7e-bf5e-b0182884c47a" },
|
||||
];
|
||||
const timeZone = process.env.TIMEZONE as string;
|
||||
const TWENTY_FOUR_HOURS = 24 * 60 * 60 * 1000;
|
||||
@@ -273,11 +276,65 @@ const postRelease = async (release: Releases) => {
|
||||
if (existing) {
|
||||
const id = existing.openDockAptId;
|
||||
|
||||
// deal with canceled stuff as we want this gone off od
|
||||
if (release.ReleaseState === 2 || release.ReleaseState === 4) {
|
||||
// delete the order in od and change the state to canceled in lst
|
||||
try {
|
||||
const response = await axios.delete(
|
||||
`${process.env.OPENDOCK_URL}/appointment/${id}`,
|
||||
{
|
||||
headers: {
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
Authorization: `Bearer ${odToken.odToken}`,
|
||||
},
|
||||
},
|
||||
// {
|
||||
// hardDelete: true,
|
||||
// },
|
||||
);
|
||||
|
||||
if (response.status === 400) {
|
||||
log.error({}, response.data.data.message);
|
||||
return;
|
||||
}
|
||||
|
||||
// update the release in the db leaving as insert just incase something weird happened
|
||||
try {
|
||||
await db
|
||||
.update(opendockApt)
|
||||
.set({
|
||||
status: "canceled",
|
||||
upd_date: sql`Now()`,
|
||||
})
|
||||
.where(eq(opendockApt.release, release.ReleaseNumber))
|
||||
.returning();
|
||||
|
||||
log.info({}, `${release.ReleaseNumber} was canceled`);
|
||||
} catch (e) {
|
||||
log.error(
|
||||
{ stack: e },
|
||||
`Error canceling the release: ${release.ReleaseNumber}`,
|
||||
);
|
||||
}
|
||||
// biome-ignore lint/suspicious/noExplicitAny: to many possibilities
|
||||
} catch (e: any) {
|
||||
//console.info(newDockApt);
|
||||
log.error(
|
||||
{ stack: e.response.data },
|
||||
`An error has occurred during canceling of the release: ${release.ReleaseNumber}`,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
(releaseLoadtypeCheck ||
|
||||
opendDockArticleCheck?.loadType === "drop" ||
|
||||
defaultDock?.value === "drop") &&
|
||||
(release.DeliveryState === 0 || release.DeliveryState === 2)
|
||||
release.DeliveryState === 2
|
||||
) {
|
||||
const setArrival = { ...newDockApt, status: "Arrived" };
|
||||
|
||||
@@ -392,7 +449,11 @@ const postRelease = async (release: Releases) => {
|
||||
return;
|
||||
}
|
||||
// changing to only trigger the change if the state is 2 meaning it has a scan to it and already in progress of being loaded.
|
||||
} else if (release.DeliveryState === 0 || release.DeliveryState === 2) {
|
||||
} else if (
|
||||
release.DeliveryState === 0 ||
|
||||
release.DeliveryState === 1 ||
|
||||
release.DeliveryState === 2
|
||||
) {
|
||||
try {
|
||||
const response = await axios.patch(
|
||||
`${process.env.OPENDOCK_URL}/appointment/${id}`,
|
||||
@@ -479,6 +540,7 @@ const postRelease = async (release: Releases) => {
|
||||
set: {
|
||||
openDockAptId: response.data.data.id,
|
||||
appointment: response.data.data,
|
||||
status: "completed",
|
||||
upd_date: sql`NOW()`,
|
||||
},
|
||||
})
|
||||
|
||||
73
backend/opendock/openDockUndoLastStatus.ts
Normal file
73
backend/opendock/openDockUndoLastStatus.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import axios from "axios";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { Router } from "express";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { opendockApt } from "../db/schema/opendock_apt.schema.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
import { odToken } from "./opendock.utils.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.patch("/:id", async (req, res) => {
|
||||
//const limit
|
||||
const { id } = req.params;
|
||||
|
||||
try {
|
||||
const response = await axios.patch(
|
||||
`${process.env.OPENDOCK_URL}/appointment/${id}/undo-latest-status`,
|
||||
{
|
||||
headers: {
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
Authorization: `Bearer ${odToken.odToken}`,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (response.status === 400) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: response.data.data.message,
|
||||
data: [],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
// update the release in the db
|
||||
const { data } = await tryCatch(
|
||||
db
|
||||
.update(opendockApt)
|
||||
.set({
|
||||
appointment: response.data.data,
|
||||
upd_date: sql`NOW()`,
|
||||
})
|
||||
.where(eq(opendockApt.openDockAptId, id)),
|
||||
);
|
||||
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: `The release was reverted to the last state.`,
|
||||
data: data as any,
|
||||
status: 200,
|
||||
});
|
||||
// biome-ignore lint/suspicious/noExplicitAny: to many possibilities
|
||||
} catch (e: any) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: `An error updating the release in opendock`,
|
||||
data: e.response.data,
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default r;
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { Express } from "express";
|
||||
import { requireAuth } from "../middleware/auth.middleware.js";
|
||||
import { featureCheck } from "../middleware/featureActive.middleware.js";
|
||||
import undo from "./openDockUndoLastStatus.js";
|
||||
import articleCheck from "./opendock.articleCheck.route.js";
|
||||
|
||||
import getApt from "./opendockGetRelease.route.js";
|
||||
import getApt from "./opendockRelease.route.js";
|
||||
|
||||
export const setupOpendockRoutes = (baseUrl: string, app: Express) => {
|
||||
//setup all the routes
|
||||
@@ -21,4 +21,11 @@ export const setupOpendockRoutes = (baseUrl: string, app: Express) => {
|
||||
requireAuth,
|
||||
articleCheck,
|
||||
);
|
||||
|
||||
app.use(
|
||||
`${baseUrl}/api/opendock/undo-latest-status`,
|
||||
featureCheck("opendock_sync"),
|
||||
requireAuth,
|
||||
undo,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import { desc, gte, sql } from "drizzle-orm";
|
||||
import { Router } from "express";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { opendockApt } from "../db/schema/opendock_apt.schema.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.get("/", async (_, res) => {
|
||||
//const limit
|
||||
|
||||
const daysCreated = 30;
|
||||
|
||||
const { data } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(opendockApt)
|
||||
.where(
|
||||
gte(
|
||||
opendockApt.createdAt,
|
||||
sql.raw(`NOW() - INTERVAL '${daysCreated} days'`),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(opendockApt.createdAt))
|
||||
.limit(500),
|
||||
);
|
||||
|
||||
apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: `The first ${data?.length} Apt(s) that were created in the last ${daysCreated} `,
|
||||
data: data ?? [],
|
||||
status: 200,
|
||||
});
|
||||
});
|
||||
|
||||
export default r;
|
||||
124
backend/opendock/opendockRelease.route.ts
Normal file
124
backend/opendock/opendockRelease.route.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import axios from "axios";
|
||||
import { and, desc, eq, gte, sql } from "drizzle-orm";
|
||||
import { Router } from "express";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { opendockApt } from "../db/schema/opendock_apt.schema.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
import { odToken } from "./opendock.utils.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.get("/", async (req, res) => {
|
||||
//const limit
|
||||
|
||||
const daysCreated = req.query.daysCreated ?? 30;
|
||||
|
||||
const { data } = await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(opendockApt)
|
||||
.where(
|
||||
and(
|
||||
gte(
|
||||
opendockApt.upd_date,
|
||||
sql.raw(`NOW() - INTERVAL '${daysCreated} days'`),
|
||||
),
|
||||
eq(opendockApt.status, "active"),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(opendockApt.upd_date))
|
||||
.limit(500),
|
||||
);
|
||||
|
||||
apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: `The first ${data?.length} Apt(s) that were created in the last ${daysCreated} `,
|
||||
data: data ?? [],
|
||||
status: 200,
|
||||
});
|
||||
});
|
||||
|
||||
r.delete("/:id", async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
const { data: releaseInfo } = (await tryCatch(
|
||||
db
|
||||
.select()
|
||||
.from(opendockApt)
|
||||
.where(eq(opendockApt.id, id as string)),
|
||||
)) as any;
|
||||
|
||||
if (releaseInfo.length === 0) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: "Invalid release id passed over please try again",
|
||||
data: [],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.delete(
|
||||
`${process.env.OPENDOCK_URL}/appointment/${releaseInfo[0].appointment.id}`,
|
||||
{
|
||||
headers: {
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
Authorization: `Bearer ${odToken.odToken}`,
|
||||
},
|
||||
},
|
||||
// {
|
||||
// hardDelete: true,
|
||||
// },
|
||||
);
|
||||
|
||||
if (response.status === 400) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: response.data.data.message,
|
||||
data: [],
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
// update the release in the db leaving as insert just in-case something weird happened
|
||||
const { data } = await tryCatch(
|
||||
db
|
||||
.delete(opendockApt)
|
||||
.where(eq(opendockApt.id, id as string))
|
||||
.returning(),
|
||||
);
|
||||
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: `The release was deleted, this is un unrecoverable`,
|
||||
data: data as any,
|
||||
status: 200,
|
||||
});
|
||||
// biome-ignore lint/suspicious/noExplicitAny: to many possibilities
|
||||
} catch (e: any) {
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "opendock",
|
||||
subModule: "apt",
|
||||
message: `An error deleting the release in opendock`,
|
||||
data: e.response.data,
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default r;
|
||||
Reference in New Issue
Block a user