-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinance.ts
407 lines (274 loc) · 11.9 KB
/
finance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//type str = string; //type int = number; type bool = boolean;
//import {FieldValue} from "@google-cloud/firestore"
//import fs from "fs";
import SSE from "../sse.js"
import { SSE_TriggersE } from '../definitions.js'
const YNAB_HOLDEM_ACCOUNT_ID = "b0b3f2b2-5067-4f57-a248-15fa97a18cf5"
const YNAB_FAMILY_ACCOUNT_ID = "dbb7396b-413f-40d7-9a3f-7c986e485233"
const payees_to_skip = [
"APPLECARD GSBANK",
"Starting Balance",
"AMZN Mktp"
]
async function Grab_Em(db:any, Firestore:any) { return new Promise<any>(async (res, _rej)=> {
const token = process.env.XEN_YNAB
let promises:any[] = [
//Firestore.Retrieve(db, ["areas", "cats", "sources", "tags", "transactions", "monthsnapshots"]),
fetch(`https://api.ynab.com/v1/budgets/${YNAB_HOLDEM_ACCOUNT_ID}/accounts`, {
method: 'GET',
headers: { "Authorization": `Bearer ${token}` }
}),
fetch(`https://api.ynab.com/v1/budgets/${YNAB_FAMILY_ACCOUNT_ID}/accounts`, {
method: 'GET',
headers: { "Authorization": `Bearer ${token}` }
})
]
let r = await Promise.all(promises)
/*
let areas = r[0][0]
let cats = r[0][1]
let sources = r[0][2]
let tags = r[0][3]
let transactions = r[0][4]
let previous_static_monthsnapshots = r[0][5]
*/
let ynab_holdem_accounts = (await r[0].json()).data.accounts
let ynab_family_accounts = (await r[1].json()).data.accounts
let ynab_accounts = [...ynab_holdem_accounts, ...ynab_family_accounts]
/*
areas.forEach((m:any)=> {
const ynab_account = ynab_accounts.find((n:any)=> n.id === m.ynab_savings_id)
m.ynab_savings = ynab_account.balance / 1000
})
*/
res({ynab_accounts})
})}
async function YNAB_Sync_Categories(db:any, Firestore:any) { return new Promise<any>(async (res, _rej)=> {
const cats_with_deleteflag:{id:string, name:string}[] = []
const ynab_cats_to_skip = ["Credit Card Payments", "Hidden Categories", "Internal Master Category"]
const batch = db.batch()
const token = process.env.XEN_YNAB
const r = await Firestore.Retrieve(db, ["areas", "cats"])
const areas = r[0]
const cats = r[1]
const all_ynab_cats:{id:string,parentid:string|null}[] = []
for(const a of areas) {
const r = await fetch(`https://api.ynab.com/v1/budgets/${a.id}/categories`, {
method: 'GET',
headers: {
"Authorization": `Bearer ${token}`
}
})
const rjson = await r.json()
const ynab_cat_groups = rjson.data.category_groups
for(const cg of ynab_cat_groups) {
if (ynab_cats_to_skip.includes(cg.name)) continue
let cat = cats.find((c:any)=> c.id === cg.id)
if (cg.hidden || cg.deleted) {
if (cat) {
cat.deleteflag = true
cat.ts = Math.floor(Date.now() / 1000)
batch.update(db.collection('cats').doc(cat.id), cat)
cats_with_deleteflag.push({id:cat.id, name:cat.name})
}
} else {
all_ynab_cats.push({id:cg.id, parentid:null})
if (!cat) {
cat = {
area: db.collection("areas").doc(a.id),
budget: null,
name: cg.name,
parent: null,
tags: null,
deleteflag: false,
ts: Math.floor(Date.now() / 1000)
}
const newcatdoc = db.collection('cats').doc(cg.id)
batch.set(newcatdoc, cat)
} else {
cat.budget = null
cat.name = cg.name
cat.parent = null
cat.tags = null
cat.deleteflag = false
cat.ts = Math.floor(Date.now() / 1000)
batch.update(db.collection('cats').doc(cat.id), cat)
}
}
for(const ccg of cg.categories) {
let c = cats.find((m:any)=> m.id === ccg.id)
let hashindex = ccg.name.lastIndexOf("#")
let cattags = (hashindex > 0) ? ccg.name.slice(hashindex+1,ccg.name.length).split(",").map((c:string)=> parseInt(c)) : []
let name = (hashindex > 0) ? ccg.name.slice(0,hashindex-1) : ccg.name
if (ccg.hidden || ccg.deleted || cattags.length === 0) {
if (c) {
c.deleteflag = true
c.ts = Math.floor(Date.now() / 1000)
batch.update(db.collection('cats').doc(c.id), c)
cats_with_deleteflag.push({id:c.id, name:c.name})
}
} else {
all_ynab_cats.push({id:ccg.id, parentid:cg.id})
if (!c) {
c = {
area: null,
budget: Math.floor(ccg.goal_target/1000),
name: name,
parent: db.collection("cats").doc(cg.id),
tags: cattags,
deleteflag: false,
ts: Math.floor(Date.now() / 1000)
}
const newcatdoc = db.collection('cats').doc(ccg.id)
batch.set(newcatdoc, c)
} else {
c.area = null
c.budget = Math.floor(ccg.goal_target/1000)
c.name = name
c.parent = db.collection("cats").doc(cg.id)
c.tags = cattags
c.deleteflag = false
c.ts = Math.floor(Date.now() / 1000)
batch.update(db.collection('cats').doc(c.id), c)
}
}
}
}
}
for(const c of cats) {
const has_ynab_corresponding = all_ynab_cats.find((m:any)=> m.id === c.id)
if (!has_ynab_corresponding) {
c.deleteflag = true
c.ts = Math.floor(Date.now() / 1000)
batch.update(db.collection('cats').doc(c.id), c)
cats_with_deleteflag.push({id:c.id, name:c.name})
}
}
await batch.commit().catch((err:any)=> console.error(err))
res({cats_with_deleteflag})
})}
async function Get_YNAB_Raw_Transactions(db:any) { return new Promise<any>(async (res, _rej)=> {
const promises:any[] = []
const token = process.env.XEN_YNAB
let d = new Date()
d.setMonth(d.getMonth() - 1)
let since_date = `${d.getUTCFullYear()}-${(d.getUTCMonth() + 1).toString().padStart(2, '0')}-${d.getUTCDate().toString().padStart(2, '0')}`
promises.push(fetch(`https://api.ynab.com/v1/budgets/${YNAB_HOLDEM_ACCOUNT_ID}/transactions?since_date=${since_date}`, {
method: 'GET',
headers: {
"Authorization": `Bearer ${token}`
}
}))
promises.push(fetch(`https://api.ynab.com/v1/budgets/${YNAB_FAMILY_ACCOUNT_ID}/transactions?since_date=${since_date}`, {
method: 'GET',
headers: {
"Authorization": `Bearer ${token}`
}
}))
promises.push(db.collection("transactions").orderBy("ts", "desc").limit(200).get())
let r = await Promise.all(promises)
const existing_transactions = r[2].docs.map((m: any) => ({ id: m.id, ...m.data() }));
const ynab_t_holdem = await r[0].json()
const ynab_t_family = await r[1].json()
ynab_t_holdem.data.transactions.forEach((m:any)=> m.preset_area_id = null)
ynab_t_family.data.transactions.forEach((m:any)=> m.preset_area_id = YNAB_FAMILY_ACCOUNT_ID)
const combined_ynab_t = [...ynab_t_holdem.data.transactions, ...ynab_t_family.data.transactions]
const ynab_raw_transactions:any[] = []
for(const nt of combined_ynab_t) {
if (is_transaction_irrelevant(nt)) continue
const d = new Date()
d.setUTCFullYear(nt.date.slice(0,4))
d.setUTCMonth( Number(nt.date.slice(5,7)) - 1)
d.setUTCDate(nt.date.slice(8,10))
d.setUTCHours(10)
d.setUTCMinutes(0)
d.setUTCSeconds(0)
if (nt.amount < 0) {
if (nt.subtransactions.length) {
for(const st of nt.subtransactions) {
if (existing_transactions.find((t:any)=> t.ynab_id === st.id)) continue
const raw_transaction = {
ynab_id: st.id,
preset_area_id: nt.preset_area_id,
preset_cat_name: st.category_name || null,
amount: Math.abs(st.amount) / 1000,
merchant: nt.payee_name,
notes: nt.memo || "",
source_id: nt.account_id,
tags: nt.flag_name ? [nt.flag_name] : [],
ts: Math.floor(d.getTime() / 1000)
}
ynab_raw_transactions.push(raw_transaction)
}
} else {
if (existing_transactions.find((t:any)=> t.ynab_id === nt.id)) continue
const raw_transaction = {
ynab_id: nt.id,
preset_area_id: nt.preset_area_id,
preset_cat_name: nt.category_name || null,
amount: Math.abs(nt.amount) / 1000,
merchant: nt.payee_name,
notes: nt.memo || "",
source_id: nt.account_id,
tags: nt.flag_name ? [nt.flag_name] : [],
ts: Math.floor(d.getTime() / 1000)
}
ynab_raw_transactions.push(raw_transaction)
}
}
}
res({ok:true, raw_transactions: ynab_raw_transactions})
})}
function is_transaction_irrelevant(t:any) {
const x = payees_to_skip.find((m:string)=> {
const p = t.import_payee_name_original || t.payee_name
return p.includes(m)
})
return x ? true : false
}
async function Save_Transactions_And_Delete_YNAB_Records(db:any, transactions:any) { return new Promise<any>(async (res, rej)=> {
const batch = db.batch()
const now = Math.floor(Date.now() / 1000)
for(const nt of transactions) {
const d = {
amount: nt.amount,
cat: db.collection("cats").doc(nt.cat_id),
merchant: nt.merchant,
notes: nt.notes,
source: db.collection("sources").doc(nt.source_id),
tags: nt.tag_ids.map((m:string)=> db.collection("tags").doc(m)),
ynab_id: nt.ynab_id,
transacted_ts: nt.ts,
ts: now
}
const docref = db.collection('transactions').doc()
batch.set(docref, d)
}
batch.commit().catch((err:any)=> { rej(err); return })
SSE.TriggerEvent(SSE_TriggersE.FIRESTORE, {paths: ["transactions"]})
/*
const token = process.env.XEN_YNAB
transactions.forEach(async (t:any)=> {
await fetch(`https://api.ynab.com/v1/budgets/${YNAB_HOLDEM_ACCOUNT_ID}/transactions/${t.ynab_id}`, {
method: 'DELETE',
headers: {
"Authorization": `Bearer ${token}`
}
})
})
*/
res({ok:true})
})}
async function Save_Month_Snapshots(db:any, snapshots:any) { return new Promise<any>(async (res, rej)=> {
const batch = db.batch()
for(const snapshot of snapshots) {
snapshot.area = db.collection("areas").doc(snapshot.areaid)
delete snapshot.areaid
const docref = db.collection('monthsnapshots').doc()
batch.set(docref, snapshot)
}
batch.commit().catch((err:any)=> { rej(err); return })
res({ok:true})
})}
const Finance = { Grab_Em, YNAB_Sync_Categories, Get_YNAB_Raw_Transactions, Save_Transactions_And_Delete_YNAB_Records, Save_Month_Snapshots };
export default Finance;