-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
113 lines (99 loc) · 3.37 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var graph = require('@microsoft/microsoft-graph-client');
require('isomorphic-fetch');
module.exports = {
getUserDetails: async (msalClient, userId)=> {
const client = getAuthenticatedClient(msalClient, userId);
const user = await client
.api('/me')
.select('displayName,mail,mailboxSettings,userPrincipalName')
.get();
return user;
},
getUserMails: async function(msalClient, userId, mailId= null, subject= null) {
var filterString = ``;
if (mailId && subject) {
filterString= `(sender/emailAddress/address) eq '${mailId}' and contains(subject, '${subject}')`
}else if (mailId) {
filterString= `(sender/emailAddress/address) eq '${mailId}'`
}else if (subject) {
filterString= `contains(subject, '${subject}')`
}
console.log(filterString);
const client = getAuthenticatedClient(msalClient, userId);
console.log(3.0, mailId)
// working filters:
// "(sender/emailAddress/address) eq 'pranavvohra2000@gmail.com' and contains(subject, 'hello')"
const mess = client.api('/me/messages')
.select('subject,sender,toRecipients,receivedDateTime')
.filter(filterString)
.get();
return mess;
},
sendMail: async(msalClient, userId, mailId= null, subject= null, body= null)=> {
try {
if(mailId){
const client = getAuthenticatedClient(msalClient, userId);
const emailObject= {
"message": {
"subject": subject || "no subject!",
"body": {
"contentType": "Text",
"content": body || "no email body!"
},
"toRecipients": [
{
"emailAddress": {
"address": mailId
}
}
]
}
};
const res= await client.api('me/sendMail').post(emailObject);
return 'sent';
}else{
return 'no mail id';
}
} catch (error) {
console.log(error);
}
}
};
const getAuthenticatedClient= (msalClient, userId)=> {
if (!msalClient || !userId) {
throw new Error(
`Invalid MSAL state. Client: ${msalClient ? 'present' : 'missing'}, User ID: ${userId ? 'present' : 'missing'}`);
}
// Initialize Graph client
const client = graph.Client.init({
// Implement an auth provider that gets a token
// from the app's MSAL instance
authProvider: async (done) => {
try {
// Get the user's account
const account = await msalClient
.getTokenCache()
.getAccountByHomeId(userId);
if (account) {
// Attempt to get the token silently
// This method uses the token cache and
// refreshes expired tokens as needed
const response = await msalClient.acquireTokenSilent({
scopes: process.env.OAUTH_SCOPES.split(','),
redirectUri: process.env.OAUTH_REDIRECT_URI,
account: account
});
// First param to callback is the error,
// Set to null in success case
done(null, response.accessToken);
}
} catch (err) {
console.log(JSON.stringify(err, Object.getOwnPropertyNames(err)));
done(err, null);
}
}
});
return client;
}