Skip to content

Commit beafd53

Browse files
committed
json formatting using s3 url
1 parent 1fdd55d commit beafd53

File tree

5 files changed

+137
-50
lines changed

5 files changed

+137
-50
lines changed

app.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require("dotenv").config();
22
const express = require("express");
3-
const Upload = require('./utils/Upload.js');
43
const StatementToJsonController = require('./controller/statementToJson.controller.js');
54
const app = express();
65

@@ -10,7 +9,6 @@ app.use(express.json());
109
// Route
1110
app.post(
1211
"/api/bank_statement_processor",
13-
Upload.single("resource_url"),
1412
StatementToJsonController.convert
1513
);
1614

controller/statementToJson.controller.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
const XLSX = require("xlsx");
2-
const fs = require("fs");
2+
const axios = require("axios");
33

44
const StatementToJsonController = {
55
async convert(req, res) {
66
try {
7-
if (!req.file) {
8-
return res.status(400).json({ flag: 0, message: "No file uploaded!" });
9-
}
10-
const { bank } = req.body;
7+
const { bank, resource_url } = req.body;
118
if (!bank) {
12-
fs.unlinkSync(req.file.path);
139
return res
1410
.status(400)
1511
.json({ flag: 0, message: "Bank Name is required!" });
1612
}
1713

18-
const workbook = XLSX.readFile(req.file.path);
14+
const signedUrl =
15+
resource_url ||
16+
"https://webledger-assets-books-dev.s3.ap-south-1.amazonaws.com/try_export/HDFC_BANK_Statement.xls";
17+
18+
const response = await axios.get(signedUrl, {
19+
responseType: "arraybuffer",
20+
});
21+
const workbook = XLSX.read(response.data, {
22+
cellDates: true,
23+
dateNF: 'd"/"m"/"yyyy',
24+
});
1925
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
2026
const rawData = XLSX.utils.sheet_to_json(worksheet, {
2127
header: 1,
@@ -27,7 +33,7 @@ const StatementToJsonController = {
2733
const selectedBank = bank.trim().toLowerCase();
2834

2935
if (!supportedBanks.includes(selectedBank)) {
30-
fs.unlinkSync(req.file.path);
36+
// fs.unlinkSync(req.file.path);
3137
throw new Error("Unsupported bank selected.");
3238
}
3339

@@ -42,12 +48,12 @@ const StatementToJsonController = {
4248
if (b == "bob") {
4349
return content.includes("barb");
4450
} else {
45-
return content.includes(b);
51+
return content.includes(b);
4652
}
4753
});
4854

4955
if (!detectedBank || detectedBank !== selectedBank) {
50-
fs.unlinkSync(req.file.path);
56+
// fs.unlinkSync(req.file.path);
5157
throw new Error(
5258
"The uploaded document does not appear to match the selected bank. Please check and try again."
5359
);
@@ -90,9 +96,6 @@ const StatementToJsonController = {
9096

9197
const result = parseHDFCExcel(cleanedData, headerRowIndex, bank);
9298

93-
//file-removed locally
94-
fs.unlinkSync(req.file.path);
95-
9699
return res.status(200).json({
97100
success: true,
98101
message: "File processed successfully",

package-lock.json

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"license": "ISC",
1010
"description": "",
1111
"dependencies": {
12+
"axios": "^1.8.4",
1213
"cors": "^2.8.5",
1314
"dotenv": "^16.5.0",
1415
"express": "^5.1.0",

utils/Upload.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)