feat: manage waitlist and remove group members
- Add group-waitlist module (list/promote/remove entries) - Remove attendee with optional atomic promotion from waitlist - Block removal when attendee has payments (attendee_has_payments) - Waitlist detail modal matching members UI; optimistic add-to-waitlist - Responsive tweaks for the members/waitlist tabs
This commit is contained in:
24
apps/backend/src/modules/attendees/features/remove/route.ts
Normal file
24
apps/backend/src/modules/attendees/features/remove/route.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { type RemoveAttendeeQuery, RemoveAttendeeQuerySchema } from '@gruperly/shared';
|
||||
import { Hono } from 'hono';
|
||||
import { problemJson, resultJson, unauthorizedProblem } from '@/http/problem-details';
|
||||
import { validate } from '@/http/validate';
|
||||
import { RemoveAttendee } from './use-case';
|
||||
|
||||
const route = new Hono();
|
||||
|
||||
route.delete('/:groupId/attendees/:attendeeId', validate.query(RemoveAttendeeQuerySchema), async (c) => {
|
||||
const user = c.get('user');
|
||||
if (!user) {
|
||||
return problemJson(c, unauthorizedProblem(c.req.path));
|
||||
}
|
||||
const groupId = c.req.param('groupId');
|
||||
const attendeeId = c.req.param('attendeeId');
|
||||
const query = c.req.valid('query') as RemoveAttendeeQuery;
|
||||
const useCase = new RemoveAttendee();
|
||||
const result = await useCase.execute(groupId, attendeeId, user.id, {
|
||||
promoteFromWaitlist: query.promoteFromWaitlist,
|
||||
});
|
||||
return resultJson(c, result);
|
||||
});
|
||||
|
||||
export default route;
|
||||
@@ -0,0 +1,73 @@
|
||||
import type { Prisma, PrismaClient } from '@generated/prisma/client';
|
||||
import type { ProblemDetails, RemoveAttendeeResult, Result } from '@gruperly/shared';
|
||||
import { err, ok } from '@gruperly/shared';
|
||||
import { attendeeHasPaymentsProblem, notFoundResourceProblem } from '@/http/problem-builders';
|
||||
import { default as prisma, UnitOfWork } from '@/lib/prisma';
|
||||
import {
|
||||
findGroupOwnedByUser,
|
||||
toGroupWaitlistEntryDto,
|
||||
} from '@/modules/group-waitlist/lib/helpers';
|
||||
import { promoteFirstPendingWaitlistEntry } from '@/modules/group-waitlist/lib/promote';
|
||||
|
||||
type RemoveAttendeeDeps = {
|
||||
db?: Pick<PrismaClient, 'group' | 'attendee' | 'payment'>;
|
||||
unitOfWork?: UnitOfWork;
|
||||
};
|
||||
|
||||
type RemoveAttendeeOptions = {
|
||||
promoteFromWaitlist?: boolean;
|
||||
};
|
||||
|
||||
export class RemoveAttendee {
|
||||
constructor(private readonly deps: RemoveAttendeeDeps = {}) {}
|
||||
|
||||
async execute(
|
||||
groupId: string,
|
||||
attendeeId: string,
|
||||
userId: string,
|
||||
options: RemoveAttendeeOptions = {},
|
||||
): Promise<Result<RemoveAttendeeResult, ProblemDetails>> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
const unitOfWork = this.deps.unitOfWork ?? new UnitOfWork(prisma);
|
||||
|
||||
const groupResult = await findGroupOwnedByUser(db, groupId, userId);
|
||||
if (!groupResult.ok) {
|
||||
return groupResult;
|
||||
}
|
||||
const group = groupResult.value;
|
||||
|
||||
const attendee = await db.attendee.findFirst({ where: { id: attendeeId, groupId } });
|
||||
if (!attendee) {
|
||||
return err(notFoundResourceProblem('Attendee', attendeeId));
|
||||
}
|
||||
|
||||
const paymentsCount = await db.payment.count({ where: { attendeeId } });
|
||||
if (paymentsCount > 0) {
|
||||
return err(attendeeHasPaymentsProblem());
|
||||
}
|
||||
|
||||
return unitOfWork.executeResult(
|
||||
async (tx: Prisma.TransactionClient): Promise<Result<RemoveAttendeeResult, ProblemDetails>> => {
|
||||
await tx.attendee.delete({ where: { id: attendeeId } });
|
||||
|
||||
if (!options.promoteFromWaitlist) {
|
||||
return ok({ removedAttendeeId: attendeeId, promoted: null });
|
||||
}
|
||||
|
||||
const promoteResult = await promoteFirstPendingWaitlistEntry(tx, group);
|
||||
if (!promoteResult.ok) {
|
||||
return err(promoteResult.error);
|
||||
}
|
||||
if (!promoteResult.value) {
|
||||
return ok({ removedAttendeeId: attendeeId, promoted: null });
|
||||
}
|
||||
|
||||
await tx.groupWaitlistEntry.delete({ where: { id: promoteResult.value.entry.id } });
|
||||
return ok({
|
||||
removedAttendeeId: attendeeId,
|
||||
promoted: toGroupWaitlistEntryDto(promoteResult.value.entry),
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user