-
Notifications
You must be signed in to change notification settings - Fork 1
/
monobank.ts
639 lines (472 loc) · 18.2 KB
/
monobank.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: university;
/*
* author: https://github.com/Nodman
* Monobank api module
*/
// NOTE: This script uses the Cache script (https://github.com/yaylinda/scriptable/blob/main/Cache.js)
'use strict'
import { fetchJson, createAlert, getDaysInMonth, Logger } from './utils'
const Cache = importModule('cache')
import type { PersonalInfoT, StatementItemT, StatementCacheT, SettingsT } from './monobank.types'
const DEFAULT_PERIOD = 12
const ACCOUNT_MASK_LEN = 4
const SCRIPT_NAME = 'monobank'
const CACHE_KEY_SETTINGS = 'settings.json'
const MONOBANK_TOKEN_KEY = 'monobank_token@scriptable'
const MONOBANK_API_URL = 'https://api.monobank.ua'
const API_PATH = {
PERSONAL_INFO: '/personal/client-info',
STATEMENT: '/personal/statement',
CURRENCIES: '/bank/currency',
}
const CURRENCIES: Record<number, {id: number, sign: string, name: string}> = {
978: { id: 978, name: 'EUR', sign: '€' },
980: { id: 980, name: 'UAH', sign: '₴' },
840: { id: 840, name: 'USD', sign: '$' },
}
const cache = new Cache(SCRIPT_NAME)
const logger = new Logger('Mono')
export class Monobank {
private parentScriptName?: string
private settings: Promise<SettingsT | void>
constructor(parentScriptName?: string) {
this.parentScriptName = parentScriptName
this.readSettings()
}
static getTodaysExpanses({ currentPeriod }: { currentPeriod: StatementCacheT['currentPeriod'] }) {
if (!currentPeriod || !currentPeriod.statement) {
return 0
}
const date = (new Date()).getDate()
const todaysStatement = currentPeriod.statement[date]
if (!todaysStatement || !todaysStatement.length) {
return 0
}
return todaysStatement.reduce((acc, item) => {
return acc - (item?.amount ?? 0)
}, 0)
}
static getStatsForPeriod({ history }: { history: StatementCacheT['history'] }, period: number = DEFAULT_PERIOD) {
const yeatsAvailable = Object.keys(history).map(Number).sort((a, b) => a - b)
const monthlyHistory: number[] = []
const dailyHistory: number[] = []
while (monthlyHistory.length < period && yeatsAvailable.length) {
const [year] = yeatsAvailable.splice(-1)
const yearPeriod = history[year] ?? {}
Object
.keys(yearPeriod)
.map(Number)
.sort((a, b) => b - a)
.slice(-period + monthlyHistory.length)
.forEach((month) => {
const daysInMonth = getDaysInMonth(year, month)
const monthValue = yearPeriod[month]
monthlyHistory.unshift(monthValue)
dailyHistory.unshift(monthValue / daysInMonth)
})
}
return {
monthlyHistory,
dailyHistory,
monthly: monthlyHistory.reduce(( acc, item ) => acc + item, 0) / (monthlyHistory.length || 1),
daily: dailyHistory.reduce(( acc, item ) => acc + item, 0) / (dailyHistory.length || 1),
}
}
private async getStatementItemsFilter() {
const settings = await this.settings
const filter = this.parentScriptName && settings ? settings[this.parentScriptName]?.filter : undefined
return (statementItem: StatementItemT) => {
const isExpanse = statementItem.amount < 0 || statementItem.description.toLowerCase().startsWith('скасування')
const isExcluded = filter ? filter.split(';').some(filterString => statementItem.description.match(filterString)) : false
return isExpanse && !isExcluded
}
}
async readSettings() {
this.settings = cache.read(CACHE_KEY_SETTINGS) ?? {}
return await this.settings
}
private async writeSettings(nextSettings: SettingsT) {
const prevSettings = await this.readSettings()
const settings = { ...prevSettings, ...nextSettings }
cache.write(CACHE_KEY_SETTINGS, settings)
this.settings = Promise.resolve(settings)
}
private get requestHeaders(): Record<string, string> {
const token = Keychain.get(MONOBANK_TOKEN_KEY)
if (!token) {
throw new Error('No token was found in keychain')
}
return {
'X-Token': token,
}
}
private async getAccountId(parentScriptName?: string) {
const parentScript = parentScriptName ?? this.parentScriptName
const settings = await this.settings
let accountId = parentScript && settings ? settings[parentScript]?.accountId : undefined
if (!accountId) {
accountId = await this.setupAccount()
}
if (!accountId) {
throw new Error('Account ID is required')
}
return accountId
}
private fetchData<R>(path: string): Promise<R> {
if (this.isTokenProvided()) {
const url = `${MONOBANK_API_URL}${path}`
return fetchJson<R>(url, this.requestHeaders)
}
throw new Error('No token provided')
}
private isTokenProvided(): boolean {
return Keychain.contains(MONOBANK_TOKEN_KEY)
}
private async getAccountCacheKey(parentScriptName?: string) {
const accountId = await this.getAccountId(parentScriptName)
return `mono-${accountId}.json`
}
async setupToken() {
if (this.isTokenProvided()) {
const tokenExistsAlert = createAlert('Monobank api token', 'Token already exists in your Keychain. Do you wish to change it?', 'Cancel')
tokenExistsAlert.addAction('Change token')
tokenExistsAlert.addDestructiveAction('Delete token')
const actionIndex = await tokenExistsAlert.presentSheet()
switch (actionIndex) {
case -1:
return
case 1: {
Keychain.remove(MONOBANK_TOKEN_KEY)
await createAlert('Token removed', '', 'Ok').present()
return
}
}
}
const setupAlert = createAlert(
'Monobank API token is required',
`In order to fetch data from Monobank you need to provide your generated API token.
If you don't have one - please visit https://api.monobank.ua and follow the instructions.`,
'Cancel')
setupAlert.addAction('I have token')
setupAlert.addAction('https://api.monobank.ua')
const setupActionIndex = await setupAlert.presentSheet()
const presentInputAlert = async () => {
const inputAlert = createAlert('Insert token', 'Your token will be saved in Keychain', 'Cancel')
inputAlert.addAction('Save')
inputAlert.addSecureTextField('token:')
const inputActionIndex = await inputAlert.present()
const tokenValue = inputAlert.textFieldValue(0)
if (inputActionIndex === -1) {
return
}
if (tokenValue) {
Keychain.set(MONOBANK_TOKEN_KEY, tokenValue)
await createAlert('Token saved', '', 'Ok').present()
} else {
await createAlert('Invalid token value', '', 'Ok').present()
}
}
switch (setupActionIndex) {
case 0: {
await presentInputAlert()
break
}
case 1:
await Safari.openInApp('https://api.monobank.ua')
await presentInputAlert()
break
}
}
async setupFilters(parentScriptName?: string) {
const scriptName = parentScriptName ?? this.parentScriptName
if (!scriptName) {
return
}
const settings = await this.settings ?? {}
const setupFiltersAlert = createAlert(
'Set statement items filter',
'Enter semicolon separated values, e.g. "rent;netflix;spotify". Items with one of those words in description will be excluded from accounting',
'Cancel',
)
setupFiltersAlert.addAction('Save')
const filter = settings ? settings[scriptName]?.filter : undefined
setupFiltersAlert.addTextField('filter:', filter ?? '')
const actionIndex = await setupFiltersAlert.present()
if (actionIndex === -1) {
return
}
const nextFilter = setupFiltersAlert.textFieldValue(0)
settings[scriptName] = settings[scriptName] ?? {}
settings[scriptName].filter = nextFilter
await this.writeSettings(settings)
await createAlert('Filters saved', '', 'Ok').present()
}
async setupAccount(parentScriptName?: string) {
const scriptName = parentScriptName ?? this.parentScriptName
const setupAccountsAlert = createAlert(
'Select account',
'ID of selected account will be copied to clipboard and used for this script.',
'Cancel',
)
const accounts = await this.fetchAccounts()
accounts.forEach((item) => {
const { name, type } = item
setupAccountsAlert.addAction(`[${type}] ****${name}`)
})
const actionIndex = await setupAccountsAlert.presentSheet()
if (actionIndex === -1) {
return
}
const id = accounts[actionIndex].id
Pasteboard.copy(id)
if (scriptName) {
await this.writeSettings({ [scriptName]: { accountId: id } })
}
return id
}
async fetchAccounts() {
const response: PersonalInfoT = await this.fetchData<PersonalInfoT>(API_PATH.PERSONAL_INFO)
const accounts = response.accounts.map((item) => {
const name = (item.maskedPan[0] ?? item.iban).slice(-ACCOUNT_MASK_LEN)
return {
id: item.id,
type: item.type,
name,
}
})
return accounts
}
async fetchAccountStatement(fromArg: Date, toArg?: Date): Promise<StatementItemT[]> {
const from = Math.round(Number(fromArg) / 1e3)
const to = Math.round(Number(toArg ?? new Date()) / 1e3)
const accountId = await this.getAccountId()
const url = `${API_PATH.STATEMENT}/${accountId}/${from}/${to}`
return this.fetchData(url)
}
fetchTodaysStatement() {
const to = new Date()
const from = new Date()
from.setHours(0, 0, 0, 0)
return this.fetchAccountStatement(from, to)
}
async fetchLatestStatement(): Promise<StatementCacheT> {
const accountCacheKey = await this.getAccountCacheKey()
const { currentPeriod, history = {} }: StatementCacheT = await cache.read(accountCacheKey) ?? {}
if (!currentPeriod?.lastUpdatedAt) {
return this.fetchInitialPeriod()
}
const { lastUpdatedAt, lastOperationId } = currentPeriod
const to = new Date()
const response = await this.fetchAccountStatement(new Date(lastUpdatedAt), to)
if (!response.length || (response[0].id === lastOperationId)) {
logger.log('nothing to process, skipping...')
return { currentPeriod, history }
}
const filter = await this.getStatementItemsFilter()
for (let index = response.length - 1; index >= 0; index--) {
const item = response[index]
if (filter(item)) {
const { id, time, amount, description, mcc, cashbackAmount } = item
const opDate = new Date(item.time * 1e3)
const month = opDate.getMonth() + 1
const year = opDate.getFullYear()
const date = opDate.getDate()
if (month > currentPeriod.month) {
history[currentPeriod.year] = history[currentPeriod.year] ?? {}
history[currentPeriod.year][currentPeriod.month] = currentPeriod.total
currentPeriod.year = year
currentPeriod.month = month
currentPeriod.total = 0
currentPeriod.statement = [null]
} else if (month < currentPeriod.month) {
continue
}
currentPeriod.statement[date] = currentPeriod.statement[date] ?? []
// @ts-ignore
currentPeriod.statement[date].push({ id, time, amount, description, mcc, cashbackAmount })
currentPeriod.total = currentPeriod.total += -1 * amount
}
}
currentPeriod.lastUpdatedAt = Number(to)
currentPeriod.lastOperationId = response[0].id
cache.write(accountCacheKey, { currentPeriod, history })
return { currentPeriod, history }
}
async fetchInitialPeriod() {
logger.log('fetching initial period...')
const to = new Date()
const from = new Date()
const currentMonth = from.getMonth() + 1
const currentYear = from.getFullYear()
from.setDate(1)
from.setHours(0, 0, 0, 0)
const response = await this.fetchAccountStatement(from, to)
const filter = await this.getStatementItemsFilter()
const currentPeriod = response.reduce((acc, item, index) => {
if (filter(item)) {
const { id, time, amount, description, mcc, cashbackAmount } = item
const date = new Date(item.time * 1e3).getDate()
const currentValue = acc.statement[date] ?? []
acc.statement[date] = [{ id, time, amount, description, mcc, cashbackAmount }, ...currentValue]
acc.total += -1 * amount
if (!index) {
acc.lastOperationId = id
}
}
return acc
}, { month: currentMonth, year: currentYear, total: 0, statement: [], lastOperationId: '', lastUpdatedAt: Number(to) } as StatementCacheT['currentPeriod'])
const statement: StatementCacheT = {
history: {},
currentPeriod,
}
const accountCacheKey = await this.getAccountCacheKey()
await cache.write(accountCacheKey, statement)
return statement
}
async editOP({ date, day, amount: editAmount }: {date: number, day: number, amount: number | null}, parentScriptName?: string) {
const accountCacheKey = await this.getAccountCacheKey(parentScriptName)
const statement: StatementCacheT = await cache.read(accountCacheKey)
const item = statement.currentPeriod.statement?.[date]?.[day]
if (item == null) {
throw new Error(`OP not found: ${date}-${day}`)
}
if (editAmount === null) {
item.amount = item.originalAmount ?? item.amount
item.originalAmount = undefined
} else {
item.originalAmount = item.amount
item.amount = item.amount + editAmount
}
const nextTotal = statement.currentPeriod.statement.reduce((total, day) => {
return day ? total + day.reduce((acc, item) => item ? -1 * item.amount + acc : acc, 0) : total
}, 0)
statement.currentPeriod.total = nextTotal
await cache.write(accountCacheKey, statement)
await createAlert('Successfuly updated', '', 'Ok').present()
}
async editStatementItem(parentScriptName?: string) {
const accountCacheKey = await this.getAccountCacheKey(parentScriptName)
const { currentPeriod: { statement } }: StatementCacheT = await cache.read(accountCacheKey)
const editAlert = createAlert('Select operation to edit', '', 'Cancel')
const actions: {summary: string, dateIndex: number, opIndex: number, originalAmount?: number, amount: number}[] = []
for (let dateIndex = statement.length - 1; dateIndex > 0; dateIndex--) {
const date = statement[dateIndex]
if (date) {
for (let opIndex = date.length - 1; opIndex >= 0; opIndex--) {
const opItem = date[opIndex]
if (!opItem) {
continue
}
const { amount, description, originalAmount } = opItem
const summary = `${amount / 100}${CURRENCIES[980].sign}${originalAmount ? '[E]' : ''}: "${description.replace('\n', '_')}"`
editAlert.addAction(summary)
actions.push({ summary, dateIndex, opIndex, originalAmount, amount })
}
}
}
const opActionIndex = await editAlert.presentSheet()
if (opActionIndex === -1) {
return
}
let editActionIndex = -1
let amount: number | null = 0
const { originalAmount, amount: currentAmount } = actions[opActionIndex]
const valueAlert = createAlert('Edit operation', `Current amount is: ${currentAmount / 1e2}.\nEnter either positive or negative value to add:`, 'Cancel')
valueAlert.addTextField('Amount:')
valueAlert.addAction('Submit')
if (originalAmount) {
valueAlert.addDestructiveAction('Restore this operation')
} else {
valueAlert.addDestructiveAction('Delete this operation')
}
const valueActionIndex = await valueAlert.present()
editActionIndex = valueActionIndex
switch (valueActionIndex) {
case -1: break
case 0: amount = Number(valueAlert.textFieldValue(0)) * 1e2; break
case 1: amount = originalAmount ? null : -1 * currentAmount; break
}
if (editActionIndex === -1) {
return
}
const { dateIndex, opIndex } = actions[opActionIndex]
const actionSummary = amount === null ? `restored to ${(originalAmount ?? 0) / 1e2}` : `edited for ${amount / 1e2}.\nNew value: ${currentAmount / 1e2 + amount / 1e2}`
const confirmationAlert = createAlert('Are you sure?', `${actions[opActionIndex].summary} will be ${actionSummary}.`, 'Cancel')
confirmationAlert.addDestructiveAction('Submit')
const confirmationIndex = await confirmationAlert.presentSheet()
if (confirmationIndex === -1) {
return
}
await this.editOP({ date: dateIndex, day: opIndex, amount }, parentScriptName)
}
async setup() {
if (!this.parentScriptName) {
return
}
if (!this.isTokenProvided()) {
await this.setupToken()
}
const settings = await this.readSettings() ?? {}
const scriptSettings = settings[this.parentScriptName]
if (!scriptSettings || !scriptSettings.accountId) {
await this.setupAccount()
await this.setupFilters()
}
}
async pickScript() {
if (this.parentScriptName) {
return
}
const settngs = await this.readSettings()
const keys = Object.keys(settngs ?? {})
const selectScriptAlert = createAlert('Select script to modify', '', 'Cancel')
if (!keys.length) {
return
}
keys.forEach(item => {
selectScriptAlert.addAction(item)
})
const keyIndex = await selectScriptAlert.presentSheet()
return keys[keyIndex]
}
async showSettings() {
const settingsAlert = createAlert('Monobank settngs', '', 'Cancel')
settingsAlert.addAction('Manage auth token')
settingsAlert.addAction('Change account number')
settingsAlert.addAction('Set filters')
settingsAlert.addAction('Edit statement')
const actionIndex = await settingsAlert.present()
switch (actionIndex) {
case -1: break
case 0: await this.setupToken(); break
case 1: {
const scriptName = await this.pickScript()
if (scriptName) {
await this.setupAccount(scriptName)
}
break
}
case 2: {
const scriptName = await this.pickScript()
if (scriptName) {
await this.setupFilters(scriptName)
}
break
}
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
case 3: {
const scriptName = await this.pickScript()
if (scriptName) {
await this.editStatementItem(scriptName)
}
break
}
}
}
}
if (config.runsInApp && Script.name() === SCRIPT_NAME) {
const monobank = new Monobank()
monobank.showSettings()
}