mirror of
https://github.com/tiennm99/exchange-rate-export.git
synced 2026-10-11 03:13:21 +00:00
feat: remove debug log
This commit is contained in:
1 parent
08402cbcf4
commit
6a572e36aa
1 file changed
-19
@@ -16,9 +16,7 @@ async function fetchBIDVRates(dateObj) {
|
|||||||
try {
|
try {
|
||||||
// First API call to get namerecord
|
// First API call to get namerecord
|
||||||
const timeUrl = `https://bidv.com.vn/ServicesBIDV/ExchangeDetailSearchTimeServlet?date=${dateStr}`;
|
const timeUrl = `https://bidv.com.vn/ServicesBIDV/ExchangeDetailSearchTimeServlet?date=${dateStr}`;
|
||||||
console.log("[BIDV] Request URL:", timeUrl);
|
|
||||||
const timeRes = await axiosInstance.get(timeUrl);
|
const timeRes = await axiosInstance.get(timeUrl);
|
||||||
console.log("[BIDV] Response:", JSON.stringify(timeRes.data));
|
|
||||||
const timeData = timeRes.data;
|
const timeData = timeRes.data;
|
||||||
if (timeData.status !== 1 || !timeData.data?.length) return null;
|
if (timeData.status !== 1 || !timeData.data?.length) return null;
|
||||||
// Get the latest record
|
// Get the latest record
|
||||||
@@ -28,9 +26,7 @@ async function fetchBIDVRates(dateObj) {
|
|||||||
);
|
);
|
||||||
// Second API call to get exchange rates
|
// Second API call to get exchange rates
|
||||||
const rateUrl = `https://bidv.com.vn/ServicesBIDV/ExchangeDetailServlet?date=${dateStr}&time=${latest.namerecord}`;
|
const rateUrl = `https://bidv.com.vn/ServicesBIDV/ExchangeDetailServlet?date=${dateStr}&time=${latest.namerecord}`;
|
||||||
console.log("[BIDV] Request URL:", rateUrl);
|
|
||||||
const rateRes = await axiosInstance.get(rateUrl);
|
const rateRes = await axiosInstance.get(rateUrl);
|
||||||
console.log("[BIDV] Response:", JSON.stringify(rateRes.data));
|
|
||||||
const rateData = rateRes.data;
|
const rateData = rateRes.data;
|
||||||
if (rateData.status !== 1 || !rateData.data) return null;
|
if (rateData.status !== 1 || !rateData.data) return null;
|
||||||
// Find USD data
|
// Find USD data
|
||||||
@@ -56,9 +52,7 @@ async function fetchTCBRates(dateObj) {
|
|||||||
const dateStr = format(dateObj, "yyyy-MM-dd");
|
const dateStr = format(dateObj, "yyyy-MM-dd");
|
||||||
const url = `https://techcombank.com/content/techcombank/web/vn/vi/cong-cu-tien-ich/ty-gia/_jcr_content.exchange-rates.${dateStr}.integration.json`;
|
const url = `https://techcombank.com/content/techcombank/web/vn/vi/cong-cu-tien-ich/ty-gia/_jcr_content.exchange-rates.${dateStr}.integration.json`;
|
||||||
try {
|
try {
|
||||||
console.log("[TCB] Request URL:", url);
|
|
||||||
const res = await axiosInstance.get(url);
|
const res = await axiosInstance.get(url);
|
||||||
console.log("[TCB] Response:", JSON.stringify(res.data));
|
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
if (!data.exchangeRate?.data) return null;
|
if (!data.exchangeRate?.data) return null;
|
||||||
// Find USD data
|
// Find USD data
|
||||||
@@ -102,11 +96,9 @@ async function getPreviousDayRate(date, bank, rateCache, maxAttempts = 30) {
|
|||||||
currentDate.setDate(currentDate.getDate() - 1);
|
currentDate.setDate(currentDate.getDate() - 1);
|
||||||
const dateStr = format(currentDate, 'yyyy-MM-dd');
|
const dateStr = format(currentDate, 'yyyy-MM-dd');
|
||||||
|
|
||||||
console.log(`[DEBUG] Attempt ${attempt}: Trying previous day ${dateStr}`);
|
|
||||||
|
|
||||||
// Try to find in cache first
|
// Try to find in cache first
|
||||||
if (rateCache.has(dateStr)) {
|
if (rateCache.has(dateStr)) {
|
||||||
console.log(`[DEBUG] Found cached data for ${dateStr}`);
|
|
||||||
return rateCache.get(dateStr);
|
return rateCache.get(dateStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +112,6 @@ async function getPreviousDayRate(date, bank, rateCache, maxAttempts = 30) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rate) {
|
if (rate) {
|
||||||
console.log(`[DEBUG] Found data for ${dateStr} after ${attempt} attempts`);
|
|
||||||
rateCache.set(dateStr, rate);
|
rateCache.set(dateStr, rate);
|
||||||
return rate;
|
return rate;
|
||||||
}
|
}
|
||||||
@@ -129,14 +120,12 @@ async function getPreviousDayRate(date, bank, rateCache, maxAttempts = 30) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`[DEBUG] No data found after ${maxAttempts} attempts`);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request) {
|
export async function POST(request) {
|
||||||
try {
|
try {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
console.log("[POST] Payload:", JSON.stringify(body));
|
|
||||||
const { startDate, endDate, bank } = body;
|
const { startDate, endDate, bank } = body;
|
||||||
if (!startDate || !endDate || !bank) {
|
if (!startDate || !endDate || !bank) {
|
||||||
return new Response(
|
return new Response(
|
||||||
@@ -152,15 +141,12 @@ export async function POST(request) {
|
|||||||
let rate = null;
|
let rate = null;
|
||||||
const dateStr = format(date, 'yyyy-MM-dd');
|
const dateStr = format(date, 'yyyy-MM-dd');
|
||||||
|
|
||||||
console.log(`[DEBUG] Processing date: ${dateStr}`);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (bank === "bidv") {
|
if (bank === "bidv") {
|
||||||
rate = await fetchBIDVRates(date);
|
rate = await fetchBIDVRates(date);
|
||||||
console.log(`[DEBUG] BIDV rate for ${dateStr}:`, rate);
|
|
||||||
} else if (bank === "tcb") {
|
} else if (bank === "tcb") {
|
||||||
rate = await fetchTCBRates(date);
|
rate = await fetchTCBRates(date);
|
||||||
console.log(`[DEBUG] TCB rate for ${dateStr}:`, rate);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rate) {
|
if (rate) {
|
||||||
@@ -168,7 +154,6 @@ export async function POST(request) {
|
|||||||
rateCache.set(dateStr, rate);
|
rateCache.set(dateStr, rate);
|
||||||
} else {
|
} else {
|
||||||
// No data for this date (likely weekend), try to get previous day's data
|
// No data for this date (likely weekend), try to get previous day's data
|
||||||
console.log(`No data for ${dateStr}, trying previous day...`);
|
|
||||||
const prevRate = await getPreviousDayRate(date, bank, rateCache);
|
const prevRate = await getPreviousDayRate(date, bank, rateCache);
|
||||||
if (prevRate) {
|
if (prevRate) {
|
||||||
// Create a new rate object with current date but previous day's data
|
// Create a new rate object with current date but previous day's data
|
||||||
@@ -177,9 +162,7 @@ export async function POST(request) {
|
|||||||
date: bank === "bidv" ? format(date, 'dd/MM/yyyy') : dateStr
|
date: bank === "bidv" ? format(date, 'dd/MM/yyyy') : dateStr
|
||||||
};
|
};
|
||||||
results.push(filledRate);
|
results.push(filledRate);
|
||||||
console.log(`Filled ${dateStr} with previous day's data`);
|
|
||||||
} else {
|
} else {
|
||||||
console.log(`No previous day data available for ${dateStr}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -192,11 +175,9 @@ export async function POST(request) {
|
|||||||
date: bank === "bidv" ? format(date, 'dd/MM/yyyy') : dateStr
|
date: bank === "bidv" ? format(date, 'dd/MM/yyyy') : dateStr
|
||||||
};
|
};
|
||||||
results.push(filledRate);
|
results.push(filledRate);
|
||||||
console.log(`Filled ${dateStr} with previous day's data after error`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("[POST] Response:", JSON.stringify({ data: results }));
|
|
||||||
return new Response(JSON.stringify({ data: results }), {
|
return new Response(JSON.stringify({ data: results }), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
|
|||||||
Reference in new issue
Block a user