Skip to content

Commit

Permalink
[#160] add pubsub implementation for uncategorized
Browse files Browse the repository at this point in the history
  • Loading branch information
manasouza committed Apr 21, 2024
1 parent 69ab234 commit 09704dd
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
24 changes: 23 additions & 1 deletion file_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ const Map = require("collections/map");
const Dict = require("collections/dict");
const List = require("collections/list");
const Firestore = require('@google-cloud/firestore');
const {PubSub} = require('@google-cloud/pubsub')

const BluebirdPromise = require('bluebird');
const spreadsheet = BluebirdPromise.promisifyAll(require('./spreadsheet'));
const projectId = 'smartfinance-bills-beta'

const db = new Firestore({
projectId: 'smartfinance-bills-beta',
projectId: projectId,
keyFilename: process.env.credentials,
});
const billsCategoryMap = db.collection('bills_config').doc('mapping')
const pubSubClient = new PubSub(projectId)

const FILENAME_DATA_SEPARATOR = "_"
const FILENAME_FIELDS_LENGTH = 3
Expand Down Expand Up @@ -113,8 +116,14 @@ var self = module.exports = {
}
} else {
console.log('[WARN] category not found: %s', receiptName)
publishMessage('sfinbills', receiptName).catch(err => {
console.error(err.message);
process.exitCode = 1;
})
return;
}
})
console.log("TODO: should stop here and not call spreadsheet")
spreadsheet.updateSpreadsheetAsync(spreadsheetMap)
.then((result) => {
for (let i = 0; i < files.length; i++) {
Expand Down Expand Up @@ -152,6 +161,19 @@ var self = module.exports = {
}
};

async function publishMessage(topic, data) {
const dataBuffer = Buffer.from(data)
try {
const messageId = await pubSubClient
.topic(topic)
.publishMessage({data: dataBuffer});
console.log(`[INFO] message ${messageId} published.`);
} catch (error) {
console.error(`Received error while publishing: ${error.message}`);
process.exitCode = 1;
}
}

async function fileAlreadyProcessed(file) {
let alreadyProcessed = false
const querySnapshot = await db.collection('bills').get()
Expand Down
149 changes: 149 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"node": "12.22.9"
},
"dependencies": {
"@google-cloud/pubsub": "3.7.3",
"@google-cloud/firestore": "6.7.0",
"async": "2.6.4",
"bluebird": "3.7.2",
Expand Down

0 comments on commit 09704dd

Please sign in to comment.