diff --git a/Dockerfile.prod b/Dockerfile.prod index 8284799..135f2f4 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -5,4 +5,4 @@ COPY package*.json ./ RUN npm ci COPY . . EXPOSE ${PORT} -CMD [ "npm", "run", "start" ] \ No newline at end of file +CMD ["node", "--max-old-space-size=4096", "app.js"] \ No newline at end of file diff --git a/app.js b/app.js index d8ab26f..e0ca65d 100644 --- a/app.js +++ b/app.js @@ -59,6 +59,16 @@ app.use(bodyParser.urlencoded({ extended: true })); // res.redirect(`${adminJs.options.rootPath}/resources/KhokhaEntryModel`); // }); app.use(process.env.BASE_URL + '/new/admin', authRoutes); +app.get((process.env.BASE_URL ) + '/health', (req, res) => { + res.status(200).json({ + status: 'ok', + service: 'GateLog APIs', + uptime: process.uptime(), + timestamp: new Date().toISOString(), + version: '2.0.0' + }); +}); + app.use(process.env.BASE_URL, khokhaEntryRouter); app.locals.BASE_URL = process.env.BASE_URL; @@ -80,4 +90,4 @@ server.listen(process.env.PORT, async () => { console.log(e.message); } console.log(`Server running on port ${process.env.PORT}`); -}); \ No newline at end of file +}); diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 12dbb6d..986808f 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -12,7 +12,8 @@ services: - .env.prod networks: - one-stop-2021_default + restart: always networks: one-stop-2021_default: - external: true + external: true \ No newline at end of file diff --git a/helpers/onestopUserHelper.js b/helpers/onestopUserHelper.js index 0713c16..f91cfb5 100644 --- a/helpers/onestopUserHelper.js +++ b/helpers/onestopUserHelper.js @@ -29,6 +29,7 @@ export const getOnestopUser = async (authHeader, onestopSecurityKey) => { } else if (res.status === 418) { throw new UserBlockedError(res.data.message); } else { + console.log(`Unexpected error: ${res.status} - ${res.data.message}: ${e}`); throw new CustomError(res.data.message, res.status, 'Internal Server Error'); } } diff --git a/routers/khokhaEntryRouter.js b/routers/khokhaEntryRouter.js index c1192d3..9716d96 100644 --- a/routers/khokhaEntryRouter.js +++ b/routers/khokhaEntryRouter.js @@ -16,6 +16,14 @@ const escapeRegex = s => String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const GATELOG_SECRET_KEY = process.env.GATELOG_SECRET_KEY; const ONESTOP_LOOKUP_URL = process.env.ONESTOP_LOOKUP_URL; +const IST_OFFSET_MS = 330 * 60 * 1000; +const toISTISOString = (value) => { + if (!value) return ''; + const date = new Date(value); + if (isNaN(date)) return ''; + return new Date(date.getTime() + IST_OFFSET_MS).toISOString(); +}; + function hmacHex(rollNo) { return crypto.createHmac("sha256", GATELOG_SECRET_KEY) .update(String(rollNo), "utf8") @@ -198,10 +206,9 @@ khokhaEntryRouter.post("/entries/table", async (req, res) => { isClosed: !!d.isClosed, checkOutGate: d.checkOutGate || '', checkInGate: d.checkInGate || '', - checkOutTime: d.checkOutTime ? new Date(d.checkOutTime).toISOString() : '', - checkInTime: d.checkInTime ? new Date(d.checkInTime).toISOString() : '', - updatedAtISO: d.updatedAt ? new Date(d.updatedAt).toISOString() - : (d.createdAt ? new Date(d.createdAt).toISOString() : '') + checkOutTime: toISTISOString(d.checkOutTime), + checkInTime: toISTISOString(d.checkInTime), + updatedAtISO: toISTISOString(d.updatedAt) || toISTISOString(d.createdAt) })); res.json({ draw, recordsTotal, recordsFiltered, data }); @@ -448,12 +455,12 @@ khokhaEntryRouter.post("/entries/export", async (req, res) => { RoomNumber: d.roomNumber, Destination: d.destination, CheckOutGate: d.checkOutGate, - CheckOutTime: d.checkOutTime ? new Date(d.checkOutTime).toISOString() : '', + CheckOutTime: toISTISOString(d.checkOutTime), CheckInGate: d.checkInGate || '', - CheckInTime: d.checkInTime ? new Date(d.checkInTime).toISOString() : '', + CheckInTime: toISTISOString(d.checkInTime), Status: d.isClosed ? 'Closed' : 'Open', - CreatedAt: d.createdAt ? new Date(d.createdAt).toISOString() : '', - UpdatedAt: d.updatedAt ? new Date(d.updatedAt).toISOString() : '', + CreatedAt: toISTISOString(d.createdAt), + UpdatedAt: toISTISOString(d.updatedAt), _id: d._id?.toString() || '' })); @@ -517,12 +524,12 @@ khokhaEntryRouter.post("/entries/export", async (req, res) => { RoomNumber: d.roomNumber, Destination: d.destination, CheckOutGate: d.checkOutGate, - CheckOutTime: d.checkOutTime ? new Date(d.checkOutTime).toISOString() : '', + CheckOutTime: toISTISOString(d.checkOutTime), CheckInGate: d.checkInGate || '', - CheckInTime: d.checkInTime ? new Date(d.checkInTime).toISOString() : '', + CheckInTime: toISTISOString(d.checkInTime), Status: d.isClosed ? 'Closed' : 'Open', - CreatedAt: d.createdAt ? new Date(d.createdAt).toISOString() : '', - UpdatedAt: d.updatedAt ? new Date(d.updatedAt).toISOString() : '', + CreatedAt: toISTISOString(d.createdAt), + UpdatedAt: toISTISOString(d.updatedAt), _id: d._id?.toString() || '' })); @@ -590,7 +597,7 @@ khokhaEntryRouter.post("/entries/export/preview", async (req, res) => { const limit = Math.min(Math.max(parseInt(req.body.limit || "25", 10), 1), 200); const skip = (page - 1) * limit; - const [total, results] = await Promise.all([ + const [total, docs] = await Promise.all([ KhokhaEntryModel.countDocuments(q), KhokhaEntryModel.find(q) .sort({ [dateField]: -1 }) @@ -606,6 +613,13 @@ khokhaEntryRouter.post("/entries/export/preview", async (req, res) => { }), ]); + const results = docs.map(d => ({ + ...d, + checkOutTime: toISTISOString(d.checkOutTime), + checkInTime: toISTISOString(d.checkInTime), + createdAt: toISTISOString(d.createdAt) + })); + res.json({ total, page, limit, results }); } catch (err) { console.error("Preview error:", err);