Skip to content

fix(opportunity-volunteer): allow re-suggesting by upserting on POST #688

@nadavosa

Description

@nadavosa

Problem

POST /opportunity-volunteer fails with a DB unique constraint violation when a link between the same (opportunityId, volunteerId) pair already exists. The entity has @Unique(["opportunityId", "volunteerId"]), so the DB rejects the INSERT and the FE shows "Something went wrong".

There is no intentional limitation on re-suggesting a volunteer for an opportunity — coordinators should be able to re-suggest (e.g. reset to pending) at any time.

Expected behaviour

POST /opportunity-volunteer with an existing (opportunityId, volunteerId) pair should update the existing record's status to the requested value instead of failing.

Fix

In src/server/routes/m2m/opportunity-volunteer.routes.ts, change the POST handler to upsert:

const opportunityVolunteer = new OpportunityVolunteer(request.body);

const existing = await opportunityVolunteerRepository.findOne({
  where: {
    opportunityId: opportunityVolunteer.opportunityId,
    volunteerId: opportunityVolunteer.volunteerId,
  },
});

if (existing) {
  opportunityVolunteerRepository.merge(existing, { status: opportunityVolunteer.status });
  await opportunityVolunteerRepository.save(existing);
} else {
  await opportunityVolunteerRepository.save(opportunityVolunteer);
}

Affected file

src/server/routes/m2m/opportunity-volunteer.routes.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions