Skip to content

Commit

Permalink
remove session changes
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDsza committed Oct 27, 2024
1 parent 493977c commit eceffc2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 68 deletions.
39 changes: 16 additions & 23 deletions src/resolvers/Mutation/createActionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
74 changes: 29 additions & 45 deletions src/resolvers/Mutation/updateVolunteerMembership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,46 @@ 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
*/
const handleAcceptedStatusUpdates = async (
membership: InterfaceVolunteerMembership,
): Promise<void> => {
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);
};

/**
Expand Down

0 comments on commit eceffc2

Please sign in to comment.