99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import { ProfileRepository } from '@app/domain/profiles/profile.repository';
|
|
import { from, Observable } from 'rxjs';
|
|
import { Profile, ProfilePaginated } from '@app/domain/profiles/profile.model';
|
|
import { Injectable } from '@angular/core';
|
|
import PocketBase from 'pocketbase';
|
|
import { environment } from '@env/environment';
|
|
import { ProfileDTO } from '@app/domain/profiles/dto/create-profile.dto';
|
|
import { SearchFilters } from '@app/domain/search/search-filters';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class PbProfileRepository implements ProfileRepository {
|
|
private pb = new PocketBase(environment.baseUrl);
|
|
|
|
private defaultOptions = {
|
|
expand: 'utilisateur,secteur',
|
|
};
|
|
|
|
list(params?: SearchFilters): Observable<ProfilePaginated> {
|
|
const requestOptions = {
|
|
...this.defaultOptions,
|
|
sort: this.onSortSetting(params),
|
|
filter: this.onFilterSetting(params).join(' && '),
|
|
};
|
|
|
|
return from(
|
|
this.pb
|
|
.collection('profiles')
|
|
.getList<ProfilePaginated>(params?.page, params?.perPage, requestOptions)
|
|
);
|
|
}
|
|
|
|
getById(userId: string): Observable<Profile> {
|
|
return from(
|
|
this.pb.collection('profiles').getOne<Profile>(`${userId}`, { expand: 'utilisateur' })
|
|
);
|
|
}
|
|
|
|
create(profile: ProfileDTO): Observable<Profile> {
|
|
return from(this.pb.collection('profiles').create<Profile>(profile));
|
|
}
|
|
|
|
update(id: string, data: Partial<Profile>): Observable<Profile> {
|
|
return from(this.pb.collection('profiles').update<Profile>(id, data));
|
|
}
|
|
|
|
private onFilterSetting(params?: SearchFilters): string[] {
|
|
const filters: string[] = [
|
|
'utilisateur.verified = true',
|
|
"utilisateur.name != ''",
|
|
"profession != 'Profession non renseignée'",
|
|
"secteur != ''",
|
|
];
|
|
|
|
if (params) {
|
|
if (params.secteur) {
|
|
filters.push(`secteur.nom ~ '${params.secteur}'`);
|
|
}
|
|
|
|
if (params.profession) {
|
|
filters.push(`profession ~ '${params.profession}'`);
|
|
}
|
|
if (params.search) {
|
|
filters.push(`utilisateur.name ~ '${params.search}'`);
|
|
}
|
|
|
|
if (params.verified) {
|
|
filters.push('estVerifier = true');
|
|
}
|
|
}
|
|
|
|
return filters;
|
|
}
|
|
|
|
private onSortSetting(params?: SearchFilters): string {
|
|
let sortSetting = '-created';
|
|
|
|
if (params?.sort) {
|
|
switch (params.sort) {
|
|
case 'recent':
|
|
sortSetting = '-created'; // Du plus récent au plus vieux
|
|
break;
|
|
case 'name-asc':
|
|
sortSetting = '+utilisateur.name'; // Alphabétique A-Z
|
|
break;
|
|
case 'name-desc':
|
|
sortSetting = '-utilisateur.name'; // Alphabétique Z-A
|
|
break;
|
|
case 'verified':
|
|
sortSetting = '-estVerifier,-created';
|
|
break;
|
|
default:
|
|
sortSetting = '-created';
|
|
}
|
|
}
|
|
|
|
return sortSetting;
|
|
}
|
|
}
|