agent done

This commit is contained in:
OnlyPapy98
2026-01-07 16:06:54 +01:00
parent 0ae7fa316e
commit d7bcbce50d
16 changed files with 658 additions and 425 deletions

View File

@@ -27,6 +27,7 @@ import { forkJoin, of } from 'rxjs';
import { switchMap, catchError } from 'rxjs/operators';
import { toast } from 'ngx-sonner';
import { AgentForm } from '@shared/forms/agent-form/agent-form';
import { TpeSelect } from "../tpe-select/tpe-select";
@Component({
standalone: true,
@@ -41,11 +42,10 @@ import { AgentForm } from '@shared/forms/agent-form/agent-form';
Modal,
ZardButtonComponent,
ZardCardComponent,
ZardSelectComponent,
ZardSelectItemComponent,
ZardFormModule,
AgentForm
],
AgentForm,
TpeSelect
],
})
export class AgentsPage {
rows = signal<Agent[]>([]);
@@ -67,11 +67,17 @@ export class AgentsPage {
// TPE Assignment modal
assignTpeModalOpen = signal(false);
assigningAgent = signal<Agent | null>(null);
assigningAgent = signal<Agent | undefined>(undefined);
availableTpes = signal<TpeDevice[]>([]);
selectedTpeId = signal<string>('');
selectedTpeId = signal<string[]>([]);
tpesLoading = signal(false);
tpeDevice = signal<TpeDevice | undefined>(undefined);
tpeDevices = signal<TpeDevice[]>([]);
tpeArray = signal<boolean>(false);
@ViewChild(AgentFullForm) formComp?: AgentFullForm;
formatTpeStatut(statut: TpeStatus): string {
@@ -95,28 +101,19 @@ export class AgentsPage {
{ key: 'profil', label: 'Profil', sortable: true, defaultVisible: true },
{ key: 'statut', label: 'Statut', sortable: true, defaultVisible: true, cell: (a) => this.renderStatutBadge(a.statut) },
{ key: 'phone', label: 'Téléphone', sortable: true, defaultVisible: true },
{ key: 'zone', label: 'Zone', sortable: true },
{ key: 'kiosk', label: 'Kiosque', sortable: true },
{ key: 'tpes', label: 'TPE', cell: (a) => `${(this.getAgentTpes(a.id) || []).length}` },
{ key: 'limites', label: 'Limites', cell: (a) => this.formatLimits(a) },
{ key: 'dateEmbauche', label: 'Embauché le', cell: (a) => (a.dateEmbauche ? new Date(a.dateEmbauche).toLocaleDateString() : '') },
{ key: 'zone', label: 'Zone', sortable: true, defaultVisible: true },
];
tpeMap = new Map<string, TpeDevice>();
agentTpesMap = new Map<string, TpeDevice[]>();
constructor(
private api: AgentService,
private tpeSvc: TpeService,
private familyMemberService: AgentFamilyMemberService
) {
// Preload TPE maps for display
this.tpeSvc
.list({ page: 1, size: 200, search: '', sortKey: 'id', sortDir: 'asc' } as any)
.subscribe((res) => {
const tpes = res.content as TpeDevice[];
this.rebuildTpeMaps(tpes);
});
effect(() => {
const params = {
page: this.page(),
@@ -139,12 +136,9 @@ export class AgentsPage {
this.loading.set(true);
this.api.list(params).subscribe({
next: (res) => {
console.log(res.content);
this.rows.set(res.content);
this.total.set(res.pageable.total);
this.loading.set(false);
// Refresh TPE map to ensure we have latest data
this.refreshTpeMap();
},
error: () => {
this.rows.set([]);
@@ -154,32 +148,6 @@ export class AgentsPage {
});
}
private refreshTpeMap() {
this.tpeSvc
.list({ page: 1, size: 200, search: '', sortKey: 'imei', sortDir: 'asc' } as any)
.subscribe((res) => {
const tpes = res.content as TpeDevice[];
this.rebuildTpeMaps(tpes);
});
}
private rebuildTpeMaps(tpes: TpeDevice[]) {
this.tpeMap.clear();
this.agentTpesMap.clear();
tpes.forEach((t) => {
this.tpeMap.set(t.id, t);
const agentId = t.agentConnecteId;
if (agentId) {
const list = this.agentTpesMap.get(agentId) || [];
list.push(t);
this.agentTpesMap.set(agentId, list);
}
});
}
getAgentTpes(agentId: string): TpeDevice[] {
return this.agentTpesMap.get(agentId) || [];
}
renderStatutBadge(statut: Agent['statut'] | string | undefined): string {
if (!statut) return '';
@@ -218,6 +186,8 @@ export class AgentsPage {
closeModal() {
this.modalOpen.set(false);
}
openDetail(row: Agent) {
// Fetch full agent details
this.api.getById(row.id).subscribe({
@@ -233,6 +203,31 @@ export class AgentsPage {
this.detailFamilyMembers.set([]);
},
});
const tpeIds = agent.terminauxIds;
if(Array.isArray(tpeIds)){
this.tpeArray.set(true);
forkJoin(
tpeIds.map(id=>this.tpeSvc.getById(String(id)))
).subscribe({
next:(tpes)=>{
this.tpeDevices.set(tpes.filter(tpe=>tpe!==undefined))
},
error:(err)=>{
console.error(err);
}
})
}else{
this.tpeArray.set(false);
this.tpeSvc.getById(String(tpeIds)).subscribe({
next:(tpe)=>{
if(tpe && tpe !== undefined)
this.tpeDevice.set(tpe);
},
error:(err)=>{
console.error(err);
}
})
}
this.detailModalOpen.set(true);
}
},
@@ -386,6 +381,23 @@ export class AgentsPage {
});
}
getSelectedTpeIds = (): string[] => {
const ids = this.assigningAgent()?.terminauxIds;
if (!ids) return []; // undefined ou null → tableau vide
// Si c'est un tableau, on map en string
if (Array.isArray(ids)) {
return ids.map(id => id.toString());
}
// Si c'est un seul nombre, on retourne un tableau avec un élément
return [ids.toString()];
};
remove(row: Agent) {
if (!confirm(`Supprimer l\'agent ${row.code} ?`)) return;
this.api.delete(row.id).subscribe(() =>
@@ -401,75 +413,91 @@ export class AgentsPage {
openAssignTpe(agent: Agent) {
this.assigningAgent.set(agent);
this.selectedTpeId.set('');
this.loadAvailableTpes();
this.selectedTpeId.set([]);
this.assignTpeModalOpen.set(true);
}
loadAvailableTpes() {
this.tpesLoading.set(true);
const agent = this.assigningAgent();
if (!agent) {
this.availableTpes.set([]);
this.tpesLoading.set(false);
return;
}
// loadAvailableTpes() {
// this.tpesLoading.set(true);
// const agent = this.assigningAgent();
// if (!agent) {
// this.availableTpes.set([]);
// this.tpesLoading.set(false);
// return;
// }
const currentAgentTpes = this.agentTpesMap.get(agent.id) || [];
const agentTpeIds = new Set(currentAgentTpes.map((t) => t.id));
// const currentAgentTpes = this.agentTpesMap.get(agent.id) || [];
// const agentTpeIds = new Set(currentAgentTpes.map((t) => t.id));
// Load available TPEs (DISPONIBLE or VALIDE status)
forkJoin([this.tpeSvc.getByStatut('ACTIF'), this.tpeSvc.getByStatut('ACTIF')]).subscribe({
next: ([disponibleTpes, valideTpes]) => {
// Combine and filter: only show TPEs that are not assigned to any agent AND not already assigned to this agent
const allTpes = [...disponibleTpes, ...valideTpes];
const available = allTpes.filter(
(t) =>
!t.agentConnecteId &&
(t.statut === 'ACTIF') &&
!agentTpeIds.has(t.id)
);
// Remove duplicates
const uniqueTpes = Array.from(new Map(available.map((t) => [t.id, t])).values());
this.availableTpes.set(uniqueTpes);
this.tpesLoading.set(false);
},
error: () => {
this.availableTpes.set([]);
this.tpesLoading.set(false);
},
});
}
// // Load available TPEs (DISPONIBLE or VALIDE status)
// forkJoin([this.tpeSvc.getByStatut('ACTIF'), this.tpeSvc.getByStatut('ACTIF')]).subscribe({
// next: ([disponibleTpes, valideTpes]) => {
// // Combine and filter: only show TPEs that are not assigned to any agent AND not already assigned to this agent
// const allTpes = [...disponibleTpes, ...valideTpes];
// const available = allTpes.filter(
// (t) =>
// !t.agentConnecteId &&
// (t.statut === 'ACTIF') &&
// !agentTpeIds.has(t.id)
// );
// // Remove duplicates
// const uniqueTpes = Array.from(new Map(available.map((t) => [t.id, t])).values());
// this.availableTpes.set(uniqueTpes);
// this.tpesLoading.set(false);
// },
// error: () => {
// this.availableTpes.set([]);
// this.tpesLoading.set(false);
// },
// });
// }
// testAssigne(tpeIds: string[]){
// console.log(tpeIds);
// }
confirmAssignTpe() {
const agent = this.assigningAgent();
const tpeId = this.selectedTpeId();
if (!agent || !tpeId) {
if (!agent || tpeId.length === 0) {
alert('Veuillez sélectionner un TPE');
return;
}
// Assign TPE to agent
this.tpeSvc.assigner(tpeId, agent.id).subscribe({
next: (tpe) => {
if (tpe) {
// Fermer le modal et recharger complètement la page
this.assignTpeModalOpen.set(false);
this.assigningAgent.set(null);
this.selectedTpeId.set('');
// Rechargement complet pour s'assurer que la liste des agents / TPE est à jour
window.location.reload();
forkJoin(this.selectedTpeId().map(id=> this.api.assigner(id, agent.id))).subscribe(
{
next:()=>{
this.assignTpeModalOpen.set(false);
this.assigningAgent.set(undefined);
this.selectedTpeId.set([]);
toast.success(`Tpe affecté à l'agent avec succès1`)
},
error: (err)=>{
console.error(err);
}
},
error: () => {
alert("Erreur lors de l'assignation du TPE");
},
});
}
)
// // Assign TPE to agent
// this.tpeSvc.assigner(tpeId, agent.id).subscribe({
// next: (tpe) => {
// if (tpe) {
// // Fermer le modal et recharger complètement la page
// this.selectedTpeId.set('');
// // Rechargement complet pour s'assurer que la liste des agents / TPE est à jour
// window.location.reload();
// }
// },
// error: () => {
// alert("Erreur lors de l'assignation du TPE");
// },
// });
}
closeAssignTpeModal() {
this.assignTpeModalOpen.set(false);
this.assigningAgent.set(null);
this.selectedTpeId.set('');
this.assigningAgent.set(undefined);
this.selectedTpeId.set([]);
}
}