From 975f9a18746490c7644cbe8224af181622e2616c Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Fri, 19 Apr 2024 18:36:37 +0530 Subject: [PATCH] add button to refresh assets --- src/controller/AssetConfigController.ts | 12 +++++++++--- src/{utils => cron}/retrieveAssetConfig.ts | 1 + src/index.ts | 2 +- src/router/assetConfigRouter.ts | 1 + src/views/pages/assets/form.ejs | 11 ++++++++++- src/views/pages/assets/list.ejs | 10 ++++++++-- 6 files changed, 30 insertions(+), 7 deletions(-) rename src/{utils => cron}/retrieveAssetConfig.ts (98%) diff --git a/src/controller/AssetConfigController.ts b/src/controller/AssetConfigController.ts index f427441..bdd2899 100644 --- a/src/controller/AssetConfigController.ts +++ b/src/controller/AssetConfigController.ts @@ -2,14 +2,12 @@ import dayjs from "dayjs"; import { Request, Response } from "express"; import prisma from "@/lib/prisma"; +import { retrieveAssetConfig } from "@/cron/retrieveAssetConfig"; export class AssetConfigController { static listAssets = async (req: Request, res: Response) => { try { const assets = await prisma.asset.findMany({ - where: { - deleted: false, - }, orderBy: [{ updatedAt: "desc" }], }); @@ -31,6 +29,7 @@ export class AssetConfigController { res.render("pages/assets/list", { req, dayjs, + csrfToken: res.locals.csrfToken, assets: [], beds: [], errors: [err.message], @@ -38,6 +37,11 @@ export class AssetConfigController { } }; + static refreshAssets = async (req: Request, res: Response) => { + await retrieveAssetConfig(); + res.redirect("/assets"); + } + static createAssetForm = async (req: Request, res: Response) => { res.render("pages/assets/form", { req, @@ -143,6 +147,7 @@ export class AssetConfigController { username, password, port, + deleted, } = req.body; const asset = await prisma.asset.findFirst({ @@ -175,6 +180,7 @@ export class AssetConfigController { updatedAt: new Date(), username, password, + deleted: deleted === "true", port: Number(port), }, }); diff --git a/src/utils/retrieveAssetConfig.ts b/src/cron/retrieveAssetConfig.ts similarity index 98% rename from src/utils/retrieveAssetConfig.ts rename to src/cron/retrieveAssetConfig.ts index d714e30..f0ecf3c 100644 --- a/src/utils/retrieveAssetConfig.ts +++ b/src/cron/retrieveAssetConfig.ts @@ -11,6 +11,7 @@ async function insertAsset(asset: AssetConfig) { externalId: asset.id, }, update: { + name: asset.name, description: asset.description, ipAddress: asset.ip_address, port: asset.port, diff --git a/src/index.ts b/src/index.ts index 68b0713..eb42161 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ import * as cron from "node-cron"; import { automatedDailyRounds } from "@/cron/automatedDailyRounds"; +import { retrieveAssetConfig } from "@/cron/retrieveAssetConfig"; import { initServer } from "@/server"; import { port } from "@/utils/configs"; -import { retrieveAssetConfig } from "@/utils/retrieveAssetConfig"; process.env.AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE = "1"; process.env.CHECKPOINT_DISABLE = "1"; diff --git a/src/router/assetConfigRouter.ts b/src/router/assetConfigRouter.ts index eb8ecc6..0cfae0b 100644 --- a/src/router/assetConfigRouter.ts +++ b/src/router/assetConfigRouter.ts @@ -12,6 +12,7 @@ router.use(csrfProtection()); router.get("/", AssetConfigController.listAssets); router.get("/new", AssetConfigController.createAssetForm); router.post("/", AssetConfigController.createAsset); +router.post("/refresh", AssetConfigController.refreshAssets); router.get("/:externalId", AssetConfigController.updateAssetForm); router.post("/:externalId", AssetConfigController.updateAsset); router.get("/:externalId/delete", AssetConfigController.confirmDeleteAsset); diff --git a/src/views/pages/assets/form.ejs b/src/views/pages/assets/form.ejs index 67fc9ec..28b9016 100644 --- a/src/views/pages/assets/form.ejs +++ b/src/views/pages/assets/form.ejs @@ -76,8 +76,17 @@ + <% if(asset?.externalId) {%> +
+ + +
+ <% } %>
- +
diff --git a/src/views/pages/assets/list.ejs b/src/views/pages/assets/list.ejs index fc11a8d..e51a850 100644 --- a/src/views/pages/assets/list.ejs +++ b/src/views/pages/assets/list.ejs @@ -3,7 +3,7 @@ <%- include('../../partials/head.ejs', { title: 'Teleicu Middleware | Asset Config'}) %> - + <%- include("../../partials/header") %>
<%- include("../../partials/pageTitle",{title:"Asset Config"}) %> @@ -15,7 +15,11 @@ <% }) %> <% } %> -
+
+
+ + +
Add New Asset
@@ -27,6 +31,7 @@ IP address Updated At + Deleted Actions <% for (var i = 0; i < assets.length; i++) { %> @@ -38,6 +43,7 @@ <%= assets[i].ipAddress %> <%= dayjs(assets[i].updatedAt).format('DD/MM/YYYY hh:mm:ss a') %> + <%= assets[i].deleted ? 'Yes' : 'No' %> Edit Delete