import { BadRequestException, ForbiddenException, Injectable, NotFoundException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { IsNull, Repository } from 'typeorm'; import { EvenementsAgendaService } from '../evenements-agenda/evenements-agenda.service'; import { StatutEvenementAgendaType, TypeEvenementAgendaType, } from 'src/entities/evenements_agenda.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 { CardType, CardResponseModeType } from 'src/entities/card_types.entity'; import { CardInstance, CardInstanceStatutType, CardOperationType, } from 'src/entities/card_instances.entity'; import { CardAudienceMember } from 'src/entities/card_audience_members.entity'; import { CardResponse, CardResponseActionType, } from 'src/entities/card_responses.entity'; import { CarteDto, CreerCarteDto, ListeCartesDto, MajCarteDto, RepondreCarteDto, } from './dto/cards.dto'; import { CardsRealtimeService } from './cards-realtime.service'; @Injectable() export class CardsService { constructor( @InjectRepository(CardType) private readonly typesRepo: Repository, @InjectRepository(CardInstance) private readonly cardsRepo: Repository, @InjectRepository(CardAudienceMember) private readonly audienceRepo: Repository, @InjectRepository(CardResponse) private readonly responsesRepo: Repository, @InjectRepository(AmChildren) private readonly amChildrenRepo: Repository, @InjectRepository(ParentsChildren) private readonly parentsChildrenRepo: Repository, private readonly evenementsService: EvenementsAgendaService, private readonly realtime: CardsRealtimeService, ) {} async listerTypes(role: RoleType): Promise< Pick< CardType, | 'code' | 'system' | 'titre' | 'emitter_roles' | 'recipient_roles' | 'audience_resolver' | 'response_mode' | 'retention_days' >[] > { const all = await this.typesRepo.find({ where: { system: true } }); return all .filter((t) => t.emitter_roles.includes(role)) .map((t) => ({ code: t.code, system: t.system, titre: t.titre, emitter_roles: t.emitter_roles, recipient_roles: t.recipient_roles, audience_resolver: t.audience_resolver, response_mode: t.response_mode, retention_days: t.retention_days, })); } async lister( userId: string, role: RoleType, placementId?: string, ): Promise { const qb = this.cardsRepo .createQueryBuilder('c') .innerJoin('c.audience', 'aud', 'aud.id_utilisateur = :userId', { userId }) .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'); if (placementId) { await this.assertPlacementAccess(userId, role, placementId); qb.andWhere('c.id_placement = :placementId', { placementId }); } const rows = await qb.getMany(); return { items: rows.map((c) => this.toDto(c, userId)), }; } async creer( userId: string, role: RoleType, dto: CreerCarteDto, ): Promise { if (dto.date_fin < dto.date_debut) { throw new BadRequestException('date_fin < date_debut'); } const type = await this.typesRepo.findOne({ where: { code: dto.type_code } }); if (!type || !type.system) { throw new NotFoundException('Type de carte inconnu'); } if (!type.emitter_roles.includes(role)) { throw new ForbiddenException('Vous ne pouvez pas émettre ce type de carte'); } const placement = await this.assertPlacementAccess( userId, role, dto.id_placement, ); const operation = dto.operation ?? CardOperationType.CREATE; let absenceId = dto.id_evenement; const absenceType = this.mapAbsenceType(dto.type_code); let motif = dto.motif ?? null; let finalPayload: Record = { date_debut: dto.date_debut, date_fin: dto.date_fin, motif, ...(dto.payload ?? {}), }; if (dto.type_code === 'soin_enfant') { const p = dto.payload as Record; if (!p || !p?.sous_type || !p?.heure_releve) { throw new BadRequestException('sous_type et heure_releve requis pour soin_enfant'); } if (!/^([01]\d|2[0-3]):([0-5]\d)$/.test(p?.heure_releve)) { throw new BadRequestException('Format heure_releve invalide (HH:mm)'); } if (p?.sous_type === 'fievre') { if (typeof p?.temperature_c !== 'number' || p?.temperature_c < 34.0 || p?.temperature_c > 43.0) { throw new BadRequestException('temperature_c invalide (34.0 - 43.0)'); } motif = `${p?.temperature_c.toFixed(1).replace('.', ',')} °C à ${p?.heure_releve}`; if (p?.medicament_donne && typeof p?.medicament_donne === 'string' && p?.medicament_donne.trim()) { motif += ` (Médicament : ${p?.medicament_donne.trim()})`; } } else if (p?.sous_type === 'bobo') { const natures = ['chute', 'coup', 'egratignure', 'piqure', 'brulure', 'autre']; if (!natures.includes(p?.bobo_nature)) { throw new BadRequestException('bobo_nature invalide'); } motif = `Bobo (${p?.bobo_nature}) à ${p?.heure_releve}`; if (p?.description && typeof p?.description === 'string' && p?.description.trim()) { const desc = p?.description.trim(); if (desc.length > 120) throw new BadRequestException('description trop longue (max 120)'); motif += ` : ${desc.length > 30 ? desc.substring(0, 30) + '...' : desc}`; } } else if (p?.sous_type === 'medicament') { if (!p?.medicament_nom || typeof p?.medicament_nom !== 'string' || !p?.medicament_nom.trim()) { throw new BadRequestException('medicament_nom requis'); } motif = `Médicament (${p?.medicament_nom.trim()}) à ${p?.heure_releve}`; if (p?.contexte && typeof p?.contexte === 'string' && p?.contexte.trim()) { if (p?.contexte.trim().length > 120) throw new BadRequestException('contexte trop long (max 120)'); } } else { throw new BadRequestException('sous_type invalide pour soin_enfant'); } finalPayload.motif = motif; } if (operation === CardOperationType.CREATE) { const absence = await this.evenementsService.creer(userId, role, { id_placement: dto.id_placement, type: absenceType, date_debut: dto.date_debut, date_fin: dto.date_fin, motif: motif ? String(motif) : undefined, }); absenceId = absence.id; } else { if (dto.type_code === 'soin_enfant') { throw new BadRequestException('operation=update interdite pour soin_enfant'); } if (!absenceId) { throw new BadRequestException('id_evenement requis pour operation=update'); } await this.evenementsService.maj(userId, role, absenceId, { date_debut: dto.date_debut, date_fin: dto.date_fin, motif: motif ? String(motif) : undefined, // Congé accepté modifié → repasse en attente via cartes respond flow; // pour absence enfant update immédiat déjà fait. ...(dto.type_code === 'conge_am' ? { statut: StatutEvenementAgendaType.EN_ATTENTE } : {}), }); } const statutInitial = type.response_mode === CardResponseModeType.NONE ? CardInstanceStatutType.TRAITEE : CardInstanceStatutType.OUVERTE; const card = this.cardsRepo.create({ type_code: type.code, id_placement: dto.id_placement, id_evenement: absenceId, cree_par: userId, operation, statut: statutInitial, payload: finalPayload, purge_at: this.purgeAt(type.retention_days, statutInitial), }); const saved = await this.cardsRepo.save(card); if (absenceId) { await this.linkAbsenceCard(absenceId, saved.id); } await this.buildAudience(saved, type, placement, userId, role); const full = await this.loadCard(saved.id); const dtoOut = this.toDto(full, userId); this.emitAudience(full, 'card.created', dtoOut); return dtoOut; } async repondre( userId: string, role: RoleType, cardId: string, dto: RepondreCarteDto, ): Promise { const card = await this.loadCard(cardId); await this.assertInAudience(card, userId); if (card.statut !== CardInstanceStatutType.OUVERTE) { throw new BadRequestException('Cette carte n’est plus ouverte'); } if (card.cree_par === userId) { throw new ForbiddenException('Le créateur ne répond pas à sa propre carte'); } const mode = card.type.response_mode; if (mode === CardResponseModeType.NONE) { throw new BadRequestException('Ce type de carte ne demande pas de réponse'); } if (mode === CardResponseModeType.ACK && dto.action !== CardResponseActionType.ACK) { throw new BadRequestException('Action attendue : ack'); } if ( mode === CardResponseModeType.ACCEPT_REFUSE && dto.action !== CardResponseActionType.ACCEPT && dto.action !== CardResponseActionType.REFUSE ) { throw new BadRequestException('Action attendue : accept ou refuse'); } if (dto.action === CardResponseActionType.REFUSE && !dto.comment?.trim()) { throw new BadRequestException('Motivation obligatoire en cas de refus'); } await this.responsesRepo.save( this.responsesRepo.create({ card: { id: card.id }, user: { id: userId }, action: dto.action, comment: dto.comment?.trim() || undefined, }), ); if (card.id_evenement) { if (dto.action === CardResponseActionType.ACCEPT || dto.action === CardResponseActionType.ACK) { await this.evenementsService.maj(userId, role, card.id_evenement, { statut: StatutEvenementAgendaType.ACCEPTE, }); card.statut = CardInstanceStatutType.TRAITEE; } else if (dto.action === CardResponseActionType.REFUSE) { await this.evenementsService.maj(userId, role, card.id_evenement, { statut: StatutEvenementAgendaType.REFUSE, motif: dto.comment!.trim(), }); 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); // Ne pas cardsRepo.save(card) avec relations chargées : TypeORM peut // resynchroniser les FK enfants (UPDATE … SET id_card=null). await this.cardsRepo.update( { id: card.id }, { statut: card.statut, purge_at: card.purge_at }, ); const full = await this.loadCard(cardId); const dtoOut = this.toDto(full, userId); this.emitAudience(full, 'response.added', { action: dto.action, card: dtoOut, }); this.emitAudience(full, 'card.updated', dtoOut); return dtoOut; } async majEnAttente( userId: string, role: RoleType, cardId: string, dto: MajCarteDto, ): Promise { const card = await this.loadCard(cardId); if (card.cree_par !== userId) { throw new ForbiddenException('Seul le créateur peut modifier cette carte'); } if (card.type_code === 'soin_enfant') { const msSinceCreation = Date.now() - new Date(card.cree_le).getTime(); if (msSinceCreation > 24 * 60 * 60 * 1000) { throw new BadRequestException('Délai de correction de 24h dépassé'); } } else { if ( card.statut !== CardInstanceStatutType.OUVERTE && card.statut !== CardInstanceStatutType.REFUSEE ) { throw new BadRequestException('Carte non modifiable dans cet état'); } } const debut = dto.date_debut ?? String(card.payload?.['date_debut'] ?? ''); const fin = dto.date_fin ?? String(card.payload?.['date_fin'] ?? ''); if (!debut || !fin || fin < debut) { throw new BadRequestException('Dates invalides'); } let motif = dto.motif ?? card.payload?.['motif'] ?? null; let finalPayload: Record = { ...card.payload, date_debut: debut, date_fin: fin, ...(dto.payload ?? {}), }; if (card.type_code === 'soin_enfant') { const p = finalPayload; if (!p || !p?.sous_type || !p?.heure_releve) { throw new BadRequestException('sous_type et heure_releve requis pour soin_enfant'); } if (!/^([01]\d|2[0-3]):([0-5]\d)$/.test(p?.heure_releve)) { throw new BadRequestException('Format heure_releve invalide (HH:mm)'); } if (p?.sous_type === 'fievre') { if (typeof p?.temperature_c !== 'number' || p?.temperature_c < 34.0 || p?.temperature_c > 43.0) { throw new BadRequestException('temperature_c invalide (34.0 - 43.0)'); } motif = `${p?.temperature_c.toFixed(1).replace('.', ',')} °C à ${p?.heure_releve}`; if (p?.medicament_donne && typeof p?.medicament_donne === 'string' && p?.medicament_donne.trim()) { motif += ` (Médicament : ${p?.medicament_donne.trim()})`; } } else if (p?.sous_type === 'bobo') { const natures = ['chute', 'coup', 'egratignure', 'piqure', 'brulure', 'autre']; if (!natures.includes(p?.bobo_nature)) { throw new BadRequestException('bobo_nature invalide'); } motif = `Bobo (${p?.bobo_nature}) à ${p?.heure_releve}`; if (p?.description && typeof p?.description === 'string' && p?.description.trim()) { const desc = p?.description.trim(); if (desc.length > 120) throw new BadRequestException('description trop longue (max 120)'); motif += ` : ${desc.length > 30 ? desc.substring(0, 30) + '...' : desc}`; } } else if (p?.sous_type === 'medicament') { if (!p?.medicament_nom || typeof p?.medicament_nom !== 'string' || !p?.medicament_nom.trim()) { throw new BadRequestException('medicament_nom requis'); } motif = `Médicament (${p?.medicament_nom.trim()}) à ${p?.heure_releve}`; if (p?.contexte && typeof p?.contexte === 'string' && p?.contexte.trim()) { if (p?.contexte.trim().length > 120) throw new BadRequestException('contexte trop long (max 120)'); } } else { throw new BadRequestException('sous_type invalide pour soin_enfant'); } finalPayload.motif = motif; const hist = Array.isArray(card.payload?.['historique']) ? card.payload['historique'] : []; const oldPayload = { ...card.payload }; delete oldPayload.historique; hist.push({ ...oldPayload, corrige_le: new Date().toISOString(), corrige_par: userId, }); finalPayload.historique = hist; finalPayload.corrige_le = new Date().toISOString(); } else { finalPayload.motif = motif; } if (card.id_evenement) { await this.evenementsService.maj(userId, role, card.id_evenement, { date_debut: debut, date_fin: fin, motif: motif ? String(motif) : undefined, ...(card.type_code !== 'soin_enfant' ? { statut: StatutEvenementAgendaType.EN_ATTENTE } : {}), }); } card.payload = finalPayload; card.statut = CardInstanceStatutType.OUVERTE; card.purge_at = this.purgeAt(card.type.retention_days, card.statut); await this.cardsRepo.manager.query( `UPDATE card_instances SET statut = $1, purge_at = $2, payload = $3::jsonb, modifie_le = now() WHERE id = $4`, [card.statut, card.purge_at, JSON.stringify(card.payload), card.id], ); const full = await this.loadCard(cardId); const dtoOut = this.toDto(full, userId); this.emitAudience(full, 'card.updated', dtoOut); return dtoOut; } async supprimer( userId: string, role: RoleType, cardId: string, ): Promise { const card = await this.loadCard(cardId); if (card.cree_par !== userId) { throw new ForbiddenException('Seul le créateur peut supprimer cette carte'); } if (card.type_code === 'soin_enfant') { const msSinceCreation = Date.now() - new Date(card.cree_le).getTime(); if (msSinceCreation > 24 * 60 * 60 * 1000) { throw new BadRequestException('Délai de correction de 24h dépassé, suppression impossible'); } } const audienceIds = await this.audienceUserIds(card); 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.cardsRepo.delete({ id: cardId }); this.realtime.emitToUsers(audienceIds, 'card.deleted', cardId, { id: cardId, }); } private emitAudience( card: CardInstance, event: 'card.created' | 'card.updated' | 'response.added', data: unknown, ): void { const ids = (card.audience ?? []).map((a) => a.id_utilisateur); if (ids.length === 0) return; this.realtime.emitToUsers(ids, event, card.id, data); } private async audienceUserIds(card: CardInstance): Promise { if (card.audience?.length) { return card.audience.map((a) => a.id_utilisateur); } const rows = await this.audienceRepo.find({ where: { id_card: card.id } }); return rows.map((r) => r.id_utilisateur); } private mapAbsenceType(typeCode: string): TypeEvenementAgendaType { if (typeCode === 'absence_enfant' || typeCode === 'absence_enfant_modif') { return TypeEvenementAgendaType.ABSENCE_ENFANT; } if (typeCode === 'conge_am') return TypeEvenementAgendaType.CONGE_AM; if (typeCode === 'arret_maladie_am') { return TypeEvenementAgendaType.ARRET_MALADIE_AM; } if (typeCode === 'soin_enfant') { return TypeEvenementAgendaType.SOIN_ENFANT; } throw new BadRequestException(`Type non mappé à une absence: ${typeCode}`); } private async linkAbsenceCard(absenceId: string, cardId: string): Promise { // Update direct pour éviter droits maj vides await this.cardsRepo.manager.query( `UPDATE evenements_agenda SET id_card_instance = $1, modifie_le = now() WHERE id = $2`, [cardId, absenceId], ); } private async buildAudience( card: CardInstance, type: CardType, placement: AmChildren, creatorId: string, creatorRole: RoleType, ): Promise { const members: Partial[] = [ { id_card: card.id, id_utilisateur: creatorId, role_snapshot: creatorRole, is_creator: true, }, ]; if (type.audience_resolver === 'couple_am') { // Destinataire métier = AM + miroir foyer : tous les co-parents de l’enfant members.push({ id_card: card.id, id_utilisateur: placement.amId, role_snapshot: RoleType.ASSISTANTE_MATERNELLE, is_creator: false, }); const liensAm = await this.parentsChildrenRepo.find({ where: { enfantId: placement.enfantId }, }); for (const l of liensAm) { if (l.parentId === creatorId) continue; members.push({ id_card: card.id, id_utilisateur: l.parentId, role_snapshot: RoleType.PARENT, is_creator: false, }); } } else if (type.audience_resolver === 'couple_parents') { const liens = await this.parentsChildrenRepo.find({ where: { enfantId: placement.enfantId }, }); for (const l of liens) { if (l.parentId === creatorId) continue; members.push({ id_card: card.id, id_utilisateur: l.parentId, role_snapshot: RoleType.PARENT, is_creator: false, }); } } // dédup const seen = new Set(); const unique = members.filter((m) => { const k = m.id_utilisateur!; if (seen.has(k)) return false; seen.add(k); return true; }); await this.audienceRepo.save(this.audienceRepo.create(unique)); } private purgeAt( retentionDays: number, statut: CardInstanceStatutType, ): Date { const days = statut === CardInstanceStatutType.OUVERTE ? Math.max(retentionDays, 15) : retentionDays; return new Date(Date.now() + days * 24 * 60 * 60 * 1000); } private async loadCard(id: string): Promise { const card = await this.cardsRepo.findOne({ where: { id }, relations: ['type', 'responses', 'audience'], }); if (!card) throw new NotFoundException('Carte introuvable'); return card; } private async assertInAudience(card: CardInstance, userId: string): Promise { const ok = (card.audience ?? []).some((a) => a.id_utilisateur === userId); if (!ok) { const row = await this.audienceRepo.findOne({ where: { id_card: card.id, id_utilisateur: userId }, }); if (!row) throw new ForbiddenException('Carte hors de votre audience'); } } private async assertPlacementAccess( userId: string, role: RoleType, placementId: string, ): Promise { const placement = await this.amChildrenRepo.findOne({ where: { id: placementId, date_fin: IsNull() }, }); if (!placement) { throw new NotFoundException('Placement introuvable ou inactif'); } if (role === RoleType.ASSISTANTE_MATERNELLE) { if (placement.amId !== userId) { throw new ForbiddenException('Placement non autorisé'); } return placement; } if (role === RoleType.PARENT) { const lien = await this.parentsChildrenRepo.findOne({ where: { parentId: userId, enfantId: placement.enfantId }, }); if (!lien) throw new ForbiddenException('Placement non autorisé'); return placement; } throw new ForbiddenException('Rôle non autorisé'); } private toDto(card: CardInstance, userId: string): CarteDto { const lastRefuse = [...(card.responses ?? [])] .reverse() .find((r) => r.action === CardResponseActionType.REFUSE); return { id: card.id, type_code: card.type_code, titre: card.type?.titre ?? card.type_code, id_placement: card.id_placement, id_evenement: card.id_evenement ?? null, operation: card.operation, statut: card.statut, payload: card.payload ?? {}, purge_at: card.purge_at?.toISOString?.() ?? String(card.purge_at), cree_par: card.cree_par ?? null, is_creator: card.cree_par === userId, response_mode: card.type?.response_mode, last_refuse_comment: lastRefuse?.comment ?? null, cree_le: card.cree_le?.toISOString?.() ?? String(card.cree_le), modifie_le: card.modifie_le?.toISOString?.() ?? String(card.modifie_le), }; } }