agent creation
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user