fix(striker-ui-api): add delete user request handler

main
Tsu-ba-me 2 years ago
parent d9e38d87e2
commit 414c4d64c6
  1. 1
      striker-ui-api/src/lib/consts/DELETED.ts
  2. 2
      striker-ui-api/src/lib/consts/index.ts
  3. 69
      striker-ui-api/src/lib/request_handlers/user/deleteUser.ts
  4. 1
      striker-ui-api/src/lib/request_handlers/user/index.ts
  5. 7
      striker-ui-api/src/routes/user.ts

@ -0,0 +1 @@
export const DELETED = 'DELETED';

@ -3,6 +3,8 @@ import SERVER_PATHS from './SERVER_PATHS';
export { SERVER_PATHS };
export * from './AN_VARIABLE_NAME_LIST';
export * from './DELETED';
export * from './EXIT_CODE_LIST';
export * from './PROCESS_OWNER';
export * from './REG_EXP_PATTERNS';
export * from './SERVER_PORT';

@ -0,0 +1,69 @@
import assert from 'assert';
import { RequestHandler } from 'express';
import { DELETED, REP_UUID } from '../../consts';
import { awrite } from '../../accessModule';
import join from '../../join';
import { sanitize } from '../../sanitize';
import { stderr, stdoutVar } from '../../shell';
export const deleteUser: RequestHandler<
DeleteUserParamsDictionary,
undefined,
DeleteUserRequestBody
> = (request, response) => {
const {
body: { uuids: rawUserUuidList } = {},
params: { userUuid },
} = request;
const userUuidList = sanitize(rawUserUuidList, 'string[]');
const ulist = userUuidList.length > 0 ? userUuidList : [userUuid];
stdoutVar({ ulist });
try {
let failedIndex = 0;
assert(
ulist.every((uuid, index) => {
failedIndex = index;
return REP_UUID.test(uuid);
}),
`All UUIDs must be valid UUIDv4; failed at ${failedIndex}, got [${ulist[failedIndex]}]`,
);
} catch (assertError) {
stderr(`Failed to assert value during delete user; CAUSE: ${assertError}`);
return response.status(400).send();
}
try {
awrite(
`UPDATE users
SET user_algorithm = '${DELETED}'
WHERE user_uuid IN (${join(ulist)});`,
{
onClose: ({ ecode, wcode }) => {
if (ecode !== 0 || wcode !== 0) {
stderr(
`SQL script failed in delete user(s); ecode=${ecode}, wcode=${wcode}`,
);
}
},
onError: (error) => {
stderr(`Delete user subprocess error; CAUSE: ${error}`);
},
},
);
} catch (error) {
stderr(`Failed to delete user(s); CAUSE: ${error}`);
return response.status(500).send();
}
response.status(204).send();
};

@ -1 +1,2 @@
export * from './deleteUser';
export * from './getUser';

@ -1,9 +1,12 @@
import express from 'express';
import { getUser } from '../lib/request_handlers/user';
import { deleteUser, getUser } from '../lib/request_handlers/user';
const router = express.Router();
router.get('/', getUser);
router
.get('/', getUser)
.delete('/', deleteUser)
.delete('/:userUuid', deleteUser);
export default router;

Loading…
Cancel
Save