Compare commits

..
Author SHA1 Message Date
jmartinandCursor 63094fe49d feat(#215): GET événement agenda + DTO enrichi
Ajoute GET /evenements-agenda/:id, enrichit le DTO (id_lot, heure,
statut_lot, statut_effectif, actions, libelle_refus anonymisé) et
autorise la lecture sur placement terminé.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-16 16:44:51 +02:00
3 changed files with 10 additions and 146 deletions
@@ -47,18 +47,16 @@ export class EvenementsAgendaController {
@Get()
@Roles(RoleType.PARENT, RoleType.ASSISTANTE_MATERNELLE)
@ApiOperation({
summary: 'Lister les événements agenda — #217 / #205',
summary: 'Lister les absences / congés / arrêts — ticket #205 / ex-#172',
description:
'Sans placementId : tous les placements du user (actifs **et** terminés — historique). ' +
'Avec placementId : un couple enfant–AM (terminé OK). ' +
'Filtres from/to inclusifs (chevauchement). Annulés exclus sauf si statut=annule. ' +
'Dates YYYY-MM-DD calendaires (DST-safe).',
'Sans placementId : tous les placements du user (parent = famille multi-AM ; AM = tous accueils). ' +
'Avec placementId : un couple enfant–AM.',
})
@ApiQuery({ name: 'placementId', required: false })
@ApiQuery({ name: 'type', required: false, enum: TypeEvenementAgendaType })
@ApiQuery({ name: 'statut', required: false, enum: StatutEvenementAgendaType })
@ApiQuery({ name: 'from', required: false, description: 'YYYY-MM-DD (inclus)' })
@ApiQuery({ name: 'to', required: false, description: 'YYYY-MM-DD (inclus)' })
@ApiQuery({ name: 'from', required: false, description: 'YYYY-MM-DD' })
@ApiQuery({ name: 'to', required: false, description: 'YYYY-MM-DD' })
@ApiResponse({ status: 200, type: ListeEvenementsAgendaDto })
lister(
@User('id') userId: string,
@@ -245,96 +245,6 @@ describe('EvenementsAgendaService (#172)', () => {
expect(res.actions.peut_modifier).toBe(true);
});
describe('lister — #217', () => {
const andWhere = jest.fn().mockReturnThis();
const qb = {
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere,
orderBy: jest.fn().mockReturnThis(),
getMany: jest.fn().mockResolvedValue([]),
};
beforeEach(() => {
andWhere.mockClear();
absencesRepo.createQueryBuilder.mockReturnValue(qb);
amChildrenRepo.find.mockResolvedValue([{ id: 'pl-1' }, { id: 'pl-ended' }]);
});
it('from > to → BadRequest', async () => {
await expect(
service.lister('am-1', RoleType.ASSISTANTE_MATERNELLE, {
from: '2026-10-31',
to: '2026-10-01',
}),
).rejects.toBeInstanceOf(BadRequestException);
});
it('date invalide (31 nov) → BadRequest', async () => {
await expect(
service.lister('am-1', RoleType.ASSISTANTE_MATERNELLE, {
from: '2026-11-31',
to: '2026-12-01',
}),
).rejects.toBeInstanceOf(BadRequestException);
});
it('exclut les annulés par défaut + bornes inclusives', async () => {
await service.lister('am-1', RoleType.ASSISTANTE_MATERNELLE, {
from: '2026-10-01',
to: '2026-10-31',
});
expect(amChildrenRepo.find).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({ amId: 'am-1' }),
}),
);
// pas de date_fin: IsNull() → historique inclus
const whereArg = amChildrenRepo.find.mock.calls[0][0].where;
expect(whereArg.date_fin).toBeUndefined();
expect(andWhere).toHaveBeenCalledWith('ag.statut != :annule', {
annule: StatutEvenementAgendaType.ANNULE,
});
expect(andWhere).toHaveBeenCalledWith('ag.date_fin >= :from', {
from: '2026-10-01',
});
expect(andWhere).toHaveBeenCalledWith('ag.date_debut <= :to', {
to: '2026-10-31',
});
});
it('DST octobre : bornes calendaires acceptées (2026-10-25)', async () => {
// dernier dimanche d’octobre 2026 = 25 oct. — strings YYYY-MM-DD, pas Date UTC
await expect(
service.lister('am-1', RoleType.ASSISTANTE_MATERNELLE, {
from: '2026-10-25',
to: '2026-10-26',
}),
).resolves.toEqual({ items: [] });
expect(andWhere).toHaveBeenCalledWith('ag.date_fin >= :from', {
from: '2026-10-25',
});
});
it('année bissextile : 2024-02-29 valide', async () => {
await expect(
service.lister('am-1', RoleType.ASSISTANTE_MATERNELLE, {
from: '2024-02-29',
to: '2024-03-01',
}),
).resolves.toEqual({ items: [] });
});
it('2025-02-29 invalide (non bissextile)', async () => {
await expect(
service.lister('am-1', RoleType.ASSISTANTE_MATERNELLE, {
from: '2025-02-29',
to: '2025-03-01',
}),
).rejects.toBeInstanceOf(BadRequestException);
});
});
it('libelle_refus n’expose pas l’auteur — #215', async () => {
absencesRepo.findOne.mockResolvedValue({
id: 'ev-ref',
@@ -50,14 +50,10 @@ export class EvenementsAgendaService {
to?: string;
},
): Promise<ListeEvenementsAgendaDto> {
this.assertDateRange(opts.from, opts.to);
// Agenda : historique inclus (placements terminés) — #217
const placementIds = await this.resolvePlacementIds(
userId,
role,
opts.placementId,
{ includeEnded: true },
);
if (placementIds.length === 0) {
return { items: [] };
@@ -76,15 +72,9 @@ export class EvenementsAgendaService {
if (opts.type) {
qb.andWhere('ag.type = :type', { type: opts.type });
}
// Exclusion des annulés par défaut ; statut=annule demande explicitement les annulés
if (opts.statut) {
qb.andWhere('ag.statut = :statut', { statut: opts.statut });
} else {
qb.andWhere('ag.statut != :annule', {
annule: StatutEvenementAgendaType.ANNULE,
});
}
// Chevauchement inclusif : date_debut <= to AND date_fin >= from
if (opts.from) {
qb.andWhere('ag.date_fin >= :from', { from: opts.from });
}
@@ -254,33 +244,6 @@ export class EvenementsAgendaService {
}
}
/** Bornes calendaires YYYY-MM-DD (comparaison lexicographique = calendaire, DST-safe). #217 */
private assertDateRange(from?: string, to?: string): void {
if (from) this.assertCalendarDate(from, 'from');
if (to) this.assertCalendarDate(to, 'to');
if (from && to && from > to) {
throw new BadRequestException('from doit être inférieure ou égale à to');
}
}
private assertCalendarDate(value: string, field: string): void {
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) {
throw new BadRequestException(
`${field} doit être une date YYYY-MM-DD`,
);
}
const [y, m, d] = value.split('-').map(Number);
// UTC : évite qu’un parse local fasse glisser le jour à l’heure d’hiver
const dt = new Date(Date.UTC(y, m - 1, d));
if (
dt.getUTCFullYear() !== y ||
dt.getUTCMonth() !== m - 1 ||
dt.getUTCDate() !== d
) {
throw new BadRequestException(`${field} n’est pas une date calendaire valide`);
}
}
private assertCanCreateType(role: RoleType, type: TypeEvenementAgendaType): void {
if (role === RoleType.PARENT) {
if (type !== TypeEvenementAgendaType.ABSENCE_ENFANT) {
@@ -392,12 +355,9 @@ export class EvenementsAgendaService {
userId: string,
role: RoleType,
placementId?: string,
opts?: { includeEnded?: boolean },
): Promise<string[]> {
if (placementId) {
await this.assertCanAccessPlacement(userId, role, placementId, {
allowEnded: opts?.includeEnded === true,
});
await this.assertCanAccessPlacement(userId, role, placementId);
return [placementId];
}
@@ -409,9 +369,7 @@ export class EvenementsAgendaService {
const enfantIds = liens.map((l) => l.enfantId);
if (enfantIds.length === 0) return [];
const placements = await this.amChildrenRepo.find({
where: opts?.includeEnded
? { enfantId: In(enfantIds) }
: { enfantId: In(enfantIds), date_fin: IsNull() },
where: { enfantId: In(enfantIds), date_fin: IsNull() },
select: ['id'],
});
return placements.map((p) => p.id);
@@ -419,9 +377,7 @@ export class EvenementsAgendaService {
if (role === RoleType.ASSISTANTE_MATERNELLE) {
const placements = await this.amChildrenRepo.find({
where: opts?.includeEnded
? { amId: userId }
: { amId: userId, date_fin: IsNull() },
where: { amId: userId, date_fin: IsNull() },
select: ['id'],
});
return placements.map((p) => p.id);
@@ -623,11 +579,11 @@ export class EvenementsAgendaService {
}
if (role === RoleType.PARENT && !isCreator) {
// ANNULE déjà exclu en tête de actionsPour (narrowing TS)
const congeEnAttente =
row.type === TypeEvenementAgendaType.CONGE_AM &&
row.statut === StatutEvenementAgendaType.EN_ATTENTE &&
statutLot !== AgendaLotStatutType.REFUSE;
statutLot !== AgendaLotStatutType.REFUSE &&
statutLot !== AgendaLotStatutType.ANNULE;
const arretAck =
row.type === TypeEvenementAgendaType.ARRET_MALADIE_AM;
const soinAck = row.type === TypeEvenementAgendaType.SOIN_ENFANT;