feat(#216): Agenda B2 — soft-cancel + matrice + cron
Closes #216 Co-authored-by: Julien Martin <julien.martin@ptits-pas.fr>
This commit was merged in pull request #234.
This commit is contained in:
@@ -373,4 +373,80 @@ describe('CardsService (#194)', () => {
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
describe('B2 — #216 soft-cancel + ack arrêt', () => {
|
||||
it('ack arrêt maladie → pas de maj événement + carte traitée', async () => {
|
||||
cardsRepo.findOne.mockResolvedValue({
|
||||
id: 'card-arret',
|
||||
type_code: 'arret_maladie_am',
|
||||
id_placement: 'pl-1',
|
||||
id_evenement: 'ev-arret',
|
||||
cree_par: 'am-1',
|
||||
statut: CardInstanceStatutType.OUVERTE,
|
||||
purge_at: new Date(Date.now() - 60_000), // déjà hors fil
|
||||
payload: {},
|
||||
type: {
|
||||
titre: 'Arrêt',
|
||||
response_mode: CardResponseModeType.ACK,
|
||||
retention_days: 7,
|
||||
},
|
||||
responses: [],
|
||||
audience: [
|
||||
{ id_utilisateur: 'am-1' },
|
||||
{ id_utilisateur: 'p-1' },
|
||||
],
|
||||
});
|
||||
audienceRepo.findOne.mockResolvedValue({
|
||||
id_card: 'card-arret',
|
||||
id_utilisateur: 'p-1',
|
||||
});
|
||||
|
||||
const res = await service.repondre('p-1', RoleType.PARENT, 'card-arret', {
|
||||
action: 'ack' as never,
|
||||
});
|
||||
|
||||
expect(evenementsService.maj).not.toHaveBeenCalled();
|
||||
expect(cardsRepo.update).toHaveBeenCalledWith(
|
||||
{ id: 'card-arret' },
|
||||
expect.objectContaining({ statut: CardInstanceStatutType.TRAITEE }),
|
||||
);
|
||||
expect(res.statut).toBe(CardInstanceStatutType.TRAITEE);
|
||||
});
|
||||
|
||||
it('supprimer carte → soft-cancel (update ANNULEE), pas delete', async () => {
|
||||
cardsRepo.findOne.mockResolvedValue({
|
||||
id: 'card-1',
|
||||
type_code: 'absence_enfant',
|
||||
id_placement: 'pl-1',
|
||||
id_evenement: 'ev-1',
|
||||
cree_par: 'p-1',
|
||||
statut: CardInstanceStatutType.OUVERTE,
|
||||
cree_le: new Date(),
|
||||
audience: [{ id_utilisateur: 'p-1' }, { id_utilisateur: 'am-1' }],
|
||||
type: { retention_days: 14 },
|
||||
});
|
||||
evenementsService.supprimer.mockResolvedValue(undefined);
|
||||
|
||||
await service.supprimer('p-1', RoleType.PARENT, 'card-1');
|
||||
|
||||
expect(cardsRepo.delete).not.toHaveBeenCalled();
|
||||
expect(evenementsService.supprimer).toHaveBeenCalledWith(
|
||||
'p-1',
|
||||
RoleType.PARENT,
|
||||
'ev-1',
|
||||
);
|
||||
expect(cardsRepo.update).toHaveBeenCalledWith(
|
||||
{ id: 'card-1' },
|
||||
expect.objectContaining({
|
||||
statut: CardInstanceStatutType.ANNULEE,
|
||||
}),
|
||||
);
|
||||
expect(realtime.emitToUsers).toHaveBeenCalledWith(
|
||||
expect.any(Array),
|
||||
'card.deleted',
|
||||
'card-1',
|
||||
expect.any(Object),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -92,11 +92,11 @@ export class CardsService {
|
||||
.leftJoinAndSelect('c.type', 'type')
|
||||
.leftJoinAndSelect('c.responses', 'responses')
|
||||
.where('c.purge_at > now()')
|
||||
.orderBy(
|
||||
`CASE c.statut WHEN 'refusee' THEN 0 WHEN 'ouverte' THEN 1 ELSE 2 END`,
|
||||
'ASC',
|
||||
)
|
||||
.addOrderBy('c.modifie_le', 'DESC');
|
||||
.andWhere('c.statut != :annulee', {
|
||||
annulee: CardInstanceStatutType.ANNULEE,
|
||||
})
|
||||
// Tri fil : COALESCE(remonte_le, cree_le) DESC (mini-spec §10)
|
||||
.orderBy('COALESCE(c.remonte_le, c.cree_le)', 'DESC');
|
||||
|
||||
if (placementId) {
|
||||
await this.assertPlacementAccess(userId, role, placementId);
|
||||
@@ -251,6 +251,10 @@ export class CardsService {
|
||||
): Promise<CarteDto> {
|
||||
const card = await this.loadCard(cardId);
|
||||
await this.assertInAudience(card, userId);
|
||||
// Respond OK même après purge_at (sortie du fil) — AC #216
|
||||
if (card.statut === CardInstanceStatutType.ANNULEE) {
|
||||
throw new BadRequestException('Cette carte est annulée');
|
||||
}
|
||||
if (card.statut !== CardInstanceStatutType.OUVERTE) {
|
||||
throw new BadRequestException('Cette carte n’est plus ouverte');
|
||||
}
|
||||
@@ -285,8 +289,11 @@ export class CardsService {
|
||||
}),
|
||||
);
|
||||
|
||||
if (card.id_evenement) {
|
||||
if (dto.action === CardResponseActionType.ACCEPT || dto.action === CardResponseActionType.ACK) {
|
||||
// ACK informatif (arrêt / soin) : n’écrit plus sur l’événement (#216)
|
||||
if (dto.action === CardResponseActionType.ACK) {
|
||||
card.statut = CardInstanceStatutType.TRAITEE;
|
||||
} else if (card.id_evenement) {
|
||||
if (dto.action === CardResponseActionType.ACCEPT) {
|
||||
await this.evenementsService.maj(userId, role, card.id_evenement, {
|
||||
statut: StatutEvenementAgendaType.ACCEPTE,
|
||||
});
|
||||
@@ -298,8 +305,6 @@ export class CardsService {
|
||||
});
|
||||
card.statut = CardInstanceStatutType.REFUSEE;
|
||||
}
|
||||
} else if (dto.action === CardResponseActionType.ACK) {
|
||||
card.statut = CardInstanceStatutType.TRAITEE;
|
||||
}
|
||||
|
||||
card.purge_at = this.purgeAt(card.type.retention_days, card.statut);
|
||||
@@ -456,22 +461,21 @@ export class CardsService {
|
||||
}
|
||||
|
||||
const audienceIds = await this.audienceUserIds(card);
|
||||
// Soft-cancel événement lié (matrice temporelle côté evenements) — #216
|
||||
if (card.id_evenement) {
|
||||
const absStatut =
|
||||
card.statut === CardInstanceStatutType.TRAITEE
|
||||
? null
|
||||
: card.id_evenement;
|
||||
// Supprime l’absence liée si pas encore acceptée définitivement (ou si soin_enfant dans les 24h)
|
||||
if (
|
||||
card.statut === CardInstanceStatutType.OUVERTE ||
|
||||
card.statut === CardInstanceStatutType.REFUSEE ||
|
||||
card.type_code === 'soin_enfant'
|
||||
) {
|
||||
await this.evenementsService.supprimer(userId, role, card.id_evenement);
|
||||
}
|
||||
void absStatut;
|
||||
await this.evenementsService.supprimer(userId, role, card.id_evenement);
|
||||
}
|
||||
await this.cardsRepo.delete({ id: cardId });
|
||||
// Soft-cancel carte — aucun DELETE physique applicatif (#216)
|
||||
const now = new Date();
|
||||
await this.cardsRepo.update(
|
||||
{ id: cardId },
|
||||
{
|
||||
statut: CardInstanceStatutType.ANNULEE,
|
||||
annule_le: now,
|
||||
annule_par: userId,
|
||||
purge_at: now,
|
||||
},
|
||||
);
|
||||
this.realtime.emitToUsers(audienceIds, 'card.deleted', cardId, {
|
||||
id: cardId,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user