Files
petitspas/backend/src/modules/evenements-agenda/evenements-agenda.service.spec.ts
T
2026-10-16 15:07:14 +00:00

669 lines
21 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { ForbiddenException, BadRequestException } from '@nestjs/common';
import { EvenementsAgendaService } from './evenements-agenda.service';
import {
EvenementsAgenda,
StatutEvenementAgendaType,
TypeEvenementAgendaType,
} from 'src/entities/evenements_agenda.entity';
import { AgendaLots } from 'src/entities/agenda_lots.entity';
import { CardInstance } from 'src/entities/card_instances.entity';
import { AmChildren } from 'src/entities/am_children.entity';
import { ParentsChildren } from 'src/entities/parents_children.entity';
import { RoleType } from 'src/entities/users.entity';
import { todayParis, yesterdayParis } from 'src/common/utils/paris-date';
function qbUpdateMock() {
const chain: Record<string, jest.Mock> = {};
chain.update = jest.fn().mockReturnValue(chain);
chain.set = jest.fn().mockReturnValue(chain);
chain.where = jest.fn().mockReturnValue(chain);
chain.andWhere = jest.fn().mockReturnValue(chain);
chain.execute = jest.fn().mockResolvedValue({ affected: 1 });
return chain;
}
describe('EvenementsAgendaService (#172)', () => {
let service: EvenementsAgendaService;
const absencesRepo = {
create: jest.fn((x) => x),
save: jest.fn(async (x) => ({
...x,
id: x.id ?? 'abs-1',
cree_le: new Date(),
modifie_le: new Date(),
})),
findOne: jest.fn(),
find: jest.fn(),
update: jest.fn(async () => ({ affected: 1 })),
delete: jest.fn(),
createQueryBuilder: jest.fn(),
};
const lotsRepo = {
findOne: jest.fn(),
update: jest.fn(async () => ({ affected: 1 })),
createQueryBuilder: jest.fn(() => qbUpdateMock()),
};
const cardsRepo = {
createQueryBuilder: jest.fn(() => qbUpdateMock()),
};
const amChildrenRepo = {
find: jest.fn(),
findOne: jest.fn(),
};
const parentsChildrenRepo = {
find: jest.fn(),
findOne: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
EvenementsAgendaService,
{ provide: getRepositoryToken(EvenementsAgenda), useValue: absencesRepo },
{ provide: getRepositoryToken(AgendaLots), useValue: lotsRepo },
{ provide: getRepositoryToken(CardInstance), useValue: cardsRepo },
{ provide: getRepositoryToken(AmChildren), useValue: amChildrenRepo },
{
provide: getRepositoryToken(ParentsChildren),
useValue: parentsChildrenRepo,
},
],
}).compile();
service = module.get(EvenementsAgendaService);
jest.clearAllMocks();
lotsRepo.createQueryBuilder.mockImplementation(() => qbUpdateMock());
cardsRepo.createQueryBuilder.mockImplementation(() => qbUpdateMock());
});
it('should be defined', () => {
expect(service).toBeDefined();
});
it('parent crée absence_enfant → statut accepte', async () => {
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
absencesRepo.findOne.mockResolvedValue({
id: 'abs-1',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.ABSENCE_ENFANT,
date_debut: '2026-10-01',
date_fin: '2026-10-02',
statut: StatutEvenementAgendaType.ACCEPTE,
expire_at: new Date('9999-12-31'),
cree_le: new Date(),
modifie_le: new Date(),
placement: {
enfantId: 'e-1',
amId: 'am-1',
child: { id: 'e-1', first_name: 'Léo' },
am: { user: { prenom: 'Marie', nom: 'AM' } },
},
});
const res = await service.creer('p-1', RoleType.PARENT, {
id_placement: 'pl-1',
type: TypeEvenementAgendaType.ABSENCE_ENFANT,
date_debut: '2026-10-01',
date_fin: '2026-10-02',
});
expect(absencesRepo.save).toHaveBeenCalled();
const savedArg = absencesRepo.save.mock.calls[0][0];
expect(savedArg.statut).toBe(StatutEvenementAgendaType.ACCEPTE);
expect(res.type).toBe(TypeEvenementAgendaType.ABSENCE_ENFANT);
});
it('parent ne peut pas créer un congé AM', async () => {
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
await expect(
service.creer('p-1', RoleType.PARENT, {
id_placement: 'pl-1',
type: TypeEvenementAgendaType.CONGE_AM,
date_debut: '2026-10-01',
date_fin: '2026-10-05',
}),
).rejects.toBeInstanceOf(ForbiddenException);
});
it('refus congé sans motif → BadRequest', async () => {
absencesRepo.findOne.mockResolvedValue({
id: 'abs-1',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.CONGE_AM,
date_debut: '2026-10-01',
date_fin: '2026-10-05',
statut: StatutEvenementAgendaType.EN_ATTENTE,
expire_at: new Date(),
});
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
await expect(
service.maj('p-1', RoleType.PARENT, 'abs-1', {
statut: StatutEvenementAgendaType.REFUSE,
}),
).rejects.toBeInstanceOf(BadRequestException);
});
it('dates invalides → BadRequest', async () => {
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
await expect(
service.creer('p-1', RoleType.PARENT, {
id_placement: 'pl-1',
type: TypeEvenementAgendaType.ABSENCE_ENFANT,
date_debut: '2026-10-10',
date_fin: '2026-10-01',
}),
).rejects.toBeInstanceOf(BadRequestException);
});
it('getById enrichit le DTO — #215', async () => {
absencesRepo.findOne.mockResolvedValue({
id: 'ev-1',
id_placement: 'pl-1',
id_lot: 'lot-1',
id_card_instance: 'card-1',
type: TypeEvenementAgendaType.SOIN_ENFANT,
date_debut: '2026-10-16',
date_fin: '2026-10-16',
heure: '18:19:00',
statut: StatutEvenementAgendaType.ACCEPTE,
expire_at: new Date('9999-12-31'),
cree_par: 'am-1',
motif: 'Piqûre',
cree_le: new Date(),
modifie_le: new Date(),
lot: null,
placement: {
enfantId: 'e-1',
amId: 'am-1',
child: { id: 'e-1', first_name: 'Léo' },
am: { user: { prenom: 'Marie', nom: 'AM' } },
},
});
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: '2026-12-01',
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
const res = await service.getById('ev-1', 'p-1', RoleType.PARENT);
expect(res.id_lot).toBe('lot-1');
expect(res.id_card_instance).toBe('card-1');
expect(res.heure).toBe('18:19');
expect(res.statut_effectif).toBe(StatutEvenementAgendaType.ACCEPTE);
expect(res.actions.peut_ack).toBe(true);
expect(res.actions.peut_accepter_refuser).toBe(false);
});
it('getById autorise placement terminé (historique) — #215', async () => {
absencesRepo.findOne.mockResolvedValue({
id: 'ev-old',
id_placement: 'pl-ended',
type: TypeEvenementAgendaType.ABSENCE_ENFANT,
date_debut: '2026-01-01',
date_fin: '2026-01-05',
statut: StatutEvenementAgendaType.ACCEPTE,
expire_at: new Date('9999-12-31'),
cree_par: 'p-1',
cree_le: new Date(),
modifie_le: new Date(),
placement: {
enfantId: 'e-1',
amId: 'am-1',
child: { id: 'e-1', first_name: 'Léo' },
am: { user: { prenom: 'Marie', nom: 'AM' } },
},
});
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-ended',
amId: 'am-1',
enfantId: 'e-1',
date_fin: '2026-06-01',
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
const res = await service.getById('ev-old', 'p-1', RoleType.PARENT);
expect(res.id).toBe('ev-old');
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',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.CONGE_AM,
date_debut: '2026-11-01',
date_fin: '2026-11-10',
statut: StatutEvenementAgendaType.REFUSE,
expire_at: new Date(),
cree_par: 'am-1',
motif: 'Vacances scolaires',
cree_le: new Date(),
modifie_le: new Date(),
lot: { statut: 'refuse' },
placement: {
enfantId: 'e-1',
amId: 'am-1',
child: { id: 'e-1', first_name: 'Léo' },
am: { user: { prenom: 'Marie', nom: 'AM' } },
},
});
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
const res = await service.getById('ev-ref', 'p-1', RoleType.PARENT);
expect(res.libelle_refus).toBe('Refusé par une autre famille');
expect(res.statut_effectif).toBe(StatutEvenementAgendaType.REFUSE);
expect(res.libelle_refus).not.toMatch(/p-2|Dupont|famille B/i);
});
describe('B2 — #216 matrice + soft-cancel + cron', () => {
it('arrêt maladie créé → statut accepte + verrouille_le', async () => {
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
absencesRepo.findOne.mockResolvedValue({
id: 'abs-1',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.ARRET_MALADIE_AM,
date_debut: '2026-10-01',
date_fin: '2026-10-05',
statut: StatutEvenementAgendaType.ACCEPTE,
expire_at: new Date('9999-12-31'),
verrouille_le: new Date(),
cree_le: new Date(),
modifie_le: new Date(),
placement: {
enfantId: 'e-1',
amId: 'am-1',
child: { id: 'e-1', first_name: 'Léo' },
am: { user: { prenom: 'Marie', nom: 'AM' } },
},
});
await service.creer('am-1', RoleType.ASSISTANTE_MATERNELLE, {
id_placement: 'pl-1',
type: TypeEvenementAgendaType.ARRET_MALADIE_AM,
date_debut: '2026-10-01',
date_fin: '2026-10-05',
});
const saved = absencesRepo.save.mock.calls[0][0];
expect(saved.statut).toBe(StatutEvenementAgendaType.ACCEPTE);
expect(saved.verrouille_le).toBeInstanceOf(Date);
});
it('parent maj statut arrêt → 400 (ack n’écrit pas sur l’événement)', async () => {
absencesRepo.findOne.mockResolvedValue({
id: 'abs-1',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.ARRET_MALADIE_AM,
date_debut: '2026-10-01',
date_fin: '2026-10-05',
statut: StatutEvenementAgendaType.ACCEPTE,
expire_at: new Date('9999-12-31'),
cree_le: new Date(),
verrouille_le: new Date(),
});
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
await expect(
service.maj('p-1', RoleType.PARENT, 'abs-1', {
statut: StatutEvenementAgendaType.ACCEPTE,
}),
).rejects.toBeInstanceOf(BadRequestException);
});
it('soin hors [hier, auj.] → BadRequest', async () => {
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
await expect(
service.creer('am-1', RoleType.ASSISTANTE_MATERNELLE, {
id_placement: 'pl-1',
type: TypeEvenementAgendaType.SOIN_ENFANT,
date_debut: '2020-01-01',
date_fin: '2020-01-01',
}),
).rejects.toBeInstanceOf(BadRequestException);
});
it('soin daté aujourd’hui (Paris) → OK', async () => {
const auj = todayParis();
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
absencesRepo.findOne.mockResolvedValue({
id: 'abs-1',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.SOIN_ENFANT,
date_debut: auj,
date_fin: auj,
statut: StatutEvenementAgendaType.ACCEPTE,
expire_at: new Date('9999-12-31'),
cree_le: new Date(),
modifie_le: new Date(),
placement: {
enfantId: 'e-1',
amId: 'am-1',
child: { id: 'e-1', first_name: 'Léo' },
am: { user: { prenom: 'Marie', nom: 'AM' } },
},
});
await expect(
service.creer('am-1', RoleType.ASSISTANTE_MATERNELLE, {
id_placement: 'pl-1',
type: TypeEvenementAgendaType.SOIN_ENFANT,
date_debut: auj,
date_fin: auj,
}),
).resolves.toBeDefined();
// hier aussi OK
const hier = yesterdayParis();
absencesRepo.findOne.mockResolvedValue({
id: 'abs-2',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.SOIN_ENFANT,
date_debut: hier,
date_fin: hier,
statut: StatutEvenementAgendaType.ACCEPTE,
expire_at: new Date('9999-12-31'),
cree_le: new Date(),
modifie_le: new Date(),
placement: {
enfantId: 'e-1',
amId: 'am-1',
child: { id: 'e-1', first_name: 'Léo' },
am: { user: { prenom: 'Marie', nom: 'AM' } },
},
});
await expect(
service.creer('am-1', RoleType.ASSISTANTE_MATERNELLE, {
id_placement: 'pl-1',
type: TypeEvenementAgendaType.SOIN_ENFANT,
date_debut: hier,
date_fin: hier,
}),
).resolves.toBeDefined();
});
it('supprimer → soft-cancel (update ANNULE), pas delete physique', async () => {
absencesRepo.findOne.mockResolvedValue({
id: 'abs-1',
id_placement: 'pl-1',
type: TypeEvenementAgendaType.ABSENCE_ENFANT,
date_debut: '2026-10-01',
date_fin: '2026-10-02',
statut: StatutEvenementAgendaType.ACCEPTE,
cree_le: new Date(),
verrouille_le: new Date(),
cree_par: 'p-1',
});
amChildrenRepo.findOne.mockResolvedValue({
id: 'pl-1',
amId: 'am-1',
enfantId: 'e-1',
date_fin: null,
});
parentsChildrenRepo.findOne.mockResolvedValue({
parentId: 'p-1',
enfantId: 'e-1',
});
await service.supprimer('p-1', RoleType.PARENT, 'abs-1');
expect(absencesRepo.delete).not.toHaveBeenCalled();
expect(absencesRepo.update).toHaveBeenCalledWith(
{ id: 'abs-1' },
expect.objectContaining({
statut: StatutEvenementAgendaType.ANNULE,
}),
);
});
it('cron auto-valid + soft-cancel refusés ; double appel idempotent', async () => {
const old = new Date(Date.now() - 8 * 24 * 60 * 60 * 1000);
absencesRepo.find.mockResolvedValue([
{
id: 'c1',
type: TypeEvenementAgendaType.CONGE_AM,
statut: StatutEvenementAgendaType.EN_ATTENTE,
verrouille_le: old,
},
]);
const legacyQb = {
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getMany: jest.fn().mockResolvedValue([]),
};
const refuseQb = {
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getMany: jest.fn().mockResolvedValue([
{
id: 'r1',
statut: StatutEvenementAgendaType.REFUSE,
refuse_le: old,
},
]),
};
absencesRepo.createQueryBuilder
.mockReturnValueOnce(legacyQb) // autoValider legacy
.mockReturnValueOnce(refuseQb); // softCancelRefuses
absencesRepo.findOne.mockImplementation(async ({ where }) => {
if (where?.id === 'r1') {
return {
id: 'r1',
statut: StatutEvenementAgendaType.REFUSE,
refuse_le: old,
};
}
return null;
});
const n1 = await service.autoValiderCongesExpires();
expect(n1).toBe(1);
expect(absencesRepo.update).toHaveBeenCalledWith(
{ id: 'c1', statut: StatutEvenementAgendaType.EN_ATTENTE },
expect.objectContaining({
statut: StatutEvenementAgendaType.ACCEPTE,
}),
);
absencesRepo.find.mockResolvedValue([]); // déjà validés
const emptyQb = {
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getMany: jest.fn().mockResolvedValue([]),
};
absencesRepo.createQueryBuilder.mockReset();
absencesRepo.createQueryBuilder.mockReturnValue(emptyQb);
const n2 = await service.autoValiderCongesExpires();
expect(n2).toBe(0);
// soft-cancel refusés
absencesRepo.createQueryBuilder.mockReturnValue(refuseQb);
refuseQb.getMany.mockResolvedValue([
{ id: 'r1', statut: StatutEvenementAgendaType.REFUSE, refuse_le: old },
]);
absencesRepo.findOne.mockResolvedValue({
id: 'r1',
statut: StatutEvenementAgendaType.REFUSE,
refuse_le: old,
});
const sc1 = await service.softCancelRefusesExpires();
expect(sc1).toBe(1);
// 2e passage : déjà ANNULE → 0
absencesRepo.findOne.mockResolvedValue({
id: 'r1',
statut: StatutEvenementAgendaType.ANNULE,
});
refuseQb.getMany.mockResolvedValue([
{ id: 'r1', statut: StatutEvenementAgendaType.ANNULE },
]);
const sc2 = await service.softCancelRefusesExpires();
expect(sc2).toBe(0);
});
});
});