diff --git a/app/features/events/server/list-user-conflicts-in-range.server.integration.test.ts b/app/features/events/server/list-user-conflicts-in-range.server.integration.test.ts new file mode 100644 index 00000000..b3b2d8a4 --- /dev/null +++ b/app/features/events/server/list-user-conflicts-in-range.server.integration.test.ts @@ -0,0 +1,81 @@ +import { PrismaPg } from '@prisma/adapter-pg' +import { afterAll, beforeAll, describe, expect, it } from 'vitest' +import { PrismaClient } from '~/database/generated/client' + +// An absent publisher sees the assignments their absence clashes with. Several of one meeting are listed in the +// programme's order — its parts, then its services — not in whatever order the table returns them. + +const adapter = new PrismaPg({ + connectionString: process.env.DB_RUNTIME_URL ?? process.env.DB_URL, + max: 3, + connectionTimeoutMillis: 5000, +}) +const testDb = new PrismaClient({ adapter }) + +type Tx = Parameters[0]>[0] + +function withScope(congregationId: number, fn: (tx: Tx) => Promise): Promise { + return testDb.$transaction(async tx => { + await tx.$executeRawUnsafe(`SET LOCAL app.congregation_id = '${String(congregationId)}'`) + return fn(tx) + }) +} + +const { listUserConflictsInRange } = await import('./list-user-conflicts-in-range.server') + +const ts = Date.now() +const ids = { cong: 0, member: 0 } +const DAY = new Date('2027-03-16T18:00:00Z') + +beforeAll(async () => { + ids.cong = ( + await testDb.congregation.create({ data: { name: `Conflicts ${ts}`, slug: `conflicts-${ts}`, active: true } }) + ).id + await withScope(ids.cong, async tx => { + const congregationId = ids.cong + ids.member = (await tx.member.create({ data: { firstname: 'Pierre', lastname: 'LEROY', congregationId } })).id + const event = await tx.event.create({ + data: { name: 'Réunion', startDate: DAY, endDate: DAY, status: 'released', congregationId }, + }) + const clash = { eventId: event.id, assigneeId: ids.member, hasConflict: true, congregationId } + // Created in an order the programme does not follow. + for (const [name, order] of [ + ['Cantique de conclusion', 20], + ['Lecture de la Bible', 10], + ] as const) { + await tx.eventPart.create({ data: { name, order, ...clash } }) + } + for (const [name, order] of [ + ['Nettoyage', 5], + ['Accueil', 0], + ] as const) { + await tx.eventServicePart.create({ data: { name, order, ...clash } }) + } + }) +}) + +afterAll(async () => { + if (ids.cong) { + await withScope(ids.cong, async tx => { + await tx.event.deleteMany({}) + await tx.member.deleteMany({}) + }) + await testDb.congregation.deleteMany({ where: { id: ids.cong } }) + } + await testDb.$disconnect() +}) + +describe('listUserConflictsInRange', () => { + it('lists one meeting’s clashing parts then services, each in the programme’s order', async () => { + const conflicts = await withScope(ids.cong, tx => + listUserConflictsInRange(tx, ids.member, new Date('2027-03-15T00:00:00Z'), new Date('2027-03-17T00:00:00Z')), + ) + + expect(conflicts.map(conflict => conflict.assignmentName)).toEqual([ + 'Lecture de la Bible', + 'Cantique de conclusion', + 'Accueil', + 'Nettoyage', + ]) + }) +}) diff --git a/app/features/events/server/list-user-conflicts-in-range.server.ts b/app/features/events/server/list-user-conflicts-in-range.server.ts index d9a0a6d9..eb165fd7 100644 --- a/app/features/events/server/list-user-conflicts-in-range.server.ts +++ b/app/features/events/server/list-user-conflicts-in-range.server.ts @@ -10,6 +10,12 @@ export interface UserConflictInRange { responsibleName: string | null } +// One meeting's clashes in the programme's order: the date sort below is stable, so parts then services, each in theirs. +const PROGRAMME_ORDER = [{ event: { startDate: 'asc' } }, { order: 'asc' }, { id: 'asc' }] as const satisfies ( + | Prisma.EventPartOrderByWithRelationInput + | Prisma.EventServicePartOrderByWithRelationInput +)[] + // Shared between the two `findMany` calls so the query shape stays in sync. const eventWithResponsiblesSelect = { startDate: true, @@ -68,6 +74,7 @@ export async function listUserConflictsInRange( name: true, event: { select: eventWithResponsiblesSelect }, }, + orderBy: PROGRAMME_ORDER, }), db.eventServicePart.findMany({ where: { @@ -79,6 +86,7 @@ export async function listUserConflictsInRange( name: true, event: { select: eventWithResponsiblesSelect }, }, + orderBy: PROGRAMME_ORDER, }), ])