forked from boxyhq/saas-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Deepak Prabhakara <[email protected]>
- Loading branch information
1 parent
ceb0ebb
commit d307c34
Showing
7 changed files
with
238 additions
and
163 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import env from '@/lib/env'; | ||
import { throwIfNoTeamAccess } from 'models/team'; | ||
import { throwIfNotAllowed } from 'models/user'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { ApiError } from '@/lib/errors'; | ||
import { | ||
deleteDirectoryConnection, | ||
getDirectoryConnections, | ||
patchDirectoryConnection, | ||
} from '@/lib/jackson/dsync'; | ||
import { sendAudit } from '@/lib/retraced'; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const { method } = req; | ||
|
||
try { | ||
if (!env.teamFeatures.dsync) { | ||
throw new ApiError(404, 'Not Found'); | ||
} | ||
|
||
switch (method) { | ||
case 'GET': | ||
await handleGET(req, res); | ||
break; | ||
case 'PATCH': | ||
await handlePATCH(req, res); | ||
break; | ||
case 'DELETE': | ||
await handleDELETE(req, res); | ||
break; | ||
default: | ||
res.setHeader('Allow', 'GET, PATCH, DELETE'); | ||
res.status(405).json({ | ||
error: { message: `Method ${method} Not Allowed` }, | ||
}); | ||
} | ||
} catch (error: any) { | ||
console.error(error); | ||
|
||
const message = error.message || 'Something went wrong'; | ||
const status = error.status || 500; | ||
|
||
res.status(status).json({ error: { message } }); | ||
} | ||
} | ||
|
||
const handleGET = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const teamMember = await throwIfNoTeamAccess(req, res); | ||
|
||
throwIfNotAllowed(teamMember, 'team_dsync', 'read'); | ||
|
||
const connection = await getDirectoryConnections({ | ||
dsyncId: req.query.directoryId as string, | ||
}); | ||
|
||
res.status(200).json({ data: connection }); | ||
}; | ||
|
||
const handlePATCH = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const teamMember = await throwIfNoTeamAccess(req, res); | ||
|
||
throwIfNotAllowed(teamMember, 'team_dsync', 'read'); | ||
|
||
const body = { ...req.query, ...req.body }; | ||
|
||
const connection = await patchDirectoryConnection(body); | ||
|
||
res.status(200).json({ data: connection }); | ||
}; | ||
|
||
const handleDELETE = async (req: NextApiRequest, res: NextApiResponse) => { | ||
const teamMember = await throwIfNoTeamAccess(req, res); | ||
|
||
throwIfNotAllowed(teamMember, 'team_dsync', 'delete'); | ||
|
||
const params = req.query; | ||
|
||
await deleteDirectoryConnection(params); | ||
|
||
sendAudit({ | ||
action: 'dsync.connection.delete', | ||
crud: 'd', | ||
user: teamMember.user, | ||
team: teamMember.team, | ||
}); | ||
|
||
res.status(200).json({ data: {} }); | ||
}; |
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
Oops, something went wrong.