feat(#215): Agenda B1 — GET :id + DTO enrichi
Closes #215 Co-authored-by: Julien Martin <julien.martin@ptits-pas.fr>
This commit was merged in pull request #231.
This commit is contained in:
@@ -11,11 +11,13 @@ import {
|
||||
StatutEvenementAgendaType,
|
||||
TypeEvenementAgendaType,
|
||||
} from 'src/entities/evenements_agenda.entity';
|
||||
import { AgendaLotStatutType } from 'src/entities/agenda_lots.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 {
|
||||
EvenementAgendaDto,
|
||||
EvenementAgendaActionsDto,
|
||||
CreerEvenementAgendaDto,
|
||||
ListeEvenementsAgendaDto,
|
||||
MajEvenementAgendaDto,
|
||||
@@ -63,6 +65,7 @@ export class EvenementsAgendaService {
|
||||
.leftJoinAndSelect('placement.child', 'child')
|
||||
.leftJoinAndSelect('placement.am', 'am')
|
||||
.leftJoinAndSelect('am.user', 'amUser')
|
||||
.leftJoinAndSelect('ag.lot', 'lot')
|
||||
.where('ag.id_placement IN (:...placementIds)', { placementIds })
|
||||
.orderBy('ag.date_debut', 'DESC');
|
||||
|
||||
@@ -80,7 +83,18 @@ export class EvenementsAgendaService {
|
||||
}
|
||||
|
||||
const rows = await qb.getMany();
|
||||
return { items: rows.map((r) => this.toDto(r)) };
|
||||
return {
|
||||
items: rows.map((r) => this.toDto(r, userId, role)),
|
||||
};
|
||||
}
|
||||
|
||||
/** GET :id — #215 */
|
||||
async getById(
|
||||
id: string,
|
||||
userId: string,
|
||||
role: RoleType,
|
||||
): Promise<EvenementAgendaDto> {
|
||||
return this.getByIdForUser(id, userId, role);
|
||||
}
|
||||
|
||||
async creer(
|
||||
@@ -186,13 +200,22 @@ export class EvenementsAgendaService {
|
||||
): Promise<EvenementAgendaDto> {
|
||||
const row = await this.absencesRepo.findOne({
|
||||
where: { id },
|
||||
relations: ['placement', 'placement.child', 'placement.am', 'placement.am.user'],
|
||||
relations: [
|
||||
'placement',
|
||||
'placement.child',
|
||||
'placement.am',
|
||||
'placement.am.user',
|
||||
'lot',
|
||||
],
|
||||
});
|
||||
if (!row) {
|
||||
throw new NotFoundException('Absence introuvable');
|
||||
throw new NotFoundException('Événement introuvable');
|
||||
}
|
||||
await this.assertCanAccessPlacement(userId, role, row.id_placement);
|
||||
return this.toDto(row);
|
||||
// Lecture : historique autorisé même si placement terminé (B1 / prépare B3)
|
||||
await this.assertCanAccessPlacement(userId, role, row.id_placement, {
|
||||
allowEnded: true,
|
||||
});
|
||||
return this.toDto(row, userId, role);
|
||||
}
|
||||
|
||||
private statutInitial(type: TypeEvenementAgendaType): StatutEvenementAgendaType {
|
||||
@@ -367,12 +390,19 @@ export class EvenementsAgendaService {
|
||||
userId: string,
|
||||
role: RoleType,
|
||||
placementId: string,
|
||||
opts?: { allowEnded?: boolean },
|
||||
): Promise<AmChildren> {
|
||||
const placement = await this.amChildrenRepo.findOne({
|
||||
where: { id: placementId, date_fin: IsNull() },
|
||||
where: opts?.allowEnded
|
||||
? { id: placementId }
|
||||
: { id: placementId, date_fin: IsNull() },
|
||||
});
|
||||
if (!placement) {
|
||||
throw new NotFoundException('Placement / couple introuvable ou inactif');
|
||||
throw new NotFoundException(
|
||||
opts?.allowEnded
|
||||
? 'Placement / couple introuvable'
|
||||
: 'Placement / couple introuvable ou inactif',
|
||||
);
|
||||
}
|
||||
|
||||
if (role === RoleType.ASSISTANTE_MATERNELLE) {
|
||||
@@ -397,26 +427,187 @@ export class EvenementsAgendaService {
|
||||
throw new ForbiddenException('Rôle non autorisé');
|
||||
}
|
||||
|
||||
private toDto(row: EvenementsAgenda): EvenementAgendaDto {
|
||||
private toDto(
|
||||
row: EvenementsAgenda,
|
||||
userId?: string,
|
||||
role?: RoleType,
|
||||
): EvenementAgendaDto {
|
||||
const child = row.placement?.child;
|
||||
const amUser = row.placement?.am?.user;
|
||||
const statutLot = (row.lot?.statut as AgendaLotStatutType | undefined) ?? null;
|
||||
const statutEffectif = this.statutEffectif(row, statutLot);
|
||||
return {
|
||||
id: row.id,
|
||||
id_placement: row.id_placement,
|
||||
id_lot: row.id_lot ?? null,
|
||||
id_card_instance: row.id_card_instance ?? null,
|
||||
type: row.type,
|
||||
date_debut: row.date_debut,
|
||||
date_fin: row.date_fin,
|
||||
heure: this.formatHeure(row.heure),
|
||||
statut: row.statut,
|
||||
statut_lot: statutLot,
|
||||
statut_effectif: statutEffectif,
|
||||
expire_at: row.expire_at?.toISOString?.() ?? String(row.expire_at),
|
||||
verrouille_le: row.verrouille_le
|
||||
? row.verrouille_le.toISOString?.() ?? String(row.verrouille_le)
|
||||
: null,
|
||||
refuse_le: row.refuse_le
|
||||
? row.refuse_le.toISOString?.() ?? String(row.refuse_le)
|
||||
: null,
|
||||
annule_le: row.annule_le
|
||||
? row.annule_le.toISOString?.() ?? String(row.annule_le)
|
||||
: null,
|
||||
cree_par: row.cree_par ?? null,
|
||||
motif: row.motif ?? null,
|
||||
libelle_refus: this.libelleRefus(row, statutLot, userId, role),
|
||||
id_enfant: child?.id ?? row.placement?.enfantId ?? null,
|
||||
prenom_enfant: child?.first_name ?? null,
|
||||
id_am: row.placement?.amId ?? null,
|
||||
prenom_am: amUser?.prenom ?? null,
|
||||
nom_am: amUser?.nom ?? null,
|
||||
actions: this.actionsPour(row, statutLot, userId, role),
|
||||
cree_le: row.cree_le?.toISOString?.() ?? String(row.cree_le),
|
||||
modifie_le: row.modifie_le?.toISOString?.() ?? String(row.modifie_le),
|
||||
};
|
||||
}
|
||||
|
||||
private formatHeure(heure?: string | null): string | null {
|
||||
if (!heure) return null;
|
||||
const s = String(heure);
|
||||
return s.length >= 5 ? s.slice(0, 5) : s;
|
||||
}
|
||||
|
||||
private statutEffectif(
|
||||
row: EvenementsAgenda,
|
||||
statutLot: AgendaLotStatutType | null,
|
||||
): StatutEvenementAgendaType {
|
||||
if (row.statut === StatutEvenementAgendaType.ANNULE) {
|
||||
return StatutEvenementAgendaType.ANNULE;
|
||||
}
|
||||
if (statutLot === AgendaLotStatutType.ANNULE) {
|
||||
return StatutEvenementAgendaType.ANNULE;
|
||||
}
|
||||
if (statutLot === AgendaLotStatutType.REFUSE) {
|
||||
return StatutEvenementAgendaType.REFUSE;
|
||||
}
|
||||
return row.statut;
|
||||
}
|
||||
|
||||
/**
|
||||
* Anonymise l’auteur du refus : jamais le nom d’une autre famille.
|
||||
* B1 : si refusé et viewer ≠ créateur → « autre famille » ;
|
||||
* si viewer est parent créateur/foyer et motif présent → « autre parent » pour co-parent
|
||||
* (sans id répondant en B1 : défaut « autre famille » pour parent destinataire).
|
||||
*/
|
||||
private libelleRefus(
|
||||
row: EvenementsAgenda,
|
||||
statutLot: AgendaLotStatutType | null,
|
||||
userId?: string,
|
||||
role?: RoleType,
|
||||
): string | null {
|
||||
const refuse =
|
||||
row.statut === StatutEvenementAgendaType.REFUSE ||
|
||||
statutLot === AgendaLotStatutType.REFUSE;
|
||||
if (!refuse) return null;
|
||||
if (!userId || !role) return 'Refusé';
|
||||
if (role === RoleType.ASSISTANTE_MATERNELLE) {
|
||||
return 'Refusé par une famille';
|
||||
}
|
||||
// Parent : ne jamais citer quelle famille. Si ce n’est pas le créateur AM, message générique.
|
||||
if (row.cree_par && row.cree_par === userId) {
|
||||
return null;
|
||||
}
|
||||
return 'Refusé par une autre famille';
|
||||
}
|
||||
|
||||
private actionsPour(
|
||||
row: EvenementsAgenda,
|
||||
statutLot: AgendaLotStatutType | null,
|
||||
userId?: string,
|
||||
role?: RoleType,
|
||||
): EvenementAgendaActionsDto {
|
||||
const none: EvenementAgendaActionsDto = {
|
||||
peut_modifier: false,
|
||||
peut_supprimer: false,
|
||||
peut_accepter_refuser: false,
|
||||
peut_ack: false,
|
||||
};
|
||||
if (!userId || !role) return none;
|
||||
if (
|
||||
row.statut === StatutEvenementAgendaType.ANNULE ||
|
||||
statutLot === AgendaLotStatutType.ANNULE
|
||||
) {
|
||||
return none;
|
||||
}
|
||||
|
||||
const isCreator = row.cree_par === userId;
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
const notStarted = row.date_debut > today;
|
||||
const enAttente =
|
||||
row.statut === StatutEvenementAgendaType.EN_ATTENTE ||
|
||||
statutLot === AgendaLotStatutType.EN_ATTENTE;
|
||||
const refuse =
|
||||
row.statut === StatutEvenementAgendaType.REFUSE ||
|
||||
statutLot === AgendaLotStatutType.REFUSE;
|
||||
const accepte =
|
||||
row.statut === StatutEvenementAgendaType.ACCEPTE ||
|
||||
statutLot === AgendaLotStatutType.ACCEPTE;
|
||||
|
||||
if (role === RoleType.ASSISTANTE_MATERNELLE && isCreator) {
|
||||
const congeFuturModifiable =
|
||||
row.type === TypeEvenementAgendaType.CONGE_AM &&
|
||||
accepte &&
|
||||
notStarted;
|
||||
return {
|
||||
peut_modifier:
|
||||
enAttente || refuse || congeFuturModifiable ||
|
||||
(row.type !== TypeEvenementAgendaType.CONGE_AM &&
|
||||
row.type !== TypeEvenementAgendaType.SOIN_ENFANT &&
|
||||
accepte),
|
||||
peut_supprimer:
|
||||
enAttente ||
|
||||
refuse ||
|
||||
(row.type === TypeEvenementAgendaType.CONGE_AM &&
|
||||
accepte &&
|
||||
notStarted) ||
|
||||
row.type === TypeEvenementAgendaType.ARRET_MALADIE_AM ||
|
||||
row.type === TypeEvenementAgendaType.SOIN_ENFANT,
|
||||
peut_accepter_refuser: false,
|
||||
peut_ack: false,
|
||||
};
|
||||
}
|
||||
|
||||
if (role === RoleType.PARENT && !isCreator) {
|
||||
const congeEnAttente =
|
||||
row.type === TypeEvenementAgendaType.CONGE_AM &&
|
||||
row.statut === StatutEvenementAgendaType.EN_ATTENTE &&
|
||||
statutLot !== AgendaLotStatutType.REFUSE &&
|
||||
statutLot !== AgendaLotStatutType.ANNULE;
|
||||
const arretAck =
|
||||
row.type === TypeEvenementAgendaType.ARRET_MALADIE_AM;
|
||||
const soinAck = row.type === TypeEvenementAgendaType.SOIN_ENFANT;
|
||||
return {
|
||||
peut_modifier: false,
|
||||
peut_supprimer: false,
|
||||
peut_accepter_refuser: congeEnAttente,
|
||||
peut_ack: arretAck || soinAck,
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
role === RoleType.PARENT &&
|
||||
isCreator &&
|
||||
row.type === TypeEvenementAgendaType.ABSENCE_ENFANT
|
||||
) {
|
||||
return {
|
||||
peut_modifier: true,
|
||||
peut_supprimer: true,
|
||||
peut_accepter_refuser: false,
|
||||
peut_ack: false,
|
||||
};
|
||||
}
|
||||
|
||||
return none;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user