ENCOA-274

This commit is contained in:
Carlos-Mesquita
2024-12-11 15:28:38 +00:00
parent 7538392e44
commit efba1939e5
12 changed files with 344 additions and 130 deletions

View File

@@ -65,7 +65,8 @@ async function POST(req: NextApiRequest, res: NextApiResponse) {
// Check whether the id of the exam matches another exam with different
// owners, throw exception if there is, else allow editing
const ownersSet = new Set(docSnap?.owners || []);
if (docSnap?.owners?.length === exam.owners.lenght && exam.owners.every((e: string) => ownersSet.has(e))) {
if (docSnap !== null && docSnap?.owners?.length === exam.owners.lenght && exam.owners.every((e: string) => ownersSet.has(e))) {
throw new Error("Name already exists");
}

View File

@@ -31,6 +31,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
if (!req.session.user) return res.status(401).json({ ok: false });
const queryParams = queryToURLSearchParams(req);
let endpoint = queryParams.getAll('module').join("/");
if (endpoint.startsWith("level")) {
@@ -41,12 +42,27 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
endpoint = "reading/"
}
const result = await axios.post(`${process.env.BACKEND_URL}/${endpoint}`,
req.body,
{
headers: { Authorization: `Bearer ${process.env.BACKEND_JWT}` },
},
);
queryParams.delete('module');
const queryString = queryParams.toString();
res.status(200).json(result.data);
}
const hasFiles = req.headers['content-type']?.startsWith('multipart/form-data');
// https://github.com/vercel/next.js/discussions/36153#discussioncomment-3029675
// This just proxies the request
const { data } = await axios.post(
`${process.env.BACKEND_URL}/${endpoint}${hasFiles ? '/attachment' : ''}${queryString.length > 0 ? `?${queryString}` : ''}`, req, {
responseType: "stream",
headers: {
"Content-Type": req.headers["content-type"],
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
},
});
data.pipe(res);
}
export const config = {
api: {
bodyParser: false,
},
};