agent and tpe save done

This commit is contained in:
OnlyPapy98
2026-01-06 14:07:09 +01:00
parent 95095016d2
commit 0ae7fa316e
33 changed files with 1910 additions and 525 deletions

View File

@@ -48,7 +48,7 @@ export interface Agent {
autreTelephone?: string;
// TPE assignés (actifs seulement)
tpes?: TpeDevice[];
tpes?: TpeDevice[] | TpeDevice;
createdAt?: string;
updatedAt?: string;

View File

@@ -0,0 +1,12 @@
export type PointVenteStatut = 'ACTIF' | 'INACTIF';
export interface PointVente {
id: string;
code: string;
nom: string;
adresse: string;
ville: string;
latitude: number;
longitude: number;
statut: PointVenteStatut;
}

View File

@@ -1,27 +1,23 @@
import { Agent } from './agent';
export type TpeStatus =
| 'VALIDE'
| 'INVALIDE'
| 'EN_PANNE'
| 'BLOQUE'
| 'DISPONIBLE'
| 'AFFECTE'
| 'EN_MAINTENANCE'
| 'HORS_SERVICE'
| 'VOLE';
| 'ACTIF'
| 'HORS_SERVICE';
export type TpeType = 'POS' | 'OTHER';
export interface TpeDevice {
id: string;
imei: string;
serial: string;
type: TpeType;
marque: string;
modele: string;
numeroSerie: string;
pointDeVenteId: string;
statut: TpeStatus;
agent?: Agent;
assigne: boolean;
createdAt?: string;
updatedAt?: string;
versionLogicielle: string;
typeTerminal: string;
plateforme: string;
modeleAppareil: string;
systemeExploitation: string;
versionOs: string;
adresseIp: string;
adresseMac: string;
agentConnecteId: string;
derniereConnexionAgent: string;
derniereDeconnexionAgent: string;
journalSession: string;
}

View File

@@ -66,7 +66,7 @@ interface AgentApiResponse {
statutMarital?: string;
epoux?: string;
autreTelephone?: string;
tpes?: TpeApiResponse[];
tpes?: TpeDevice;
createdAt?: string;
updatedAt?: string;
createdBy?: string;
@@ -88,44 +88,37 @@ export class AgentService {
}
// Transform API TPE response to TpeDevice
private transformTpe(apiTpe: TpeApiResponse): TpeDevice {
const transformStatut = (apiStatut: string): TpeStatus => {
const upperStatut = apiStatut.toUpperCase() as TpeStatus;
const validStatuses: TpeStatus[] = [
'VALIDE',
'INVALIDE',
'EN_PANNE',
'BLOQUE',
'DISPONIBLE',
'AFFECTE',
'EN_MAINTENANCE',
'HORS_SERVICE',
'VOLE',
];
return validStatuses.includes(upperStatut) ? upperStatut : 'INVALIDE';
};
// private transformTpe(apiTpe: TpeApiResponse): TpeDevice {
// const transformStatut = (apiStatut: string): TpeStatus => {
// const upperStatut = apiStatut.toUpperCase() as TpeStatus;
// const validStatuses: TpeStatus[] = [
// 'ACTIF',
// 'HORS_SERVICE'
// ];
// return validStatuses.includes(upperStatut) ? upperStatut : 'INVALIDE';
// };
// Transform agent if it's an object (not just a string reference)
let transformedAgent: Agent | undefined = undefined;
if (apiTpe.agent && typeof apiTpe.agent === 'object' && apiTpe.agent.id) {
// If agent is a full object, transform it
transformedAgent = this.transformAgent(apiTpe.agent as any);
}
// // Transform agent if it's an object (not just a string reference)
// let transformedAgent: Agent | undefined = undefined;
// if (apiTpe.agent && typeof apiTpe.agent === 'object' && apiTpe.agent.id) {
// // If agent is a full object, transform it
// transformedAgent = this.transformAgent(apiTpe.agent as any);
// }
return {
id: String(apiTpe.id),
imei: apiTpe.imei,
serial: apiTpe.serial,
type: apiTpe.type as TpeType,
marque: apiTpe.marque,
modele: apiTpe.modele,
statut: transformStatut(apiTpe.statut),
agent: transformedAgent,
assigne: apiTpe.assigne,
createdAt: apiTpe.createdAt,
updatedAt: apiTpe.updatedAt,
};
}
// return {
// id: String(apiTpe.id),
// imei: apiTpe.imei,
// serial: apiTpe.serial,
// type: apiTpe.type as TpeType,
// marque: apiTpe.marque,
// modele: apiTpe.modele,
// statut: transformStatut(apiTpe.statut),
// agent: transformedAgent,
// assigne: apiTpe.assigne,
// createdAt: apiTpe.createdAt,
// updatedAt: apiTpe.updatedAt,
// };
// }
// Transform API response to Agent
private transformAgent(apiAgent: AgentApiResponse): Agent {
@@ -166,10 +159,7 @@ export class AgentService {
statutMarital: apiAgent.statutMarital,
epoux: apiAgent.epoux,
autreTelephone: apiAgent.autreTelephone,
tpes: apiAgent.tpes?.map((tpe) => {
const transformed = this.transformTpe(tpe);
return transformed;
}),
tpes: apiAgent.tpes ,
createdAt: apiAgent.createdAt,
updatedAt: apiAgent.updatedAt,
createdBy: apiAgent.createdBy,

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { PointsVente } from './points-vente';
describe('PointsVente', () => {
let service: PointsVente;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(PointsVente);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,92 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ListParams, PagedResult } from '@shared/paging/paging';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { PointVente, PointVenteStatut } from 'src/app/core/interfaces/points-ventes';
import { environment } from 'src/environments/environment.development';
import { ServicesUtils } from './services-utils';
const API_BASE = '/api/points-de-vente';
@Injectable({
providedIn: 'root',
})
export class PointsVenteService {
private apiUrl = environment.apiBaseUrl + API_BASE;
constructor(private http: HttpClient, private servicesUtils: ServicesUtils) {}
private getNgrokHeaders(): Record<string, string> {
const isNgrok =
environment.apiBaseUrl.includes('ngrok-free.app') ||
environment.apiBaseUrl.includes('ngrok.io') ||
environment.apiBaseUrl.includes('ngrok');
return isNgrok ? { 'ngrok-skip-browser-warning': 'true' } : {};
}
list(params: ListParams): Observable<PagedResult<PointVente>> {
return this.http
.get<PagedResult<PointVente>>(this.apiUrl, {
params: this.servicesUtils.getParamsFromModel(params),
headers: this.getNgrokHeaders(),
})
.pipe(
catchError((err) => {
console.error('Error fetching points de vente:', err);
return of({
content: [],
pageable: { pageNumber: 0, pageSize: params.size || 10, total: 0 },
totalPages: 0,
totalElements: 0,
});
})
);
}
getById(id: string): Observable<PointVente | undefined> {
return this.http
.get<PointVente>(`${this.apiUrl}/${id}`, { headers: this.getNgrokHeaders() })
.pipe(
catchError((err) => {
console.error(`Error fetching point de vente ${id}:`, err);
return of(undefined);
})
);
}
create(pointVente: Omit<PointVente, 'id'>): Observable<PointVente> {
return this.http
.post<PointVente>(this.apiUrl, pointVente, { headers: this.getNgrokHeaders() })
.pipe(
catchError((err) => {
console.log(err)
console.error('Error creating point de vente:', err);
throw err;
})
);
}
update(id: string, payload: Partial<PointVente>): Observable<PointVente | undefined> {
return this.http
.put<PointVente>(`${this.apiUrl}/${id}`, payload, { headers: this.getNgrokHeaders() })
.pipe(
catchError((err) => {
console.error(`Error updating point de vente ${id}:`, err);
return of(undefined);
})
);
}
delete(id: string): Observable<boolean> {
return this.http
.delete<void>(`${this.apiUrl}/${id}`, { headers: this.getNgrokHeaders() })
.pipe(
map(() => true),
catchError((err) => {
console.error(`Error deleting point de vente ${id}:`, err);
return of(false);
})
);
}
}

View File

@@ -104,17 +104,10 @@ export class TpeService {
private transformStatut(apiStatut: string): TpeStatus {
const upperStatut = apiStatut.toUpperCase() as TpeStatus;
const validStatuses: TpeStatus[] = [
'VALIDE',
'INVALIDE',
'EN_PANNE',
'BLOQUE',
'DISPONIBLE',
'AFFECTE',
'EN_MAINTENANCE',
'HORS_SERVICE',
'VOLE',
'ACTIF',
'HORS_SERVICE'
];
return validStatuses.includes(upperStatut) ? upperStatut : 'INVALIDE';
return validStatuses.includes(upperStatut) ? upperStatut : 'HORS_SERVICE';
}
// Transform interface statut to API statut (both use uppercase now, so direct return)
@@ -168,55 +161,55 @@ export class TpeService {
}
// Transform API response to TpeDevice
private transformTpe(apiTpe: TpeApiResponse): TpeDevice {
// Map API-specific names to our generic interface where possible
const serial = (apiTpe as any).numeroSerie || (apiTpe as any).serial || '';
const imei = (apiTpe as any).imei || serial || '';
const typeRaw = String((apiTpe as any).typeTerminal || '').toUpperCase();
const type = typeRaw.includes('POS') ? ('POS' as TpeType) : ('OTHER' as TpeType);
const marque = (apiTpe as any).plateforme || (apiTpe as any).marque || '';
const modele = (apiTpe as any).modeleAppareil || (apiTpe as any).modele || '';
const statut = this.transformStatut(String(apiTpe.statut || 'INVALIDE'));
// Agent mapping: sometimes API returns an agent object or only an id
let agent: Agent | undefined = undefined;
if ((apiTpe as any).agent && typeof (apiTpe as any).agent === 'object' && (apiTpe as any).agent.id) {
agent = this.transformAgent((apiTpe as any).agent as AgentApiResponse);
}
const assigne = Boolean((apiTpe as any).agentConnecteId || (apiTpe as any).assigne);
// private transformTpe(apiTpe: TpeApiResponse): TpeDevice {
// // Map API-specific names to our generic interface where possible
// const serial = (apiTpe as any).numeroSerie || (apiTpe as any).serial || '';
// const imei = (apiTpe as any).imei || serial || '';
// const typeRaw = String((apiTpe as any).typeTerminal || '').toUpperCase();
// const type = typeRaw.includes('POS') ? ('POS' as TpeType) : ('OTHER' as TpeType);
// const marque = (apiTpe as any).plateforme || (apiTpe as any).marque || '';
// const modele = (apiTpe as any).modeleAppareil || (apiTpe as any).modele || '';
// const statut = this.transformStatut(String(apiTpe.statut || 'INVALIDE'));
// // Agent mapping: sometimes API returns an agent object or only an id
// let agent: Agent | undefined = undefined;
// if ((apiTpe as any).agent && typeof (apiTpe as any).agent === 'object' && (apiTpe as any).agent.id) {
// agent = this.transformAgent((apiTpe as any).agent as AgentApiResponse);
// }
// const assigne = Boolean((apiTpe as any).agentConnecteId || (apiTpe as any).assigne);
return {
id: String(apiTpe.id),
imei: String(imei),
serial: String(serial),
type,
marque: String(marque),
modele: String(modele),
statut,
agent,
assigne,
createdAt: (apiTpe as any).createdAt,
updatedAt: (apiTpe as any).updatedAt,
};
}
// return {
// id: String(apiTpe.id),
// imei: String(imei),
// serial: String(serial),
// type,
// marque: String(marque),
// modele: String(modele),
// statut,
// agent,
// assigne,
// createdAt: (apiTpe as any).createdAt,
// updatedAt: (apiTpe as any).updatedAt,
// };
// }
// Transform TpeDevice to API payload (best-effort)
private transformToApiPayload(tpe: Partial<TpeDevice>): any {
const payload: any = {};
if (tpe.imei !== undefined) payload.numeroSerie = tpe.imei;
if (tpe.serial !== undefined) payload.numeroSerie = tpe.serial;
if (tpe.type !== undefined) payload.typeTerminal = tpe.type;
if (tpe.marque !== undefined) payload.plateforme = tpe.marque;
if (tpe.modele !== undefined) payload.modeleAppareil = tpe.modele;
if (tpe.statut !== undefined) payload.statut = this.transformStatutToApi(tpe.statut);
if (tpe.assigne !== undefined) payload.assigne = tpe.assigne;
return payload;
}
// private transformToApiPayload(tpe: Partial<TpeDevice>): any {
// const payload: any = {};
// if (tpe.imei !== undefined) payload.numeroSerie = tpe.imei;
// if (tpe.serial !== undefined) payload.numeroSerie = tpe.serial;
// if (tpe.type !== undefined) payload.typeTerminal = tpe.type;
// if (tpe.marque !== undefined) payload.plateforme = tpe.marque;
// if (tpe.modele !== undefined) payload.modeleAppareil = tpe.modele;
// if (tpe.statut !== undefined) payload.statut = this.transformStatutToApi(tpe.statut);
// if (tpe.assigne !== undefined) payload.assigne = tpe.assigne;
// return payload;
// }
// GET /api/v1/tpes/{id} - Get by ID
getById(id: string): Observable<TpeDevice | undefined> {
if (USE_SERVER) {
return this.http.get<TpeApiResponse>(`${this.apiUrl}/${id}`, { headers: this.getNgrokHeaders() }).pipe(
map((api) => this.transformTpe(api)),
return this.http.get<TpeDevice>(`${this.apiUrl}/${id}`, { headers: this.getNgrokHeaders() }).pipe(
map((api) => api),
catchError((err) => {
console.error(`Error fetching TPE ${id}:`, err);
return of(undefined);
@@ -239,13 +232,13 @@ getById(id: string): Observable<TpeDevice | undefined> {
}
return this.http
.get<PagedResult<TpeApiResponse>>(this.apiUrl, {
.get<PagedResult<TpeDevice>>(this.apiUrl, {
params: httpParams,
headers: this.getNgrokHeaders(),
})
.pipe(
map((list) => {
const content = (list.content || []).map((api) => this.transformTpe(api));
const content = (list.content || []).map((api) => api);
return { ...list, content } as PagedResult<TpeDevice>;
}),
catchError((err) => {
@@ -257,11 +250,10 @@ getById(id: string): Observable<TpeDevice | undefined> {
// POST /api/v1/tpes - Create
create(payload: Partial<TpeDevice>): Observable<TpeDevice> {
const apiPayload = this.transformToApiPayload(payload);
return this.http
.post<TpeApiResponse>(this.apiUrl, apiPayload, { headers: this.getNgrokHeaders() })
.post<TpeDevice>(this.apiUrl, payload, { headers: this.getNgrokHeaders() })
.pipe(
map((apiTpe) => this.transformTpe(apiTpe)),
map((apiTpe) => apiTpe),
catchError((err) => {
console.error('Error creating TPE:', err);
throw err;
@@ -271,13 +263,12 @@ getById(id: string): Observable<TpeDevice | undefined> {
// PUT /api/v1/tpes/{id} - Update
update(id: string, payload: Partial<TpeDevice>): Observable<TpeDevice | undefined> {
const apiPayload = this.transformToApiPayload(payload);
return this.http
.put<TpeApiResponse>(`${this.apiUrl}/${id}`, apiPayload, {
.put<TpeDevice>(`${this.apiUrl}/${id}`, payload, {
headers: this.getNgrokHeaders(),
})
.pipe(
map((apiTpe) => this.transformTpe(apiTpe)),
map((apiTpe) => apiTpe),
catchError((err) => {
console.error(`Error updating TPE ${id}:`, err);
return of(undefined);
@@ -305,13 +296,13 @@ update(id: string, payload: Partial<TpeDevice>): Observable<TpeDevice | undefine
updateStatut(id: string, statut: TpeStatus): Observable<TpeDevice | undefined> {
if (USE_SERVER) {
return this.http
.patch<TpeApiResponse>(
.patch<TpeDevice>(
`${this.apiUrl}/${id}/statut`,
{ statut: this.transformStatutToApi(statut) },
{ headers: this.getNgrokHeaders() }
)
.pipe(
map((apiTpe) => this.transformTpe(apiTpe)),
map((apiTpe) => apiTpe),
catchError((err) => {
console.error(`Error updating TPE statut ${id}:`, err);
return of(undefined);
@@ -323,77 +314,71 @@ update(id: string, payload: Partial<TpeDevice>): Observable<TpeDevice | undefine
// PATCH /api/v1/tpes/{id}/liberer - Liberate TPE (updates whole TPE, sets assigne to false and statut to DISPONIBLE)
liberer(id: string): Observable<TpeDevice | undefined> {
if (USE_SERVER) {
// First get the current TPE data
return this.getById(id).pipe(
switchMap((tpe) => {
if (!tpe) {
return of(undefined);
}
// Update the whole TPE with assigne set to false and statut to DISPONIBLE
const updatedTpe = { ...tpe, assigne: false, statut: 'DISPONIBLE' as TpeStatus };
const apiPayload = this.transformToApiPayload(updatedTpe);
return this.http
.patch<TpeApiResponse>(`${this.apiUrl}/liberer/${id}`, apiPayload, {
headers: this.getNgrokHeaders(),
})
.pipe(
map((apiTpe) => this.transformTpe(apiTpe)),
catchError((err) => {
console.error(`Error liberating TPE ${id}:`, err);
return of(undefined);
})
);
}),
catchError((err) => {
console.error(`Error fetching TPE ${id} for liberation:`, err);
return this.getById(id).pipe(
switchMap((tpe) => {
if (!tpe) {
return of(undefined);
})
);
}
return of(undefined);
}
// Update the whole TPE with assigne set to false and statut to DISPONIBLE
const updatedTpe = {
...tpe,
statut: 'ACTIF'
};
return this.http
.patch<TpeDevice>(`${this.apiUrl}/liberer/${id}`, updatedTpe, {
headers: this.getNgrokHeaders(),
})
.pipe(
map((apiTpe) => apiTpe),
catchError((err) => {
console.error(`Error liberating TPE ${id}:`, err);
return of(undefined);
})
);
}),
catchError((err) => {
console.error(`Error fetching TPE ${id} for liberation:`, err);
return of(undefined);
})
);
}
// PATCH /api/v1/tpes/assigner - Assign TPE
// Payload: { tpeId: number, agentId: number }
assigner(id: string, agentId: string): Observable<TpeDevice | undefined> {
if (USE_SERVER) {
const payload = {
tpeId: Number(id),
agentId: Number(agentId),
};
return this.http
.patch<TpeApiResponse>(`${this.apiUrl}/assigner`, payload, {
headers: this.getNgrokHeaders(),
const payload = {
tpeId: Number(id),
agentId: Number(agentId),
};
return this.http
.patch<TpeDevice>(`${this.apiUrl}/assigner`, payload, {
headers: this.getNgrokHeaders(),
})
.pipe(
map((apiTpe) => apiTpe),
catchError((err) => {
console.error(`Error assigning TPE ${id}:`, err);
return of(undefined);
})
.pipe(
map((apiTpe) => this.transformTpe(apiTpe)),
catchError((err) => {
console.error(`Error assigning TPE ${id}:`, err);
return of(undefined);
})
);
}
return of(undefined);
);
}
// GET /api/v1/tpes/statut/{statut} - List by statut
getByStatut(statut: TpeStatus): Observable<TpeDevice[]> {
if (USE_SERVER) {
const apiStatut = this.transformStatutToApi(statut);
return this.http
.get<TpeApiResponse[]>(`${this.apiUrl}/statut/${apiStatut}`, {
headers: this.getNgrokHeaders(),
const apiStatut = this.transformStatutToApi(statut);
return this.http
.get<TpeDevice[]>(`${this.apiUrl}/statut/${apiStatut}`, {
headers: this.getNgrokHeaders(),
})
.pipe(
map((list) => list.map((apiTpe) => apiTpe)),
catchError((err) => {
console.error(`Error fetching TPEs by statut ${statut}:`, err);
return of([]);
})
.pipe(
map((list) => list.map((apiTpe) => this.transformTpe(apiTpe))),
catchError((err) => {
console.error(`Error fetching TPEs by statut ${statut}:`, err);
return of([]);
})
);
}
return of([]);
);
}
// GET /api/v1/tpes/stats/count-by-statut - Get count by statut
@@ -432,36 +417,30 @@ update(id: string, payload: Partial<TpeDevice>): Observable<TpeDevice | undefine
// GET /api/v1/tpes/search - Search
search(query: string): Observable<TpeDevice[]> {
if (USE_SERVER) {
return this.http
.get<TpeApiResponse[]>(`${this.apiUrl}/search`, {
params: { q: query.trim() },
headers: this.getNgrokHeaders(),
return this.http
.get<TpeDevice[]>(`${this.apiUrl}/search`, {
params: { q: query.trim() },
headers: this.getNgrokHeaders(),
})
.pipe(
map((list) => list.map((apiTpe) => apiTpe)),
catchError((err) => {
console.error(`Error searching TPEs with query ${query}:`, err);
return of([]);
})
.pipe(
map((list) => list.map((apiTpe) => this.transformTpe(apiTpe))),
catchError((err) => {
console.error(`Error searching TPEs with query ${query}:`, err);
return of([]);
})
);
}
return of([]);
);
}
// GET /api/v1/tpes/disponibles - List available TPEs
getDisponibles(): Observable<TpeDevice[]> {
if (USE_SERVER) {
return this.http
.get<TpeApiResponse[]>(`${this.apiUrl}/disponibles`, { headers: this.getNgrokHeaders() })
.pipe(
map((list) => list.map((apiTpe) => this.transformTpe(apiTpe))),
catchError((err) => {
console.error('Error fetching available TPEs:', err);
return of([]);
})
);
}
return of([]);
return this.http
.get<TpeDevice[]>(`${this.apiUrl}/disponibles`, { headers: this.getNgrokHeaders() })
.pipe(
map((list) => list.map((apiTpe) => apiTpe)),
catchError((err) => {
console.error('Error fetching available TPEs:', err);
return of([]);
})
);
}
}