Skip to content

Commit

Permalink
refactor: inject instead of constructor
Browse files Browse the repository at this point in the history
Co-Authored-by: Bertrand Zuchuat <[email protected]>
  • Loading branch information
Garfield-fr committed Oct 29, 2024
1 parent 8500eda commit 09b142a
Show file tree
Hide file tree
Showing 291 changed files with 1,646 additions and 3,281 deletions.
30 changes: 11 additions & 19 deletions projects/admin/src/app/acquisition/api/acq-account-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RERO ILS UI
* Copyright (C) 2021 RERO
* Copyright (C) 2021-2024 RERO
* Copyright (C) 2021 UCLouvain
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -17,7 +17,7 @@
*/

import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { Record, RecordService } from '@rero/ng-core';
import { Error } from '@rero/ng-core/lib/error/error';
import { Observable } from 'rxjs';
Expand All @@ -29,6 +29,9 @@ import { IAcqAccount } from '../classes/account';
})
export class AcqAccountApiService {

private httpClient: HttpClient = inject(HttpClient);
private recordService: RecordService = inject(RecordService);

// SERVICES ATTRIBUTES ======================================================
/** The resource name of acquisition account */
resourceName = 'acq_accounts';
Expand Down Expand Up @@ -57,17 +60,6 @@ export class AcqAccountApiService {
remaining_balance: this.allocatedAmountDefaultData
};

// SERVICE CONSTRUCTORS =====================================================
/**
* Constructor
* @param _http: HttpClient
* @param _recordService - RecordService
*/
constructor(
private _http: HttpClient,
private _recordService: RecordService
) {}

// SERVICES FUNCTIONS =======================================================

/**
Expand All @@ -76,10 +68,10 @@ export class AcqAccountApiService {
* @returns the corresponding account
*/
getAccount(accountPid: string): Observable<IAcqAccount> {
return this._recordService
return this.recordService
.getRecords(this.resourceName, `pid:${accountPid}`, 1, 1)
.pipe(
map((result: Record) => this._recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((result: Record) => this.recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((hits: any[]) => hits.map((hit: any) => ({...this.accountDefaultData, ...hit.metadata}) )),
map((hits: IAcqAccount[]) => hits.find(Boolean)) // Get first element of array if exists
);
Expand All @@ -103,10 +95,10 @@ export class AcqAccountApiService {
}
const query = defaultQueryParams.join(' AND ');
options = { ...{sort: 'name'}, ...options }; // add some default params
return this._recordService
return this.recordService
.getRecords(this.resourceName, query, 1, RecordService.MAX_REST_RESULTS_SIZE, undefined, undefined, undefined, options.sort)
.pipe(
map((result: Record) => this._recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((result: Record) => this.recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((hits: any[]) => hits.map(hit => ({...this.accountDefaultData, ...hit.metadata}) ))
);
}
Expand All @@ -117,7 +109,7 @@ export class AcqAccountApiService {
* @return The observable on delete REST call.
*/
delete(pid: string): Observable<void | Error> {
return this._recordService.delete(this.resourceName, pid);
return this.recordService.delete(this.resourceName, pid);
}

/**
Expand All @@ -133,6 +125,6 @@ export class AcqAccountApiService {
.set('source', sourcePid)
.set('target', targetPid)
.set('amount', amount.toString());
return this._http.get<any>(apiUrl, { params });
return this.httpClient.get<any>(apiUrl, { params });
}
}
30 changes: 11 additions & 19 deletions projects/admin/src/app/acquisition/api/acq-budget-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RERO ILS UI
* Copyright (C) 2021 RERO
* Copyright (C) 2021-2024 RERO
* Copyright (C) 2021 UCLouvain
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -15,43 +15,35 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { Record, RecordService } from '@rero/ng-core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { AcqAccountApiService } from './acq-account-api.service';
import { IAcqAccount } from '../classes/account';
import { AcqBudget } from '../classes/budget';
import { AcqAccountApiService } from './acq-account-api.service';

@Injectable({
providedIn: 'root'
})
export class AcqBudgetApiService {

private recordService: RecordService = inject(RecordService);
private acqAccountApiService: AcqAccountApiService = inject(AcqAccountApiService);

/** The resource name of acquisition budget */
resourceName = 'budgets';

/**
* Constructor
* @param _recordService - RecordService
* @param _acqAccountApiService - AcqAccountApiService
*/
constructor(
private _recordService: RecordService,
private _acqAccountApiService: AcqAccountApiService
) { }


/**
* Get the budget corresponding to filters.
* @param query: query to apply to find records
* @return an observable of budget
*/
getBudgets(query?: string): Observable<AcqBudget[]> {
query = query || '';
return this._recordService.getRecords(this.resourceName, query, 1, RecordService.MAX_REST_RESULTS_SIZE)
return this.recordService.getRecords(this.resourceName, query, 1, RecordService.MAX_REST_RESULTS_SIZE)
.pipe(
map((result: Record) => this._recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((result: Record) => this.recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((hits: any[]) => hits.map((hit: any) => new AcqBudget(hit.metadata)))
);
}
Expand All @@ -62,11 +54,11 @@ export class AcqBudgetApiService {
* @return An observable with the total amount for this budget.
*/
getBudgetTotalAmount(budgetPid: string): Observable<number> {
return this._recordService
return this.recordService
.getRecords('acq_accounts', `budget.pid:${budgetPid} AND depth:0`, 1, RecordService.MAX_REST_RESULTS_SIZE)
.pipe(
map((result: Record) => this._recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((hits: any[]) => hits.map((hit: any) => ({...this._acqAccountApiService.accountDefaultData, ...hit.metadata}) )),
map((result: Record) => this.recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((hits: any[]) => hits.map((hit: any) => ({...this.acqAccountApiService.accountDefaultData, ...hit.metadata}) )),
map((accounts: IAcqAccount[]) => accounts.reduce((total, acc) => total + acc.allocated_amount, 0))
);
}
Expand Down
44 changes: 16 additions & 28 deletions projects/admin/src/app/acquisition/api/acq-order-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* RERO ILS UI
* Copyright (C) 2019-2022 RERO
* Copyright (C) 2019-2024 RERO
* Copyright (C) 2019-2022 UCLouvain
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -16,7 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { IPreview } from '@app/admin/shared/preview-email/IPreviewInterface';
import { ApiService, Record, RecordService, RecordUiService } from '@rero/ng-core';
import { BaseApi } from '@rero/shared';
import { BehaviorSubject, Observable, Subject } from 'rxjs';
Expand All @@ -31,13 +32,17 @@ import {
IAcqOrder,
IAcqOrderLine
} from '../classes/order';
import { IPreview } from '@app/admin/shared/preview-email/IPreviewInterface';

@Injectable({
providedIn: 'root'
})
export class AcqOrderApiService extends BaseApi {

private httpClient: HttpClient = inject(HttpClient);
private recordService: RecordService = inject(RecordService);
private recordUiService: RecordUiService = inject(RecordUiService);
private apiService: ApiService = inject(ApiService);

// SERVICES ATTRIBUTES ======================================================
/** Default values */
public readonly orderDefaultData = {
Expand Down Expand Up @@ -68,31 +73,14 @@ export class AcqOrderApiService extends BaseApi {
/** expose _deletedOrderLineSubject$ in 'readonly' mode */
get deletedOrderLineSubject$(): Observable<IAcqOrderLine> { return this._deletedOrderLineSubject$.asObservable(); }

// CONSTRUCTOR ==============================================================
/**
* Constructor
* @param _http - HttpClient
* @param _recordService - RecordService
* @param _recordUiService - RecordUiService
* @param _apiService - ApiService
*/
constructor(
private _http: HttpClient,
private _recordService: RecordService,
private _recordUiService: RecordUiService,
private _apiService: ApiService
) {
super();
}

// SERVICE PUBLIC FUNCTIONS =================================================
/**
* Load an order from this pid
* @param orderPid: the order pid
* @return: the corresponding AcqOrder
*/
getOrder(orderPid: string): Observable<IAcqOrder> {
return this._recordService.getRecord('acq_orders', orderPid, 0, BaseApi.reroJsonheaders).pipe(
return this.recordService.getRecord('acq_orders', orderPid, 0, BaseApi.reroJsonheaders).pipe(
map(data => ({...this.orderDefaultData, ...data.metadata}) )
);
}
Expand All @@ -103,7 +91,7 @@ export class AcqOrderApiService extends BaseApi {
*/
getOrderPreview(orderPid: string): Observable<IPreview> {
const apiUrl = `/api/acq_order/${orderPid}/acquisition_order/preview`;
return this._http.get<any>(apiUrl);
return this.httpClient.get<any>(apiUrl);
}

/**
Expand All @@ -113,15 +101,15 @@ export class AcqOrderApiService extends BaseApi {
*/
sendOrder(orderPid: string, emails: AcqAddressRecipient[]): Observable<Notification> {
const apiUrl = `/api/acq_order/${orderPid}/send_order`;
return this._http.post<any>(apiUrl, {emails}).pipe(
return this.httpClient.post<any>(apiUrl, {emails}).pipe(
map((data: any) => new Notification(data.data))
);
}

getOrderHistory(orderPid: string) {
// check if orderPid is already present into the previously loaded history items.
// If YES :: not needed to reload the history, just update the `current` attribute of history items
const orderRef = new URL(this._apiService.getRefEndpoint('acq_orders', orderPid));
const orderRef = new URL(this.apiService.getRefEndpoint('acq_orders', orderPid));
const idx = this._acqOrderHistory.findIndex(item => item.$ref === orderRef.toString());
if (idx !== -1) {
if (!this._acqOrderHistory[idx].current) {
Expand All @@ -132,7 +120,7 @@ export class AcqOrderApiService extends BaseApi {
} else {
// If NO :: Load the acquisition order history
const apiUrl = `/api/acq_order/${orderPid}/history`;
this._http
this.httpClient
.get<AcqOrderHistoryVersionResponseInterface[]>(apiUrl)
.subscribe(versions => {
versions.map(version => version.current = version.$ref === orderRef.toString());
Expand All @@ -153,10 +141,10 @@ export class AcqOrderApiService extends BaseApi {
if (extraQuery) {
query += ` ${extraQuery}`;
}
return this._recordService
return this.recordService
.getRecords('acq_order_lines', query, 1, RecordService.MAX_REST_RESULTS_SIZE, undefined, undefined, undefined, 'priority')
.pipe(
map((result: Record) => this._recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((result: Record) => this.recordService.totalHits(result.hits.total) === 0 ? [] : result.hits.hits),
map((hits: any[]) => hits.map(hit => ({...this.orderDefaultData, ...hit.metadata}) ))
);
}
Expand All @@ -168,7 +156,7 @@ export class AcqOrderApiService extends BaseApi {
* @param orderLine: the order line to delete
*/
deleteOrderLine(orderLine: IAcqOrderLine): void {
this._recordUiService
this.recordUiService
.deleteRecord('acq_order_lines', orderLine.pid)
.subscribe((success: boolean) => {
if (success) {
Expand Down
Loading

0 comments on commit 09b142a

Please sign in to comment.