Skip to content

Commit

Permalink
[ms-connector] Add oauth connection (#5803)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier authored Jun 24, 2024
1 parent 254892f commit 67e6d74
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
42 changes: 39 additions & 3 deletions connectors/src/connectors/microsoft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,55 @@ import type {
NangoConnectionId,
Result,
} from "@dust-tt/types";
import { Ok } from "@dust-tt/types";
import { Err, Ok } from "@dust-tt/types";
import { Client } from "@microsoft/microsoft-graph-client";

import type { ConnectorPermissionRetriever } from "@connectors/connectors/interface";
import { microsoftConfig } from "@connectors/connectors/microsoft/lib/config";
import {
getSites,
getTeams,
} from "@connectors/connectors/microsoft/lib/graph_api";
import { launchMicrosoftFullSyncWorkflow } from "@connectors/connectors/microsoft/temporal/client";
import { getAccessTokenFromNango } from "@connectors/lib/nango_helpers";
import { syncSucceeded } from "@connectors/lib/sync_status";
import logger from "@connectors/logger/logger";
import { ConnectorResource } from "@connectors/resources/connector_resource";
import type { DataSourceConfig } from "@connectors/types/data_source_config";

async function getClient(connectionId: NangoConnectionId) {
const nangoConnectionId = connectionId;

const msAccessToken = await getAccessTokenFromNango({
connectionId: nangoConnectionId,
integrationId: microsoftConfig.getRequiredNangoMicrosoftConnectorId(),
useCache: false,
});

return Client.init({
authProvider: (done) => done(null, msAccessToken),
});
}

export async function createMicrosoftConnector(
dataSourceConfig: DataSourceConfig,
connectionId: NangoConnectionId
): Promise<Result<string, Error>> {
console.log("createMicrosoftConnector", dataSourceConfig, connectionId);
) {
const client = await getClient(connectionId);

try {
// Sanity checks - check connectivity and permissions. User should be able to access the sites and teams list.
await getSites(client);
await getTeams(client);
} catch (err) {
logger.error(
{
err,
},
"Error creating Microsoft connector"
);
return new Err(new Error("Error creating Microsoft connector"));
}

const connector = await ConnectorResource.makeNew(
"microsoft",
Expand Down
7 changes: 7 additions & 0 deletions connectors/src/connectors/microsoft/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { EnvironmentConfig } from "@dust-tt/types";

export const microsoftConfig = {
getRequiredNangoMicrosoftConnectorId: (): string => {
return EnvironmentConfig.getEnvVariable("NANGO_MICROSOFT_CONNECTOR_ID");
},
};
73 changes: 73 additions & 0 deletions connectors/src/connectors/microsoft/lib/graph_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { Client } from "@microsoft/microsoft-graph-client";
import type * as MicrosoftGraph from "@microsoft/microsoft-graph-types";

export async function getSites(client: Client): Promise<MicrosoftGraph.Site[]> {
const res = await client
.api("/sites?search=*")
.select("id,name,displayName")
.get();
return res.value;
}

export async function getDrives(
client: Client,
siteId: string
): Promise<MicrosoftGraph.Drive[]> {
const res = await client
.api(`/sites/${siteId}/drives`)
.select("id,name")
.get();
return res.value;
}

export async function getFilesAndFolders(
client: Client,
driveId: string,
parentId?: string
): Promise<MicrosoftGraph.DriveItem[]> {
const parent = parentId ? `items/${parentId}` : "root";
const res = await client
.api(`/drives/${driveId}/${parent}/children`)
.select("id,displayName")
.get();
return res.value;
}

export async function getFolders(
client: Client,
driveId: string,
parentId?: string
): Promise<MicrosoftGraph.DriveItem[]> {
const res = await getFilesAndFolders(client, driveId, parentId);
return res.filter((item) => item.folder);
}

export async function getTeams(client: Client): Promise<MicrosoftGraph.Team[]> {
const res = await client
.api("/me/joinedTeams")
.select("id,displayName")
.get();
return res.value;
}

export async function getChannels(
client: Client,
teamId: string
): Promise<MicrosoftGraph.Channel[]> {
const res = await client
.api(`/teams/${teamId}/channels`)
.select("id,displayName")
.get();
return res.value;
}

export async function getMessages(
client: Client,
teamId: string,
channelId: string
): Promise<MicrosoftGraph.Message[]> {
const res = await client
.api(`/teams/${teamId}/channels/${channelId}/messages`)
.get();
return res.value;
}

0 comments on commit 67e6d74

Please sign in to comment.