90 lines
2.9 KiB
JavaScript
90 lines
2.9 KiB
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const port = Number(process.env.ERP_MOCK_PORT || 4523);
|
|
const fixturePath = process.env.ERP_MOCK_DATA
|
|
|| path.resolve(__dirname, '../../test-data/erp/load-dispatch-response.json');
|
|
|
|
function sendJson(response, statusCode, value) {
|
|
response.writeHead(statusCode, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
response.end(JSON.stringify(value));
|
|
}
|
|
|
|
function readBody(request) {
|
|
return new Promise((resolve, reject) => {
|
|
let body = '';
|
|
request.setEncoding('utf8');
|
|
request.on('data', chunk => { body += chunk; });
|
|
request.on('end', () => {
|
|
try {
|
|
resolve(body ? JSON.parse(body) : {});
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
request.on('error', reject);
|
|
});
|
|
}
|
|
|
|
const server = http.createServer(async (request, response) => {
|
|
const requestPath = new URL(request.url, `http://${request.headers.host}`).pathname;
|
|
if (request.method === 'GET' && requestPath === '/health') {
|
|
return sendJson(response, 200, { status: 'ok', fixturePath });
|
|
}
|
|
|
|
if (request.method !== 'POST' || !requestPath.toLowerCase().endsWith('/loaddispatch')) {
|
|
return sendJson(response, 404, { code: 404, msg: 'Not Found', count: 0, data: [] });
|
|
}
|
|
|
|
try {
|
|
const body = await readBody(request);
|
|
const fixture = JSON.parse(fs.readFileSync(fixturePath, 'utf8'));
|
|
let rows = Array.isArray(fixture.data) ? fixture.data : [];
|
|
|
|
const exactFilters = [
|
|
['DLCode', 'DLCode'],
|
|
['InvCode', 'InvCode'],
|
|
['CusCode', 'CusCode']
|
|
];
|
|
for (const [requestName, rowName] of exactFilters) {
|
|
const value = String(body[requestName] ?? '').trim();
|
|
if (value) rows = rows.filter(row => String(row[rowName] ?? '') === value);
|
|
}
|
|
|
|
const containsFilters = [
|
|
['InvName', 'InvName'],
|
|
['CusName', 'CusName'],
|
|
['InvDefine1', 'InvDefine1'],
|
|
['InvDefine4', 'InvDefine4']
|
|
];
|
|
for (const [requestName, rowName] of containsFilters) {
|
|
const value = String(body[requestName] ?? '').trim().toLowerCase();
|
|
if (value) {
|
|
rows = rows.filter(row => String(row[rowName] ?? '').toLowerCase().includes(value));
|
|
}
|
|
}
|
|
|
|
const count = rows.length;
|
|
const size = Math.max(1, Number(body.Size) || 100);
|
|
const page = Math.max(1, Number(body.Page) || 1);
|
|
rows = rows.slice((page - 1) * size, page * size);
|
|
|
|
console.log(`${new Date().toISOString()} LoadDispatch DLCode=${body.DLCode || ''} page=${page} size=${size} count=${count}`);
|
|
return sendJson(response, 200, {
|
|
data: rows,
|
|
count,
|
|
code: Number(fixture.code) || 0,
|
|
msg: fixture.msg || ''
|
|
});
|
|
} catch (error) {
|
|
console.error(error);
|
|
return sendJson(response, 500, { data: [], count: 0, code: 500, msg: error.message });
|
|
}
|
|
});
|
|
|
|
server.listen(port, '127.0.0.1', () => {
|
|
console.log(`ERP mock listening on http://127.0.0.1:${port}`);
|
|
console.log(`Fixture: ${fixturePath}`);
|
|
});
|