Skip to content

Commit 07d7056

Browse files
committed
fixed TypeScript errors
1 parent b54c800 commit 07d7056

File tree

4 files changed

+66
-59
lines changed

4 files changed

+66
-59
lines changed

src/models/adapter.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@ export default abstract class Adapter {
22
adapterId: string;
33
documentId: string;
44
config: {
5-
SheetName: string;
6-
ClientId: string;
7-
ClientSecret: string;
8-
RefreshToken: string;
9-
MaxColumns?: number;
5+
credentials: {};
106
};
117

128
constructor(
139
documentId: string,
1410
config: {
15-
SheetName: string;
16-
ClientId: string;
17-
ClientSecret: string;
18-
RefreshToken: string;
19-
MaxColumns?: number;
11+
credentials: {};
2012
}
2113
) {
2214
this.documentId = documentId;

src/models/adapters/office.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import Adapter from "../adapter";
22
import * as GraphTypes from "@microsoft/microsoft-graph-types";
33
import * as Graph from "@microsoft/microsoft-graph-client";
44
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";
710
import { Error } from "mongoose";
811
import { resolve } from "url";
912
import { Bool } from "../../../node_modules/aws-sdk/clients/inspector";
@@ -12,33 +15,42 @@ import { MaxKey } from "../../../node_modules/@types/bson";
1215
export default class Office extends Adapter {
1316
client: Graph.Client;
1417
accessToken: string;
15-
sheetUrl: string;
18+
configuration: {
19+
SheetName: string;
20+
ClientId: string;
21+
ClientSecret: string;
22+
RefreshToken: string;
23+
MaxColumns: number;
24+
};
1625

1726
// default first row in the table is row index 4
1827
public static DEFAULT_ROW_INDEX = 4;
1928

2029
constructor(
2130
documentId: string,
2231
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+
};
2839
}
2940
) {
3041
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;
3345
this.adapterId = "office";
3446

3547
console.log(
3648
"Sheet Name: " +
37-
this.config.SheetName +
49+
config.credentials.SheetName +
3850
", Client ID: " +
39-
this.config.ClientId +
51+
config.credentials.ClientId +
4052
", Client Secret: " +
41-
this.config.ClientSecret
53+
config.credentials.ClientSecret
4254
);
4355

4456
this.client = Graph.Client.init({
@@ -51,10 +63,10 @@ export default class Office extends Adapter {
5163
.post("https://login.microsoftonline.com/common/oauth2/v2.0/token")
5264
.type("form")
5365
.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,
5668
grant_type: "refresh_token",
57-
refresh_token: this.config.RefreshToken,
69+
refresh_token: config.credentials.RefreshToken,
5870
scope: "Files.ReadWrite.All offline_access"
5971
})
6072
.then(res => {
@@ -74,11 +86,13 @@ export default class Office extends Adapter {
7486
}
7587

7688
getTableName() {
77-
return "FeedbotData_" + this.config.SheetName;
89+
return "FeedbotData_" + this.configuration.SheetName;
7890
}
7991

8092
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+
);
8296
}
8397

8498
login(config: {}): boolean {
@@ -171,7 +185,7 @@ export default class Office extends Adapter {
171185
// a "ItemNowFound" in this exact promise, while trying to get data about the table. This seems like a
172186
// server-side error. Thats why scanning for the exact error code eliminates the problem, it gets into a loop
173187
// 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.
175189
this.checkForExpectedErrors(err, false) || err.code === "ItemNotFound"
176190
? request()
177191
: reject(err);
@@ -285,7 +299,7 @@ export default class Office extends Adapter {
285299
.catch(err => {
286300
this.checkForExpectedErrors(err)
287301
? request()
288-
: console.log("\nERROR: Document doesn't exist.\n") || reject(err);
302+
: console.log("\nERROR: Couldn't find the document.\n") && reject(err);
289303
});
290304
};
291305
request();
@@ -369,8 +383,6 @@ export default class Office extends Adapter {
369383
// if promise successful, reAddItem should resolve a boolean value equal to false
370384
// pass it to updateItem in order to update row values without triggering item re-adding loop.
371385
updateItem(res);
372-
// TODO in case of UnknownError happening somewhere, this could be set to True to
373-
// retry continuously until everything works
374386
})
375387
.catch(err => {
376388
this.checkForExpectedErrors(err) ? request() : reject(err);
@@ -422,7 +434,11 @@ export default class Office extends Adapter {
422434
private async getSheetHeader() {
423435
// trying first process.env.ResultStorageMaximumColumns number of columns to get headers.
424436
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+
"'";
426442
return new Promise((resolve, reject) => {
427443
const request = () => {
428444
this.client
@@ -443,25 +459,27 @@ export default class Office extends Adapter {
443459
private async reAddItem(namedItemId: string, values: any[]) {
444460
const sheetUrl = this.getSheetUrl();
445461
let namedItemRowIndex = Office.DEFAULT_ROW_INDEX;
446-
// get row number from existing named item
447462
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
449463
// define tryReAdding reference to execute later
450464
const tryReAdding = (RowIndex: number) => {
451465
const request = () => {
466+
// deleting old item to insert a new, correct one, right after
452467
this.client
453468
.api(sheetUrl + "/names/" + namedItemId)
454469
.delete()
455470
.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+
);
458475
const request = () => {
476+
// adding new range of an item, which is different than the old one
459477
this.client
460478
.api(sheetUrl + "/names/add")
461479
.post({
462480
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(
465483
values.length
466484
)}$${RowIndex}`
467485
})
@@ -503,7 +521,7 @@ export default class Office extends Adapter {
503521
.catch(err => {
504522
if (this.checkForExpectedErrors(err, false)) request();
505523
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.
507525
namedItemRowIndex = Office.DEFAULT_ROW_INDEX;
508526
console.log(
509527
"couldn't retrieve row index, because of exception: ",
@@ -536,9 +554,8 @@ export default class Office extends Adapter {
536554
.post({
537555
name: namedItemId,
538556
// 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}`
542559
})
543560
.then((namedItem: GraphTypes.WorkbookNamedItem) => {
544561
console.log("named item added");

src/services/config.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Config from "../models/config";
2-
import Adapter from "../models/bot";
32

43
export default abstract class ConfigService {
54
config: Config;
@@ -13,14 +12,14 @@ export default abstract class ConfigService {
1312
return new Promise(resolve => resolve(this.config));
1413
} else {
1514
return new Promise(resolve =>
16-
this.load().then((conf: any) => {
17-
this.config = conf;
18-
resolve(conf);
15+
this.load().then((res: any) => {
16+
this.config = res;
17+
resolve(res);
1918
})
2019
);
2120
}
2221
}
2322

24-
load(): Promise<Config>;
25-
save(config: Config): void;
23+
abstract load(): Promise<Config>;
24+
abstract save(config: Config): void;
2625
}

src/services/config/env.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Config from "../../models/config";
22
import ConfigService from "../config";
33
import Google from "../../models/adapters/google";
44
import Office from "../../models/adapters/office";
5+
import { Credentials } from "../../../node_modules/aws-sdk";
56

67
export default class EnvConfigService extends ConfigService {
78
load(): Promise<Config> {
@@ -31,17 +32,15 @@ export default class EnvConfigService extends ConfigService {
3132

3233
// Office365 Excel sheet adapter
3334
if (process.env.ResultStorageExcelSpreadsheetId) {
34-
const name = process.env.ResultStorageExcelSheetName;
35-
const id = process.env.ResultStorageClientId;
36-
const secret = process.env.ResultStorageClientSecret;
37-
const refreshToken = process.env.ResultStorageRefreshToken;
38-
const colNum = process.env.ResultStorageMaximumColumns || 64; // if not specified, set default value of 64
3935
const configProcess = {
40-
SheetName: name,
41-
ClientId: id,
42-
ClientSecret: secret,
43-
RefreshToken: refreshToken,
44-
MaxColumns: colNum
36+
credentials: {
37+
SheetName: process.env.ResultStorageExcelSheetName,
38+
ClientId: process.env.ResultStorageClientId,
39+
ClientSecret: process.env.ResultStorageClientSecret,
40+
RefreshToken: process.env.ResultStorageRefreshToken,
41+
// if max colmuns not specified, set default value of 64
42+
MaxColumns: process.env.ResultStorageMaximumColumns || 64
43+
}
4544
};
4645
config.addAdapter(new Office(process.env.ResultStorageExcelSpreadsheetId, configProcess));
4746
}

0 commit comments

Comments
 (0)