instanciate all workflows configured for an exam author based on different entities.
This commit is contained in:
@@ -9,10 +9,11 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
interface PostRequestBody {
|
||||
examAuthor: string,
|
||||
examId: string,
|
||||
examName: string,
|
||||
examModule: Module,
|
||||
examAuthor: string;
|
||||
examEntities: string[];
|
||||
examId: string;
|
||||
examName: string;
|
||||
examModule: Module;
|
||||
}
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -39,22 +40,39 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
return res.status(403).json({ ok: false });
|
||||
}
|
||||
|
||||
const { examAuthor, examId, examModule } = req.body as PostRequestBody;
|
||||
const { examAuthor, examEntities, examId, examModule } = req.body as PostRequestBody;
|
||||
|
||||
const results = await Promise.all(
|
||||
examEntities.map(async (entity) => {
|
||||
const configuredWorkflow = await getApprovalWorkflowByFormIntaker(entity, examAuthor);
|
||||
if (!configuredWorkflow) {
|
||||
return { entity, created: false, error: "No configured workflow found for examAuthor." };
|
||||
}
|
||||
|
||||
if (examAuthor) {
|
||||
const configuredWorkflow = await getApprovalWorkflowByFormIntaker(examAuthor);
|
||||
if(configuredWorkflow) {
|
||||
configuredWorkflow.modules.push(examModule);
|
||||
configuredWorkflow.name = `${examId}`;
|
||||
configuredWorkflow.examId = examId;
|
||||
configuredWorkflow.entityId = entity;
|
||||
configuredWorkflow.startDate = Date.now();
|
||||
|
||||
return res.status(201).json(await createApprovalWorkflow("active-workflows", configuredWorkflow));
|
||||
} else {
|
||||
return res.status(404).json("No configured workflow found for examAuthor.");
|
||||
}
|
||||
|
||||
try {
|
||||
const creationResponse = await createApprovalWorkflow("active-workflows", configuredWorkflow);
|
||||
return { entity, created: true, creationResponse };
|
||||
} catch (error) {
|
||||
const err = error as Error;
|
||||
return { entity, created: false, error: err.message };
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
const successCount = results.filter((r) => r.created).length;
|
||||
const totalCount = examEntities.length;
|
||||
|
||||
if (successCount === totalCount) {
|
||||
return res.status(200).json({ ok: true, results });
|
||||
} else if (successCount > 0) {
|
||||
return res.status(207).json({ ok: true, results });
|
||||
} else {
|
||||
return res.status(404).json({ ok: false, message: "No workflows were created", results });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -256,8 +256,9 @@ export default function Home({ user, allConfiguredWorkflows, userEntitiesWithLab
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="flex flex-row gap-6">
|
||||
<Tip text="Setting a teacher as a Form Intaker means the configured workflow will be instanciated when said teacher publishes an exam. Only one Form Intake per teacher per entity is allowed."/>
|
||||
|
||||
<section className="flex flex-row gap-6">
|
||||
<Button
|
||||
color="purple"
|
||||
variant="solid"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import Tip from "@/components/ApprovalWorkflows/Tip";
|
||||
import Layout from "@/components/High/Layout";
|
||||
import Button from "@/components/Low/Button";
|
||||
import Input from "@/components/Low/Input";
|
||||
@@ -173,7 +174,7 @@ export default function ApprovalWorkflows({ user, initialWorkflows, workflowsAss
|
||||
|
||||
const columns = [
|
||||
columnHelper.accessor("name", {
|
||||
header: "NAME",
|
||||
header: "EXAM NAME",
|
||||
cell: (info) => (
|
||||
<span className="font-medium">
|
||||
{info.getValue()}
|
||||
@@ -371,6 +372,8 @@ export default function ApprovalWorkflows({ user, initialWorkflows, workflowsAss
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Tip text="An exam submission will instanciate the approval workflow configured for the exam author. The exam will be valid only when all the steps of the workflow have been approved."></Tip>
|
||||
|
||||
<div className="px-6 pb-4 bg-mti-purple-ultralight rounded-2xl border-2 border-mti-purple-light border-opacity-40">
|
||||
<table
|
||||
className="w-full table-auto border-separate border-spacing-y-2"
|
||||
|
||||
Reference in New Issue
Block a user