parent
22fa465f1b
commit
6288a52d99
8 changed files with 256 additions and 0 deletions
@ -0,0 +1,35 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { execManageAlerts } from '../../execManageAlerts'; |
||||
import { getMailRecipientRequestBody } from './getMailRecipientRequestBody'; |
||||
import { stderr, stdout } from '../../shell'; |
||||
|
||||
export const createMailRecipient: RequestHandler< |
||||
undefined, |
||||
undefined, |
||||
MailRecipientRequestBody |
||||
> = (request, response) => { |
||||
const { body: rBody = {} } = request; |
||||
|
||||
stdout('Begin creating alert recipient.'); |
||||
|
||||
let body: MailRecipientRequestBody; |
||||
|
||||
try { |
||||
body = getMailRecipientRequestBody(rBody); |
||||
} catch (error) { |
||||
stderr(`Failed to process alert recipient input; CAUSE: ${error}`); |
||||
|
||||
return response.status(400).send(); |
||||
} |
||||
|
||||
try { |
||||
execManageAlerts('recipients', 'add', { body }); |
||||
} catch (error) { |
||||
stderr(`Failed to create alert recipient; CAUSE: ${error}`); |
||||
|
||||
return response.status(500).send(); |
||||
} |
||||
|
||||
return response.status(201).send(); |
||||
}; |
@ -0,0 +1,22 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { execManageAlerts } from '../../execManageAlerts'; |
||||
import { stderr } from '../../shell'; |
||||
|
||||
export const deleteMailRecipient: RequestHandler< |
||||
MailRecipientParamsDictionary |
||||
> = (request, response) => { |
||||
const { |
||||
params: { uuid }, |
||||
} = request; |
||||
|
||||
try { |
||||
execManageAlerts('recipients', 'delete', { uuid }); |
||||
} catch (error) { |
||||
stderr(`Failed to delete alert recipient; CAUSE: ${error}`); |
||||
|
||||
return response.status(500).send(); |
||||
} |
||||
|
||||
return response.status(204).send(); |
||||
}; |
@ -0,0 +1,34 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { DELETED } from '../../consts'; |
||||
|
||||
import buildGetRequestHandler from '../buildGetRequestHandler'; |
||||
import { buildQueryResultReducer } from '../../buildQueryResultModifier'; |
||||
|
||||
export const getMailRecipient: RequestHandler = buildGetRequestHandler( |
||||
(request, options) => { |
||||
const query = ` |
||||
SELECT |
||||
a.recipient_uuid, |
||||
a.recipient_name |
||||
FROM recipients AS a |
||||
WHERE a.recipient_name != '${DELETED}' |
||||
ORDER BY a.recipient_name ASC;`;
|
||||
|
||||
const afterQueryReturn: QueryResultModifierFunction = |
||||
buildQueryResultReducer<MailRecipientOverviewList>( |
||||
(previous, [uuid, name]) => { |
||||
previous[uuid] = { name, uuid }; |
||||
|
||||
return previous; |
||||
}, |
||||
{}, |
||||
); |
||||
|
||||
if (options) { |
||||
options.afterQueryReturn = afterQueryReturn; |
||||
} |
||||
|
||||
return query; |
||||
}, |
||||
); |
@ -0,0 +1,53 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { DELETED } from '../../consts'; |
||||
|
||||
import buildGetRequestHandler from '../buildGetRequestHandler'; |
||||
import { buildQueryResultModifier } from '../../buildQueryResultModifier'; |
||||
import { sanitize } from '../../sanitize'; |
||||
|
||||
export const getMailRecipientDetail: RequestHandler<MailRecipientParamsDictionary> = |
||||
buildGetRequestHandler((request, options) => { |
||||
const { |
||||
params: { uuid: rUuid }, |
||||
} = request; |
||||
|
||||
const uuid = sanitize(rUuid, 'string', { modifierType: 'sql' }); |
||||
|
||||
const query = ` |
||||
SELECT |
||||
a.recipient_uuid, |
||||
a.recipient_name, |
||||
a.recipient_email, |
||||
a.recipient_language, |
||||
a.recipient_level |
||||
FROM recipients AS a |
||||
WHERE a.recipient_name != '${DELETED}' |
||||
AND a.recipient_uuid = '${uuid}' |
||||
ORDER BY a.recipient_name ASC;`;
|
||||
|
||||
const afterQueryReturn: QueryResultModifierFunction = |
||||
buildQueryResultModifier<MailRecipientDetail | undefined>((rows) => { |
||||
if (!rows.length) { |
||||
return undefined; |
||||
} |
||||
|
||||
const { |
||||
0: [uuid, name, email, language, level], |
||||
} = rows; |
||||
|
||||
return { |
||||
email, |
||||
language, |
||||
level: Number(level), |
||||
name, |
||||
uuid, |
||||
}; |
||||
}); |
||||
|
||||
if (options) { |
||||
options.afterQueryReturn = afterQueryReturn; |
||||
} |
||||
|
||||
return query; |
||||
}); |
@ -0,0 +1,49 @@ |
||||
import assert from 'assert'; |
||||
|
||||
import { REP_UUID } from '../../consts'; |
||||
|
||||
import { sanitize } from '../../sanitize'; |
||||
|
||||
export const getMailRecipientRequestBody = ( |
||||
body: Partial<MailRecipientRequestBody>, |
||||
uuid?: string, |
||||
): MailRecipientRequestBody => { |
||||
const { |
||||
email: rEmail, |
||||
language: rLanguage, |
||||
level: rLevel, |
||||
name: rName, |
||||
} = body; |
||||
|
||||
const email = sanitize(rEmail, 'string'); |
||||
const language = sanitize(rLanguage, 'string', { fallback: 'en_CA' }); |
||||
const level = sanitize(rLevel, 'number'); |
||||
const name = sanitize(rName, 'string'); |
||||
|
||||
if (uuid) { |
||||
assert(REP_UUID.test(uuid), `Expected valid UUIDv4; got [${uuid}]`); |
||||
} |
||||
|
||||
assert.ok(email.length, `Expected email; got [${email}]`); |
||||
|
||||
if (language) { |
||||
assert.ok( |
||||
language.length, |
||||
`Expected valid language code; got [${language}]`, |
||||
); |
||||
} |
||||
|
||||
assert( |
||||
Number.isSafeInteger(level), |
||||
`Expected level to be an integer; got [${level}]`, |
||||
); |
||||
|
||||
assert.ok(name.length, `Expected name; got [${name}]`); |
||||
|
||||
return { |
||||
email, |
||||
language, |
||||
level, |
||||
name, |
||||
}; |
||||
}; |
@ -0,0 +1,5 @@ |
||||
export * from './createMailRecipient'; |
||||
export * from './deleteMailRecipient'; |
||||
export * from './getMailRecipient'; |
||||
export * from './getMailRecipientDetail'; |
||||
export * from './updateMailRecipient'; |
@ -0,0 +1,38 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { execManageAlerts } from '../../execManageAlerts'; |
||||
import { getMailRecipientRequestBody } from './getMailRecipientRequestBody'; |
||||
import { stderr, stdout } from '../../shell'; |
||||
|
||||
export const updateMailRecipient: RequestHandler< |
||||
MailRecipientParamsDictionary, |
||||
undefined, |
||||
MailRecipientRequestBody |
||||
> = (request, response) => { |
||||
const { |
||||
body: rBody = {}, |
||||
params: { uuid }, |
||||
} = request; |
||||
|
||||
stdout('Begin updating alert recipient.'); |
||||
|
||||
let body: MailRecipientRequestBody; |
||||
|
||||
try { |
||||
body = getMailRecipientRequestBody(rBody, uuid); |
||||
} catch (error) { |
||||
stderr(`Failed to process alert recipient input; CAUSE: ${error}`); |
||||
|
||||
return response.status(400).send(); |
||||
} |
||||
|
||||
try { |
||||
execManageAlerts('recipients', 'edit', { body, uuid }); |
||||
} catch (error) { |
||||
stderr(`Failed to update mail server; CAUSE: ${error}`); |
||||
|
||||
return response.status(500).send(); |
||||
} |
||||
|
||||
return response.status(200).send(); |
||||
}; |
@ -0,0 +1,20 @@ |
||||
type MailRecipientOverview = { |
||||
name: string; |
||||
uuid: string; |
||||
}; |
||||
|
||||
type MailRecipientDetail = MailRecipientOverview & { |
||||
email: string; |
||||
language: string; |
||||
level: number; |
||||
}; |
||||
|
||||
type MailRecipientOverviewList = { |
||||
[uuid: string]: MailRecipientOverview; |
||||
}; |
||||
|
||||
type MailRecipientParamsDictionary = { |
||||
uuid: string; |
||||
}; |
||||
|
||||
type MailRecipientRequestBody = Omit<MailRecipientDetail, 'uuid'>; |
Loading…
Reference in new issue