Skip to content

Commit 4705140

Browse files
Update new-email.mjs (#15001)
* Update new-email.mjs Allow for all folders in Outlook to be checked for new emails * Update new-email.mjs * updates * pnpm-lock.yaml * updates * pnpm-lock.yaml * updates * pnpm-lock.yaml * remove console.log * pnpm-lock.yaml --------- Co-authored-by: michelle0927 <michellelbergero@hotmail.com>
1 parent f8a44e3 commit 4705140

File tree

11 files changed

+121
-37
lines changed

11 files changed

+121
-37
lines changed

components/clear_books/clear_books.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/dixa/dixa.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/egnyte/egnyte.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/linkup/linkup.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/mailboxlayer/mailboxlayer.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/microsoft_outlook/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/microsoft_outlook",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Pipedream Microsoft Outlook Components",
55
"main": "microsoft_outlook.app.mjs",
66
"keywords": [
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"axios": "^0.21.1",
1616
"js-base64": "^3.7.2",
17+
"md5": "^2.3.0",
1718
"mime-types": "^2.1.35"
1819
},
1920
"publishConfig": {
Lines changed: 105 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
import common from "../common.mjs";
2+
import md5 from "md5";
23
import sampleEmit from "./test-event.mjs";
34

45
export default {
56
...common,
67
key: "microsoft_outlook-new-email",
78
name: "New Email Event (Instant)",
8-
description: "Emit new event when an email received",
9-
version: "0.0.9",
9+
description: "Emit new event when an email is received in specified folders.",
10+
version: "0.0.10",
1011
type: "source",
12+
dedupe: "unique",
13+
props: {
14+
...common.props,
15+
folderIds: {
16+
type: "string[]",
17+
label: "Folder IDs to Monitor",
18+
description: "Specify the folder IDs or names in Outlook that you want to monitor for new emails. Leave empty to monitor all folders (excluding \"Sent Items\" and \"Drafts\").",
19+
optional: true,
20+
async options() {
21+
const { value: folders } = await this.listFolders();
22+
return folders?.map(({
23+
id: value, displayName: label,
24+
}) => ({
25+
value,
26+
label,
27+
})) || [];
28+
},
29+
},
30+
},
1131
hooks: {
1232
...common.hooks,
33+
async deploy() {
34+
this.db.set("sentItemFolderId", await this.getFolderIdByName("Sent Items"));
35+
this.db.set("draftsFolderId", await this.getFolderIdByName("Drafts"));
36+
37+
const events = await this.getSampleEvents({
38+
pageSize: 25,
39+
});
40+
if (!events || events.length == 0) {
41+
return;
42+
}
43+
for (const item of events) {
44+
this.emitEvent(item);
45+
}
46+
},
1347
async activate() {
1448
await this.activate({
1549
changeType: "created",
16-
resource: "/me/mailfolders('inbox')/messages",
50+
resource: "/me/messages",
1751
});
1852
},
1953
async deactivate() {
@@ -22,37 +56,86 @@ export default {
2256
},
2357
methods: {
2458
...common.methods,
25-
async getSampleEvents({ pageSize }) {
26-
return this.microsoftOutlook.listMessages({
27-
params: {
28-
$top: pageSize,
29-
$orderby: "createdDateTime desc",
30-
},
59+
listFolders() {
60+
return this.microsoftOutlook._makeRequest({
61+
path: "/me/mailFolders",
3162
});
3263
},
64+
async getFolderIdByName(name) {
65+
const { value: folders } = await this.listFolders();
66+
const { id } = folders.find(({ displayName }) => displayName === name);
67+
return id;
68+
},
69+
async getSampleEvents({ pageSize }) {
70+
const folders = this.folderIds?.length
71+
? this.folderIds.map((id) => `/me/mailFolders/${id}/messages`)
72+
: [
73+
"/me/messages",
74+
];
75+
76+
const results = [];
77+
for (const folder of folders) {
78+
const { value: messages } = await this.microsoftOutlook.listMessages({
79+
resource: folder,
80+
params: {
81+
$top: pageSize,
82+
$orderby: "createdDateTime desc",
83+
},
84+
});
85+
results.push(...messages);
86+
}
87+
return results;
88+
},
89+
isRelevant(item) {
90+
if (this.folderIds?.length) {
91+
return this.folderIds.includes(item.parentFolderId);
92+
}
93+
// if no folderIds are specified, filter out items in Sent Items & Drafts
94+
const sentItemFolderId = this.db.get("sentItemFolderId");
95+
const draftsFolderId = this.db.get("draftsFolderId");
96+
return item.parentFolderId !== sentItemFolderId && item.parentFolderId !== draftsFolderId;
97+
},
3398
emitEvent(item) {
34-
this.$emit({
35-
email: item,
36-
}, this.generateMeta(item));
99+
if (this.isRelevant(item)) {
100+
this.$emit(
101+
{
102+
email: item,
103+
},
104+
this.generateMeta(item),
105+
);
106+
}
37107
},
38108
generateMeta(item) {
39109
return {
40-
id: item.id,
110+
id: md5(item.id), // id > 64 characters, so dedupe on hash of id
41111
summary: `New email (ID:${item.id})`,
42112
ts: Date.parse(item.createdDateTime),
43113
};
44114
},
45115
},
46116
async run(event) {
47-
await this.run({
48-
event,
49-
emitFn: async ({ resourceId } = {}) => {
50-
const item = await this.microsoftOutlook.getMessage({
51-
messageId: resourceId,
52-
});
53-
this.emitEvent(item);
54-
},
55-
});
117+
const folders = this.folderIds?.length
118+
? this.folderIds.map((id) => `/me/mailFolders/${id}/messages`)
119+
: [
120+
"/me/messages",
121+
];
122+
123+
for (const folder of folders) {
124+
await this.run({
125+
event,
126+
emitFn: async ({ resourceId } = {}) => {
127+
try {
128+
const item = await this.microsoftOutlook.getMessage({
129+
resource: folder,
130+
messageId: resourceId,
131+
});
132+
this.emitEvent(item);
133+
} catch {
134+
console.log(`Could not fetch message with ID: ${resourceId}`);
135+
}
136+
},
137+
});
138+
}
56139
},
57140
sampleEmit,
58141
};

components/nextdoor/nextdoor.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/osu/osu.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

components/uber_direct/uber_direct.app.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export default {
88
console.log(Object.keys(this.$auth));
99
},
1010
},
11-
};
11+
};

0 commit comments

Comments
 (0)