Velar/Backend/testing.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-08-19 22:07:44 -07:00
const testEmails = [
2025-08-21 16:54:47 -07:00
`Dear Customer, Rs.20.00 has been debited from account 5488 to VPA paytm.s1eeznf@pty SHS CANTEEN 1 on 18-08-25. Your UPI transaction reference number is 109983023268. If you did not authorize this `
2025-08-19 22:07:44 -07:00
];
function parseBankMessage(snippet) {
const result = {
amount: null,
vendor: null,
type: null,
date: null,
referenceNumber: null,
};
// Amount
const amtMatch = snippet.match(/(?:Rs\.?|INR)\s*([\d,]+\.?\d*)/i);
if (amtMatch) result.amount = parseFloat(amtMatch[1].replace(/,/g, ""));
// Debit / Credit
if (/debited/i.test(snippet)) result.type = "debit";
else if (/credited/i.test(snippet)) result.type = "credit";
// Date
const dateMatch = snippet.match(/on\s+(\d{2}[-/]\d{2}[-/]\d{2,4})/i);
2025-08-21 16:54:47 -07:00
if (dateMatch) result.date = dateMatch;
// Reference number
const refMatch = snippet.match(/reference number is\s*([\d]+)/i);
if (refMatch) result.referenceNumber = refMatch.trim();
2025-08-19 22:07:44 -07:00
2025-08-21 16:54:47 -07:00
// Improved vendor extraction:
// Try to extract vendor from phrases like "to <vendor>" or "at <vendor>"
2025-08-19 22:07:44 -07:00
2025-08-21 16:54:47 -07:00
let vendor = null;
2025-08-19 22:07:44 -07:00
2025-08-21 16:54:47 -07:00
const toMatch = snippet.match(/to\s+([A-Za-z0-9\s.&'-]+)/i);
if (toMatch) {
vendor = toMatch.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
2025-08-19 22:07:44 -07:00
} else {
2025-08-21 16:54:47 -07:00
const atMatch = snippet.match(/at\s+([A-Za-z0-9\s.&'-]+)/i);
if (atMatch) {
vendor = atMatch.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
2025-08-19 22:07:44 -07:00
}
}
2025-08-21 16:54:47 -07:00
// Clean extra spaces or trailing punctuation
if (vendor) {
vendor = vendor.replace(/\s{2,}/g, ' ').replace(/[.,]$/, '').trim();
}
result.vendor = vendor || "Unknown";
2025-08-19 22:07:44 -07:00
return result;
}
2025-08-21 16:54:47 -07:00
2025-08-19 22:07:44 -07:00
console.log("=== Bank Email Regex Test ===");
testEmails.forEach((email, idx) => {
console.log(`\n-- Email ${idx + 1} --`);
console.log(email);
const parsed = parseBankMessage(email);
console.log("Parsed Result:", parsed);
});