-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathValidation.gs
400 lines (391 loc) · 20.9 KB
/
Validation.gs
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
/**
* Retrieves and validates the ledger records.
* Uses the error handler to handle any ValidatioError.
* Displays toast on success.
*/
CryptoTracker.prototype.validateLedger = function () {
try {
let ledgerRecords = this.getLedgerRecords();
this.validateLedgerRecords(ledgerRecords);
}
catch (error) {
if (error instanceof ValidationError) {
this.handleError('validation', error.message, error.rowIndex, error.columnName);
return;
}
else {
throw error;
}
}
SpreadsheetApp.getActive().toast('All looks good', 'Ledger Valid', 10);
};
/**
* Validates a set of ledger records and throws a ValidationError on failure.
* Stops reading if it encounters the stop action.
* @param {LedgerRecord[]} ledgerRecords - The colection of ledger records to validate.
*/
CryptoTracker.prototype.validateLedgerRecords = function (ledgerRecords) {
if (LedgerRecord.inReverseOrder(ledgerRecords)) {
ledgerRecords = ledgerRecords.slice().reverse();
let previousRecord;
let rowIndex = this.ledgerHeaderRows + ledgerRecords.length;
for (let ledgerRecord of ledgerRecords) {
if (ledgerRecord.action === 'Stop') {
break;
}
this.validateLedgerRecord(ledgerRecord, previousRecord, rowIndex--);
previousRecord = ledgerRecord;
}
}
else {
let previousRecord;
let rowIndex = this.ledgerHeaderRows + 1;
for (let ledgerRecord of ledgerRecords) {
if (ledgerRecord.action === 'Stop') {
break;
}
this.validateLedgerRecord(ledgerRecord, previousRecord, rowIndex++);
previousRecord = ledgerRecord;
}
}
};
/**
* Validates a ledger record and throws a ValidationError on failure.
* @param {LedgerRecord} ledgerRecord - The ledger record to validate.
* @param {LedgerRecord} previousRecord - The previous ledger record validated.
* @param {number} rowIndex - The index of the row in the ledger sheet used to set the current cell in case of an error.
*/
CryptoTracker.prototype.validateLedgerRecord = function (ledgerRecord, previousRecord, rowIndex) {
let date = ledgerRecord.date;
let action = ledgerRecord.action;
let debitCurrency = ledgerRecord.debitCurrency;
let debitExRate = ledgerRecord.debitExRate;
let debitAmount = ledgerRecord.debitAmount;
let debitFee = ledgerRecord.debitFee;
let debitWalletName = ledgerRecord.debitWalletName;
let creditCurrency = ledgerRecord.creditCurrency;
let creditExRate = ledgerRecord.creditExRate;
let creditAmount = ledgerRecord.creditAmount;
let creditFee = ledgerRecord.creditFee;
let creditWalletName = ledgerRecord.creditWalletName;
let lotMatching = ledgerRecord.lotMatching;
if (isNaN(date)) {
throw new ValidationError(`${action} row ${rowIndex}: Invalid date.`, rowIndex, 'date');
}
else if (previousRecord && date < previousRecord.date) {
throw new ValidationError(`${action} row ${rowIndex}: Dates must be in chronological or reverse chronological order.`, rowIndex, 'date');
}
else if (date > new Date()) {
throw new ValidationError(`${action} row ${rowIndex}: Date must be in the past.`, rowIndex, 'date');
}
else if (action === '') {
throw new ValidationError(`Ledger row ${rowIndex}: No action specified.`, rowIndex, 'action');
}
else if (debitCurrency && !Currency.isFiat(debitCurrency) && !Currency.isCrypto(debitCurrency)) {
throw new ValidationError(`${action} row ${rowIndex}: Debit currency (${debitCurrency}) is not recognized - neither fiat (${Currency.validFiats.join(', ')}) nor crypto (1-9 characters [A-Za-z0-9_$@]).`, rowIndex, 'debitCurrency');
}
else if (isNaN(debitExRate)) {
throw new ValidationError(`${action} row ${rowIndex}: Debit exchange rate is not valid (number or blank).`, rowIndex, 'debitExRate');
}
else if (isNaN(debitAmount)) {
throw new ValidationError(`${action} row ${rowIndex}: Debit amount is not valid (number or blank).`, rowIndex, 'debitAmount');
}
else if (isNaN(debitFee)) {
throw new ValidationError(`${action} row ${rowIndex}: Debit fee is not valid (number or blank).`, rowIndex, 'debitFee');
}
else if (creditCurrency && !Currency.isFiat(creditCurrency) && !Currency.isCrypto(creditCurrency)) {
throw new ValidationError(`${action} row ${rowIndex}: Credit currency (${creditCurrency}) is not recognized - neither fiat (${Currency.validFiats.join(', ')}) nor crypto (1-9 characters [A-Za-z0-9_$@]).`, rowIndex, 'creditCurrency');
}
else if (isNaN(creditExRate)) {
throw new ValidationError(`${action} row ${rowIndex}: Credit exchange rate is not valid (number or blank).`, rowIndex, 'creditExRate');
}
else if (isNaN(creditAmount)) {
throw new ValidationError(`${action} row ${rowIndex}: Credit amount is not valid (number or blank).`, rowIndex, 'creditAmount');
}
else if (isNaN(creditFee)) {
throw new ValidationError(`${action} row ${rowIndex}: Credit fee is not valid (number or blank).`, rowIndex, 'creditFee');
}
else if (lotMatching && !CryptoTracker.lotMatchings.includes(lotMatching)) {
throw new ValidationError(`${action} row ${rowIndex}: Lot matching (${lotMatching}) is not valid (${CryptoTracker.lotMatchings.join(', ')}) or blank.`, rowIndex, 'lotMatching');
}
else if (action === 'Transfer') { //Transfer
if (!debitCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: No debit currency specified.`, rowIndex, 'debitCurrency');
}
else if (debitExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit exchange rate blank.`, rowIndex, 'debitExRate');
}
else if (debitAmount === '') {
throw new ValidationError(`${action} row ${rowIndex}: No debit amount specified.`, rowIndex, 'debitAmount');
}
else if (debitAmount <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit amount must be greater than 0.`, rowIndex, 'debitAmount');
}
else if (debitFee < 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit fee must be greater or equal to 0 (or blank).`, rowIndex, 'debitFee');
}
else if (creditCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit currency (${creditCurrency}) blank. It is inferred from the debit currency (${debitCurrency}).`, rowIndex, 'creditCurrency');
}
else if (creditExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit exchange rate blank.`, rowIndex, 'creditExRate');
}
else if (creditAmount !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit amount blank. It is inferred from the debit amount and debit fee.`, rowIndex, 'creditAmount');
}
else if (creditFee !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit fee blank.`, rowIndex, 'creditFee');
}
else if (!debitWalletName && !creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No debit or credit wallet specified.`, rowIndex, 'debitWalletName');
}
else if (Currency.isFiat(debitCurrency)) { //Fiat transfer
if (debitWalletName && creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: For fiat transfers, leave debit wallet (${debitWalletName}) blank for deposits or credit wallet (${creditWalletName}) blank for withdrawals.`, rowIndex, 'debitWalletName');
}
}
else if (Currency.isCrypto(debitCurrency)) { //Crypto transfer
if (!debitWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No debit wallet specified.`, rowIndex, 'debitWalletName');
}
else if (!creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No credit wallet specified.`, rowIndex, 'creditWalletName');
}
else if (debitWalletName === creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: Debit wallet (${debitWalletName}) and credit wallet (${creditWalletName}) must be different.`, rowIndex, 'debitWalletName');
}
}
}
else if (action === 'Trade') { //Trade
if (!debitCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: No debit currency specified.`, rowIndex, 'debitCurrency');
}
else if (!creditCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: No credit currency specified.`, rowIndex, 'creditCurrency');
}
else if (debitCurrency === creditCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: Debit currency (${debitCurrency}) and credit currency (${creditCurrency}) must be different.`, rowIndex, 'debitCurrency');
}
else if (Currency.isFiat(debitCurrency) && Currency.isFiat(creditCurrency)) {
throw new ValidationError(`${action} row ${rowIndex}: Both debit currency (${debitCurrency}) and credit currency (${creditCurrency}) are fiat, not supported.`, rowIndex, 'debitCurrency');
}
else if (debitAmount === '') {
throw new ValidationError(`${action} row ${rowIndex}: No debit amount specified.`, rowIndex, 'debitAmount');
}
else if (debitAmount < 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit amount must be greater or equal to 0.`, rowIndex, 'debitAmount');
}
else if (debitFee < 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit fee must be greater or equal to 0 (or blank).`, rowIndex, 'debitFee');
}
else if (!debitWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No debit wallet specified.`, rowIndex, 'debitWalletName');
}
else if (creditAmount === '') {
throw new ValidationError(`${action} row ${rowIndex}: No credit amount specified.`, rowIndex, 'creditAmount');
}
else if (creditAmount < 0) {
throw new ValidationError(`${action} row ${rowIndex}: Credit amount must be greater or equal to 0.`, rowIndex, 'creditAmount');
}
else if (creditFee < 0) {
throw new ValidationError(`${action} row ${rowIndex}: Credit fee must be greater or equal to 0 (or blank).`, rowIndex, 'creditFee');
}
else if (Currency.isCrypto(creditCurrency) && creditFee >= creditAmount) {
throw new ValidationError(`${action} row ${rowIndex}: Crypto credit fee must be less than the credit amount (or blank).`, rowIndex, 'creditFee');
}
else if (creditFee > creditAmount) {
throw new ValidationError(`${action} row ${rowIndex}: Fiat credit fee must be less than or equal to credit amount (or blank).`, rowIndex, 'creditFee');
}
else if (creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit wallet (${creditWalletName}) blank. It is inferred from the debit wallet (${debitWalletName}).`, rowIndex, 'creditWalletName');
}
else if (debitCurrency === this.accountingCurrency) { //Base currency buy trade
if (debitExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Debit currency is the accounting currency (${this.accountingCurrency}). Leave debit exchange rate blank.`, rowIndex, 'debitExRate');
}
if (creditExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Debit currency is the accounting currency (${this.accountingCurrency}). Leave credit exchange rate blank.`, rowIndex, 'creditExRate');
}
}
else if (creditCurrency === this.accountingCurrency) { //Base currency sell trade
if (debitExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Credit currency is the accounting currency (${this.accountingCurrency}). Leave debit exchange rate blank.`, rowIndex, 'debitExRate');
}
if (creditExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Credit currency is the accounting currency (${this.accountingCurrency}). Leave credit exchange rate blank.`, rowIndex, 'creditExRate');
}
}
else { //Non base currency trade
if(debitExRate === '' && creditExRate === '') {
throw new ValidationError(`${action} row ${rowIndex}: Non accounting currency trade requires debit currency (${debitCurrency}) and/or credit currency (${creditCurrency}) to accounting currency (${this.accountingCurrency}) exchange rate.`, rowIndex, 'debitExRate');
}
else if (debitExRate && debitExRate <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit exchange rate must be greater than 0.`, rowIndex, 'debitExRate');
}
else if (creditExRate && creditExRate <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Credit exchange rate must be greater than 0.`, rowIndex, 'creditExRate');
}
}
}
else if (action === 'Income') { //Income
if (debitCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit currency (${debitCurrency}) blank.`, rowIndex, 'debitCurrency');
}
else if (debitExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit exchange rate blank.`, rowIndex, 'debitExRate');
}
else if (debitAmount !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit amount blank.`, rowIndex, 'debitAmount');
}
else if (debitFee !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit fee blank.`, rowIndex, 'debitFee');
}
else if (debitWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit wallet (${debitWalletName}) blank.`, rowIndex, 'debitWalletName');
}
else if (!creditCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: No credit currency specified.`, rowIndex, 'creditCurrency');
}
else if (Currency.isFiat(creditCurrency)) {
throw new ValidationError(`${action} row ${rowIndex}: Credit currency (${creditCurrency}) is fiat, not supported.`, rowIndex, 'creditCurrency');
}
else if (creditExRate === '') {
throw new ValidationError(`${action} row ${rowIndex}: Missing credit currency (${creditCurrency}) to accounting currency (${this.accountingCurrency}) exchange rate.`, rowIndex, 'creditExRate');
}
else if (creditExRate <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Credit exchange rate must be greater than 0.`, rowIndex, 'creditExRate');
}
else if (creditAmount === '') {
throw new ValidationError(`${action} row ${rowIndex}: No credit amount specified.`, rowIndex, 'creditAmount');
}
else if (creditAmount <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Credit amount must be greater than 0.`, rowIndex, 'creditAmount');
}
else if (creditFee !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit fee blank.`, rowIndex, 'creditFee');
}
else if (!creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No credit wallet specified.`, rowIndex, 'creditWalletName');
}
}
else if (action === 'Donation') { //Donation
if (!debitCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: No debit currency specified.`, rowIndex, 'debitCurrency');
}
else if (Currency.isFiat(debitCurrency)) {
throw new ValidationError(`${action} row ${rowIndex}: Debit currency (${debitCurrency}) is fiat, not supported.`, rowIndex, 'debitCurrency');
}
else if (debitExRate === '') {
throw new ValidationError(`${action} row ${rowIndex}: Missing debit currency (${debitCurrency}) to accounting currency (${this.accountingCurrency}) exchange rate.`, rowIndex, 'debitExRate');
}
else if (debitExRate <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit exchange rate must be greater than 0.`, rowIndex, 'debitExRate');
}
else if (debitAmount === '') {
throw new ValidationError(`${action} row ${rowIndex}: No debit amount specified.`, rowIndex, 'debitAmount');
}
else if (debitAmount <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit amount must be greater than 0.`, rowIndex, 'debitAmount');
}
else if (debitFee < 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit fee must be greater or equal to 0 (or blank).`, rowIndex, 'debitFee');
}
else if (!debitWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No debit wallet specified.`, rowIndex, 'debitWalletName');
}
else if (creditCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit currency (${creditCurrency}) blank.`, rowIndex, 'creditCurrency');
}
else if (creditExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit exchange rate blank.`, rowIndex, 'creditExRate');
}
else if (creditAmount !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit amount blank.`, rowIndex, 'creditAmount');
}
else if (creditFee !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit fee blank.`, rowIndex, 'creditFee');
}
else if (creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit wallet (${creditWalletName}) blank.`, rowIndex, 'creditWalletName');
}
}
else if (action === 'Gift') { //Gift
if (!debitCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: No debit currency specified.`, rowIndex, 'debitCurrency');
}
else if (Currency.isFiat(debitCurrency)) {
throw new ValidationError(`${action} row ${rowIndex}: Debit currency (${debitCurrency}) is fiat, not supported.`, rowIndex, 'debitCurrency');
}
else if (debitExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit exchange rate blank.`, rowIndex, 'debitExRate');
}
else if (debitAmount === '') {
throw new ValidationError(`${action} row ${rowIndex}: No debit amount specified.`, rowIndex, 'debitAmount');
}
else if (debitAmount <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit amount must be greater than 0.`, rowIndex, 'debitAmount');
}
else if (debitFee < 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit fee must be greater or equal to 0 (or blank).`, rowIndex, 'debitFee');
}
else if (!debitWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No debit wallet specified.`, rowIndex, 'debitWalletName');
}
else if (creditCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit currency (${creditCurrency}) blank.`, rowIndex, 'creditCurrency');
}
else if (creditExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit exchange rate blank.`, rowIndex, 'creditExRate');
}
else if (creditAmount !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit amount blank.`, rowIndex, 'creditAmount');
}
else if (creditFee !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit fee blank.`, rowIndex, 'creditFee');
}
else if (creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit wallet (${creditWalletName}) blank.`, rowIndex, 'creditWalletName');
}
}
else if (action === 'Fee') { //Fee
if (!debitCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: No debit currency specified.`, rowIndex, 'debitCurrency');
}
else if (Currency.isFiat(debitCurrency)) {
throw new ValidationError(`${action} row ${rowIndex}: Debit currency (${debitCurrency}) is fiat, not supported.`, rowIndex, 'debitCurrency');
}
else if (debitExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit exchange rate blank.`, rowIndex, 'debitExRate');
}
else if (debitAmount !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave debit amount blank.`, rowIndex, 'debitAmount');
}
else if (debitFee === '') {
throw new ValidationError(`${action} row ${rowIndex}: No debit fee specified.`, rowIndex, 'debitFee');
}
else if (debitFee <= 0) {
throw new ValidationError(`${action} row ${rowIndex}: Debit fee must be greater than 0.`, rowIndex, 'debitFee');
}
else if (!debitWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: No debit wallet specified.`, rowIndex, 'debitWalletName');
}
else if (creditCurrency) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit currency (${creditCurrency}) blank.`, rowIndex, 'creditCurrency');
}
else if (creditExRate !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit exchange rate blank.`, rowIndex, 'creditExRate');
}
else if (creditAmount !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit amount blank.`, rowIndex, 'creditAmount');
}
else if (creditFee !== '') {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit fee blank.`, rowIndex, 'creditFee');
}
else if (creditWalletName) {
throw new ValidationError(`${action} row ${rowIndex}: Leave credit wallet (${creditWalletName}) blank.`, rowIndex, 'creditWalletName');
}
}
else {
throw new ValidationError(`Ledger row ${rowIndex}: Action (${action}) is invalid.`, rowIndex, 'action');
}
};