-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enable slack bot by default, graceful error when enabling bot twice * UI for slack bot toggle * Mange Slack bot self serve
- Loading branch information
Showing
7 changed files
with
307 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
front/pages/api/w/[wId]/data_sources/[name]/managed/bot_enabled.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { isLeft } from "fp-ts/lib/Either"; | ||
import * as t from "io-ts"; | ||
import * as reporter from "io-ts-reporters"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getDataSource } from "@app/lib/api/data_sources"; | ||
import { Authenticator, getSession } from "@app/lib/auth"; | ||
import { ConnectorsAPI } from "@app/lib/connectors_api"; | ||
import { ReturnedAPIErrorType } from "@app/lib/error"; | ||
import { apiError, withLogging } from "@app/logger/withlogging"; | ||
|
||
export const PostBotEnabledRequestBodySchema = t.type({ | ||
botEnabled: t.boolean, | ||
}); | ||
|
||
export type GetOrPostBotEnabledResponseBody = { | ||
botEnabled: boolean; | ||
}; | ||
|
||
async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse< | ||
GetOrPostBotEnabledResponseBody | ReturnedAPIErrorType | void | ||
> | ||
): Promise<void> { | ||
const session = await getSession(req, res); | ||
const auth = await Authenticator.fromSession( | ||
session, | ||
req.query.wId as string | ||
); | ||
|
||
const owner = auth.workspace(); | ||
if (!owner) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "data_source_not_found", | ||
message: "The data source you requested was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
if (!req.query.name || typeof req.query.name !== "string") { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "data_source_not_found", | ||
message: "The data source you requested was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
const dataSource = await getDataSource(auth, req.query.name); | ||
if (!dataSource) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "data_source_not_found", | ||
message: "The data source you requested was not found.", | ||
}, | ||
}); | ||
} | ||
|
||
if (!dataSource.connectorId) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "data_source_error", | ||
message: "The data source you requested is not managed.", | ||
}, | ||
}); | ||
} | ||
|
||
switch (req.method) { | ||
case "GET": | ||
const botEnabledRes = await ConnectorsAPI.getBotEnabled( | ||
dataSource.connectorId | ||
); | ||
|
||
if (botEnabledRes.isErr()) { | ||
return apiError(req, res, { | ||
status_code: 404, | ||
api_error: { | ||
type: "data_source_error", | ||
message: `Failed to retrieve bot enablement: ${botEnabledRes.error.error.message}`, | ||
}, | ||
}); | ||
} | ||
|
||
res.status(200).json(botEnabledRes.value); | ||
return; | ||
|
||
case "POST": | ||
if (!auth.isAdmin()) { | ||
return apiError(req, res, { | ||
status_code: 403, | ||
api_error: { | ||
type: "data_source_auth_error", | ||
message: | ||
"Only the users that are `admins` for the current workspace can edit the (bot) permissions of a data source.", | ||
}, | ||
}); | ||
} | ||
|
||
const bodyValidation = PostBotEnabledRequestBodySchema.decode(req.body); | ||
if (isLeft(bodyValidation)) { | ||
const pathError = reporter.formatValidationErrors(bodyValidation.left); | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "invalid_request_error", | ||
message: `Invalid request body: ${pathError}`, | ||
}, | ||
}); | ||
} | ||
|
||
const setBotEnabledRes = await ConnectorsAPI.setBotEnabled( | ||
dataSource.connectorId, | ||
bodyValidation.right.botEnabled | ||
); | ||
|
||
if (setBotEnabledRes.isErr()) { | ||
return apiError(req, res, { | ||
status_code: 400, | ||
api_error: { | ||
type: "data_source_error", | ||
message: setBotEnabledRes.error.error.message, | ||
}, | ||
}); | ||
} | ||
|
||
res.status(200).json(setBotEnabledRes.value); | ||
return; | ||
|
||
default: | ||
return apiError(req, res, { | ||
status_code: 405, | ||
api_error: { | ||
type: "method_not_supported_error", | ||
message: | ||
"The method passed is not supported, GET or POST is expected.", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export default withLogging(handler); |
Oops, something went wrong.