agent creation

This commit is contained in:
OnlyPapy98
2026-01-05 14:14:41 +01:00
parent afa5fab55d
commit 95095016d2
11 changed files with 126 additions and 324 deletions

View File

@@ -5,7 +5,7 @@ export type AgentStatus = 'ACTIF' | 'INACTIF' | 'SUSPENDU';
export interface Agent {
id: string;
code: string;
profile: string; // ex. AGENT, SUPERVISEUR, CAISSIER
profil: string; // ex. AGENT, SUPERVISEUR, CAISSIER
principalCode?: string; // Agent principal
caisseProfile?: string;
statut: AgentStatus;

View File

@@ -7,6 +7,7 @@ import { TpeDevice, TpeStatus, TpeType } from '../interfaces/tpe';
import { environment } from 'src/environments/environment.development';
import { normalizePage } from '@shared/paging/normalize-page';
import { ListParams, PagedResult } from '@shared/paging/paging';
import { ServicesUtils } from './services-utils';
const USE_SERVER = true;
const API_BASE = '/api/agents';
@@ -31,7 +32,7 @@ interface TpeApiResponse {
interface AgentApiResponse {
id: number;
code: string;
profile: string;
profil: string;
principalCode?: string;
caisseProfile?: string;
statut: string;
@@ -75,7 +76,7 @@ interface AgentApiResponse {
export class AgentService {
private apiUrl = environment.apiBaseUrl + API_BASE;
constructor(private http: HttpClient) {}
constructor(private http: HttpClient, private serviceUtils:ServicesUtils) {}
// Helper method to get ngrok bypass headers
private getNgrokHeaders(): Record<string, string> {
@@ -131,7 +132,7 @@ export class AgentService {
return {
id: String(apiAgent.id),
code: apiAgent.code,
profile: apiAgent.profile,
profil: apiAgent.profil,
principalCode: apiAgent.principalCode,
caisseProfile: apiAgent.caisseProfile,
statut: apiAgent.statut as AgentStatus,
@@ -193,7 +194,7 @@ export class AgentService {
private transformToApiPayload(agent: Partial<Agent>): any {
const payload: any = {};
if (agent.code !== undefined) payload.code = agent.code;
if (agent.profile !== undefined) payload.profile = agent.profile;
if (agent.profil !== undefined) payload.profil = agent.profil;
if (agent.principalCode !== undefined) payload.principalCode = agent.principalCode;
if (agent.caisseProfile !== undefined) payload.caisseProfile = agent.caisseProfile;
if (agent.statut !== undefined) payload.statut = agent.statut;
@@ -233,22 +234,6 @@ export class AgentService {
if (agent.epoux !== undefined) payload.epoux = agent.epoux;
if (agent.autreTelephone !== undefined) payload.autreTelephone = agent.autreTelephone;
if (agent.createdBy !== undefined) payload.createdBy = agent.createdBy;
// Include tpes if provided - transform to API format
if (agent.tpes !== undefined) {
payload.tpes = agent.tpes.map((tpe) => ({
id: tpe.id ? Number(tpe.id) : undefined,
imei: tpe.imei,
serial: tpe.serial,
type: tpe.type,
marque: tpe.marque,
modele: tpe.modele,
statut: tpe.statut,
agent: undefined, // Will be set by backend
assigne: tpe.assigne,
createdAt: tpe.createdAt,
updatedAt: tpe.updatedAt,
}));
}
return payload;
}
@@ -269,23 +254,23 @@ export class AgentService {
}
// GET /api/v1/agents - List all
list(params?: ListParams): Observable<PagedResult<Agent>> {
let httpParams = new HttpParams();
if (params) {
if (params.page) httpParams = httpParams.set('page', params.page.toString());
if (params.size) httpParams = httpParams.set('perPage', params.size.toString());
if (params.search) httpParams = httpParams.set('search', params.search);
if (params.sortKey) httpParams = httpParams.set('sortKey', params.sortKey);
if (params.sortDir) httpParams = httpParams.set('sortDir', params.sortDir);
}
list(params: ListParams): Observable<PagedResult<Agent>> {
// let httpParams = new HttpParams();
// if (params) {
// if (params.page) httpParams = httpParams.set('page', params.page.toString());
// if (params.size) httpParams = httpParams.set('perPage', params.size.toString());
// if (params.search) httpParams = httpParams.set('search', params.search);
// if (params.sortKey) httpParams = httpParams.set('sortKey', params.sortKey);
// if (params.sortDir) httpParams = httpParams.set('sortDir', params.sortDir);
// }
return this.http
.get<PagedResult<AgentApiResponse>>(this.apiUrl, {
params: httpParams,
params: this.serviceUtils.getParamsFromModel(params),
headers: this.getNgrokHeaders(),
})
.pipe(
map((res) => {
console.log(res);
const agents = res.content.map((apiAgent) => {
const transformed = this.transformAgent(apiAgent);
return transformed;
@@ -305,21 +290,18 @@ export class AgentService {
);
}
// POST /api/v1/agents - Create
// POST /api/agents - Create
create(payload: Omit<Agent, 'id' | 'createdAt' | 'updatedAt'>): Observable<Agent> {
if (USE_SERVER) {
const apiPayload = this.transformToApiPayload(payload);
return this.http
.post<AgentApiResponse>(this.apiUrl, apiPayload, { headers: this.getNgrokHeaders() })
.pipe(
map((apiAgent) => this.transformAgent(apiAgent)),
catchError((err) => {
console.error('Error creating agent:', err);
throw err;
})
);
}
throw new Error('Server mode is required');
const apiPayload = this.transformToApiPayload(payload);
return this.http
.post<AgentApiResponse>(this.apiUrl, apiPayload, { headers: this.getNgrokHeaders() })
.pipe(
map((apiAgent) => this.transformAgent(apiAgent)),
catchError((err) => {
console.error('Error creating agent:', err);
throw err;
})
);
}
// PUT /api/v1/agents/{id} - Update

View File

@@ -127,7 +127,7 @@ export class TpeService {
return {
id: String(apiAgent.id),
code: String((apiAgent as any).code || ''),
profile: String((apiAgent as any).profile || ''),
profil: String((apiAgent as any).profil || ''),
principalCode: (apiAgent as any).principalCode ? String((apiAgent as any).principalCode) : undefined,
caisseProfile: (apiAgent as any).caisseProfile ? String((apiAgent as any).caisseProfile) : undefined,
statut: apiAgent.statut as AgentStatus,