Genesis
This commit is contained in:
48
src/endpoints/order.js
Normal file
48
src/endpoints/order.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import { sendJson } from '../http.js';
|
||||
import { logOrder } from '../order-log.js';
|
||||
|
||||
export const method = 'POST';
|
||||
export const path = '/v1/order';
|
||||
|
||||
function parseBody(buffer) {
|
||||
if (!buffer || !buffer.length) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return JSON.parse(buffer.toString('utf8'));
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function getOrders(body) {
|
||||
if (!body || typeof body !== 'object') {
|
||||
return [];
|
||||
}
|
||||
if (Array.isArray(body.orders)) {
|
||||
return body.orders;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export async function handle(req, res) {
|
||||
const body = parseBody(req.rawBody);
|
||||
|
||||
if (body === undefined) {
|
||||
return sendJson(res, 500, []);
|
||||
}
|
||||
|
||||
const orders = getOrders(body);
|
||||
const results = orders.map((order) => {
|
||||
logOrder(order);
|
||||
const externalOrderId = String(order?.externalId ?? '');
|
||||
|
||||
return {
|
||||
status: 'OK',
|
||||
externalOrderId,
|
||||
message: '',
|
||||
};
|
||||
});
|
||||
|
||||
return sendJson(res, 200, results);
|
||||
}
|
||||
Reference in New Issue
Block a user