Get the site functional for the game release

This commit is contained in:
Oliver-Akins 2024-03-27 21:14:01 -06:00
parent 4b61e73573
commit 33ab4afe52
16 changed files with 575 additions and 30 deletions

View file

@ -3,7 +3,7 @@ import { ServerRoute } from "@hapi/hapi";
import Joi from "joi";
const route: ServerRoute = {
method: `POST`, path: `/{channel}/questions/{question_id}`,
method: `PATCH`, path: `/{channel}/questions/{question_id}`,
options: {
validate: {
params: Joi.object({
@ -21,6 +21,7 @@ const route: ServerRoute = {
question: Joi.string().optional(),
asker: Joi.string().optional(),
answered: Joi.boolean().optional(),
hidden: Joi.boolean().optional(),
id: Joi.forbidden(),
})
.min(1),
@ -41,17 +42,24 @@ const route: ServerRoute = {
let values = [];
for (const key in payload) {
let v = payload[key];
if (v.startsWith(`__`)) {
let type = typeof v;
if (type == "boolean") {
setters.push(`${key} = ${v}`);
} else {
setters.push(`${key} = ?`);
values.push(v);
}
else if (type == "string") {
if (v.startsWith(`__`)) {
setters.push(`${key} = ${v.slice(2)}`);
} else {
setters.push(`${key} = ?`);
values.push(v);
};
};
};
await db.query(
`update tqna.questions
( ${setters.join(`, `)} )
set ${setters.join(`, `)}
where channel = ? and id = ?
limit 1`,
[...values, channel, question_id ]
@ -64,8 +72,9 @@ const route: ServerRoute = {
question = questions[0];
await conn.commit();
} catch {
log.error(`Failed to add the question`);
} catch (e) {
log.error(e)
log.error(`Failed to save the question`);
await conn.rollback();
} finally {
conn.release();

View file

@ -1,4 +1,5 @@
import { Server } from "@hapi/hapi";
import { isBoom } from "@hapi/boom";
import { globSync } from "glob";
import path from "path";
import { log } from "./main";
@ -10,6 +11,42 @@ const server = new Server({
debug: {
request: [ `*` ],
},
router: {
stripTrailingSlash: true,
},
routes: {
cors: {
origin: [ `*` ],
},
},
});
/*
This event listener makes it so that the error that is returned from the system
is more user-friendly when it's a validation error, and so that nothing gets
leaked accidentally through allowing other data to make it out of the API.
*/
server.ext(`onPreResponse`, (req, h) => {
if (isBoom(req.response)) {
let oldResponse = req.response.output.payload as any;
let newResponse: any = {
statusCode: oldResponse.statusCode,
error: oldResponse.error,
message: oldResponse.message,
};
let deets = (req.response as any).details as any[];
if (deets) {
let messages = deets.map(e => e.message);
newResponse.message = (req.response as any).output.payload.validation.source + ` failed to validate`;
newResponse.violations = messages;
};
req.response.output.payload = newResponse;
return h.continue;
}
return h.continue;
});