Skip to content

Commit

Permalink
Merge pull request #314 from brendanrmoore/getUserByEmail
Browse files Browse the repository at this point in the history
Add new Graph API method to search a user by email
  • Loading branch information
MichaelRyanWebber authored May 30, 2024
2 parents b833ff1 + 6e6e35f commit 6e7417f
Showing 1 changed file with 59 additions and 35 deletions.
94 changes: 59 additions & 35 deletions api-module-library/microsoft-teams/api/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,30 @@ class graphApi extends OAuth2Requester {

// Assuming team id as a param for now
this.team_id = get(params, 'team_id', null);
this.generateUrls = ()=> {
this.generateUrls = () => {
this.baseUrl = 'https://graph.microsoft.com/v1.0';
this.URLs = {
userDetails: '/me', //https://graph.microsoft.com/v1.0/me
orgDetails: '/organization',
groups: '/groups',
user: (userId) => `/users/${userId}`,
users: '/users',
createChannel: `/teams/${this.team_id}/channels`,
channel: (channelId) => `/teams/${this.team_id}/channels/${channelId}/`,
channel: (channelId) =>
`/teams/${this.team_id}/channels/${channelId}/`,
primaryChannel: `/teams/${this.team_id}/primaryChannel`,
channelMembers: (channelId) => `/teams/${this.team_id}/channels/${channelId}/members`,
installedAppsForUser: (userId) => `/users/${userId}/teamwork/installedApps`,
installedAppsForTeam: (teamId) => `/teams/${teamId}/installedApps`,
channelMembers: (channelId) =>
`/teams/${this.team_id}/channels/${channelId}/members`,
installedAppsForUser: (userId) =>
`/users/${userId}/teamwork/installedApps`,
installedAppsForTeam: (teamId) =>
`/teams/${teamId}/installedApps`,
appCatalog: '/appCatalogs/teamsApps',
};
this.authorizationUri = `https://login.microsoftonline.com/${this.tenant_id}/oauth2/v2.0/authorize`;
this.tokenUri = `https://login.microsoftonline.com/${this.tenant_id}/oauth2/v2.0/token`;
this.adminConsentUrl = `https://login.microsoftonline.com/${this.tenant_id}/adminconsent?client_id=${this.client_id}&redirect_uri=${this.redirect_uri}`
}
this.adminConsentUrl = `https://login.microsoftonline.com/${this.tenant_id}/adminconsent?client_id=${this.client_id}&redirect_uri=${this.redirect_uri}`;
};
this.generateUrls();
}
async getAuthUri() {
Expand All @@ -55,10 +60,13 @@ class graphApi extends OAuth2Requester {
body.append('client_secret', this.client_secret);
body.append('grant_type', 'client_credentials');

const tokenRes = await this._post( {
url,
body,
}, false);
const tokenRes = await this._post(
{
url,
body,
},
false
);

await this.setTokens(tokenRes);
return tokenRes;
Expand All @@ -78,22 +86,32 @@ class graphApi extends OAuth2Requester {
}
async getUser() {
const options = {
url: `${this.baseUrl}${this.URLs.userDetails}`
url: `${this.baseUrl}${this.URLs.userDetails}`,
};
const response = await this._get(options);
return response;
}

async getUserById(userId) {
const options = {
url: `${this.baseUrl}${this.URLs.user(userId)}`
url: `${this.baseUrl}${this.URLs.user(userId)}`,
};
const response = await this._get(options);
return response;
}

async getUserByEmail(email) {
const query = `?$filter=mail eq '${email}'`;
const options = {
url: `${this.baseUrl}${this.URLs.users}${query}`,
};
const response = await this._get(options);
return response.value[0];
}

async getOrganization() {
const options = {
url: `${this.baseUrl}${this.URLs.orgDetails}`
url: `${this.baseUrl}${this.URLs.orgDetails}`,
};
const response = await this._get(options);
return response.value[0];
Expand All @@ -102,27 +120,29 @@ class graphApi extends OAuth2Requester {
async getGroups(query) {
const options = {
url: `${this.baseUrl}${this.URLs.groups}`,
query
query,
};
const response = await this._get(options);
return response;
}

async getTeams() {
// not using the getGroups query passing because the single qoutes need to be encoded
const query = "?$filter=resourceProvisioningOptions/any(c:c+eq+'Team')"
const query = "?$filter=resourceProvisioningOptions/any(c:c+eq+'Team')";
const options = {
url: `${this.baseUrl}${this.URLs.groups}${query}`
url: `${this.baseUrl}${this.URLs.groups}${query}`,
};
const response = await this._get(options);
return response;
}

async getJoinedTeams(userId) {
// no userId is only valid for delgated authentication
const userPart = userId ? this.URLs.user(userId) : this.URLs.userDetails;
const userPart = userId
? this.URLs.user(userId)
: this.URLs.userDetails;
const options = {
url: `${this.baseUrl}${userPart}/joinedTeams`
url: `${this.baseUrl}${userPart}/joinedTeams`,
};
const response = await this._get(options);
return response;
Expand All @@ -131,7 +151,7 @@ class graphApi extends OAuth2Requester {
async getAppCatalog(query) {
const options = {
url: `${this.baseUrl}${this.URLs.appCatalog}`,
query
query,
};
const response = await this._get(options);
return response;
Expand All @@ -141,7 +161,7 @@ class graphApi extends OAuth2Requester {
//this is also valid for /me but not implementing yet
const options = {
url: `${this.baseUrl}${this.URLs.installedAppsForUser(userId)}`,
query
query,
};
const response = await this._get(options);
return response;
Expand All @@ -151,7 +171,7 @@ class graphApi extends OAuth2Requester {
const options = {
url: `${this.baseUrl}${this.URLs.installedAppsForUser(userId)}`,
body: {
'[email protected]': `https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/${teamsAppId}`
'[email protected]': `https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/${teamsAppId}`,
},
headers: {
'Content-Type': 'application/json',
Expand All @@ -163,7 +183,9 @@ class graphApi extends OAuth2Requester {

async removeAppForUser(userId, teamsAppInstallationId) {
const options = {
url: `${this.baseUrl}${this.URLs.installedAppsForUser(userId)}/${teamsAppInstallationId}`,
url: `${this.baseUrl}${this.URLs.installedAppsForUser(
userId
)}/${teamsAppInstallationId}`,
};
const response = await this._delete(options);
return response;
Expand All @@ -173,7 +195,7 @@ class graphApi extends OAuth2Requester {
//this is also valid for /me but not implementing yet
const options = {
url: `${this.baseUrl}${this.URLs.installedAppsForTeam(teamId)}`,
query
query,
};
const response = await this._get(options);
return response;
Expand All @@ -183,20 +205,22 @@ class graphApi extends OAuth2Requester {
const options = {
url: `${this.baseUrl}${this.URLs.installedAppsForTeam(teamId)}`,
body: {
'[email protected]': `https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/${teamsAppId}`
'[email protected]': `https://graph.microsoft.com/v1.0/appCatalogs/teamsApps/${teamsAppId}`,
},
headers: {
'Content-Type': 'application/json',
},
returnFullRes: true
returnFullRes: true,
};
const response = await this._post(options);
return response;
}

async removeAppForTeam(teamId, teamsAppInstallationId) {
const options = {
url: `${this.baseUrl}${this.URLs.installedAppsForTeam(teamId)}/${teamsAppInstallationId}`,
url: `${this.baseUrl}${this.URLs.installedAppsForTeam(
teamId
)}/${teamsAppInstallationId}`,
};
const response = await this._delete(options);
return response;
Expand All @@ -205,15 +229,15 @@ class graphApi extends OAuth2Requester {
async getChannels(query) {
const options = {
url: `${this.baseUrl}${this.URLs.createChannel}`,
query
query,
};
const response = await this._get(options);
return response;
}

async getPrimaryChannel() {
const options = {
url: `${this.baseUrl}${this.URLs.primaryChannel}`
url: `${this.baseUrl}${this.URLs.primaryChannel}`,
};
const response = await this._get(options);
return response;
Expand All @@ -222,20 +246,20 @@ class graphApi extends OAuth2Requester {
async createChannel(body) {
// creating a private channel as an application requires a member owner to be added at creation
const options = {
url : `${this.baseUrl}${this.URLs.createChannel}`,
url: `${this.baseUrl}${this.URLs.createChannel}`,
body: body,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
}
},
};
const response = await this._post(options);
return response;
}

async deleteChannel(channelId) {
const options = {
url : `${this.baseUrl}${this.URLs.channel(channelId)}`
url: `${this.baseUrl}${this.URLs.channel(channelId)}`,
};
const response = await this._delete(options);
return response;
Expand All @@ -244,20 +268,20 @@ class graphApi extends OAuth2Requester {
async listChannelMembers(channelId) {
//TODO: add search odata options
const options = {
url : `${this.baseUrl}${this.URLs.channelMembers(channelId)}`
url: `${this.baseUrl}${this.URLs.channelMembers(channelId)}`,
};
const response = await this._get(options);
return response;
}

async addUserToChannel(channelId, user) {
const options = {
url : `${this.baseUrl}${this.URLs.channelMembers(channelId)}`,
url: `${this.baseUrl}${this.URLs.channelMembers(channelId)}`,
body: user,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
}
},
};
const response = await this._post(options);
return response;
Expand Down

0 comments on commit 6e7417f

Please sign in to comment.