diff --git a/src/api/index.js b/src/api/index.js index e69acf3..5e29ec7 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,8 +1,8 @@ -import {application, Router} from 'express'; -import {gatherStats} from '../ws/util/statsUtil.js'; -import {BASE_URL, FLAG_HAS_PRIORITY_MAPPING, FLAG_SHOW_CREATOR} from '../constants.js'; +import { application, Router } from 'express'; +import { gatherStats } from '../ws/util/statsUtil.js'; +import { BASE_URL, FLAG_HAS_PRIORITY_MAPPING, FLAG_SHOW_CREATOR } from '../constants.js'; -const {chief} = application; +const { chief } = application; const router = new Router(); router.get('/stats', (req, res) => { @@ -51,4 +51,45 @@ router.get('/orders', async (req, res) => { res.json(orders); }) +router.get('/order', async (req, res) => { + const orders = await chief.sql`SELECT * FROM orders ORDER BY created_at DESC LIMIT 1;`; + const order = orders[0] + + if ((order.flags & FLAG_SHOW_CREATOR) !== 0) { + [order.creator] = await chief.sql`SELECT * + FROM users + WHERE id = ${order.created_by};`; + delete order.creator.id; + } else { + order.creator = null; + } + + order.images = { + order: `${BASE_URL}/orders/${order.id}.png`, + priority: (order.flags & FLAG_HAS_PRIORITY_MAPPING) !== 0 ? `${BASE_URL}/orders/${order.id}-priority.png` : null + }; + + order.size = { + height: order.height, + width: order.width + }; + + order.offset = { + x: order.offset_x, + y: order.offset_y + }; + + order.createdAt = order.created_at; + + delete order.created_at; + delete order.created_by; + delete order.flags; // implementation detail + delete order.height; + delete order.width; + delete order.offset_x; + delete order.offset_y; + + res.json(orders); +}) + export default router;