parent
63dafe0e6a
commit
830e07d30c
4 changed files with 101 additions and 2 deletions
@ -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(); |
||||
}; |
@ -1,3 +1,5 @@ |
||||
export * from './createUps'; |
||||
export * from './deleteUps'; |
||||
export * from './getUPS'; |
||||
export * from './getUPSTemplate'; |
||||
export * from './updateUps'; |
||||
|
@ -0,0 +1,3 @@ |
||||
import { createUps } from './createUps'; |
||||
|
||||
export const updateUps = createUps; |
@ -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; |
||||
|
Loading…
Reference in new issue