diff --git a/packages/api/src/routes/studies.ts b/packages/api/src/routes/studies.ts index 7174512..23f15f9 100644 --- a/packages/api/src/routes/studies.ts +++ b/packages/api/src/routes/studies.ts @@ -52,13 +52,20 @@ router.post("/", (req, res, next) => { .catch(next); }); -router.put("/:id", (req, res, next) => { - Study.updateOne({ _id: req.params["id"] }, req.body) - .then((study) => { - if (!study) throw new HttpError(404); - res.json(study); - }) - .catch(next); +router.put("/:id", isAuthenticated, async (req, res, next) => { + try { + const study = await Study.findOneAndUpdate( + { _id: req.params["id"] }, + req.body, + { upsert: true, new: true } + ); + const user = req.user as HydratedDocument; + if (!user.studies.includes(study._id)) + await user.updateOne({ $push: { studies: study._id } }); + res.json(study); + } catch (e) { + next(); + } }); export default router; diff --git a/packages/ui/src/api/studies.ts b/packages/ui/src/api/studies.ts index c40e19f..abd42e2 100644 --- a/packages/ui/src/api/studies.ts +++ b/packages/ui/src/api/studies.ts @@ -9,9 +9,12 @@ const axiosInstance = axios.create({ axiosInstance.interceptors.response.use( (response) => response, (error) => { + // TODO fix this to instantly cause redirect to login if (error.response.status === 401) { const authStore = useAuthStore(); authStore.currentUser = null; + } else { + throw error; } } ); @@ -25,6 +28,10 @@ async function deleteStudy(id: string) { await axiosInstance.delete(`/studies/${id}`); } +async function saveStudy(id: string, studyData: GetStudyResponse) { + await axiosInstance.put(`/studies/${id}`, studyData); +} + export interface ITaskInstance { _id: string; task: string; @@ -55,4 +62,4 @@ async function getStudy(id: string) { return result.data; } -export default { getStudies, deleteStudy, getStudy }; +export default { getStudies, deleteStudy, getStudy, saveStudy }; diff --git a/packages/ui/src/pages/StudyBuilderPage/StudyBuilderPage.vue b/packages/ui/src/pages/StudyBuilderPage/StudyBuilderPage.vue index 966a0de..4599da8 100644 --- a/packages/ui/src/pages/StudyBuilderPage/StudyBuilderPage.vue +++ b/packages/ui/src/pages/StudyBuilderPage/StudyBuilderPage.vue @@ -8,6 +8,7 @@ import TaskBank from "./components/TaskBank.vue"; import AppEditModal from "@/components/ui/AppEditModal.vue"; import { useTaskEditingStore } from "@/stores/taskEditing"; import TaskEditingPanel from "./components/TaskEditingPanel.vue"; +import { useStudyBuilderStore } from "@/stores/studyBuilder"; const router = useRouter(); const authStore = useAuthStore(); @@ -16,12 +17,14 @@ if (!authStore.currentUser) { router.push("/login"); } +const studyBuilderStore = useStudyBuilderStore(); const taskEditingStore = useTaskEditingStore();