@tamu-dor/famis-db 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +715 -598
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,27 +1,34 @@
1
- const knex = require('knex');
2
- const { createId } = require('@paralleldrive/cuid2');
3
- const { each, map } = require('awaity');
4
- const revisionHash = require('rev-hash');
5
-
1
+ const knex = require("knex");
2
+ const { createId } = require("@paralleldrive/cuid2");
3
+ const { each, map } = require("awaity");
4
+ const revisionHash = require("rev-hash");
5
+ const customerAccountPrefixes = ["68", "RC", "56"];
6
6
  const famisInvoiceNumbers = [
7
7
  {
8
8
  start: 300128,
9
9
  end: 399999,
10
- prefix: 'I',
11
- aliases: [
12
- 'ilabs',
13
- ]
10
+ prefix: "I",
11
+ aliases: ["ilabs"],
14
12
  },
15
13
  {
16
14
  start: 100000,
17
15
  end: 999999,
18
- prefix: 'V',
19
- aliases: [
20
- 'mosaic',
21
- ]
16
+ prefix: "V",
17
+ aliases: ["mosaic"],
22
18
  },
23
19
  ];
24
- const returningColumns = ['uid', 'number', 'lab', 'totalAmount', 'data', 'date', 'type', 'famisInvoiceNumber', 'customerAccount', 'sellingAccount'];
20
+ const returningColumns = [
21
+ "uid",
22
+ "number",
23
+ "lab",
24
+ "totalAmount",
25
+ "data",
26
+ "date",
27
+ "type",
28
+ "famisInvoiceNumber",
29
+ "customerAccount",
30
+ "sellingAccount",
31
+ ];
25
32
 
