import { INestApplicationContext } from '@nestjs/common'; import { DomainEventDispatcher } from '../../src/domain-events/domain-event-dispatcher'; import { PrismaService } from '../../src/prisma/prisma.service'; /** * Runs the dispatcher for the events of the given root aggregates and every * follow-up event their handlers record (same correlation id) - and for * nothing else, so e2e files running in parallel never process each * other's events. Without a worker, e2e tests call this after the request * whose side effects they want to assert. */ export async function drainDomainEvents( app: INestApplicationContext, rootAggregateIds: readonly string[], ): Promise { const roots = await app.get(PrismaService).domainEvent.findMany({ where: { aggregateId: { in: [...rootAggregateIds] } }, select: { correlationId: true }, distinct: ['correlationId'], }); if (roots.length === 0) { return; } await app.get(DomainEventDispatcher).drain({ correlationIds: roots.map((root) => root.correlationId), }); }