Skip to content

Commit 220fc72

Browse files
author
Olha Virolainen
authored
Add expand child for getFolder (#33)
Add expand child for getFolder
1 parent 681677f commit 220fc72

File tree

8 files changed

+80
-8
lines changed

8 files changed

+80
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.1 (July 31, 2020)
2+
3+
* Fix retrieving mail folders issue
4+
15
# 1.0.0 (July 29, 2020)
26

37
* Use this.logger functionality instead of console.log

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ Name|Mandatory|Description|Values|
5353
|`OAUTH_CLIENT_ID`| true | Microsoft Graph Application OAuth2 Client ID | Can be found in your application page on [https://portal.azure.com](https://portal.azure.com) |
5454
|`OAUTH_CLIENT_SECRET`| true | Microsoft Graph Application OAuth2 Client Secret | Can be found in your application page on [https://portal.azure.com](https://portal.azure.com) |
5555
|`LOG_LEVEL`| false | Controls logger level | `trace`, `debug`, `info`, `warn`, `error` |
56-
|`MAIL_RETRIEVE_MAX_COUNT`| false | Define max count mails could be retrieved per one `Poll for New Mail` trigger execution. Default to 1000| 1000 |
56+
|`MAIL_RETRIEVE_MAX_COUNT`| false | Define max count mails could be retrieved per one `Poll for New Mail` trigger execution. Defaults to 1000| 1000 |
57+
|`TOP_LIST_MAIL_FOLDER`| false | Define the maximum number of folders that can be found for dropdown fields containing a list of Mail Folder. Defaults to 100| 100 |
5758

5859
## Credentials
5960
To create new credentials you need to authorize in Microsoft system using OAuth2 protocol - details are described in [Requirements](#requirements) section.

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"label": "Start Time",
7272
"viewClass": "TextFieldView",
7373
"required": false,
74-
"note": "Default: minimum time",
74+
"note": "Default: minimum time 1970-01-01T00:00:00.000Z",
7575
"placeholder": "2020-07-20T11:44:53.999Z"
7676
},
7777
"pollOnlyUnreadMail": {

lib/Client.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { OAuth2RestClient } = require('@elastic.io/component-commons-library');
22

33
const MAIL_RETRIEVE_MAX_COUNT = process.env.MAIL_RETRIEVE_MAX_COUNT || 1000;
4+
const TOP_LIST_MAIL_FOLDER = process.env.TOP_LIST_MAIL_FOLDER || 100;
45

56
class Client {
67
constructor(emitter, cfg) {
@@ -22,7 +23,16 @@ class Client {
2223

2324
async getMailFolders() {
2425
const response = await this.restClient.makeRequest({
25-
url: '/me/mailFolders',
26+
url: `/me/mailFolders?$top=${TOP_LIST_MAIL_FOLDER}`,
27+
method: 'GET',
28+
});
29+
30+
return response.value;
31+
}
32+
33+
async listChildFolders(parentId) {
34+
const response = await this.restClient.makeRequest({
35+
url: `/me/mailFolders/${parentId}/childFolders?$top=${TOP_LIST_MAIL_FOLDER}`,
2636
method: 'GET',
2737
});
2838

lib/utils/selectViewModels.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1+
/* eslint-disable no-restricted-syntax,no-param-reassign,no-await-in-loop */
12
const { Client } = require('../Client');
23

4+
async function getChildFolders(client, parentId, result) {
5+
const childResults = await client.listChildFolders(parentId);
6+
for (const childResult of childResults) {
7+
result[childResult.id] = childResult.displayName;
8+
if (childResult.childFolderCount > 0) {
9+
await getChildFolders(client, childResult.id, result);
10+
}
11+
}
12+
}
13+
314
exports.getFolders = async function getFolders(cfg) {
415
const client = new Client(this, cfg);
516
this.logger.info('Getting list of folders...');
617
const items = await client.getMailFolders();
718
this.logger.trace('Found folders: %j', items);
819
this.logger.info('Processing list of folders...');
920
const result = {};
10-
items.forEach((item) => {
21+
for (const item of items) {
1122
result[item.id] = item.displayName;
12-
});
23+
if (item.childFolderCount > 0) {
24+
await getChildFolders(client, item.id, result);
25+
}
26+
}
1327
this.logger.trace('Result: %j', result);
1428
return result;
1529
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "outlook-component",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "elastic.io integration component for Office 365 Outlook REST API",
55
"homepage": "http://www.outlook.com/",
66
"license": "Apache-2.0",

spec/utils/selectViewModel.spec.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Outlook Read Mail', () => {
1717
const refreshTokenUri = 'https://login.microsoftonline.com';
1818
const refreshTokenApi = '/common/oauth2/v2.0/token';
1919
const microsoftGraphUri = 'https://graph.microsoft.com/v1.0';
20-
const microsoftGraphMailFolders = '/me/mailFolders';
20+
const microsoftGraphMailFolders = '/me/mailFolders?$top=100';
2121

2222
let self;
2323
let cfg;
@@ -48,4 +48,47 @@ describe('Outlook Read Mail', () => {
4848
expect(scope1.isDone()).to.eql(true);
4949
expect(scope2.isDone()).to.eql(true);
5050
});
51+
52+
it('getFolder test with child', async () => {
53+
const parentFolder = {
54+
id: 1,
55+
displayName: 'Level 1',
56+
childFolderCount: 1,
57+
};
58+
const childFolder = {
59+
id: 2,
60+
displayName: 'Level 2',
61+
childFolderCount: 1,
62+
};
63+
const childChildFolder = {
64+
id: 3,
65+
displayName: 'Level 3',
66+
childFolderCount: 0,
67+
};
68+
const scope1 = nock(refreshTokenUri).post(refreshTokenApi)
69+
.reply(200, {
70+
access_token: 1,
71+
expires_in: 3600,
72+
});
73+
74+
const scope2 = nock(microsoftGraphUri).get(microsoftGraphMailFolders)
75+
.reply(200, { value: [parentFolder] });
76+
77+
const scope3 = nock(microsoftGraphUri).get(`/me/mailFolders/${parentFolder.id}/childFolders?$top=100`)
78+
.reply(200, { value: [childFolder] });
79+
80+
const scope4 = nock(microsoftGraphUri).get(`/me/mailFolders/${childFolder.id}/childFolders?$top=100`)
81+
.reply(200, { value: [childChildFolder] });
82+
83+
const result = await getFolders.call(self, cfg);
84+
expect(result).to.eql({
85+
1: 'Level 1',
86+
2: 'Level 2',
87+
3: 'Level 3',
88+
});
89+
expect(scope1.isDone()).to.eql(true);
90+
expect(scope2.isDone()).to.eql(true);
91+
expect(scope3.isDone()).to.eql(true);
92+
expect(scope4.isDone()).to.eql(true);
93+
});
5194
});

0 commit comments

Comments
 (0)