Skip to content

Firefly III: a personal finances manager

2.9 KB
Raw Blame
import Get from "../../../api/model/transaction/get.js";

export function loadTransactionLinks(index, journalId) {
    // console.log(
    //     "Get transaction links for index #" +
    //         index +
    //         " and journal #" +
    //         journalId,
    // );
    new Get().transactionLinks(journalId).then((data) => {
        let links = data.data.data;
        // console.log('Get link info for links.');
        for (let i = 0; i < links.length; i++) {
            if (Object.hasOwn(links, i)) {
                let current = links[i];
                // console.log('Get link info for link #' + current.id);
                let direction = parseInt(journalId) === parseInt(current.attributes.inward_id) ? "outward" : "inward";
                let otherJournal = parseInt(current.attributes.inward_id);
                if (parseInt(journalId) === parseInt(current.attributes.inward_id)) {
                    otherJournal = parseInt(current.attributes.outward_id);
                }
                new Get().showJournal(otherJournal).then((response) => {
                    let group = response.data.data;
                    let foundJournal = null;
                    for (let j = 0; j < group.attributes.transactions.length; j++) {
                        if (Object.hasOwn(group.attributes.transactions, j)) {
                            let journal = group.attributes.transactions[j];
                            if (parseInt(journal.transaction_journal_id) === otherJournal) {
                                foundJournal = journal;
                            }
                        }
                    }
                    if (null !== foundJournal) {
                        //console.log('Found other journal ',  parseInt(foundJournal.transaction_journal_id));
                        // console.log("Current link is, found opposing. ", parseInt(current.id));
                        this.links[index].push({
                            id: parseInt(current.id),
                            link_type: parseInt(current.attributes.link_type_id) + "_" + direction,
                            link_type_id: parseInt(current.attributes.link_type_id),
                            link_type_direction: direction,
                            link_type_label: this.formData.linkTypes.find(
                                (link) => link.id === parseInt(current.attributes.link_type_id),
                            )[direction],
                            journal_id: parseInt(foundJournal.transaction_journal_id),
                            group_id: parseInt(group.id),
                            journal_description: foundJournal.description,
                            editMode: false,
                            stored: false,
                        });
                    }
                });
            }
        }
        // console.log('Done, link list is now', this.links);
    });
}