From eceffc29270fee84c272dfde4316872ecd68a278 Mon Sep 17 00:00:00 2001 From: Glen Date: Mon, 28 Oct 2024 00:35:06 +0530 Subject: [PATCH] remove session changes --- src/resolvers/Mutation/createActionItem.ts | 39 ++++------ .../Mutation/updateVolunteerMembership.ts | 74 ++++++++----------- 2 files changed, 45 insertions(+), 68 deletions(-) diff --git a/src/resolvers/Mutation/createActionItem.ts b/src/resolvers/Mutation/createActionItem.ts index 673f9282fa..74abab9c2a 100644 --- a/src/resolvers/Mutation/createActionItem.ts +++ b/src/resolvers/Mutation/createActionItem.ts @@ -194,29 +194,22 @@ export const createActionItem: MutationResolvers["createActionItem"] = async ( creator: context.userId, }); - const session = await mongoose.startSession(); - await session.withTransaction(async () => { - if (assigneeType === "EventVolunteer") { - await EventVolunteer.findByIdAndUpdate( - assigneeId, - { $addToSet: { assignments: createActionItem._id } }, - { session }, - ); - } else if (assigneeType === "EventVolunteerGroup") { - const newGrp = (await EventVolunteerGroup.findByIdAndUpdate( - assigneeId, - { $addToSet: { assignments: createActionItem._id } }, - { new: true, session }, - ).lean()) as InterfaceEventVolunteerGroup; - - await EventVolunteer.updateMany( - { _id: { $in: newGrp.volunteers } }, - { $addToSet: { assignments: createActionItem._id } }, - { session }, - ); - } - }); - session.endSession(); + if (assigneeType === "EventVolunteer") { + await EventVolunteer.findByIdAndUpdate(assigneeId, { + $addToSet: { assignments: createActionItem._id }, + }); + } else if (assigneeType === "EventVolunteerGroup") { + const newGrp = (await EventVolunteerGroup.findByIdAndUpdate( + assigneeId, + { $addToSet: { assignments: createActionItem._id } }, + { new: true }, + ).lean()) as InterfaceEventVolunteerGroup; + + await EventVolunteer.updateMany( + { _id: { $in: newGrp.volunteers } }, + { $addToSet: { assignments: createActionItem._id } }, + ); + } return createActionItem.toObject(); }; diff --git a/src/resolvers/Mutation/updateVolunteerMembership.ts b/src/resolvers/Mutation/updateVolunteerMembership.ts index 0a9515e3aa..aa376f344f 100644 --- a/src/resolvers/Mutation/updateVolunteerMembership.ts +++ b/src/resolvers/Mutation/updateVolunteerMembership.ts @@ -17,7 +17,6 @@ import { import { adminCheck } from "../../utilities"; import { errors, requestContext } from "../../libraries"; import { USER_NOT_AUTHORIZED_ERROR } from "../../constants"; -import mongoose from "mongoose"; /** * Helper function to handle updates when status is accepted @@ -25,54 +24,39 @@ import mongoose from "mongoose"; const handleAcceptedStatusUpdates = async ( membership: InterfaceVolunteerMembership, ): Promise => { - const session = await mongoose.startSession(); - try { - await session.withTransaction(async () => { - const updatePromises = []; + const updatePromises = []; - // Always update EventVolunteer to set hasAccepted to true - updatePromises.push( - EventVolunteer.findOneAndUpdate( - { _id: membership.volunteer, event: membership.event }, - { - $set: { hasAccepted: true }, - ...(membership.group && { $push: { groups: membership.group } }), - }, - { session }, - ), - ); - - // Always update Event to add volunteer - updatePromises.push( - Event.findOneAndUpdate( - { _id: membership.event }, - { $addToSet: { volunteers: membership.volunteer } }, - { session }, - ), - ); + // Always update EventVolunteer to set hasAccepted to true + updatePromises.push( + EventVolunteer.findOneAndUpdate( + { _id: membership.volunteer, event: membership.event }, + { + $set: { hasAccepted: true }, + ...(membership.group && { $push: { groups: membership.group } }), + }, + ), + ); - // If group exists, update the EventVolunteerGroup as well - if (membership.group) { - updatePromises.push( - EventVolunteerGroup.findOneAndUpdate( - { _id: membership.group }, - { $addToSet: { volunteers: membership.volunteer } }, - { session }, - ), - ); - } + // Always update Event to add volunteer + updatePromises.push( + Event.findOneAndUpdate( + { _id: membership.event }, + { $addToSet: { volunteers: membership.volunteer } }, + ), + ); - // Execute all updates in parallel - await Promise.all(updatePromises); - }); - /* c8 ignore start */ - } catch (error: unknown) { - await session.abortTransaction(); - throw error; - } finally { - /* c8 ignore stop */ - session.endSession(); + // If group exists, update the EventVolunteerGroup as well + if (membership.group) { + updatePromises.push( + EventVolunteerGroup.findOneAndUpdate( + { _id: membership.group }, + { $addToSet: { volunteers: membership.volunteer } }, + ), + ); } + + // Execute all updates in parallel + await Promise.all(updatePromises); }; /**