@@ -2,8 +2,11 @@ import Adapter from "../adapter";
2
2
import * as GraphTypes from "@microsoft/microsoft-graph-types" ;
3
3
import * as Graph from "@microsoft/microsoft-graph-client" ;
4
4
import * as request from "superagent" ;
5
- import * as xl from "excel4node" ;
6
- import * as md5 from "md5" ;
5
+ // following two imports throw and error if IMPORTED, but they're fine if REQUIRED
6
+ const xl = require ( "excel4node" ) ;
7
+ // import * as xl from "excel4node";
8
+ const md5 = require ( "md5" ) ;
9
+ // import * as md5 from "md5";
7
10
import { Error } from "mongoose" ;
8
11
import { resolve } from "url" ;
9
12
import { Bool } from "../../../node_modules/aws-sdk/clients/inspector" ;
@@ -12,33 +15,42 @@ import { MaxKey } from "../../../node_modules/@types/bson";
12
15
export default class Office extends Adapter {
13
16
client : Graph . Client ;
14
17
accessToken : string ;
15
- sheetUrl : string ;
18
+ configuration : {
19
+ SheetName : string ;
20
+ ClientId : string ;
21
+ ClientSecret : string ;
22
+ RefreshToken : string ;
23
+ MaxColumns : number ;
24
+ } ;
16
25
17
26
// default first row in the table is row index 4
18
27
public static DEFAULT_ROW_INDEX = 4 ;
19
28
20
29
constructor (
21
30
documentId : string ,
22
31
config : {
23
- SheetName : string ;
24
- ClientId : string ;
25
- ClientSecret : string ;
26
- RefreshToken : string ;
27
- MaxColumns ?: number ;
32
+ credentials : {
33
+ SheetName : string ;
34
+ ClientId : string ;
35
+ ClientSecret : string ;
36
+ RefreshToken : string ;
37
+ MaxColumns : number ;
38
+ } ;
28
39
}
29
40
) {
30
41
super ( documentId , config ) ;
31
- if ( typeof config . MaxColumns === "undefined" ) config . MaxColumns = 64 ;
32
- this . config = config ;
42
+ this . documentId = documentId ;
43
+ if ( typeof config . credentials . MaxColumns === "undefined" ) config . credentials . MaxColumns = 64 ;
44
+ this . configuration = config . credentials ;
33
45
this . adapterId = "office" ;
34
46
35
47
console . log (
36
48
"Sheet Name: " +
37
- this . config . SheetName +
49
+ config . credentials . SheetName +
38
50
", Client ID: " +
39
- this . config . ClientId +
51
+ config . credentials . ClientId +
40
52
", Client Secret: " +
41
- this . config . ClientSecret
53
+ config . credentials . ClientSecret
42
54
) ;
43
55
44
56
this . client = Graph . Client . init ( {
@@ -51,10 +63,10 @@ export default class Office extends Adapter {
51
63
. post ( "https://login.microsoftonline.com/common/oauth2/v2.0/token" )
52
64
. type ( "form" )
53
65
. send ( {
54
- client_id : this . config . ClientId ,
55
- client_secret : this . config . ClientSecret ,
66
+ client_id : config . credentials . ClientId ,
67
+ client_secret : config . credentials . ClientSecret ,
56
68
grant_type : "refresh_token" ,
57
- refresh_token : this . config . RefreshToken ,
69
+ refresh_token : config . credentials . RefreshToken ,
58
70
scope : "Files.ReadWrite.All offline_access"
59
71
} )
60
72
. then ( res => {
@@ -74,11 +86,13 @@ export default class Office extends Adapter {
74
86
}
75
87
76
88
getTableName ( ) {
77
- return "FeedbotData_" + this . config . SheetName ;
89
+ return "FeedbotData_" + this . configuration . SheetName ;
78
90
}
79
91
80
92
getSheetUrl ( ) {
81
- return "/me/drive/items/" + this . documentId + "/workbook/worksheets/" + this . config . SheetName ;
93
+ return (
94
+ "/me/drive/items/" + this . documentId + "/workbook/worksheets/" + this . configuration . SheetName
95
+ ) ;
82
96
}
83
97
84
98
login ( config : { } ) : boolean {
@@ -171,7 +185,7 @@ export default class Office extends Adapter {
171
185
// a "ItemNowFound" in this exact promise, while trying to get data about the table. This seems like a
172
186
// server-side error. Thats why scanning for the exact error code eliminates the problem, it gets into a loop
173
187
// until (usually in about 20s) the server would finally handle itself and behave correctly.
174
- // poor user input isn't handled here, so it shouldn 't trigger an infinite loop.
188
+ // poor user input isn't handled here, so it wouldn 't trigger an infinite loop.
175
189
this . checkForExpectedErrors ( err , false ) || err . code === "ItemNotFound"
176
190
? request ( )
177
191
: reject ( err ) ;
@@ -285,7 +299,7 @@ export default class Office extends Adapter {
285
299
. catch ( err => {
286
300
this . checkForExpectedErrors ( err )
287
301
? request ( )
288
- : console . log ( "\nERROR: Document doesn 't exist .\n" ) || reject ( err ) ;
302
+ : console . log ( "\nERROR: Couldn 't find the document .\n" ) && reject ( err ) ;
289
303
} ) ;
290
304
} ;
291
305
request ( ) ;
@@ -369,8 +383,6 @@ export default class Office extends Adapter {
369
383
// if promise successful, reAddItem should resolve a boolean value equal to false
370
384
// pass it to updateItem in order to update row values without triggering item re-adding loop.
371
385
updateItem ( res ) ;
372
- // TODO in case of UnknownError happening somewhere, this could be set to True to
373
- // retry continuously until everything works
374
386
} )
375
387
. catch ( err => {
376
388
this . checkForExpectedErrors ( err ) ? request ( ) : reject ( err ) ;
@@ -422,7 +434,11 @@ export default class Office extends Adapter {
422
434
private async getSheetHeader ( ) {
423
435
// trying first process.env.ResultStorageMaximumColumns number of columns to get headers.
424
436
const inputRange =
425
- "'" + xl . getExcelCellRef ( 1 , 1 ) + ":" + xl . getExcelCellRef ( 1 , this . config . MaxColumns ) + "'" ;
437
+ "'" +
438
+ xl . getExcelCellRef ( 1 , 1 ) +
439
+ ":" +
440
+ xl . getExcelCellRef ( 1 , this . configuration . MaxColumns ) +
441
+ "'" ;
426
442
return new Promise ( ( resolve , reject ) => {
427
443
const request = ( ) => {
428
444
this . client
@@ -443,25 +459,27 @@ export default class Office extends Adapter {
443
459
private async reAddItem ( namedItemId : string , values : any [ ] ) {
444
460
const sheetUrl = this . getSheetUrl ( ) ;
445
461
let namedItemRowIndex = Office . DEFAULT_ROW_INDEX ;
446
- // get row number from existing named item
447
462
return new Promise ( ( resolve , reject ) => {
448
- // TODO in case UnknownError comes up, use resolve(true). it will re-add an item continuously until the error is no longer present
449
463
// define tryReAdding reference to execute later
450
464
const tryReAdding = ( RowIndex : number ) => {
451
465
const request = ( ) => {
466
+ // deleting old item to insert a new, correct one, right after
452
467
this . client
453
468
. api ( sheetUrl + "/names/" + namedItemId )
454
469
. delete ( )
455
470
. then ( ( row : GraphTypes . WorkbookTableRow ) => {
456
- console . log ( namedItemId + " named item deleted, trying to add new range..." ) ;
457
- // adding new range of an item, which is longer than the old one
471
+ console . log (
472
+ namedItemId + " named item deleted, trying to add new range on row %s..." ,
473
+ RowIndex
474
+ ) ;
458
475
const request = ( ) => {
476
+ // adding new range of an item, which is different than the old one
459
477
this . client
460
478
. api ( sheetUrl + "/names/add" )
461
479
. post ( {
462
480
name : namedItemId ,
463
- // building reference. format: ' =List1!$A$5:$F$5'
464
- reference : `=${ this . config . SheetName } !$A$${ RowIndex } :$${ xl . getExcelAlpha (
481
+ // building reference. format example: =List1!$A$5:$F$5
482
+ reference : `=${ this . getTableName ( ) } !$A$${ RowIndex } :$${ xl . getExcelAlpha (
465
483
values . length
466
484
) } $${ RowIndex } `
467
485
} )
@@ -503,7 +521,7 @@ export default class Office extends Adapter {
503
521
. catch ( err => {
504
522
if ( this . checkForExpectedErrors ( err , false ) ) request ( ) ;
505
523
else {
506
- // if retreiving row index was unsuccessful, just set it to a default value and continue.
524
+ // if retrieving row index was unsuccessful, just set it to a default value and continue.
507
525
namedItemRowIndex = Office . DEFAULT_ROW_INDEX ;
508
526
console . log (
509
527
"couldn't retrieve row index, because of exception: " ,
@@ -536,9 +554,8 @@ export default class Office extends Adapter {
536
554
. post ( {
537
555
name : namedItemId ,
538
556
// building reference. format: '=List1!$A$5:$F$5'
539
- reference : `=${ this . config . SheetName } !$A$${ row . index + 2 } :$${ xl . getExcelAlpha (
540
- values . length
541
- ) } $${ row . index + 2 } `
557
+ reference : `=${ this . configuration . SheetName } !$A$${ row . index +
558
+ 2 } :$${ xl . getExcelAlpha ( values . length ) } $${ row . index + 2 } `
542
559
} )
543
560
. then ( ( namedItem : GraphTypes . WorkbookNamedItem ) => {
544
561
console . log ( "named item added" ) ;
0 commit comments