From 0d5df96b3bebf092d0be99beea7493383e05ca02 Mon Sep 17 00:00:00 2001 From: var-poro <110412302+var-poro@users.noreply.github.com> Date: Wed, 3 Jul 2024 17:55:04 +0200 Subject: [PATCH] Remove transaction session from programController The commit eliminates use of mongoose session transactions in `programController.ts`. The Program save method and the User save method no longer use sessions, thus simplifying the code and removing potential transactional conflicts. --- api/src/controllers/programController.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/api/src/controllers/programController.ts b/api/src/controllers/programController.ts index 36b1d7c..76b5e92 100644 --- a/api/src/controllers/programController.ts +++ b/api/src/controllers/programController.ts @@ -2,33 +2,26 @@ import {Request, Response} from 'express'; import Program from '../models/Program'; import Session from '../models/Session'; import User from "../models/User"; -import mongoose, {Schema} from "mongoose"; +import {Schema} from "mongoose"; interface AuthRequest extends Request { userId?: string; } export const createProgram = async (req: AuthRequest, res: Response) => { - const session = await mongoose.startSession(); - session.startTransaction(); - try { const program = new Program({...req.body, userId: req.userId}); - const savedProgram = await program.save({session}); + const savedProgram = await program.save(); - const user = await User.findById(req.userId).session(session); + const user = await User.findById(req.userId); if (user && !user.activeProgram) { user.activeProgram = savedProgram._id as Schema.Types.ObjectId; - await user.save({session}); + await user.save(); } - await session.commitTransaction(); - session.endSession(); res.status(201).send(savedProgram); } catch (error) { - await session.abortTransaction(); - session.endSession(); res.status(400).send({error: (error as Error).message}); } };