Files
Online-Topup-V1/application/helpers/reservation-request-log.js
T

56 lines
2.0 KiB
JavaScript

const validationFields = ['amount', 'mid', 'tid', 'card_number'];
const customerFields = ['customer_name', 'phone_number'];
const cancellationFields = ['reservation_number', 'mid', 'tid'];
const fieldsByRoute = new Map([
['/api/reservation/validate', validationFields],
['/api/reservation/validate/v2', validationFields],
['/api/reservation', [...validationFields, 'ref_no', ...customerFields]],
['/api/reservation/v2', ['amount', 'card_number', 'ref_no', 'reservation_data', ...customerFields]],
['/api/reservation/cancel/validate', cancellationFields],
['/api/reservation/cancel', cancellationFields],
['/api/reservation/cancel/validate/v2', [...cancellationFields, 'ref_no']],
['/api/reservation/cancel/v2', ['cancel_reservation_data']],
]);
function getReservationNumber(value) {
return typeof value === 'string' && /^RBL[a-z0-9-]{1,61}$/i.test(value) ? value : undefined;
}
function getAmount(value) {
if (typeof value !== 'number' && typeof value !== 'string') return undefined;
return /^-?\d{1,12}(?:\.\d{1,2})?$/.test(value) ? value : undefined;
}
function getTerminalIdentifier(value) {
return typeof value === 'string' && /^[a-z0-9]{4}$/i.test(value) ? value : undefined;
}
function getCardNumber(value) {
return typeof value === 'string' && /^\d{8,19}$/.test(value) ? value : undefined;
}
const safeFields = {
amount: getAmount,
reservation_number: getReservationNumber,
mid: getTerminalIdentifier,
tid: getTerminalIdentifier,
card_number: getCardNumber,
};
// Pertahankan nama field API yang dikirim klien agar log mudah dibandingkan
// dengan request. Nilai yang disamarkan diganti dengan lima tanda bintang.
function summarizeReservationRequest(route, body) {
const fields = fieldsByRoute.get(route);
if (!fields) return undefined;
const summary = {};
for (const field of fields) {
if (body == null || !Object.hasOwn(body, field)) continue;
summary[field] = safeFields[field]?.(body[field]) ?? '*****';
}
return summary;
}
module.exports = { fieldsByRoute, getReservationNumber, summarizeReservationRequest };