26
33
  const pad = (str, max, right) => {
27
34
  let retVal = str;
@@ -38,15 +45,16 @@ const pad = (str, max, right) => {
38
45
  };
39
46
 
40
47
  const sanitizeAccountNumber = (account) => {
41
- if (account.substring(0,2) === 'RC') {
42
- return account.replace(/\W/g, '');
48
+ if (account.substring(0, 2) === "RC") {
49
+ return account.replace(/\W/g, "");
43
50
  } else {
44
- return pad(account.replace(/\D/g,''), 13, true);
51
+ return pad(account.replace(/\D/g, ""), 13, true);
45
52
  }
46
53
  };
47
54
 
48
55
  const getCoreAccountNumber = (famis) => {
49
- const { sellingCampusCode, sellingAccountNumber, sellingSupportAccount } = famis;
56
+ const { sellingCampusCode, sellingAccountNumber, sellingSupportAccount } =
57
+ famis;
50
58
  const account = `${sellingCampusCode}${sellingAccountNumber}${sellingSupportAccount}`;
51
59
  return sanitizeAccountNumber(account);
52
60
  };
@@ -55,23 +63,22 @@ const sortObjectKeys = (obj) => {
55
63
  if (obj === null || obj === undefined) {
56
64
  return obj;
57
65
  }
58
- if (typeof obj !== 'object') {
66
+ if (typeof obj !== "object") {
59
67
  // it is a primitive: number/string (in an array)
60
68
  return obj;
61
69
  }
62
- const result = Object.keys(obj).sort().reduce(
63
- (acc,key) => {
70
+ const result = Object.keys(obj)
71
+ .sort()
72
+ .reduce((acc, key) => {
64
73
  if (Array.isArray(obj[key])) {
65
74
  acc[key] = obj[key].map(sortObjectKeys);
66
- } else if (typeof obj[key] === 'object') {
75
+ } else if (typeof obj[key] === "object") {
67
76
  acc[key] = sortObjectKeys(obj[key]);
68
77
  } else {
69
78
  acc[key] = obj[key];
70
79
  }
71
80
  return acc;
72
- },
73
- {},
74
- );
81
+ }, {});
75
82
  return result;
76
83
  };
77
84
 
@@ -84,7 +91,7 @@ class FAMISDb {
84
91
 
85
92
  init(config, people) {
86
93
  this.db = knex({
87
- client: 'pg',
94
+ client: "pg",
88
95
  connection: config,
89
96
  pool: {
90
97
  min: 0,
@@ -113,14 +120,29 @@ class FAMISDb {
113
120
  poNumber: null,
114
121
  };
115
122
 
116
- if (famis.customerAccountNumber || (replace_data && replace_data.customerAccountNumber)) {
117
- retVal.accountNumber = replace_data && replace_data.customerAccountNumber ? replace_data.customerAccountNumber : famis.customerAccountNumber;
118
- } else if (famis.customerPONumber.includes('|') || (replace_data && replace_data.customerPONumber.includes('|'))) {
119
- const result = replace_data && replace_data.customerPONumber.includes('|') ? replace_data.customerPONumber.split('|') : famis.customerPONumber.split('|');
123
+ if (
124
+ famis.customerAccountNumber ||
125
+ (replace_data && replace_data.customerAccountNumber)
126
+ ) {
127
+ retVal.accountNumber =
128
+ replace_data && replace_data.customerAccountNumber
129
+ ? replace_data.customerAccountNumber
130
+ : famis.customerAccountNumber;
131
+ } else if (
132
+ famis.customerPONumber.includes("|") ||
133
+ (replace_data && replace_data.customerPONumber.includes("|"))
134
+ ) {
135
+ const result =
136
+ replace_data && replace_data.customerPONumber.includes("|")
137
+ ? replace_data.customerPONumber.split("|")
138
+ : famis.customerPONumber.split("|");
120
139
  retVal.accountNumber = sanitizeAccountNumber(result[0]);
121
140
  retVal.poNumber = result[1].trim();
122
141
  } else {
123
- retVal.accountNumber = replace_data && replace_data.customerPONumber ? sanitizeAccountNumber(replace_data.customerPONumber) : sanitizeAccountNumber(famis.customerPONumber);
142
+ retVal.accountNumber =
143
+ replace_data && replace_data.customerPONumber
144
+ ? sanitizeAccountNumber(replace_data.customerPONumber)
145
+ : sanitizeAccountNumber(famis.customerPONumber);
124
146
  }
125
147
 
126
148
  return retVal;
@@ -134,40 +156,70 @@ class FAMISDb {
134
156
  * @returns {Promise<object>} invoice object
135
157
  * @memberof FAMISDb
136
158
  */
137
- async processInvoice(invoice, historical=false) {
159
+ async processInvoice(invoice, historical = false) {
138
160
  // console.log(invoice);
139
161
  try {
140
162
  const account = getCoreAccountNumber(invoice.data.details[0].famis);
141
163
  invoice.core = await this.getCoreByAccount(account);
142
164
  if (invoice.core) {
143
- invoice.core.systemPart = await this.getSystemById(invoice.core.systemId);
165
+ invoice.core.systemPart = await this.getSystemById(
166
+ invoice.core.systemId,
167
+ );
144
168
  } else {
145
- const e = Error(`Revenue account ${account} is not associated with a core.`)
146
- throw(e);
169
+ const e = Error(
170
+ `Revenue account ${account} is not associated with a core.`,
171
+ );
172
+ throw e;
147
173
  }
148
- const campus = account.substring(0,2);
149
- const revenuAccount = await this.getByAccountNumber(account, campus, historical);
150
- const customerAccountNumber = this.getAccountNumber(invoice.data.details[0].famis, invoice.replace_data);
151
- invoice.customerAccountInfo = await this.getByAccountNumber(customerAccountNumber.accountNumber, campus, historical);
174
+ const campus = account.substring(0, 2);
175
+ const revenuAccount = await this.getByAccountNumber(
176
+ account,
177
+ campus,
178
+ historical,
179
+ );
180
+ const customerAccountNumber = this.getAccountNumber(
181
+ invoice.data.details[0].famis,
182
+ invoice.replace_data,
183
+ );
184
+ invoice.customerAccountInfo = await this.getByAccountNumber(
185
+ customerAccountNumber.accountNumber,
186
+ campus,
187
+ historical,
188
+ );
152
189
  if (invoice.customerAccountInfo) {
153
- let idtAllowed = 'idt_allowed' in invoice.customerAccountInfo && invoice.customerAccountInfo.idt_allowed !== null ?
154
- invoice.customerAccountInfo.idt_allowed :
155
- 'parent_account_idt_allowed' in invoice.customerAccountInfo && invoice.customerAccountInfo.parent_account_idt_allowed;
156
- idtAllowed = idtAllowed &&
157
- !( invoice.replace_data &&
158
- typeof invoice.replace_data === 'object' &&
190
+ let idtAllowed =
191
+ "idt_allowed" in invoice.customerAccountInfo &&
192
+ invoice.customerAccountInfo.idt_allowed !== null
193
+ ? invoice.customerAccountInfo.idt_allowed
194
+ : "parent_account_idt_allowed" in invoice.customerAccountInfo &&
195
+ invoice.customerAccountInfo.parent_account_idt_allowed;
196
+ idtAllowed =
197
+ idtAllowed &&
198
+ !(
199
+ invoice.replace_data &&
200
+ typeof invoice.replace_data === "object" &&
159
201
  invoice.replace_data.forcedAr
160
202
  );
161
- const isIdt = customerAccountNumber && (customerAccountNumber.accountNumber.substring(0,2) === account.substring(0,2)) && idtAllowed;
203
+ const isIdt =
204
+ customerAccountNumber &&
205
+ customerAccountNumber.accountNumber.substring(0, 2) ===
206
+ account.substring(0, 2) &&
207
+ idtAllowed;
162
208
  invoice.isIdt = isIdt;
163
209
  } else {
164
210
  invoice.isIdt = false;
165
211
  }
166
- let poNumber = (customerAccountNumber.accountNumber.substring(0,2) !== '68') ? `${campus}-${revenuAccount.department}` : customerAccountNumber.poNumber;
212
+ let poNumber = customerAccountPrefixes.includes(
213
+ customerAccountNumber.accountNumber.substring(0, 2),
214
+ )
215
+ ? customerAccountNumber.poNumber
216
+ : `${campus}-${revenuAccount.department}`;
167
217
  invoice.poNumber = poNumber;
168
218
  invoice.customerAccount = customerAccountNumber.accountNumber;
169
219
  if (invoice.customerAccountInfo && this.people) {
170
- const pi = invoice.customerAccountInfo.uin ? await this.people.getByUin(invoice.customerAccountInfo.uin) : [];
220
+ const pi = invoice.customerAccountInfo.uin
221
+ ? await this.people.getByUin(invoice.customerAccountInfo.uin)
222
+ : [];
171
223
  if (pi.length) {
172
224
  invoice.pi = JSON.parse(JSON.stringify(pi[0]));
173
225
  }
@@ -180,7 +232,6 @@ class FAMISDb {
180
232
  return invoice;
181
233
  }
182
234
 
183
-
184
235
  /**
185
236
  *
186
237
  * Get invoice by uid
@@ -193,14 +244,14 @@ class FAMISDb {
193
244
  let retVal;
194
245
 
195
246
  try {
196
- records = await this.db('invoices').where('uid', uid);
247
+ records = await this.db("invoices").where("uid", uid);
197
248
  if (records.length) {
198
249
  retVal = this.processInvoice(records[0]);
199
- }
250
+ }
200
251
  } catch (e) {
201
252
  throw e;
202
253
  }
203
-
254
+
204
255
  return retVal;
205
256
  }
206
257
 
@@ -216,7 +267,7 @@ class FAMISDb {
216
267
  let retVal;
217
268
 
218
269
  try {
219
- records = await this.db('invoices').whereILike('number', `${number}%`);
270
+ records = await this.db("invoices").whereILike("number", `${number}%`);
220
271
  if (records.length) {
221
272
  retVal = this.processInvoice(records[0]);
222
273
  }
@@ -238,10 +289,10 @@ class FAMISDb {
238
289
  let retVal;
239
290
 
240
291
  try {
241
- records = await this.db('invoices').where('fingerprint', fingerPrint);
292
+ records = await this.db("invoices").where("fingerprint", fingerPrint);
242
293
  if (records.length) {
243
294
  retVal = this.processInvoice(records[0]);
244
- }
295
+ }
245
296
  } catch (e) {
246
297
  throw e;
247
298
  }
@@ -261,28 +312,29 @@ class FAMISDb {
261
312
  let retVal;
262
313
 
263
314
  try {
264
- let records = await this.db('invoices')
265
- .whereNull('deleted')
266
- .andWhere(function() {
267
- this.where('customerAccount', accountNumber);
268
- this.orWhere('sellingAccount', accountNumber);
269
- this.orWhereRaw("replace_data->>'customerAccountNumber' = ?",[accountNumber]);
270
- this.orWhereRaw("replace_data->>'customerPONumber' LIKE ?",[`${accountNumber}%`]);
315
+ let records = await this.db("invoices")
316
+ .whereNull("deleted")
317
+ .andWhere(function () {
318
+ this.where("customerAccount", accountNumber);
319
+ this.orWhere("sellingAccount", accountNumber);
320
+ this.orWhereRaw("replace_data->>'customerAccountNumber' = ?", [
321
+ accountNumber,
322
+ ]);
323
+ this.orWhereRaw("replace_data->>'customerPONumber' LIKE ?", [
324
+ `${accountNumber}%`,
325
+ ]);
271
326
  });
272
327
  if (records.length) {
273
- records = await map(
274
- records,
275
- async (record) => {
276
- const rec = self.processInvoice(record);
277
- return rec;
278
- }
279
- );
280
- }
328
+ records = await map(records, async (record) => {
329
+ const rec = self.processInvoice(record);
330
+ return rec;
331
+ });
332
+ }
281
333
  retVal = records;
282
334
  } catch (e) {
283
335
  throw e;
284
336
  }
285
-
337
+
286
338
  return retVal;
287
339
  }
288
340
 
@@ -297,31 +349,34 @@ class FAMISDb {
297
349
  * @returns {Promise<array>} invoice objects
298
350
  * @memberof FAMISDb
299
351
  */
300
- async getAllInvoices(orderBy='date', dir='desc', deleted=false, page=null, limit=null) {
352
+ async getAllInvoices(
353
+ orderBy = "date",
354
+ dir = "desc",
355
+ deleted = false,
356
+ page = null,
357
+ limit = null,
358
+ ) {
301
359
  const self = this;
302
360
  let records;
303
361
  let metadata;
304
362
  try {
305
- const sql = this.db('invoices')
306
- const meta = this.db('invoices');
363
+ const sql = this.db("invoices");
364
+ const meta = this.db("invoices");
307
365
  if (!deleted) {
308
- sql.whereNull('deleted');
309
- meta.whereNull('deleted');
366
+ sql.whereNull("deleted");
367
+ meta.whereNull("deleted");
310
368
  }
311
369
  //console.log(page, limit);
312
370
  if (page && limit) {
313
- sql.limit(limit).offset((page-1)*limit);
371
+ sql.limit(limit).offset((page - 1) * limit);
314
372
  }
315
373
  records = await sql.orderBy(orderBy, dir);
316
374
  metadata = await meta.orderBy(orderBy, dir);
317
375
  //console.log(records);
318
- records = await map(
319
- records,
320
- async (record) => {
321
- const rec = self.processInvoice(record, true);
322
- return rec;
323
- }
324
- );
376
+ records = await map(records, async (record) => {
377
+ const rec = self.processInvoice(record, true);
378
+ return rec;
379
+ });
325
380
  } catch (e) {
326
381
  throw e;
327
382
  }
@@ -330,7 +385,7 @@ class FAMISDb {
330
385
  metadata: {
331
386
  total: metadata.length,
332
387
  },
333
- }
388
+ };
334
389
  }
335
390
 
336
391
  /**
@@ -344,18 +399,15 @@ class FAMISDb {
344
399
  let records;
345
400
 
346
401
  try {
347
- records = await this.db('invoices')
348
- .whereNull('deleted')
349
- .whereIn('date', function() {
350
- this.max('date').from('invoices');
402
+ records = await this.db("invoices")
403
+ .whereNull("deleted")
404
+ .whereIn("date", function () {
405
+ this.max("date").from("invoices");
351
406
  });
352
- records = await map(
353
- records,
354
- async (record) => {
355
- const rec = self.processInvoice(record);
356
- return rec;
357
- }
358
- );
407
+ records = await map(records, async (record) => {
408
+ const rec = self.processInvoice(record);
409
+ return rec;
410
+ });
359
411
  } catch (e) {
360
412
  throw e;
361
413
  }
@@ -365,34 +417,36 @@ class FAMISDb {
365
417
  /**
366
418
  *
367
419
  * Returns all held invoices
368
- * @returns {Promise<array>} held invoice objects
420
+ * @returns {Promise<array>} held invoice objects
369
421
  * @memberof FAMISDb
370
422
  */
371
- async getHeldInvoices(orderBy='date', dir='desc', page=null, limit=null) {
423
+ async getHeldInvoices(
424
+ orderBy = "date",
425
+ dir = "desc",
426
+ page = null,
427
+ limit = null,
428
+ ) {
372
429
  const self = this;
373
430
  let records;
374
431
  let metadata;
375
- const sql = this.db('invoices');
376
- const meta = this.db('invoices');
432
+ const sql = this.db("invoices");
433
+ const meta = this.db("invoices");
377
434
  try {
378
435
  if (page && limit) {
379
- sql.limit(limit).offset((page-1)*limit);
436
+ sql.limit(limit).offset((page - 1) * limit);
380
437
  }
381
438
  records = await sql
382
- .whereNull('deleted')
383
- .andWhere('invoiced', '0')
439
+ .whereNull("deleted")
440
+ .andWhere("invoiced", "0")
384
441
  .orderBy(orderBy, dir);
385
442
  metadata = await meta
386
- .whereNull('deleted')
387
- .andWhere('invoiced', '0')
443
+ .whereNull("deleted")
444
+ .andWhere("invoiced", "0")
388
445
  .orderBy(orderBy, dir);
389
- records = await map(
390
- records,
391
- async (record) => {
392
- const rec = self.processInvoice(record);
393
- return rec;
394
- }
395
- );
446
+ records = await map(records, async (record) => {
447
+ const rec = self.processInvoice(record);
448
+ return rec;
449
+ });
396
450
  } catch (e) {
397
451
  throw e;
398
452
  }
@@ -407,7 +461,7 @@ class FAMISDb {
407
461
  /**
408
462
  *
409
463
  * Returns all awaiting email invoices
410
- * @returns {Promise<array>} awaiting email invoice objects
464
+ * @returns {Promise<array>} awaiting email invoice objects
411
465
  * @memberof FAMISDb
412
466
  */
413
467
  async getInvoicesAwaitingEmail() {
@@ -415,16 +469,13 @@ class FAMISDb {
415
469
  let records;
416
470
 
417
471
  try {
418
- records = await this.db('invoices')
419
- .whereNull('deleted')
420
- .andWhere('emailed', 'False');
421
- records = await map(
422
- records,
423
- async (record) => {
424
- const rec = self.processInvoice(record);
425
- return rec;
426
- }
427
- );
472
+ records = await this.db("invoices")
473
+ .whereNull("deleted")
474
+ .andWhere("emailed", "False");
475
+ records = await map(records, async (record) => {
476
+ const rec = self.processInvoice(record);
477
+ return rec;
478
+ });
428
479
  } catch (e) {
429
480
  throw e;
430
481
  }
@@ -444,18 +495,15 @@ class FAMISDb {
444
495
  let retVal;
445
496
 
446
497
  try {
447
- records = await this.db('invoices')
448
- .whereNull('deleted')
449
- .andWhere('number', 'ilike', `${number}%`);
498
+ records = await this.db("invoices")
499
+ .whereNull("deleted")
500
+ .andWhere("number", "ilike", `${number}%`);
450
501
  if (records.length) {
451
- retVal = await map(
452
- records,
453
- async (record) => {
454
- const rec = self.processInvoice(record);
455
- return rec;
456
- }
457
- );
458
- }
502
+ retVal = await map(records, async (record) => {
503
+ const rec = self.processInvoice(record);
504
+ return rec;
505
+ });
506
+ }
459
507
  } catch (e) {
460
508
  throw e;
461
509
  }
@@ -475,18 +523,15 @@ class FAMISDb {
475
523
  let retVal;
476
524
 
477
525
  try {
478
- records = await this.db('invoices')
479
- .whereNull('deleted')
480
- .andWhere('famisInvoiceNumber', 'ilike', `${famis}%`);
526
+ records = await this.db("invoices")
527
+ .whereNull("deleted")
528
+ .andWhere("famisInvoiceNumber", "ilike", `${famis}%`);
481
529
  if (records.length) {
482
- retVal = await map(
483
- records,
484
- async (record) => {
485
- const rec = self.processInvoice(record);
486
- return rec;
487
- }
488
- );
489
- }
530
+ retVal = await map(records, async (record) => {
531
+ const rec = self.processInvoice(record);
532
+ return rec;
533
+ });
534
+ }
490
535
  } catch (e) {
491
536
  throw e;
492
537
  }
@@ -506,12 +551,15 @@ class FAMISDb {
506
551
  let records;
507
552
 
508
553
  try {
509
- famisInvoiceNumber = (typeof famisInvoiceNumber === 'undefined') ? null : famisInvoiceNumber;
510
- famisInvoiceNumber = famisInvoiceNumber ? famisInvoiceNumber : await this.getNextFAMISInvoiceNumber(type);
511
- invoice.details.forEach(d => {
554
+ famisInvoiceNumber =
555
+ typeof famisInvoiceNumber === "undefined" ? null : famisInvoiceNumber;
556
+ famisInvoiceNumber = famisInvoiceNumber
557
+ ? famisInvoiceNumber
558
+ : await this.getNextFAMISInvoiceNumber(type);
559
+ invoice.details.forEach((d) => {
512
560
  d.famis.invoiceNumber = famisInvoiceNumber;
513
561
  if (!d.other) {
514
- d.other = '0.00';
562
+ d.other = "0.00";
515
563
  }
516
564
  if (!d.comments) {
517
565
  d.comments = null;
@@ -520,7 +568,7 @@ class FAMISDb {
520
568
  d.billingPeriod = null;
521
569
  }
522
570
  });
523
- records = await this.db('invoices')
571
+ records = await this.db("invoices")
524
572
  .returning(returningColumns)
525
573
  .insert({
526
574
  number: invoice.number,
@@ -532,7 +580,8 @@ class FAMISDb {
532
580
  fingerprint: invoice.fingerprint,
533
581
  type: type,
534
582
  famisInvoiceNumber: famisInvoiceNumber,
535
- customerAccount: this.getAccountNumber(invoice.details[0].famis).accountNumber,
583
+ customerAccount: this.getAccountNumber(invoice.details[0].famis)
584
+ .accountNumber,
536
585
  sellingAccount: getCoreAccountNumber(invoice.details[0].famis),
537
586
  });
538
587
  } catch (e) {
@@ -552,9 +601,9 @@ class FAMISDb {
552
601
  */
553
602
  async updateInvoice(uid, invoice) {
554
603
  let records;
555
- invoice.details.forEach(d => {
604
+ invoice.details.forEach((d) => {
556
605
  if (!d.other) {
557
- d.other = '0.00';
606
+ d.other = "0.00";
558
607
  }
559
608
  if (!d.comments) {
560
609
  d.comments = null;
@@ -564,7 +613,7 @@ class FAMISDb {
564
613
  }
565
614
  });
566
615
  try {
567
- records = await this.db('invoices')
616
+ records = await this.db("invoices")
568
617
  .returning(returningColumns)
569
618
  .update({
570
619
  number: invoice.number,
@@ -572,12 +621,13 @@ class FAMISDb {
572
621
  totalAmount: parseFloat(invoice.totalAmount),
573
622
  data: JSON.stringify(invoice),
574
623
  date: invoice.date,
575
- customerAccount: this.getAccountNumber(invoice.details[0].famis).accountNumber,
624
+ customerAccount: this.getAccountNumber(invoice.details[0].famis)
625
+ .accountNumber,
576
626
  sellingAccount: getCoreAccountNumber(invoice.details[0].famis),
577
627
  fingerprint: invoice.fingerprint ? invoice.fingerprint : null,
578
628
  replace_data: invoice.replace_data,
579
629
  })
580
- .where('uid', uid);
630
+ .where("uid", uid);
581
631
  } catch (e) {
582
632
  throw e;
583
633
  }
@@ -586,7 +636,7 @@ class FAMISDb {
586
636
 
587
637
  /**
588
638
  *
589
- * Toggles an invoice as paid
639
+ * Toggles an invoice as paid
590
640
  * @param {string} uid
591
641
  * @param {boolean} invoiced
592
642
  * @returns {Promise<object>} invoice object
@@ -596,12 +646,12 @@ class FAMISDb {
596
646
  let records;
597
647
 
598
648
  try {
599
- records = await this.db('invoices')
649
+ records = await this.db("invoices")
600
650
  .returning(returningColumns)
601
651
  .update({
602
652
  invoiced,
603
653
  })
604
- .where('uid', uid);
654
+ .where("uid", uid);
605
655
  } catch (e) {
606
656
  throw e;
607
657
  }
@@ -610,7 +660,7 @@ class FAMISDb {
610
660
 
611
661
  /**
612
662
  *
613
- * Toggles an invoice as emailed
663
+ * Toggles an invoice as emailed
614
664
  * @param {string} uid
615
665
  * @param {boolean} emailed
616
666
  * @returns {Promise<object>} invoice object
@@ -620,12 +670,12 @@ class FAMISDb {
620
670
  let records;
621
671
 
622
672
  try {
623
- records = await this.db('invoices')
673
+ records = await this.db("invoices")
624
674
  .returning(returningColumns)
625
675
  .update({
626
676
  emailed,
627
677
  })
628
- .where('uid', uid);
678
+ .where("uid", uid);
629
679
  } catch (e) {
630
680
  throw e;
631
681
  }
@@ -644,12 +694,12 @@ class FAMISDb {
644
694
  let records;
645
695
 
646
696
  try {
647
- records = await this.db('invoices')
697
+ records = await this.db("invoices")
648
698
  .returning(returningColumns)
649
699
  .update({
650
700
  deleted,
651
701
  })
652
- .where('uid', uid);
702
+ .where("uid", uid);
653
703
  } catch (e) {
654
704
  throw e;
655
705
  }
@@ -658,7 +708,7 @@ class FAMISDb {
658
708
 
659
709
  /**
660
710
  *
661
- * Sets an invoice's posted date
711
+ * Sets an invoice's posted date
662
712
  * @param {string} uid
663
713
  * @param {date} postedDate
664
714
  * @returns {Promise<object>} invoice object
@@ -668,12 +718,12 @@ class FAMISDb {
668
718
  let records;
669
719
 
670
720
  try {
671
- records = await this.db('invoices')
721
+ records = await this.db("invoices")
672
722
  .returning(returningColumns)
673
723
  .update({
674
724
  postedDate,
675
725
  })
676
- .where('uid', uid);
726
+ .where("uid", uid);
677
727
  } catch (e) {
678
728
  throw e;
679
729
  }
@@ -692,12 +742,12 @@ class FAMISDb {
692
742
  let records;
693
743
 
694
744
  try {
695
- records = await this.db('invoices')
745
+ records = await this.db("invoices")
696
746
  .returning(returningColumns)
697
747
  .update({
698
748
  fingerprint,
699
749
  })
700
- .where('uid', uid);
750
+ .where("uid", uid);
701
751
  } catch (e) {
702
752
  throw e;
703
753
  }
@@ -713,59 +763,60 @@ class FAMISDb {
713
763
  * @returns {Promise<object>} account object
714
764
  * @memberof FAMISDb
715
765
  */
716
- async getByAccountNumber(number, campus, historical=false) {
766
+ async getByAccountNumber(number, campus, historical = false) {
717
767
  let records;
718
768
  let retVal;
719
- let table = historical ? 'accounts_all' : 'accounts'
769
+ let table = historical ? "accounts_all" : "accounts";
720
770
  const self = this;
721
771
 
722
772
  if (!number) {
723
- throw Error('Null account number');
773
+ throw Error("Null account number");
724
774
  }
725
775
  try {
726
776
  records = await this.db(`${table}`)
727
- .leftJoin('departments',
728
- function() {
729
- this
730
- .on(`${table}.department`, '=', 'departments.dept')
731
- .on('departments.part', '=', self.db.raw('?', [number.substring(0,2)]))
732
- }
733
- )
734
- .leftJoin(`${table} as parent_account`,
735
- function() {
736
- this
737
- .on(`${table}.campus_code`, '=', 'parent_account.campus_code')
738
- .on(`${table}.account`, '=', 'parent_account.account')
739
- .on('parent_account.support_account', '=', self.db.raw('?', ["00000"]))
740
- .on(`${table}.fiscal_year`, '=', 'parent_account.fiscal_year')
741
- }
742
- )
777
+ .leftJoin("departments", function () {
778
+ this.on(`${table}.department`, "=", "departments.dept").on(
779
+ "departments.part",
780
+ "=",
781
+ self.db.raw("?", [number.substring(0, 2)]),
782
+ );
783
+ })
784
+ .leftJoin(`${table} as parent_account`, function () {
785
+ this.on(`${table}.campus_code`, "=", "parent_account.campus_code")
786
+ .on(`${table}.account`, "=", "parent_account.account")
787
+ .on(
788
+ "parent_account.support_account",
789
+ "=",
790
+ self.db.raw("?", ["00000"]),
791
+ )
792
+ .on(`${table}.fiscal_year`, "=", "parent_account.fiscal_year");
793
+ })
743
794
  .select(
744
795
  `${table}.*`,
745
- 'departments.idt_email as billingEmailId',
746
- 'parent_account.idt_allowed as parent_account_idt_allowed'
796
+ "departments.idt_email as billingEmailId",
797
+ "parent_account.idt_allowed as parent_account_idt_allowed",
747
798
  )
748
- .whereNull('departments.sub')
749
- .andWhere(function() {
750
- this.whereNull('departments.exp_date').orWhere('departments.exp_date', '>', self.db.raw('CURRENT_DATE'))
799
+ .whereNull("departments.sub")
800
+ .andWhere(function () {
801
+ this.whereNull("departments.exp_date").orWhere(
802
+ "departments.exp_date",
803
+ ">",
804
+ self.db.raw("CURRENT_DATE"),
805
+ );
751
806
  })
752
807
  .andWhere(`${table}.account_number`, number);
753
808
  if (records.length) {
754
809
  retVal = records[0];
755
810
  } else {
756
- records = await this.db('customers')
757
- .where('account_number', number)
758
- .andWhere('campus_code', campus);
811
+ records = await this.db("customers")
812
+ .where("account_number", number)
813
+ .andWhere("campus_code", campus);
759
814
  if (records.length) {
760
- retVal = Object.assign(
761
- {},
762
- records[0],
763
- {
764
- idt_allowed: false,
765
- parent_account_idt_allowed: false,
766
- billingEmailId: records[0].email,
767
- }
768
- );
815
+ retVal = Object.assign({}, records[0], {
816
+ idt_allowed: false,
817
+ parent_account_idt_allowed: false,
818
+ billingEmailId: records[0].email,
819
+ });
769
820
  }
770
821
  }
771
822
  } catch (e) {
@@ -786,30 +837,41 @@ class FAMISDb {
786
837
  const self = this;
787
838
 
788
839
  try {
789
- const pis = await this.db('accounts')
790
- .leftJoin('departments',
791
- function() {
792
- this
793
- .on('accounts.department', '=', 'departments.dept')
794
- .on('departments.part', '=', self.db.raw('?', [number.substring(0,2)]))
795
- }
796
- )
797
- .leftJoin('account_balances',
798
- function() {
799
- this
800
- .on('account_balances.account_number', '=', 'accounts.budget_account_number')
801
- .on('account_balances.fiscal_year', '=', 'accounts.fiscal_year')
802
- }
840
+ const pis = await this.db("accounts")
841
+ .leftJoin("departments", function () {
842
+ this.on("accounts.department", "=", "departments.dept").on(
843
+ "departments.part",
844
+ "=",
845
+ self.db.raw("?", [number.substring(0, 2)]),
846
+ );
847
+ })
848
+ .leftJoin("account_balances", function () {
849
+ this.on(
850
+ "account_balances.account_number",
851
+ "=",
852
+ "accounts.budget_account_number",
853
+ ).on("account_balances.fiscal_year", "=", "accounts.fiscal_year");
854
+ })
855
+ .select(
856
+ "accounts.*",
857
+ "departments.idt_email as billingEmailId",
858
+ "account_balances.balance",
803
859
  )
804
- .select('accounts.*', 'departments.idt_email as billingEmailId', 'account_balances.balance')
805
- .whereNull('departments.sub')
806
- .andWhere(function() {
807
- this.whereNull('departments.exp_date').orWhere('departments.exp_date', '>', self.db.raw('CURRENT_DATE'))
860
+ .whereNull("departments.sub")
861
+ .andWhere(function () {
862
+ this.whereNull("departments.exp_date").orWhere(
863
+ "departments.exp_date",
864
+ ">",
865
+ self.db.raw("CURRENT_DATE"),
866
+ );
808
867
  })
809
- .where('accounts.account_number', 'ilike', `%${number}%`);
810
- const customers = await this.db('customers')
811
- .where('customers.account_number', 'ilike', `%${number}%`)
812
- .whereNotIn('customers.account_number', pis.map(p => p.account_number));
868
+ .where("accounts.account_number", "ilike", `%${number}%`);
869
+ const customers = await this.db("customers")
870
+ .where("customers.account_number", "ilike", `%${number}%`)
871
+ .whereNotIn(
872
+ "customers.account_number",
873
+ pis.map((p) => p.account_number),
874
+ );
813
875
  records = [...customers, ...pis];
814
876
  } catch (e) {
815
877
  throw e;
@@ -829,21 +891,28 @@ class FAMISDb {
829
891
  const self = this;
830
892
 
831
893
  try {
832
- records = await this.db('accounts')
833
- .join('departments',
834
- function() {
835
- this
836
- .on('accounts.department', '=', 'departments.dept')
837
- .on('accounts.campus_code', '=', 'departments.part')
894
+ records = await this.db("accounts")
895
+ .join(
896
+ "departments",
897
+ function () {
898
+ this.on("accounts.department", "=", "departments.dept").on(
899
+ "accounts.campus_code",
900
+ "=",
901
+ "departments.part",
902
+ );
838
903
  },
839
- 'left'
904
+ "left",
840
905
  )
841
- .select('accounts.*', 'departments.idt_email as billingEmailId')
842
- .whereNull('departments.sub')
843
- .andWhere(function() {
844
- this.whereNull('departments.exp_date').orWhere('departments.exp_date', '>', self.db.raw('CURRENT_DATE'))
906
+ .select("accounts.*", "departments.idt_email as billingEmailId")
907
+ .whereNull("departments.sub")
908
+ .andWhere(function () {
909
+ this.whereNull("departments.exp_date").orWhere(
910
+ "departments.exp_date",
911
+ ">",
912
+ self.db.raw("CURRENT_DATE"),
913
+ );
845
914
  })
846
- .where('uin', uin);
915
+ .where("uin", uin);
847
916
  } catch (e) {
848
917
  throw e;
849
918
  }
@@ -862,21 +931,28 @@ class FAMISDb {
862
931
  const self = this;
863
932
 
864
933
  try {
865
- records = await this.db('accounts')
866
- .join('departments',
867
- function() {
868
- this
869
- .on('accounts.department', '=', 'departments.dept')
870
- .on('accounts.campus_code', '=', 'departments.part')
934
+ records = await this.db("accounts")
935
+ .join(
936
+ "departments",
937
+ function () {
938
+ this.on("accounts.department", "=", "departments.dept").on(
939
+ "accounts.campus_code",
940
+ "=",
941
+ "departments.part",
942
+ );
871
943
  },
872
- 'left'
944
+ "left",
873
945
  )
874
- .select('accounts.*', 'departments.idt_email as billingEmailId')
875
- .whereNull('departments.sub')
876
- .andWhere(function() {
877
- this.whereNull('departments.exp_date').orWhere('departments.exp_date', '>', self.db.raw('CURRENT_DATE'))
946
+ .select("accounts.*", "departments.idt_email as billingEmailId")
947
+ .whereNull("departments.sub")
948
+ .andWhere(function () {
949
+ this.whereNull("departments.exp_date").orWhere(
950
+ "departments.exp_date",
951
+ ">",
952
+ self.db.raw("CURRENT_DATE"),
953
+ );
878
954
  })
879
- .where('responsible_uin', uin);
955
+ .where("responsible_uin", uin);
880
956
  } catch (e) {
881
957
  throw e;
882
958
  }
@@ -894,16 +970,19 @@ class FAMISDb {
894
970
  let records;
895
971
  let prefix;
896
972
  let retVal;
897
- famisInvoiceNumbers.forEach(f => {
973
+ famisInvoiceNumbers.forEach((f) => {
898
974
  if (f.aliases.includes(type)) {
899
975
  prefix = f;
900
976
  }
901
977
  });
902
978
  try {
903
979
  retVal = `${prefix.prefix}${prefix.start}`;
904
- records = await this.db('invoices').whereIn('type', prefix.aliases).orderBy('famisInvoiceNumber', 'desc');
980
+ records = await this.db("invoices")
981
+ .whereIn("type", prefix.aliases)
982
+ .orderBy("famisInvoiceNumber", "desc");
905
983
  if (records.length) {
906
- const number = parseInt(records[0].famisInvoiceNumber.substring(1), 10) + 1;
984
+ const number =
985
+ parseInt(records[0].famisInvoiceNumber.substring(1), 10) + 1;
907
986
  retVal = `${prefix.prefix}${number}`;
908
987
  }
909
988
  } catch (e) {
@@ -923,10 +1002,12 @@ class FAMISDb {
923
1002
  let records;
924
1003
  let retVal;
925
1004
  try {
926
- records = await this.db('codes').where('revenue', revenue).orderBy('id', 'desc');
1005
+ records = await this.db("codes")
1006
+ .where("revenue", revenue)
1007
+ .orderBy("id", "desc");
927
1008
  if (records.length) {
928
1009
  retVal = records[0].expense;
929
- }
1010
+ }
930
1011
  } catch (e) {
931
1012
  throw e;
932
1013
  }
@@ -944,10 +1025,12 @@ class FAMISDb {
944
1025
  let records;
945
1026
  let retVal;
946
1027
  try {
947
- records = await this.db('codes').where('revenue', revenue).orderBy('id', 'desc');
1028
+ records = await this.db("codes")
1029
+ .where("revenue", revenue)
1030
+ .orderBy("id", "desc");
948
1031
  if (records.length) {
949
1032
  retVal = records[0];
950
- }
1033
+ }
951
1034
  } catch (e) {
952
1035
  throw e;
953
1036
  }
@@ -961,14 +1044,16 @@ class FAMISDb {
961
1044
  * @returns {Promise<object>} expense code object
962
1045
  * @memberof FAMISDb
963
1046
  */
964
- async getExpenseCode(code) {
1047
+ async getExpenseCode(code) {
965
1048
  let records;
966
1049
  let retVal;
967
1050
  try {
968
- records = await this.db('codes').where('expense', code).orderBy('id', 'desc');
1051
+ records = await this.db("codes")
1052
+ .where("expense", code)
1053
+ .orderBy("id", "desc");
969
1054
  if (records.length) {
970
1055
  retVal = records[0];
971
- }
1056
+ }
972
1057
  } catch (e) {
973
1058
  throw e;
974
1059
  }
@@ -990,33 +1075,35 @@ class FAMISDb {
990
1075
  let d = new Date();
991
1076
  let year = fiscal ? fiscal : this.getCurrentFiscalYear();
992
1077
  try {
993
- records = await this.db('subcode_edits')
994
- .select('allowed')
995
- .where('campus_code', accountNumber.substring(0, 2))
996
- .andWhere('account', accountNumber.substring(2, 8))
997
- .andWhere('support_account', accountNumber.substring(8, 13))
998
- .andWhere('fiscal_year', year)
999
- .andWhereRaw('? BETWEEN low AND high', [parseInt(code)]);
1078
+ records = await this.db("subcode_edits")
1079
+ .select("allowed")
1080
+ .where("campus_code", accountNumber.substring(0, 2))
1081
+ .andWhere("account", accountNumber.substring(2, 8))
1082
+ .andWhere("support_account", accountNumber.substring(8, 13))
1083
+ .andWhere("fiscal_year", year)
1084
+ .andWhereRaw("? BETWEEN low AND high", [parseInt(code)]);
1000
1085
  if (records.length) {
1001
1086
  retVal = records[0].allowed;
1002
1087
  } else {
1003
- records = await this.db('abr_rules')
1004
- .select('abr_rules.*')
1005
- .join('accounts',
1006
- function() {
1007
- this
1008
- .on('accounts.abr_rule', '=', 'abr_rules.abr_rule')
1009
- .on('accounts.campus_code', '=', 'abr_rules.campus_code')
1010
- .on('accounts.fiscal_year', '=', 'abr_rules.fiscal_year')
1088
+ records = await this.db("abr_rules")
1089
+ .select("abr_rules.*")
1090
+ .join(
1091
+ "accounts",
1092
+ function () {
1093
+ this.on("accounts.abr_rule", "=", "abr_rules.abr_rule")
1094
+ .on("accounts.campus_code", "=", "abr_rules.campus_code")
1095
+ .on("accounts.fiscal_year", "=", "abr_rules.fiscal_year");
1011
1096
  },
1012
- 'left'
1097
+ "left",
1013
1098
  )
1014
- .where('accounts.account_number', accountNumber)
1015
- .andWhere('accounts.fiscal_year', year)
1016
- .andWhereRaw('? BETWEEN abr_rules.low AND abr_rules.high', [parseInt(code)]);
1099
+ .where("accounts.account_number", accountNumber)
1100
+ .andWhere("accounts.fiscal_year", year)
1101
+ .andWhereRaw("? BETWEEN abr_rules.low AND abr_rules.high", [
1102
+ parseInt(code),
1103
+ ]);
1017
1104
  if (records.length) {
1018
1105
  retVal = true;
1019
- }
1106
+ }
1020
1107
  }
1021
1108
  } catch (e) {
1022
1109
  throw e;
@@ -1041,50 +1128,50 @@ class FAMISDb {
1041
1128
  };
1042
1129
  let year = fiscal ? fiscal : this.getCurrentFiscalYear();
1043
1130
  try {
1044
- subcode = await this.db('subcode_edits')
1045
- .select('low', 'high')
1046
- .where('campus_code', accountNumber.substring(0, 2))
1047
- .andWhere('account', accountNumber.substring(2, 8))
1048
- .andWhere('support_account', accountNumber.substring(8, 13))
1049
- .andWhere('fiscal_year', year)
1050
- .andWhere('allowed', true)
1051
- .orderBy('low', 'asc');
1052
- disallowed = await this.db('subcode_edits')
1053
- .select('low', 'high')
1054
- .where('campus_code', accountNumber.substring(0, 2))
1055
- .andWhere('account', accountNumber.substring(2, 8))
1056
- .andWhere('support_account', accountNumber.substring(8, 13))
1057
- .andWhere('fiscal_year', year)
1058
- .andWhere('allowed', false)
1059
- .orderBy('low', 'asc');
1060
- abr = await this.db('abr_rules')
1061
- .select('abr_rules.low', 'abr_rules.high')
1062
- .join('accounts',
1063
- function() {
1064
- this
1065
- .on('accounts.abr_rule', '=', 'abr_rules.abr_rule')
1066
- .on('accounts.campus_code', '=', 'abr_rules.campus_code')
1067
- .on('accounts.fiscal_year', '=', 'abr_rules.fiscal_year')
1131
+ subcode = await this.db("subcode_edits")
1132
+ .select("low", "high")
1133
+ .where("campus_code", accountNumber.substring(0, 2))
1134
+ .andWhere("account", accountNumber.substring(2, 8))
1135
+ .andWhere("support_account", accountNumber.substring(8, 13))
1136
+ .andWhere("fiscal_year", year)
1137
+ .andWhere("allowed", true)
1138
+ .orderBy("low", "asc");
1139
+ disallowed = await this.db("subcode_edits")
1140
+ .select("low", "high")
1141
+ .where("campus_code", accountNumber.substring(0, 2))
1142
+ .andWhere("account", accountNumber.substring(2, 8))
1143
+ .andWhere("support_account", accountNumber.substring(8, 13))
1144
+ .andWhere("fiscal_year", year)
1145
+ .andWhere("allowed", false)
1146
+ .orderBy("low", "asc");
1147
+ abr = await this.db("abr_rules")
1148
+ .select("abr_rules.low", "abr_rules.high")
1149
+ .join(
1150
+ "accounts",
1151
+ function () {
1152
+ this.on("accounts.abr_rule", "=", "abr_rules.abr_rule")
1153
+ .on("accounts.campus_code", "=", "abr_rules.campus_code")
1154
+ .on("accounts.fiscal_year", "=", "abr_rules.fiscal_year");
1068
1155
  },
1069
- 'left'
1156
+ "left",
1070
1157
  )
1071
- .where('accounts.account_number', accountNumber)
1072
- .andWhere('accounts.fiscal_year', year)
1073
- .orderBy('low', 'asc');
1158
+ .where("accounts.account_number", accountNumber)
1159
+ .andWhere("accounts.fiscal_year", year)
1160
+ .orderBy("low", "asc");
1074
1161
  if (abr.length || subcode.length) {
1075
1162
  const allowed = [...abr, ...subcode];
1076
1163
  retVal = {
1077
1164
  allowed,
1078
1165
  disallowed,
1079
1166
  };
1080
- }
1167
+ }
1081
1168
  } catch (e) {
1082
1169
  throw e;
1083
1170
  }
1084
1171
  return retVal;
1085
1172
  }
1086
1173
 
1087
- /**
1174
+ /**
1088
1175
  *
1089
1176
  * Get core by id
1090
1177
  * @param {string} id
@@ -1095,54 +1182,52 @@ class FAMISDb {
1095
1182
  let records;
1096
1183
  let retVal;
1097
1184
  try {
1098
- const maxDates = this.db('core_accounts')
1099
- .select(
1100
- 'coreId'
1101
- )
1102
- .max('effective', {as: 'effective'})
1103
- .groupBy('coreId')
1104
- .where('effective', '<=', this.db.raw('now()'))
1185
+ const maxDates = this.db("core_accounts")
1186
+ .select("coreId")
1187
+ .max("effective", { as: "effective" })
1188
+ .groupBy("coreId")
1189
+ .where("effective", "<=", this.db.raw("now()"))
1105
1190
  .as("max_dates");
1106
- const accounts = this.db('core_accounts')
1191
+ const accounts = this.db("core_accounts")
1107
1192
  .select(
1108
- 'core_accounts.coreId',
1109
- 'core_accounts.account',
1110
- 'core_accounts.effective',
1111
- )
1112
- .join(maxDates,
1113
- function() {
1114
- this
1115
- .on('core_accounts.coreId', '=', 'max_dates.coreId')
1116
- .andOn('core_accounts.effective', '=', 'max_dates.effective')
1117
- }
1193
+ "core_accounts.coreId",
1194
+ "core_accounts.account",
1195
+ "core_accounts.effective",
1118
1196
  )
1197
+ .join(maxDates, function () {
1198
+ this.on("core_accounts.coreId", "=", "max_dates.coreId").andOn(
1199
+ "core_accounts.effective",
1200
+ "=",
1201
+ "max_dates.effective",
1202
+ );
1203
+ })
1119
1204
  .as("core_accounts");
1120
- records = await this.db('cores')
1205
+ records = await this.db("cores")
1121
1206
  .select(
1122
- 'cores.id',
1123
- 'core_accounts.account',
1124
- 'core_accounts.effective',
1125
- 'cores.facility',
1126
- 'cores.contact',
1127
- 'cores.contactEmail',
1128
- 'cores.systemId',
1129
- 'system.name as partName',
1130
- 'system.part as partNumber',
1131
- 'cores.enabled',
1132
- 'cores.deleted',
1133
- 'cores.coreEmail',
1134
- 'cores.corePhone',
1135
- 'cores.coreAddress',
1136
- 'cores.from_email',
1137
- 'cores.invoice_email'
1207
+ "cores.id",
1208
+ "core_accounts.account",
1209
+ "core_accounts.effective",
1210
+ "cores.facility",
1211
+ "cores.contact",
1212
+ "cores.contactEmail",
1213
+ "cores.systemId",
1214
+ "system.name as partName",
1215
+ "system.part as partNumber",
1216
+ "cores.enabled",
1217
+ "cores.deleted",
1218
+ "cores.coreEmail",
1219
+ "cores.corePhone",
1220
+ "cores.coreAddress",
1221
+ "cores.from_email",
1222
+ "cores.invoice_email",
1138
1223
  )
1139
- .join(accounts, 'cores.id', '=', 'core_accounts.coreId')
1140
- .join('system', 'cores.systemId', '=', 'system.id')
1141
- .where('cores.id', id)
1142
- .orderBy('cores.id', 'desc');
1224
+ .join(accounts, "cores.id", "=", "core_accounts.coreId")
1225
+ .join("system", "cores.systemId", "=", "system.id")
1226
+ .where("cores.id", id)
1227
+ .orderBy("cores.id", "desc");
1143
1228
  if (records.length) {
1144
1229
  retVal = records[0];
1145
- }
1230
+ }
1146
1231
  } catch (e) {
1147
1232
  throw e;
1148
1233
  }
@@ -1160,30 +1245,30 @@ class FAMISDb {
1160
1245
  let records;
1161
1246
  let retVal;
1162
1247
  try {
1163
- records = await this.db('cores')
1248
+ records = await this.db("cores")
1164
1249
  .select(
1165
- 'cores.id',
1166
- 'core_accounts.account',
1167
- 'core_accounts.effective',
1168
- 'core_accounts.active',
1169
- 'cores.facility',
1170
- 'cores.contact',
1171
- 'cores.contactEmail',
1172
- 'cores.systemId',
1173
- 'cores.enabled',
1174
- 'cores.deleted',
1175
- 'cores.coreEmail',
1176
- 'cores.corePhone',
1177
- 'cores.coreAddress',
1178
- 'cores.from_email',
1179
- 'cores.invoice_email'
1250
+ "cores.id",
1251
+ "core_accounts.account",
1252
+ "core_accounts.effective",
1253
+ "core_accounts.active",
1254
+ "cores.facility",
1255
+ "cores.contact",
1256
+ "cores.contactEmail",
1257
+ "cores.systemId",
1258
+ "cores.enabled",
1259
+ "cores.deleted",
1260
+ "cores.coreEmail",
1261
+ "cores.corePhone",
1262
+ "cores.coreAddress",
1263
+ "cores.from_email",
1264
+ "cores.invoice_email",
1180
1265
  )
1181
- .join('core_accounts', 'cores.id', '=', 'core_accounts.coreId')
1182
- .where('core_accounts.account', account)
1183
- .orderBy('cores.id', 'desc');
1266
+ .join("core_accounts", "cores.id", "=", "core_accounts.coreId")
1267
+ .where("core_accounts.account", account)
1268
+ .orderBy("cores.id", "desc");
1184
1269
  if (records.length) {
1185
1270
  retVal = records[0];
1186
- }
1271
+ }
1187
1272
  } catch (e) {
1188
1273
  throw e;
1189
1274
  }
@@ -1201,51 +1286,49 @@ class FAMISDb {
1201
1286
  let records;
1202
1287
  let retVal;
1203
1288
  try {
1204
- const maxDates = this.db('core_accounts')
1205
- .select(
1206
- 'coreId'
1207
- )
1208
- .max('effective', {as: 'effective'})
1209
- .groupBy('coreId')
1210
- .where('effective', '<=', this.db.raw('now()'))
1211
- .andWhere('coreId', '=', coreId)
1289
+ const maxDates = this.db("core_accounts")
1290
+ .select("coreId")
1291
+ .max("effective", { as: "effective" })
1292
+ .groupBy("coreId")
1293
+ .where("effective", "<=", this.db.raw("now()"))
1294
+ .andWhere("coreId", "=", coreId)
1212
1295
  .as("max_dates");
1213
- const accounts = this.db('core_accounts')
1296
+ const accounts = this.db("core_accounts")
1214
1297
  .select(
1215
- 'core_accounts.coreId',
1216
- 'core_accounts.account',
1217
- 'core_accounts.effective',
1218
- )
1219
- .join(maxDates,
1220
- function() {
1221
- this
1222
- .on('core_accounts.coreId', '=', 'max_dates.coreId')
1223
- .andOn('core_accounts.effective', '=', 'max_dates.effective')
1224
- }
1298
+ "core_accounts.coreId",
1299
+ "core_accounts.account",
1300
+ "core_accounts.effective",
1225
1301
  )
1302
+ .join(maxDates, function () {
1303
+ this.on("core_accounts.coreId", "=", "max_dates.coreId").andOn(
1304
+ "core_accounts.effective",
1305
+ "=",
1306
+ "max_dates.effective",
1307
+ );
1308
+ })
1226
1309
  .as("core_accounts");
1227
- records = await this.db('cores')
1310
+ records = await this.db("cores")
1228
1311
  .select(
1229
- 'cores.id',
1230
- 'core_accounts.account',
1231
- 'core_accounts.effective',
1232
- 'cores.facility',
1233
- 'cores.contact',
1234
- 'cores.contactEmail',
1235
- 'cores.systemId',
1236
- 'cores.enabled',
1237
- 'cores.deleted',
1238
- 'cores.coreEmail',
1239
- 'cores.corePhone',
1240
- 'cores.coreAddress',
1241
- 'cores.from_email',
1242
- 'cores.invoice_email'
1312
+ "cores.id",
1313
+ "core_accounts.account",
1314
+ "core_accounts.effective",
1315
+ "cores.facility",
1316
+ "cores.contact",
1317
+ "cores.contactEmail",
1318
+ "cores.systemId",
1319
+ "cores.enabled",
1320
+ "cores.deleted",
1321
+ "cores.coreEmail",
1322
+ "cores.corePhone",
1323
+ "cores.coreAddress",
1324
+ "cores.from_email",
1325
+ "cores.invoice_email",
1243
1326
  )
1244
- .join(accounts, 'cores.id', '=', 'core_accounts.coreId')
1245
- .orderBy('cores.id', 'desc');
1327
+ .join(accounts, "cores.id", "=", "core_accounts.coreId")
1328
+ .orderBy("cores.id", "desc");
1246
1329
  if (records.length) {
1247
1330
  retVal = records[0];
1248
- }
1331
+ }
1249
1332
  } catch (e) {
1250
1333
  throw e;
1251
1334
  }
@@ -1263,10 +1346,10 @@ class FAMISDb {
1263
1346
  let records;
1264
1347
  let retVal;
1265
1348
  try {
1266
- records = await this.db('system').where('id', id).orderBy('id', 'desc');
1349
+ records = await this.db("system").where("id", id).orderBy("id", "desc");
1267
1350
  if (records.length) {
1268
1351
  retVal = records[0];
1269
- }
1352
+ }
1270
1353
  } catch (e) {
1271
1354
  throw e;
1272
1355
  }
@@ -1284,10 +1367,12 @@ class FAMISDb {
1284
1367
  let records;
1285
1368
  let retVal;
1286
1369
  try {
1287
- records = await this.db('system').where('part', number).orderBy('id', 'desc');
1370
+ records = await this.db("system")
1371
+ .where("part", number)
1372
+ .orderBy("id", "desc");
1288
1373
  if (records.length) {
1289
1374
  retVal = records[0];
1290
- }
1375
+ }
1291
1376
  } catch (e) {
1292
1377
  throw e;
1293
1378
  }
@@ -1305,51 +1390,49 @@ class FAMISDb {
1305
1390
  let records;
1306
1391
  let retVal = [];
1307
1392
  try {
1308
- const maxDates = this.db('core_accounts')
1309
- .select(
1310
- 'coreId'
1311
- )
1312
- .max('effective', {as: 'effective'})
1313
- .groupBy('coreId')
1314
- .where('effective', '<=', date ? date : this.db.raw('now()'))
1393
+ const maxDates = this.db("core_accounts")
1394
+ .select("coreId")
1395
+ .max("effective", { as: "effective" })
1396
+ .groupBy("coreId")
1397
+ .where("effective", "<=", date ? date : this.db.raw("now()"))
1315
1398
  .as("max_dates");
1316
- const accounts = this.db('core_accounts')
1399
+ const accounts = this.db("core_accounts")
1317
1400
  .select(
1318
- 'core_accounts.coreId',
1319
- 'core_accounts.account',
1320
- 'core_accounts.effective',
1321
- )
1322
- .join(maxDates,
1323
- function() {
1324
- this
1325
- .on('core_accounts.coreId', '=', 'max_dates.coreId')
1326
- .andOn('core_accounts.effective', '=', 'max_dates.effective')
1327
- }
1401
+ "core_accounts.coreId",
1402
+ "core_accounts.account",
1403
+ "core_accounts.effective",
1328
1404
  )
1405
+ .join(maxDates, function () {
1406
+ this.on("core_accounts.coreId", "=", "max_dates.coreId").andOn(
1407
+ "core_accounts.effective",
1408
+ "=",
1409
+ "max_dates.effective",
1410
+ );
1411
+ })
1329
1412
  .as("core_accounts");
1330
- records = await this.db('cores')
1413
+ records = await this.db("cores")
1331
1414
  .select(
1332
- 'cores.id',
1333
- 'core_accounts.account',
1334
- 'core_accounts.effective',
1335
- 'cores.facility',
1336
- 'cores.contact',
1337
- 'cores.contactEmail',
1338
- 'cores.systemId',
1339
- 'cores.enabled',
1340
- 'cores.deleted',
1341
- 'cores.coreEmail',
1342
- 'cores.corePhone',
1343
- 'cores.coreAddress',
1344
- 'cores.from_email',
1345
- 'cores.invoice_email'
1415
+ "cores.id",
1416
+ "core_accounts.account",
1417
+ "core_accounts.effective",
1418
+ "cores.facility",
1419
+ "cores.contact",
1420
+ "cores.contactEmail",
1421
+ "cores.systemId",
1422
+ "cores.enabled",
1423
+ "cores.deleted",
1424
+ "cores.coreEmail",
1425
+ "cores.corePhone",
1426
+ "cores.coreAddress",
1427
+ "cores.from_email",
1428
+ "cores.invoice_email",
1346
1429
  )
1347
- .join(accounts, 'cores.id', '=', 'core_accounts.coreId')
1348
- .where('enabled', '1')
1349
- .orderBy('cores.id', 'desc');
1430
+ .join(accounts, "cores.id", "=", "core_accounts.coreId")
1431
+ .where("enabled", "1")
1432
+ .orderBy("cores.id", "desc");
1350
1433
  if (records.length) {
1351
1434
  retVal = records;
1352
- }
1435
+ }
1353
1436
  } catch (e) {
1354
1437
  throw e;
1355
1438
  }
@@ -1366,10 +1449,10 @@ class FAMISDb {
1366
1449
  let records;
1367
1450
  let retVal = [];
1368
1451
  try {
1369
- records = await this.db('cores').orderBy('id', 'asc');
1452
+ records = await this.db("cores").orderBy("id", "asc");
1370
1453
  if (records.length) {
1371
1454
  retVal = records;
1372
- }
1455
+ }
1373
1456
  } catch (e) {
1374
1457
  throw e;
1375
1458
  }
@@ -1386,14 +1469,14 @@ class FAMISDb {
1386
1469
  async getCoreUsers(coreId) {
1387
1470
  let retVal;
1388
1471
  try {
1389
- retVal = await this.db('user_cores')
1390
- .join('users', 'user_cores.userId', '=', 'users.id')
1391
- .where('coreId', coreId)
1472
+ retVal = await this.db("user_cores")
1473
+ .join("users", "user_cores.userId", "=", "users.id")
1474
+ .where("coreId", coreId)
1392
1475
  .select(
1393
- 'users.*',
1394
- 'user_cores.id as userCoreId',
1395
- 'user_cores.coreId',
1396
- 'user_cores.email',
1476
+ "users.*",
1477
+ "user_cores.id as userCoreId",
1478
+ "user_cores.coreId",
1479
+ "user_cores.email",
1397
1480
  );
1398
1481
  } catch (e) {
1399
1482
  throw e;
@@ -1411,15 +1494,15 @@ class FAMISDb {
1411
1494
  async getUserCores(userId) {
1412
1495
  let retVal;
1413
1496
  try {
1414
- retVal = await this.db('user_cores')
1415
- .join('cores', 'user_cores.coreId', '=', 'cores.id')
1416
- .join('users', 'user_cores.userId', '=', 'users.id')
1417
- .where('users.userId', userId)
1497
+ retVal = await this.db("user_cores")
1498
+ .join("cores", "user_cores.coreId", "=", "cores.id")
1499
+ .join("users", "user_cores.userId", "=", "users.id")
1500
+ .where("users.userId", userId)
1418
1501
  .select(
1419
- 'cores.*',
1420
- 'user_cores.id as userCoreId',
1421
- 'user_cores.userId',
1422
- 'user_cores.email',
1502
+ "cores.*",
1503
+ "user_cores.id as userCoreId",
1504
+ "user_cores.userId",
1505
+ "user_cores.email",
1423
1506
  );
1424
1507
  } catch (e) {
1425
1508
  throw e;
@@ -1437,37 +1520,37 @@ class FAMISDb {
1437
1520
  let records;
1438
1521
  let retVal = [];
1439
1522
  try {
1440
- const accounts = this.db('core_accounts')
1523
+ const accounts = this.db("core_accounts")
1441
1524
  .select(
1442
- 'core_accounts.coreId',
1443
- 'core_accounts.account',
1444
- 'core_accounts.active',
1525
+ "core_accounts.coreId",
1526
+ "core_accounts.account",
1527
+ "core_accounts.active",
1445
1528
  )
1446
- .where({active: true})
1529
+ .where({ active: true })
1447
1530
  .as("core_accounts");
1448
- records = await this.db('cores')
1531
+ records = await this.db("cores")
1449
1532
  .select(
1450
- 'cores.id',
1451
- 'core_accounts.account',
1452
- 'core_accounts.active',
1453
- 'cores.facility',
1454
- 'cores.contact',
1455
- 'cores.contactEmail',
1456
- 'cores.systemId',
1457
- 'cores.enabled',
1458
- 'cores.deleted',
1459
- 'cores.coreEmail',
1460
- 'cores.corePhone',
1461
- 'cores.coreAddress',
1462
- 'cores.from_email',
1463
- 'cores.invoice_email'
1533
+ "cores.id",
1534
+ "core_accounts.account",
1535
+ "core_accounts.active",
1536
+ "cores.facility",
1537
+ "cores.contact",
1538
+ "cores.contactEmail",
1539
+ "cores.systemId",
1540
+ "cores.enabled",
1541
+ "cores.deleted",
1542
+ "cores.coreEmail",
1543
+ "cores.corePhone",
1544
+ "cores.coreAddress",
1545
+ "cores.from_email",
1546
+ "cores.invoice_email",
1464
1547
  )
1465
- .join(accounts, 'cores.id', '=', 'core_accounts.coreId')
1466
- .where({enabled: true})
1467
- .orderBy('cores.id', 'desc');
1548
+ .join(accounts, "cores.id", "=", "core_accounts.coreId")
1549
+ .where({ enabled: true })
1550
+ .orderBy("cores.id", "desc");
1468
1551
  if (records.length) {
1469
1552
  retVal = records;
1470
- }
1553
+ }
1471
1554
  } catch (e) {
1472
1555
  throw e;
1473
1556
  }
@@ -1484,14 +1567,14 @@ class FAMISDb {
1484
1567
  async getSystemUsers(systemId) {
1485
1568
  let retVal;
1486
1569
  try {
1487
- retVal = await this.db('user_systems')
1488
- .join('users', 'user_systems.userId', '=', 'users.id')
1489
- .where('systemId', systemId)
1570
+ retVal = await this.db("user_systems")
1571
+ .join("users", "user_systems.userId", "=", "users.id")
1572
+ .where("systemId", systemId)
1490
1573
  .select(
1491
- 'users.*',
1492
- 'user_systems.id as userSystemId',
1493
- 'user_systems.systemId',
1494
- 'user_systems.email',
1574
+ "users.*",
1575
+ "user_systems.id as userSystemId",
1576
+ "user_systems.systemId",
1577
+ "user_systems.email",
1495
1578
  );
1496
1579
  } catch (e) {
1497
1580
  throw e;
@@ -1510,15 +1593,15 @@ class FAMISDb {
1510
1593
  let records;
1511
1594
  let retVal;
1512
1595
  try {
1513
- retVal = await this.db('user_systems')
1514
- .join('system', 'user_systems.systemId', '=', 'system.id')
1515
- .join('users', 'user_systems.userId', '=', 'users.id')
1516
- .where('users.userId', userId)
1596
+ retVal = await this.db("user_systems")
1597
+ .join("system", "user_systems.systemId", "=", "system.id")
1598
+ .join("users", "user_systems.userId", "=", "users.id")
1599
+ .where("users.userId", userId)
1517
1600
  .select(
1518
- 'system.*',
1519
- 'user_systems.id as userSystemId',
1520
- 'user_systems.userId',
1521
- 'user_systems.email',
1601
+ "system.*",
1602
+ "user_systems.id as userSystemId",
1603
+ "user_systems.userId",
1604
+ "user_systems.email",
1522
1605
  );
1523
1606
  } catch (e) {
1524
1607
  throw e;
@@ -1536,7 +1619,7 @@ class FAMISDb {
1536
1619
  let records;
1537
1620
  let retVal;
1538
1621
  try {
1539
- retVal = await this.db('system').orderBy('id', 'asc');
1622
+ retVal = await this.db("system").orderBy("id", "asc");
1540
1623
  } catch (e) {
1541
1624
  throw e;
1542
1625
  }
@@ -1549,10 +1632,10 @@ class FAMISDb {
1549
1632
  * @returns {Promise<array>} array of user objects
1550
1633
  * @memberof FAMISDb
1551
1634
  */
1552
- async getAdminUsers() {
1635
+ async getAdminUsers() {
1553
1636
  let retVal;
1554
1637
  try {
1555
- retVal = await this.db('users').where('admin', true);
1638
+ retVal = await this.db("users").where("admin", true);
1556
1639
  } catch (e) {
1557
1640
  throw e;
1558
1641
  }
@@ -1570,12 +1653,12 @@ class FAMISDb {
1570
1653
  let records;
1571
1654
  let retVal;
1572
1655
  try {
1573
- records = await this.db('users').where('userId', userId);
1656
+ records = await this.db("users").where("userId", userId);
1574
1657
  if (records.length) {
1575
1658
  retVal = records[0];
1576
1659
  retVal.cores = await this.getUserCores(userId);
1577
1660
  retVal.systems = await this.getUserSystemParts(userId);
1578
- }
1661
+ }
1579
1662
  } catch (e) {
1580
1663
  throw e;
1581
1664
  }
@@ -1589,14 +1672,23 @@ class FAMISDb {
1589
1672
  * @returns {Promise<object>} user object
1590
1673
  * @memberof FAMISDb
1591
1674
  */
1592
- async updateUser(user) {
1675
+ async updateUser(user) {
1593
1676
  let retVal;
1594
- const columns = ['id', 'userId', 'admin', 'role', 'roleName', 'roleEmail', 'deleted'];
1677
+ const columns = [
1678
+ "id",
1679
+ "userId",
1680
+ "admin",
1681
+ "role",
1682
+ "roleName",
1683
+ "roleEmail",
1684
+ "deleted",
1685
+ ];
1595
1686
  try {
1596
- if ('id' in user) retVal = await this.db('users')
1597
- .returning(columns)
1598
- .update(user)
1599
- .where('id', user.id);
1687
+ if ("id" in user)
1688
+ retVal = await this.db("users")
1689
+ .returning(columns)
1690
+ .update(user)
1691
+ .where("id", user.id);
1600
1692
  } catch (e) {
1601
1693
  throw e;
1602
1694
  }
@@ -1617,8 +1709,10 @@ class FAMISDb {
1617
1709
  const code = pad(campusCode, 2);
1618
1710
 
1619
1711
  try {
1620
- records = await this.db('customers')
1621
- .where('uid', `${code}${accountNumber}`);
1712
+ records = await this.db("customers").where(
1713
+ "uid",
1714
+ `${code}${accountNumber}`,
1715
+ );
1622
1716
  if (records.length) {
1623
1717
  retVal = records[0];
1624
1718
  }
@@ -1637,7 +1731,7 @@ class FAMISDb {
1637
1731
  getCurrentFiscalYear() {
1638
1732
  let year;
1639
1733
  let today = new Date();
1640
- if ((today.getMonth() + 1) >= 9) {
1734
+ if (today.getMonth() + 1 >= 9) {
1641
1735
  year = today.getFullYear() + 1;
1642
1736
  } else {
1643
1737
  year = today.getFullYear();
@@ -1654,10 +1748,9 @@ class FAMISDb {
1654
1748
  */
1655
1749
  getInvoiceFingerPrint(invoice) {
1656
1750
  const fingerPrint = revisionHash(
1657
- JSON.stringify(
1658
- sortObjectKeys(invoice),
1659
- (k, v) => undefined ? "undef" : v
1660
- )
1751
+ JSON.stringify(sortObjectKeys(invoice), (k, v) =>
1752
+ undefined ? "undef" : v,
1753
+ ),
1661
1754
  );
1662
1755
  return fingerPrint;
1663
1756
  }
@@ -1670,16 +1763,19 @@ class FAMISDb {
1670
1763
  * @memberof FAMISDb
1671
1764
  */
1672
1765
  formatAccountNumber(accountNumber) {
1673
- const formatted = accountNumber.replace(/(\d{2})(\d{6})(\d{5})/, "$1-$2-$3");
1766
+ const formatted = accountNumber.replace(
1767
+ /(\d{2})(\d{6})(\d{5})/,
1768
+ "$1-$2-$3",
1769
+ );
1674
1770
 
1675
1771
  return formatted;
1676
1772
  }
1677
1773
 
1678
1774
  /**
1679
- * @typedef {Object} HighRisk
1680
- * @property {boolean} isHighRisk - indicates if invoice triggers SRS high risk policy
1681
- * @property {object} flaggedTransactions - object of transactions that flag
1682
- */
1775
+ * @typedef {Object} HighRisk
1776
+ * @property {boolean} isHighRisk - indicates if invoice triggers SRS high risk policy
1777
+ * @property {object} flaggedTransactions - object of transactions that flag
1778
+ */
1683
1779
 
1684
1780
  /**
1685
1781
  *
@@ -1693,25 +1789,46 @@ class FAMISDb {
1693
1789
  const objectCodes = new Map();
1694
1790
  //console.log(invoice);
1695
1791
  try {
1696
- await each(
1697
- invoice.data.details,
1698
- async (detail) => {
1699
- //console.log(detail);
1700
- if (detail.famis.customerAccountNumber && detail.famis.sellingCampusCode) {
1701
- const acct = await this.getByAccountNumber(detail.famis.customerAccountNumber, detail.famis.sellingCampusCode);
1702
- const acctNum = this.formatAccountNumber(detail.famis.customerAccountNumber);
1703
- const objCode = await this.getRevenueCode(detail.sellingObjectCode);
1704
- const isFederal = acct && acct.cfda_no && (acct.sponsor_type && acct.sponsor_type === 'F');
1705
- const srsApproved = invoice.replace_data && "srs_approved" in invoice.replace_data ? invoice.replace_data.srs_approved : false;
1706
- if (objCode && objCode.srs && acct && acct.srs && isFederal && !srsApproved && invoice.isIdt) {
1707
- srsHighRisk = true;
1708
- let val = [detail];
1709
- if (objectCodes.has(objCode.expense)) val.push(...objectCodes.get(objCode.expense));
1710
- objectCodes.set(objCode.expense, val);
1711
- }
1792
+ await each(invoice.data.details, async (detail) => {
1793
+ //console.log(detail);
1794
+ if (
1795
+ detail.famis.customerAccountNumber &&
1796
+ detail.famis.sellingCampusCode
1797
+ ) {
1798
+ const acct = await this.getByAccountNumber(
1799
+ detail.famis.customerAccountNumber,
1800
+ detail.famis.sellingCampusCode,
1801
+ );
1802
+ const acctNum = this.formatAccountNumber(
1803
+ detail.famis.customerAccountNumber,
1804
+ );
1805
+ const objCode = await this.getRevenueCode(detail.sellingObjectCode);
1806
+ const isFederal =
1807
+ acct &&
1808
+ acct.cfda_no &&
1809
+ acct.sponsor_type &&
1810
+ acct.sponsor_type === "F";
1811
+ const srsApproved =
1812
+ invoice.replace_data && "srs_approved" in invoice.replace_data
1813
+ ? invoice.replace_data.srs_approved
1814
+ : false;
1815
+ if (
1816
+ objCode &&
1817
+ objCode.srs &&
1818
+ acct &&
1819
+ acct.srs &&
1820
+ isFederal &&
1821
+ !srsApproved &&
1822
+ invoice.isIdt
1823
+ ) {
1824
+ srsHighRisk = true;
1825
+ let val = [detail];
1826
+ if (objectCodes.has(objCode.expense))
1827
+ val.push(...objectCodes.get(objCode.expense));
1828
+ objectCodes.set(objCode.expense, val);
1712
1829
  }
1713
1830
  }
1714
- );
1831
+ });
1715
1832
  } catch (e) {
1716
1833
  //console.log(invoice);
1717
1834
  console.log(e);
@@ -1719,7 +1836,7 @@ class FAMISDb {
1719
1836
  return {
1720
1837
  isHighRisk: srsHighRisk,
1721
1838
  flaggedTransactions: Object.fromEntries(objectCodes),
1722
- }
1839
+ };
1723
1840
  }
1724
1841
 
1725
1842
  /**
@@ -1743,25 +1860,27 @@ class FAMISDb {
1743
1860
  */
1744
1861
  async saveComment(comment) {
1745
1862
  const returningColumns = [
1746
- 'id',
1747
- 'uid',
1748
- 'invoice_id',
1749
- 'user_id',
1750
- 'comment',
1751
- 'type',
1752
- 'created',
1753
- 'deleted',
1863
+ "id",
1864
+ "uid",
1865
+ "invoice_id",
1866
+ "user_id",
1867
+ "comment",
1868
+ "type",
1869
+ "created",
1870
+ "deleted",
1754
1871
  ];
1755
1872
  let response;
1756
1873
  try {
1757
- const records = await this.db('invoice_comments').where({ uid: comment.uid });
1874
+ const records = await this.db("invoice_comments").where({
1875
+ uid: comment.uid,
1876
+ });
1758
1877
  if (records.length) {
1759
- response = await this.db('invoice_comments')
1878
+ response = await this.db("invoice_comments")
1760
1879
  .returning(returningColumns)
1761
1880
  .update(comment)
1762
- .where('uid', comment.uid);
1881
+ .where("uid", comment.uid);
1763
1882
  } else {
1764
- response = await this.db('invoice_comments')
1883
+ response = await this.db("invoice_comments")
1765
1884
  .returning(returningColumns)
1766
1885
  .insert(comment);
1767
1886
  }
@@ -1772,7 +1891,7 @@ class FAMISDb {
1772
1891
  } catch (e) {
1773
1892
  //console.log(JSON.stringify(comment, null, 2));
1774
1893
  console.log(e);
1775
- throw (e);
1894
+ throw e;
1776
1895
  }
1777
1896
  return response;
1778
1897
  }
@@ -1788,30 +1907,25 @@ class FAMISDb {
1788
1907
  let self = this;
1789
1908
  let response;
1790
1909
  try {
1791
- const records = await this.db('invoice_comments')
1792
- .select(
1793
- 'invoice_comments.*',
1794
- )
1910
+ const records = await this.db("invoice_comments")
1911
+ .select("invoice_comments.*")
1795
1912
  .where({ invoice_id: invoiceUid })
1796
- .orderBy('created', 'desc');
1797
- response = await map(
1798
- records,
1799
- async (r) => {
1800
- const retVal = r;
1801
- const files = await self.getCommentFiles(r.uid);
1802
- r.files = files;
1803
- if (self.people) {
1804
- const user = await self.people.getByUid(r.user_id);
1805
- if (user) {
1806
- retVal.user = user;
1807
- }
1913
+ .orderBy("created", "desc");
1914
+ response = await map(records, async (r) => {
1915
+ const retVal = r;
1916
+ const files = await self.getCommentFiles(r.uid);
1917
+ r.files = files;
1918
+ if (self.people) {
1919
+ const user = await self.people.getByUid(r.user_id);
1920
+ if (user) {
1921
+ retVal.user = user;
1808
1922
  }
1809
- return retVal;
1810
1923
  }
1811
- );
1924
+ return retVal;
1925
+ });
1812
1926
  } catch (e) {
1813
1927
  console.log(e);
1814
- throw (e);
1928
+ throw e;
1815
1929
  }
1816
1930
  return response;
1817
1931
  }
@@ -1837,25 +1951,27 @@ class FAMISDb {
1837
1951
  */
1838
1952
  async saveCommentFile(commentFile) {
1839
1953
  const returningColumns = [
1840
- 'id',
1841
- 'uid',
1842
- 'invoice_id',
1843
- 'comment_id',
1844
- 'original_filename',
1845
- 'filename',
1846
- 'created',
1847
- 'deleted',
1954
+ "id",
1955
+ "uid",
1956
+ "invoice_id",
1957
+ "comment_id",
1958
+ "original_filename",
1959
+ "filename",
1960
+ "created",
1961
+ "deleted",
1848
1962
  ];
1849
1963
  let response;
1850
1964
  try {
1851
- const records = await this.db('invoice_comment_files').where({ uid: commentFile.uid });
1965
+ const records = await this.db("invoice_comment_files").where({
1966
+ uid: commentFile.uid,
1967
+ });
1852
1968
  if (records.length) {
1853
- response = await this.db('invoice_comment_files')
1969
+ response = await this.db("invoice_comment_files")
1854
1970
  .returning(returningColumns)
1855
1971
  .update(commentFile)
1856
- .where('uid', commentFile.uid);
1972
+ .where("uid", commentFile.uid);
1857
1973
  } else {
1858
- response = await this.db('invoice_comment_files')
1974
+ response = await this.db("invoice_comment_files")
1859
1975
  .returning(returningColumns)
1860
1976
  .insert(commentFile);
1861
1977
  }
@@ -1866,7 +1982,7 @@ class FAMISDb {
1866
1982
  } catch (e) {
1867
1983
  //console.log(JSON.stringify(comment, null, 2));
1868
1984
  console.log(e);
1869
- throw (e);
1985
+ throw e;
1870
1986
  }
1871
1987
  return response;
1872
1988
  }
@@ -1881,13 +1997,13 @@ class FAMISDb {
1881
1997
  async getCommentFiles(commentUid) {
1882
1998
  let response;
1883
1999
  try {
1884
- const records = await this.db('invoice_comment_files')
2000
+ const records = await this.db("invoice_comment_files")
1885
2001
  .where({ comment_id: commentUid })
1886
- .orderBy('created', 'desc');
2002
+ .orderBy("created", "desc");
1887
2003
  response = records;
1888
2004
  } catch (e) {
1889
2005
  console.log(e);
1890
- throw (e);
2006
+ throw e;
1891
2007
  }
1892
2008
  return response;
1893
2009
  }
@@ -1899,15 +2015,16 @@ class FAMISDb {
1899
2015
  * @returns {Promise(<CommentFile>} array of comment file objects
1900
2016
  * @memberof FAMISDb
1901
2017
  */
1902
- async getCommentFile(fileUid) {
2018
+ async getCommentFile(fileUid) {
1903
2019
  let response;
1904
2020
  try {
1905
- const records = await this.db('invoice_comment_files')
1906
- .where({ uid: fileUid });
2021
+ const records = await this.db("invoice_comment_files").where({
2022
+ uid: fileUid,
2023
+ });
1907
2024
  if (records.length) response = records[0];
1908
2025
  } catch (e) {
1909
2026
  console.log(e);
1910
- throw (e);
2027
+ throw e;
1911
2028
  }
1912
2029
  return response;
1913
2030
  }