fix(trips): end a plugin-added member's MCP sessions where the trip opened an addon

trips.addMember now calls reattachJoinedMember like the REST, MCP and invite joins, so the new member's session lists the trip's addon tools.
This commit is contained in:
Maurice
2026-11-23 15:43:29 +01:00
parent 807c6a16b4
commit 1a6639eb74
6 changed files with 40 additions and 4 deletions
+3
View File
@@ -8,6 +8,7 @@ import { PluginController, PluginMethod } from '../../nest-rpc/rpc-kit/decorator
import type { PluginRpcContext } from '../../nest-rpc/rpc-kit/types';
import { schemaMessage, rowIdParam } from '../../nest-rpc/rpc-params';
import { AccommodationsService } from '../accommodations/accommodations.service';
import { TripAddonsService } from '../addons/trip-addons.service';
import { DaysService } from '../days/days.service';
import { RealtimeService } from '../realtime/realtime.service';
import { ReservationsService } from '../reservations/reservations.service';
@@ -57,6 +58,7 @@ export class TripsRpc {
// `.isOwner` used since Task 0b — `EntityManager` is `@Global()`, so no
// module needs new wiring. Appended last, per this class's own convention.
private readonly em: EntityManager,
private readonly tripAddons: TripAddonsService,
) {}
@PluginMethod('trips.getById', { permission: 'db:read:trips' })
@@ -219,6 +221,7 @@ export class TripsRpc {
// The open planners learn of the member as they do from REST and MCP. A
// plugin has no socket of its own, so the whole room hears it.
if (result.joined) {
await this.tripAddons.reattachJoinedMember(tripId, targetUserId);
const member = await this.roster.findMember(tripId, targetUserId);
if (member) this.realtime.broadcast(tripId, 'member:added', { member });
}
+2
View File
@@ -613,6 +613,8 @@ export async function createPluginRpcHostParts(
accommodations,
members,
(await sharedTestOrm(db)).em,
// TripAddonsService: ending a joined member's MCP sessions has its own suites.
{ reattachJoinedMember: async () => {} } as never,
),
new CostsRpc(budget, new TripAccessService(await createTestTripsRepo(db)), realtime, guards, membership),
new ReservationsRpc(reservations, realtime, guards),
+1
View File
@@ -81,6 +81,7 @@ export function allRpcControllers(): object[] {
anyService(),
anyService(),
anyService(),
anyService(),
),
new CostsRpc(anyService(), anyService(), anyService(), anyService(), anyService()),
new ReservationsRpc(anyService(), anyService(), anyService()),
@@ -281,6 +281,7 @@ describe('service errors are translated into the RPC taxonomy', () => {
{} as never,
{} as never,
em,
{} as never,
);
return new PluginRpcHost('p', ALL, makeDeps(), createTestPluginRegistry([rpc]));
}
@@ -416,7 +417,7 @@ describe('every schema-validated method rejects a payload its schema refuses', (
new PlacesRpc(anything, anything, realtime(), guards, anything),
new DaysRpc(anything, realtime(), guards, anything),
new PackingRpc(anything, realtime(), guards),
new TripsRpc(anything, anything, anything, anything, realtime(), guards, anything, anything, em),
new TripsRpc(anything, anything, anything, anything, realtime(), guards, anything, anything, em, anything),
new AccommodationsRpc(anything, realtime(), guards),
new ReservationsRpc(anything, realtime(), guards),
]);
@@ -48,7 +48,18 @@ const tripsRegistry = () => {
getRepository: () => ({ findRaw: async () => ({ id: 1, title: 'Japan', feed_token: null }) }),
} as unknown as EntityManager;
return createTestPluginRegistry([
new TripsRpc({} as never, {} as never, {} as never, {} as never, {} as never, guards, {} as never, {} as never, em),
new TripsRpc(
{} as never,
{} as never,
{} as never,
{} as never,
{} as never,
guards,
{} as never,
{} as never,
em,
{} as never,
),
]);
};
+20 -2
View File
@@ -17,6 +17,7 @@ import { PluginGuards } from '../../../src/nest-rpc/plugin-guards.service';
import { createTestPluginRegistry } from '../../../src/nest-rpc/rpc-kit/testing';
import type { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import type { TripAddonsService } from '../../../src/nest/addons/trip-addons.service';
import type { DaysService } from '../../../src/nest/days/days.service';
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import { PluginRpcHost } from '../../../src/nest/plugins/host/rpc-host';
@@ -141,7 +142,20 @@ export function build(
{ isAddonEnabled: vi.fn(() => true) } as unknown as AddonsService,
{ getRole: vi.fn(async () => 'user') } as unknown as UsersRepository,
);
const rpc = new TripsRpc(trips, reservations, days, membership, realtime, guards, accommodations, roster, em);
const tripAddons = { reattachJoinedMember: vi.fn(async () => undefined) } as unknown as TripAddonsService &
Record<string, ReturnType<typeof vi.fn>>;
const rpc = new TripsRpc(
trips,
reservations,
days,
membership,
realtime,
guards,
accommodations,
roster,
em,
tripAddons,
);
const host = (...grants: string[]) =>
new PluginRpcHost(
'p',
@@ -149,7 +163,7 @@ export function build(
makeDeps(),
createTestPluginRegistry([rpc]),
);
return { trips, reservations, days, accommodations, roster, membership, realtime, permissions, host };
return { trips, reservations, days, accommodations, roster, membership, realtime, permissions, tripAddons, host };
}
describe('TripsRpc reads', () => {
@@ -238,6 +252,7 @@ describe('TripsRpc reads', () => {
f.accommodations,
f.roster,
em,
f.tripAddons,
);
const host = new PluginRpcHost('p', new Set(ALL_TRIP_GRANTS), makeDeps(), createTestPluginRegistry([rpc]));
@@ -367,6 +382,8 @@ describe('TripsRpc membership', () => {
const f = build();
expect((await f.host().dispatch(req('trips.addMember', { tripId: 1, userId: 7 }), 42)).ok).toBe(true);
expect(f.membership.joinTripAsMember).toHaveBeenCalledWith(1, 7, 42);
// The joined member's MCP session is ended where the trip opened an addon, as REST, MCP and invites do.
expect(f.tripAddons.reattachJoinedMember).toHaveBeenCalledWith(1, 7);
// The whole room hears it: a plugin has no socket to leave out.
expect(f.realtime.broadcast).toHaveBeenCalledWith(1, 'member:added', {
member: { id: 7, username: 'bob', role: 'member' },
@@ -378,6 +395,7 @@ describe('TripsRpc membership', () => {
vi.mocked(f.membership.joinTripAsMember).mockResolvedValueOnce({ joined: false, tripId: 1 });
expect((await f.host().dispatch(req('trips.addMember', { tripId: 1, userId: 7 }), 42)).ok).toBe(true);
expect(f.roster.findMember).not.toHaveBeenCalled();
expect(f.tripAddons.reattachJoinedMember).not.toHaveBeenCalled();
expect(f.realtime.broadcast).not.toHaveBeenCalled();
});