// Wallet is a great app to track your expenses
// I just didn't want to buy premium subscription just to export my own data :shrug:

// First follow this gist to export all the data in indexeddb to console
// https://gist.github.com/harryi3t/d8779d3c0c0c4d41c37fdde81b268fd3

// I will be assuming from here on that the the entire data is in a variable `data`

// All the transactions are store in the table 'by-sequence'
let sequence = data[2]['by-sequence'];

// group all sequences by type of record. eg are 'account', 'category', 'record', 'currency', etc
// similar to _.groupBy(sequence, 'reservedModelType')
let groupedSequence = sequence.reduce((group, item) => {
 if (!group[item.reservedModelType]) 
    group[item.reservedModelType] = [];

 group[item.reservedModelType].push(item)
 return group;
}, {})

// Convert the category array into a map (key is category id and value is category item itself)
groupedSequence.categoryMap = groupedSequence.Category.reduce((group, item) => {
 let categoryId = item._doc_id_rev.split('::')[0];
 group[categoryId] = item;
 return group;
}, {})

// Sort the records by date
groupedSequence.Record = groupedSequence.Record.sort((a, b) => {
 return a.reservedCreatedAt.localeCompare(b.reservedCreatedAt)
})

function mapRecordWithCategory (item) {
 return {
  title: item.note,
  date: item.recordDate,
  category: groupedSequence.categoryMap[item.categoryId].name,
  amount: item.amount/100
 }
}