Deleting some unused API methods, storing interactions properly to the Task Backend, Adding explanatory comments, using a task queue in the client side

This commit is contained in:
Keith Stevens
2022-12-18 15:43:19 +09:00
parent 699d0a948c
commit a53d69b682
5 changed files with 97 additions and 58 deletions
+30 -4
View File
@@ -2,18 +2,28 @@ import { unstable_getServerSession } from "next-auth/next";
import { authOptions } from "./auth/[...nextauth]";
/**
* Returns a list of prompts from the Labeler Backend.
* Stores the task interaction with the Task Backend and then returns the next task generated.
*
* This implicity does a few things:
* 1) Stores the answer with the Task Backend.
* 2) Records the new task in our local database.
* 3) (TODO) Acks the new task with our local task ID to the Task Backend.
* 4) Returns the newly created task to the client.
*/
export default async (req, res) => {
const session = await unstable_getServerSession(req, res, authOptions);
// Return nothing if the user isn't registered.
if (!session) {
res.status(401).end();
return;
}
// Parse out the local task ID and the interaction contents.
const { id, content } = await JSON.parse(req.body);
// Log the interaction locally to create our user_post_id needed by the Task
// Backend.
const interaction = await prisma.taskInteraction.create({
data: {
content,
@@ -25,6 +35,8 @@ export default async (req, res) => {
},
});
// Send the interaction to the Task Backend. This automatically fetches the
// next task in the sequence (or the done task).
const interactionRes = await fetch(
`${process.env.FASTAPI_URL}/api/v1/tasks/interaction`,
{
@@ -46,9 +58,23 @@ export default async (req, res) => {
}),
}
);
console.log(interactionRes.status);
const newTask = await interactionRes.json();
console.log(newTask);
res.status(200).json(newTask);
// Stores the new task with our database.
const newRegisteredTask = await prisma.registeredTask.create({
data: {
task: newTask,
user: {
connect: {
id: session.user.id,
},
},
},
});
// TODO: Ack the task with the Task Backend using the newly created local
// task ID.
// Send the next task in the sequence to the client.
res.status(200).json(newRegisteredTask);
};