Feature #99 + BugFixes
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, lastValueFrom, Observable } from 'rxjs';
|
||||
import { PaymentMethod } from '@stripe/stripe-js';
|
||||
import { BehaviorSubject, catchError, forkJoin, lastValueFrom, map, Observable, of } from 'rxjs';
|
||||
import urlcat from 'urlcat';
|
||||
import { User } from '../../../../bizmatch-server/src/models/db.model';
|
||||
import { ResponseUsersArray, UserListingCriteria } from '../../../../bizmatch-server/src/models/main.model';
|
||||
import { CombinedUser, KeycloakUser, ResponseUsersArray, StripeSubscription, StripeUser, UserListingCriteria } from '../../../../bizmatch-server/src/models/main.model';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
@@ -46,7 +47,104 @@ export class UserService {
|
||||
getNumberOfBroker(criteria?: UserListingCriteria): Observable<number> {
|
||||
return this.http.post<number>(`${this.apiBaseUrl}/bizmatch/user/findTotal`, criteria);
|
||||
}
|
||||
// async getAllStates(): Promise<any> {
|
||||
// return await lastValueFrom(this.http.get<StatesResult[]>(`${this.apiBaseUrl}/bizmatch/user/states/all`));
|
||||
// }
|
||||
// -------------------------------
|
||||
// ADMIN SERVICES
|
||||
// -------------------------------
|
||||
getKeycloakUsers(): Observable<KeycloakUser[]> {
|
||||
return this.http.get<KeycloakUser[]>(`${this.apiBaseUrl}/bizmatch/auth/user/all`).pipe(
|
||||
catchError(error => {
|
||||
console.error('Fehler beim Laden der Keycloak-Benutzer', error);
|
||||
return of([]);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getAppUsers(): Observable<User[]> {
|
||||
return this.http.get<User[]>(`${this.apiBaseUrl}/bizmatch/user/user/all`).pipe(
|
||||
catchError(error => {
|
||||
console.error('Fehler beim Laden der App-Benutzer', error);
|
||||
return of([]);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getAllStripeSubscriptions(): Observable<StripeSubscription[]> {
|
||||
return this.http.get<StripeSubscription[]>(`${this.apiBaseUrl}/bizmatch/payment/subscription/all`).pipe(
|
||||
catchError(error => {
|
||||
console.error('Fehler beim Laden der Stripe-Subscriptions', error);
|
||||
return of([]);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getAllStripeUsers(): Observable<StripeUser[]> {
|
||||
return this.http.get<StripeUser[]>(`${this.apiBaseUrl}/bizmatch/payment/user/all`).pipe(
|
||||
catchError(error => {
|
||||
console.error('Fehler beim Laden der Stripe-Benutzer', error);
|
||||
return of([]);
|
||||
}),
|
||||
);
|
||||
}
|
||||
getPaymentMethods(email: string): Observable<PaymentMethod[]> {
|
||||
return this.http.get<PaymentMethod[]>(`${this.apiBaseUrl}/bizmatch/payment/paymentmethod/${email}`).pipe(
|
||||
catchError(error => {
|
||||
console.error('Fehler beim Laden der Zahlungsinformationen', error);
|
||||
return of([]);
|
||||
}),
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Lädt alle Benutzer aus den verschiedenen Quellen und kombiniert sie.
|
||||
* @returns Ein Observable mit einer Liste von CombinedUser.
|
||||
*/
|
||||
loadUsers(): Observable<CombinedUser[]> {
|
||||
return forkJoin({
|
||||
keycloakUsers: this.getKeycloakUsers(),
|
||||
appUsers: this.getAppUsers(),
|
||||
stripeSubscriptions: this.getAllStripeSubscriptions(),
|
||||
stripeUsers: this.getAllStripeUsers(),
|
||||
}).pipe(
|
||||
map(({ keycloakUsers, appUsers, stripeSubscriptions, stripeUsers }) => {
|
||||
const combinedUsers: CombinedUser[] = [];
|
||||
|
||||
// Map App Users mit Keycloak und Stripe Subscription
|
||||
appUsers.forEach(appUser => {
|
||||
const keycloakUser = keycloakUsers.find(kcUser => kcUser.email.toLowerCase() === appUser.email.toLowerCase());
|
||||
|
||||
// const stripeSubscription = appUser.subscriptionId ? stripeSubscriptions.find(sub => sub.id === appUser.subscriptionId) : null;
|
||||
const stripeUser = stripeUsers.find(suser => suser.email === appUser.email);
|
||||
const stripeSubscription = stripeUser ? stripeSubscriptions.find(sub => sub.customer === stripeUser.id) : null;
|
||||
combinedUsers.push({
|
||||
appUser,
|
||||
keycloakUser,
|
||||
stripeUser,
|
||||
stripeSubscription,
|
||||
});
|
||||
});
|
||||
|
||||
// Füge Stripe-Benutzer hinzu, die nicht in App oder Keycloak vorhanden sind
|
||||
stripeUsers.forEach(stripeUser => {
|
||||
const existsInApp = appUsers.some(appUser => appUser.email.toLowerCase() === stripeUser.email.toLowerCase());
|
||||
const existsInKeycloak = keycloakUsers.some(kcUser => kcUser.email.toLowerCase() === stripeUser.email.toLowerCase());
|
||||
|
||||
if (!existsInApp && !existsInKeycloak) {
|
||||
combinedUsers.push({
|
||||
stripeUser,
|
||||
// Optional: Verknüpfe Stripe-Benutzer mit ihren Subscriptions
|
||||
stripeSubscription: stripeSubscriptions.find(sub => sub.customer === stripeUser.id) || null,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return combinedUsers;
|
||||
}),
|
||||
catchError(err => {
|
||||
console.error('Fehler beim Kombinieren der Benutzer', err);
|
||||
return of([]);
|
||||
}),
|
||||
);
|
||||
}
|
||||
async deleteCustomerFromStripe(customerId: string): Promise<void> {
|
||||
await lastValueFrom(this.http.delete(`${this.apiBaseUrl}/bizmatch/payment/customer/${customerId}`));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user