Skip to content

Instantly share code, notes, and snippets.

@userhooke
Created February 3, 2020 11:16
Show Gist options
  • Select an option

  • Save userhooke/51f0160fdebeb0bab3a3a159acdff9d5 to your computer and use it in GitHub Desktop.

Select an option

Save userhooke/51f0160fdebeb0bab3a3a159acdff9d5 to your computer and use it in GitHub Desktop.

Revisions

  1. Vladimir Hooke created this gist Feb 3, 2020.
    41 changes: 41 additions & 0 deletions monobank-ynab.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    const ynab = require("ynab");
    const ynabAPI = new ynab.API(process.env.YNAB_TOKEN);
    const adMaioraBudgetId = process.env.BUDGET_ID;
    const monoDebitAccountId = process.env.MONO_DEBIT_ACCOUNT_ID;

    module.exports.handler = (event, context, callback) => {

    const body = JSON.parse(event.body);
    console.log('Event body: ', body);

    if (!body || !body.data || !body.data.statementItem) {
    callback(null, {
    statusCode: 400,
    headers: {},
    body: JSON.stringify({error: "No data provided :("})
    });
    }

    callback(null, {
    statusCode: 200,
    headers: {},
    body: JSON.stringify({status: 'Ok!'})
    });


    ynabAPI.transactions
    .createTransaction(adMaioraBudgetId, {
    transaction: {
    account_id: monoDebitAccountId,
    date: ynab.utils.getCurrentDateInISOFormat(),
    amount: body.data.statementItem.amount + "0",
    payee_name: body.data.statementItem.description
    }
    })
    .then(res => {
    console.log('YNAB Response: ', res);
    })
    .catch(err => {
    console.error('YNAB error: ', err);
    });
    };