Created
March 14, 2022 18:21
-
-
Save tomjohndesign/056f2f5eb5cb66cac4d384b16a1cb085 to your computer and use it in GitHub Desktop.
A snippet to help with domain based access at Basedash
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
const updateWorkspaceAccessDomain = async (req: Request, res: Response) => { | |
const { userId, workspaceId, accessDomain } = req.body; | |
// Get user | |
const user = await getUserById(userId); | |
// Get workspace | |
const workspace = await getWorkspaceById(workspaceId); | |
// Check that the accessDomain is valid, otherwise return an Error | |
if (!isValidAccessDomain(accessDomain)) { | |
return res.status(400).json({ title: 'Access domain is invalid' }); | |
} | |
try { | |
// If the access domain is null, this means that the user | |
// requested to Remove the access domain / email domain | |
if (accessDomain === null) { | |
// Remove the access domain | |
await removeAccessDomain(workspace); | |
// If all goes well return a status of 200! | |
return res.sendStatus(200); | |
} | |
// Update the workspace's access domain | |
await updateWorkspaceAccessDomain(workspace); | |
// Find all users with the matching access domain | |
const usersWithMatchingDomain = await getUsersWithMatchingDomain( | |
accessDomain | |
); | |
// Filter out users who are not already members of the workspace | |
const usersToAddToWorkspaces = await getUsersWhoAreNotMembers( | |
usersWithMatchingDomain, | |
workspace | |
); | |
// Iterate through array of users who are not already members | |
for (const user of usersToAddToWorkspaces) { | |
// Add the user as a collaborator member to the workspace | |
await addUserAsCollaborator(user, workspace); | |
// Send an email notification that they have been added to | |
// the workspace as a collaborator member | |
await sendAddedToWorkspaceEmailNotification(user); | |
} | |
// If all goes well return a status of 200! | |
return res.sendStatus(200); | |
} catch (error) { | |
if (error instanceof Error) { | |
// Catch and return any errors | |
return res.status(400).json({ title: error.message }); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment