export DB, Event creation, broker with city/state

This commit is contained in:
2024-09-12 15:13:56 +02:00
parent 60866473f7
commit 68d2615f0f
21 changed files with 242 additions and 40 deletions

View File

@@ -0,0 +1,66 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject, lastValueFrom, Observable } from 'rxjs';
import { ListingEvent } from '../../../../bizmatch-server/src/models/db.model';
import { LogMessage } from '../../../../bizmatch-server/src/models/main.model';
import { environment } from '../../environments/environment';
@Injectable({
providedIn: 'root',
})
export class AuditService {
private apiBaseUrl = environment.apiBaseUrl;
private apiKey = environment.ipinfo_token;
// private ipifyUrl = 'https://api.ipify.org?format=json';
// private ipInfoUrl = 'https://ipinfo.io';
private ipifyUrl = 'https://api.ipify.org?format=json';
private ipInfoUrl = 'https://ipinfo.io';
// BehaviorSubject to store the geolocation data
private geoLocationSubject = new BehaviorSubject<any>(null);
public geoLocation$: Observable<any> = this.geoLocationSubject.asObservable();
constructor(private http: HttpClient) {}
async log(message: LogMessage): Promise<void> {
lastValueFrom(this.http.post(`${this.apiBaseUrl}/bizmatch/log`, message));
}
async createEvent(event: ListingEvent): Promise<void> {
lastValueFrom(this.http.post(`${this.apiBaseUrl}/bizmatch/event`, event));
}
// Function to get the IP address
private getIpAddress(): Observable<{ ip: string }> {
return this.http.get<{ ip: string }>(`/ipinfo?format=json`);
}
// Function to get geolocation using IP address
private getGeolocation(ip: string): Observable<any> {
return this.http.get(`/ipinfo/${ip}?token=${this.apiKey}`);
}
// Fetch IP and Geolocation only once, if not already fetched
fetchIpAndGeoLocation(): void {
if (!this.geoLocationSubject.getValue()) {
this.getIpAddress().subscribe({
next: response => {
this.getGeolocation(response.ip).subscribe({
next: geoData => {
this.geoLocationSubject.next(geoData); // Store the geolocation data
},
error: geoError => {
console.error('Error fetching geolocation:', geoError);
},
});
},
error: ipError => {
console.error('Error fetching IP address:', ipError);
},
});
}
}
// Method to provide the stored geolocation data
getGeoLocationData(): Observable<any> {
return this.geoLocation$; // Returns the observable for other components to subscribe
}
}