Skip to content

Commit

Permalink
Remove transaction session from programController
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
var-poro committed Jul 3, 2024
1 parent 950cda2 commit 0d5df96
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions api/src/controllers/programController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
};
Expand Down

0 comments on commit 0d5df96

Please sign in to comment.