2022-09-08 14:36:18 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
2022-12-16 15:38:59 +00:00
|
|
|
import type { DirectoryType } from '@boxyhq/saml-jackson';
|
2022-09-08 14:36:18 +00:00
|
|
|
import jackson from '@lib/jackson';
|
2024-03-06 20:14:14 +00:00
|
|
|
import { parsePaginateApiParams } from '@lib/utils';
|
2022-09-08 14:36:18 +00:00
|
|
|
|
2022-12-29 13:31:50 +00:00
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
2022-09-08 14:36:18 +00:00
|
|
|
const { method } = req;
|
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case 'POST':
|
2022-12-27 12:26:36 +00:00
|
|
|
return await handlePOST(req, res);
|
|
|
|
case 'GET':
|
|
|
|
return await handleGET(req, res);
|
2022-09-08 14:36:18 +00:00
|
|
|
default:
|
2022-12-27 12:26:36 +00:00
|
|
|
res.setHeader('Allow', 'POST, GET');
|
2022-12-07 19:02:27 +00:00
|
|
|
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
|
2022-09-08 14:36:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handlePOST = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
const { directorySyncController } = await jackson();
|
|
|
|
|
2023-06-15 13:31:38 +00:00
|
|
|
const { name, tenant, product, type, webhook_url, webhook_secret, google_domain } = req.body;
|
2022-09-08 14:36:18 +00:00
|
|
|
|
|
|
|
const { data, error } = await directorySyncController.directories.create({
|
|
|
|
name,
|
|
|
|
tenant,
|
|
|
|
product,
|
|
|
|
type: type as DirectoryType,
|
|
|
|
webhook_url,
|
|
|
|
webhook_secret,
|
2023-06-15 13:31:38 +00:00
|
|
|
google_domain,
|
2022-09-08 14:36:18 +00:00
|
|
|
});
|
|
|
|
|
2022-12-07 19:02:27 +00:00
|
|
|
if (data) {
|
|
|
|
return res.status(201).json({ data });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return res.status(error.code).json({ error });
|
|
|
|
}
|
2022-09-08 14:36:18 +00:00
|
|
|
};
|
|
|
|
|
2022-12-27 12:26:36 +00:00
|
|
|
const handleGET = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
const { directorySyncController } = await jackson();
|
|
|
|
|
2024-03-06 20:14:14 +00:00
|
|
|
const { pageOffset, pageLimit, pageToken } = parsePaginateApiParams(req.query);
|
2022-12-27 12:26:36 +00:00
|
|
|
|
2023-03-16 21:42:36 +00:00
|
|
|
const {
|
|
|
|
data,
|
|
|
|
error,
|
|
|
|
pageToken: nextPageToken,
|
|
|
|
} = await directorySyncController.directories.getAll({
|
2024-03-06 20:14:14 +00:00
|
|
|
pageOffset,
|
|
|
|
pageLimit,
|
2023-03-16 21:42:36 +00:00
|
|
|
pageToken,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (nextPageToken) {
|
|
|
|
res.setHeader('jackson-pagetoken', nextPageToken);
|
|
|
|
}
|
2022-12-27 12:26:36 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
return res.status(200).json({ data });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return res.status(error.code).json({ error });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-06 23:37:08 +00:00
|
|
|
export default handler;
|