From ecc21cb93516074bf4d9e8e190b76e8800f603e0 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Mon, 19 Feb 2024 16:40:41 -0600 Subject: [PATCH] Normalize / and /{id} responses. --- src/deployments.ts | 27 ++++++++++++++++++++------- src/main.ts | 24 ++++++++++++++++-------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/deployments.ts b/src/deployments.ts index dd0b3f2..cc4ef66 100644 --- a/src/deployments.ts +++ b/src/deployments.ts @@ -104,17 +104,30 @@ export class RegHelper { return [...records]; } - async deploymentRequestStatus() { - const requests = await this.queryRecords({ - type: 'ApplicationDeploymentRequest', - }); - const deployments = await this.queryRecords({ - type: 'ApplicationDeploymentRecord', - }); + async deploymentRequestStatus(requestId?: string) { + const requests: any[] = []; + const deployments: any[] = []; const removalRequests = await this.queryRecords({ type: 'ApplicationDeploymentRemovalRequest', }); + if (requestId) { + const request = await this.getRecordById(requestId); + if (request) { + requests.push(request); + } + deployments.push(...await this.queryRecords({ + type: 'ApplicationDeploymentRecord', request: requestId + })); + } else { + requests.push(...await this.queryRecords({ + type: 'ApplicationDeploymentRequest', + })); + deployments.push(...await this.queryRecords({ + type: 'ApplicationDeploymentRecord', + })); + } + requests.sort((a, b) => a.createTime === b.createTime ? 0 : a.createTime > b.createTime ? 1 : -1,); requests.reverse(); diff --git a/src/main.ts b/src/main.ts index 17c5e6a..1133286 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ import express from 'express'; import {existsSync, readdirSync, readFileSync} from 'fs'; -import {Config, getRegistry} from './config.js'; +import {Config} from './config.js'; import {RegHelper} from './deployments.js'; @@ -26,9 +26,7 @@ const logForRequest = (req_id) => { return ret; }; -app.get('/', async (_req, res) => { - const reg = new RegHelper(); - const regStatus = await reg.deploymentRequestStatus(); +const adjustFromState = (regStatus: any[]) => { let deployerState = {}; if (Config.DEPLOYER_STATE && existsSync(Config.DEPLOYER_STATE)) { deployerState = JSON.parse(readFileSync(Config.DEPLOYER_STATE).toString()); @@ -53,19 +51,29 @@ app.get('/', async (_req, res) => { } } } + return regStatus; +} + +app.get('/', async (_req, res) => { + const reg = new RegHelper(); + const regStatus = adjustFromState(await reg.deploymentRequestStatus()); res.send(regStatus); }); app.get('/:id', async (req, res) => { - const registry = getRegistry(); - const records = await registry.getRecordsByIds([req.params.id]); - res.send(records); + const reg = new RegHelper(); + const records = adjustFromState(await reg.deploymentRequestStatus(req.params.id)); + if (records.length) { + res.send(records[0]); + } else { + res.sendStatus(410); + } }); app.get('/log/:id', async (req, res) => { const logFile = logForRequest(req.params.id); if (!logFile) { - res.sendStatus(404); + res.sendStatus(410); return; }