feat(forklifts): added the crud
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
meta {
|
||||||
|
name: Get forklift
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url: {{url}}/lst/api/forklifts
|
||||||
|
body: none
|
||||||
|
auth: inherit
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"name":"Delage DLL"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
settings {
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
meta {
|
||||||
|
name: Update forklfit
|
||||||
|
type: http
|
||||||
|
seq: 3
|
||||||
|
}
|
||||||
|
|
||||||
|
patch {
|
||||||
|
url: {{url}}/lst/api/forklifts/:id
|
||||||
|
body: json
|
||||||
|
auth: inherit
|
||||||
|
}
|
||||||
|
|
||||||
|
params:path {
|
||||||
|
id: ec2f3759-1580-4c1b-8fbf-8a4b0b506758
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
|
||||||
|
"glCode": 31
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
settings {
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
meta {
|
||||||
|
name: add forklift
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
url: {{url}}/lst/api/forklifts
|
||||||
|
body: json
|
||||||
|
auth: inherit
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"serialNumber":"FN682004",
|
||||||
|
"model": "EFG220",
|
||||||
|
"plant": "Iowa City ISBM",
|
||||||
|
"glCode": 31,
|
||||||
|
"profitCenter": 30,
|
||||||
|
"manufacturer":"Jungheinrich",
|
||||||
|
"manufacturerYear":"2022",
|
||||||
|
"engine":"electric",
|
||||||
|
"batteryType":"lead acid",
|
||||||
|
"leaseId":"0147d082-aee0-4594-b0f4-c6f4ee777e92"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
settings {
|
||||||
|
encodeUrl: true
|
||||||
|
timeout: 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
meta {
|
||||||
|
name: forklifts
|
||||||
|
seq: 3
|
||||||
|
}
|
||||||
|
|
||||||
|
auth {
|
||||||
|
mode: inherit
|
||||||
|
}
|
||||||
117
app/src/internal/forklifts/routes/forklifts/addForklift.ts
Normal file
117
app/src/internal/forklifts/routes/forklifts/addForklift.ts
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
import { type DrizzleError, sql } from "drizzle-orm";
|
||||||
|
|
||||||
|
import type { Request, Response } from "express";
|
||||||
|
import { Router } from "express";
|
||||||
|
import https from "https";
|
||||||
|
import { db } from "../../../../pkg/db/db.js";
|
||||||
|
|
||||||
|
import {
|
||||||
|
forklifts,
|
||||||
|
newForkliftsSchema,
|
||||||
|
} from "../../../../pkg/db/schema/forklifts.js";
|
||||||
|
import { createLogger } from "../../../../pkg/logger/logger.js";
|
||||||
|
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.post("/", async (req: Request, res: Response) => {
|
||||||
|
// when a new server is posted from localhost or 127.0.0.1 we also want to post it to the test server so we can see it from there
|
||||||
|
//res.status(200).json({ message: "Server added", ip: req.hostname });
|
||||||
|
const log = createLogger({ module: "forklift", subModule: "add forklift" });
|
||||||
|
const parsed = newForkliftsSchema.safeParse(req.body);
|
||||||
|
|
||||||
|
if (!parsed.success) {
|
||||||
|
return res.status(400).json({ errors: parsed.error.flatten() });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data, error } = await tryCatch(
|
||||||
|
db
|
||||||
|
.insert(forklifts)
|
||||||
|
.values({
|
||||||
|
...parsed.data,
|
||||||
|
add_user: req.user?.username,
|
||||||
|
add_date: sql`NOW()`,
|
||||||
|
upd_user: req.user?.username,
|
||||||
|
upd_date: sql`NOW()`,
|
||||||
|
})
|
||||||
|
.onConflictDoUpdate({
|
||||||
|
target: forklifts.serialNumber,
|
||||||
|
set: {
|
||||||
|
...parsed.data,
|
||||||
|
add_user: req.user?.username,
|
||||||
|
add_date: sql`NOW()`,
|
||||||
|
upd_user: req.user?.username,
|
||||||
|
upd_date: sql`NOW()`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.returning({
|
||||||
|
serialNumber: forklifts.serialNumber,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.log(error);
|
||||||
|
const err: DrizzleError = error;
|
||||||
|
return res.status(400).json({
|
||||||
|
message: `Error adding forklift`,
|
||||||
|
error: err.cause,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (req.hostname === "localhost" && process.env.MAIN_SERVER) {
|
||||||
|
// log.info({}, "Running in dev server about to add in a new server");
|
||||||
|
// const axiosInstance = axios.create({
|
||||||
|
// httpsAgent: new https.Agent({ rejectUnauthorized: false }),
|
||||||
|
// baseURL: process.env.MAIN_SERVER, // e.g. "https://example.com"
|
||||||
|
// withCredentials: true,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const loginRes = (await axiosInstance.post(
|
||||||
|
// `${process.env.MAIN_SERVER}/lst/api/auth/sign-in/username`,
|
||||||
|
// {
|
||||||
|
// username: process.env.MAIN_SERVER_USERNAME,
|
||||||
|
// password: process.env.MAIN_SERVER_PASSWORD,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// headers: { "Content-Type": "application/json" },
|
||||||
|
// },
|
||||||
|
// )) as any;
|
||||||
|
// const setCookie = loginRes.headers["set-cookie"][0];
|
||||||
|
|
||||||
|
// if (!setCookie) {
|
||||||
|
// throw new Error("Did not receive a Set-Cookie header from login");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const { data, error } = await tryCatch(
|
||||||
|
// axios.post(
|
||||||
|
// `${process.env.MAIN_SERVER}/lst/api/forklifts/companies`,
|
||||||
|
// parsed.data,
|
||||||
|
// {
|
||||||
|
// headers: {
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// Cookie: setCookie.split(";")[0],
|
||||||
|
// },
|
||||||
|
// withCredentials: true,
|
||||||
|
// },
|
||||||
|
// ),
|
||||||
|
// );
|
||||||
|
|
||||||
|
// if (error) {
|
||||||
|
// log.error(
|
||||||
|
// { stack: error },
|
||||||
|
// "There was an error adding the company to Main Server",
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// log.info(
|
||||||
|
// { stack: data?.data },
|
||||||
|
// "A new Company was just added to the server.",
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
|
||||||
|
return res
|
||||||
|
.status(201)
|
||||||
|
.json({ message: `Forklift ${data[0]?.serialNumber} added`, data: data });
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
import { requireAuth } from "../../../../pkg/middleware/authMiddleware.js";
|
||||||
|
|
||||||
|
import addForklift from "./addForklift.js";
|
||||||
|
import getForklifts from "./getForklifts.js";
|
||||||
|
import updateForklift from "./updateForklift.js";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.use(
|
||||||
|
"/",
|
||||||
|
requireAuth("forklifts", ["systemAdmin", "admin", "manager", "supervisor"]),
|
||||||
|
getForklifts,
|
||||||
|
);
|
||||||
|
router.use(
|
||||||
|
"/",
|
||||||
|
requireAuth("forklifts", ["systemAdmin", "admin"]),
|
||||||
|
addForklift,
|
||||||
|
);
|
||||||
|
router.use(
|
||||||
|
"/",
|
||||||
|
requireAuth("forklifts", ["systemAdmin", "admin"]),
|
||||||
|
updateForklift,
|
||||||
|
);
|
||||||
|
|
||||||
|
export default router;
|
||||||
38
app/src/internal/forklifts/routes/forklifts/getForklifts.ts
Normal file
38
app/src/internal/forklifts/routes/forklifts/getForklifts.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import { and, asc, eq } from "drizzle-orm";
|
||||||
|
import type { Request, Response } from "express";
|
||||||
|
import { Router } from "express";
|
||||||
|
import { db } from "../../../../pkg/db/db.js";
|
||||||
|
import { forkliftCompanies } from "../../../../pkg/db/schema/forkliftLeaseCompanys.js";
|
||||||
|
import { leases } from "../../../../pkg/db/schema/forkliftLeases.js";
|
||||||
|
import { forklifts } from "../../../../pkg/db/schema/forklifts.js";
|
||||||
|
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
|
const plant = req.query.plant;
|
||||||
|
|
||||||
|
const conditions = [];
|
||||||
|
|
||||||
|
if (plant !== undefined) {
|
||||||
|
conditions.push(eq(leases.leaseNumber, `${plant}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
//conditions.push(eq(forkliftCompanies.active, true));
|
||||||
|
|
||||||
|
const { data, error } = await tryCatch(
|
||||||
|
db
|
||||||
|
.select()
|
||||||
|
.from(forklifts)
|
||||||
|
//.innerJoin(forkliftCompanies, eq(forkliftCompanies.id, leases.companyId))
|
||||||
|
.where(and(...conditions))
|
||||||
|
.orderBy(asc(forklifts.serialNumber)),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return res.status(400).json({ error: error });
|
||||||
|
}
|
||||||
|
res.status(200).json({ message: "Current Forklifts", data: data });
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
136
app/src/internal/forklifts/routes/forklifts/updateForklift.ts
Normal file
136
app/src/internal/forklifts/routes/forklifts/updateForklift.ts
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
import { eq, sql } from "drizzle-orm";
|
||||||
|
import type { Request, Response } from "express";
|
||||||
|
import { Router } from "express";
|
||||||
|
import https from "https";
|
||||||
|
import { db } from "../../../../pkg/db/db.js";
|
||||||
|
import { forkliftCompanies } from "../../../../pkg/db/schema/forkliftLeaseCompanys.js";
|
||||||
|
import { leases } from "../../../../pkg/db/schema/forkliftLeases.js";
|
||||||
|
import { forklifts } from "../../../../pkg/db/schema/forklifts.js";
|
||||||
|
import { serverData } from "../../../../pkg/db/schema/servers.js";
|
||||||
|
import { createLogger } from "../../../../pkg/logger/logger.js";
|
||||||
|
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.patch("/:id", async (req: Request, res: Response) => {
|
||||||
|
const log = createLogger({
|
||||||
|
module: "forklifts",
|
||||||
|
subModule: "update leases",
|
||||||
|
});
|
||||||
|
|
||||||
|
// when a server is updated and is posted from localhost or 127.0.0.1 we also want to post it to the test server so we can see it from there, we want to insert with update on conflict.
|
||||||
|
const id = req.params.id;
|
||||||
|
const updates: Record<string, any> = {};
|
||||||
|
if (req.body?.model !== undefined) {
|
||||||
|
updates.model = req.body.model;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.plant !== undefined) {
|
||||||
|
updates.plant = req.body.plant;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.glCode !== undefined) {
|
||||||
|
updates.glCode = req.body.glCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.profitCenter !== undefined) {
|
||||||
|
updates.profitCenter = req.body.profitCenter;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.manufacturer !== undefined) {
|
||||||
|
updates.manufacturer = req.body.manufacturer;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.manufacturerYear !== undefined) {
|
||||||
|
updates.manufacturerYear = req.body.manufacturerYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.engine !== undefined) {
|
||||||
|
updates.engine = req.body.engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.batteryType !== undefined) {
|
||||||
|
updates.batteryType = req.body.batteryType;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.dataPlate !== undefined) {
|
||||||
|
updates.dataPlate = req.body.dataPlate;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.body?.forkliftNumber !== undefined) {
|
||||||
|
updates.forkliftNumber = req.body.forkliftNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
updates.upd_user = req.user!.username || "lst_user";
|
||||||
|
updates.upd_date = sql`NOW()`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (Object.keys(updates).length > 0) {
|
||||||
|
await db
|
||||||
|
.update(forklifts)
|
||||||
|
.set(updates)
|
||||||
|
.where(eq(forklifts.forklift_id, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (req.hostname === "localhost" && process.env.MAIN_SERVER) {
|
||||||
|
// log.info({}, "Running in dev server about to add in a new server");
|
||||||
|
// const axiosInstance = axios.create({
|
||||||
|
// httpsAgent: new https.Agent({ rejectUnauthorized: false }),
|
||||||
|
// baseURL: process.env.MAIN_SERVER,
|
||||||
|
// withCredentials: true,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const loginRes = (await axiosInstance.post(
|
||||||
|
// `${process.env.MAIN_SERVER}/lst/api/auth/sign-in/username`,
|
||||||
|
// {
|
||||||
|
// username: process.env.MAIN_SERVER_USERNAME,
|
||||||
|
// password: process.env.MAIN_SERVER_PASSWORD,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// headers: { "Content-Type": "application/json" },
|
||||||
|
// },
|
||||||
|
// )) as any;
|
||||||
|
|
||||||
|
// const setCookie = loginRes?.headers["set-cookie"][0];
|
||||||
|
|
||||||
|
// //console.log(setCookie.split(";")[0].replace("__Secure-", ""));
|
||||||
|
|
||||||
|
// if (!setCookie) {
|
||||||
|
// throw new Error("Did not receive a Set-Cookie header from login");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const { data, error } = await tryCatch(
|
||||||
|
// axios.patch(
|
||||||
|
// `${process.env.MAIN_SERVER}/lst/api/forklifts/companies/${id}`,
|
||||||
|
// updates,
|
||||||
|
// {
|
||||||
|
// headers: {
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// Cookie: setCookie.split(";")[0],
|
||||||
|
// },
|
||||||
|
// withCredentials: true,
|
||||||
|
// },
|
||||||
|
// ),
|
||||||
|
// );
|
||||||
|
|
||||||
|
// if (error) {
|
||||||
|
// //console.log(error);
|
||||||
|
// log.error(
|
||||||
|
// { stack: error },
|
||||||
|
// "There was an error updating the lease to Main Server",
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// log.info(
|
||||||
|
// { stack: data?.data },
|
||||||
|
// "A new lease was just updated to the server.",
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
res.status(200).json({ message: `${id} was just updated` });
|
||||||
|
} catch (error) {
|
||||||
|
//console.log(error);
|
||||||
|
res.status(200).json({ message: "Error updating lease", error });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
@@ -1,8 +1,14 @@
|
|||||||
import type { Express, Request, Response } from "express";
|
import type { Express, Request, Response } from "express";
|
||||||
import { requireAuth } from "../../../pkg/middleware/authMiddleware.js";
|
import { requireAuth } from "../../../pkg/middleware/authMiddleware.js";
|
||||||
import companies from "./companies/companiesRoutes.js";
|
import companies from "./companies/companiesRoutes.js";
|
||||||
|
import forklifts from "./forklifts/forkliftRoutes.js";
|
||||||
|
import invoices from "./invoices/invoiceRoutes.js";
|
||||||
import leases from "./leases/leaseRoutes.js";
|
import leases from "./leases/leaseRoutes.js";
|
||||||
export const setupForkliftRoutes = (app: Express, basePath: string) => {
|
export const setupForkliftRoutes = (app: Express, basePath: string) => {
|
||||||
|
app.use(
|
||||||
|
basePath + "/api/forklifts", // will pass bc system admin but this is just telling us we need this
|
||||||
|
forklifts,
|
||||||
|
);
|
||||||
app.use(
|
app.use(
|
||||||
basePath + "/api/forklifts/companies", // will pass bc system admin but this is just telling us we need this
|
basePath + "/api/forklifts/companies", // will pass bc system admin but this is just telling us we need this
|
||||||
companies,
|
companies,
|
||||||
@@ -11,4 +17,8 @@ export const setupForkliftRoutes = (app: Express, basePath: string) => {
|
|||||||
basePath + "/api/forklifts/leases", // will pass bc system admin but this is just telling us we need this
|
basePath + "/api/forklifts/leases", // will pass bc system admin but this is just telling us we need this
|
||||||
leases,
|
leases,
|
||||||
);
|
);
|
||||||
|
app.use(
|
||||||
|
basePath + "/api/forklifts/invoices", // will pass bc system admin but this is just telling us we need this
|
||||||
|
invoices,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user