Files
firefly-iii/resources/assets/v2/support/ag-grid/TransactionDataSource.js

101 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-02-10 08:28:59 +01:00
/*
* TransactionDataSource.js
* Copyright (c) 2024 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
import Get from "../../api/v2/model/transaction/get.js";
export default class TransactionDataSource {
constructor() {
this.type = 'all';
this.rowCount = null;
}
rowCount() {
return this.rowCount;
}
getRows(params) {
let getter = new Get();
getter.listByCount({start_row: params.startRow, end_row: params.endRow, type: this.type}).then(response => {
this.parseTransactions(response.data.data, params.successCallback);
// set meta data
this.rowCount = response.data.meta.pagination.total;
}).catch(error => {
// todo this is auto generated
//this.notifications.wait.show = false;
//this.notifications.error.show = true;
//this.notifications.error.text = error.response.data.message;
console.log(error);
});
}
parseTransactions(data, callback) {
let transactions = [];
// no parse, just save
for (let i in data) {
if (data.hasOwnProperty(i)) {
let current = data[i];
let isSplit = current.attributes.transactions.length > 1;
let firstSplit = true;
// foreach on transactions, no matter how many.
for (let ii in current.attributes.transactions) {
if (current.attributes.transactions.hasOwnProperty(ii)) {
let transaction = current.attributes.transactions[ii];
let entry = {};
// split info
entry.split = isSplit;
entry.firstSplit = firstSplit;
// group attributes
entry.group_title = current.attributes.group_title;
entry.created_at = current.attributes.created_at;
entry.updated_at = current.attributes.updated_at;
entry.user = current.attributes.user;
entry.user_group = current.attributes.user_group;
// create actual transaction:
entry.id = parseInt(current.id);
entry.transaction_journal_id = parseInt(transaction.transaction_journal_id);
entry.icon = 'fa fa-solid fa-arrow-left';
entry.date = new Date(transaction.date);
entry.description = transaction.description;
// set firstSplit = false for next run if applicable.
firstSplit = false;
transactions.push(entry);
}
}
}
}
callback(transactions, false)
return transactions;
}
setType(type) {
this.type = type;
}
}