mirror of https://github.com/boxyhq/jackson.git
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
import jackson from '@lib/jackson';
|
|
import { sendAudit } from '@ee/audit-log/lib/retraced';
|
|
|
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
const { method } = req;
|
|
|
|
switch (method) {
|
|
case 'GET':
|
|
return await handleGET(req, res);
|
|
default:
|
|
res.setHeader('Allow', 'GET');
|
|
res.status(405).json({ error: { message: `Method ${method} Not Allowed` } });
|
|
}
|
|
};
|
|
|
|
// Get all groups for a directory
|
|
const handleGET = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
const { directorySyncController } = await jackson();
|
|
|
|
const { directoryId, offset, limit } = req.query as { directoryId: string; offset: string; limit: string };
|
|
|
|
const { data: directory } = await directorySyncController.directories.get(directoryId);
|
|
|
|
if (!directory) {
|
|
return res.status(404).json({ error: { message: 'Directory not found.' } });
|
|
}
|
|
|
|
const pageOffset = parseInt(offset);
|
|
const pageLimit = parseInt(limit);
|
|
|
|
const { data: groups, error } = await directorySyncController.groups
|
|
.setTenantAndProduct(directory.tenant, directory.product)
|
|
.getAll({ pageOffset, pageLimit, directoryId });
|
|
|
|
if (error) {
|
|
return res.status(error.code).json({ error });
|
|
}
|
|
|
|
if (groups) {
|
|
return res.status(200).json({ data: groups });
|
|
}
|
|
};
|
|
|
|
export default handler;
|