diff --git a/striker-ui-api/src/lib/request_handlers/ups/createUps.ts b/striker-ui-api/src/lib/request_handlers/ups/createUps.ts new file mode 100644 index 00000000..04694727 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/ups/createUps.ts @@ -0,0 +1,86 @@ +import assert from 'assert'; +import { RequestHandler } from 'express'; + +import { REP_IPV4, REP_PEACEFUL_STRING, REP_UUID } from '../../consts'; + +import { timestamp, write } from '../../accessModule'; +import { sanitize } from '../../sanitize'; +import { stderr, uuid } from '../../shell'; + +export const createUps: RequestHandler< + { uuid?: string }, + UpsOverview, + { agent: string; ipAddress: string; name: string } +> = async (request, response) => { + const { + body: { agent: rAgent, ipAddress: rIpAddress, name: rName } = {}, + params: { uuid: rUuid }, + } = request; + + const agent = sanitize(rAgent, 'string'); + const ipAddress = sanitize(rIpAddress, 'string'); + const name = sanitize(rName, 'string'); + const upsUuid = sanitize(rUuid, 'string', { fallback: uuid() }); + + try { + assert( + REP_PEACEFUL_STRING.test(agent), + `Agent must be a peaceful string; got [${agent}]`, + ); + + assert( + REP_IPV4.test(ipAddress), + `IP address must be a valid IPv4; got [${ipAddress}]`, + ); + + assert( + REP_PEACEFUL_STRING.test(name), + `Name must be a peaceful string; got [${name}]`, + ); + + assert( + REP_UUID.test(upsUuid), + `UPS UUID must be a valid UUIDv4; got [${upsUuid}]`, + ); + } catch (error) { + stderr(`Assert value failed during create UPS; CAUSE: ${error}`); + + return response.status(400).send(); + } + + const modifiedDate = timestamp(); + + try { + const wcode = await write( + `INSERT INTO + upses ( + ups_uuid, + ups_name, + ups_agent, + ups_ip_address, + modified_date + ) VALUES ( + '${upsUuid}', + '${name}', + '${agent}', + '${ipAddress}', + '${modifiedDate}' + ) ON CONFLICT (ups_uuid) + DO UPDATE SET + ups_name = '${name}', + ups_agent = '${agent}', + ups_ip_address = '${ipAddress}', + modified_date = '${modifiedDate}';`, + ); + + assert(wcode === 0, `Write exited with code ${wcode}`); + } catch (error) { + stderr(`Failed to write UPS record; CAUSE: ${error}`); + + return response.status(500).send(); + } + + const scode = rUuid ? 201 : 200; + + return response.status(scode).send(); +}; diff --git a/striker-ui-api/src/lib/request_handlers/ups/index.ts b/striker-ui-api/src/lib/request_handlers/ups/index.ts index ad64d4e9..f5a6c8ea 100644 --- a/striker-ui-api/src/lib/request_handlers/ups/index.ts +++ b/striker-ui-api/src/lib/request_handlers/ups/index.ts @@ -1,3 +1,5 @@ +export * from './createUps'; export * from './deleteUps'; export * from './getUPS'; export * from './getUPSTemplate'; +export * from './updateUps'; diff --git a/striker-ui-api/src/lib/request_handlers/ups/updateUps.ts b/striker-ui-api/src/lib/request_handlers/ups/updateUps.ts new file mode 100644 index 00000000..10ada94d --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/ups/updateUps.ts @@ -0,0 +1,3 @@ +import { createUps } from './createUps'; + +export const updateUps = createUps; diff --git a/striker-ui-api/src/routes/ups.ts b/striker-ui-api/src/routes/ups.ts index f93ca5cd..a6b94272 100644 --- a/striker-ui-api/src/routes/ups.ts +++ b/striker-ui-api/src/routes/ups.ts @@ -1,12 +1,20 @@ import express from 'express'; -import { deleteUps, getUPS, getUPSTemplate } from '../lib/request_handlers/ups'; +import { + createUps, + deleteUps, + getUPS, + getUPSTemplate, + updateUps, +} from '../lib/request_handlers/ups'; const router = express.Router(); router .delete('/:uuid?', deleteUps) .get('/', getUPS) - .get('/template', getUPSTemplate); + .get('/template', getUPSTemplate) + .post('/', createUps) + .put('/:uuid', updateUps); export default router;