Fixes für input fields, #60 -> AuditService
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
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 { EventTypeEnum, ListingEvent } from '../../../../bizmatch-server/src/models/db.model';
|
||||
import { LogMessage } from '../../../../bizmatch-server/src/models/main.model';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { GeoService } from './geo.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -20,47 +21,26 @@ export class AuditService {
|
||||
private geoLocationSubject = new BehaviorSubject<any>(null);
|
||||
public geoLocation$: Observable<any> = this.geoLocationSubject.asObservable();
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
constructor(private http: HttpClient, private geoService: GeoService) {}
|
||||
|
||||
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
|
||||
async createEvent(id: string, eventType: EventTypeEnum, userId: string): Promise<void> {
|
||||
const ipInfo = await this.geoService.getIpInfo();
|
||||
const [latitude, longitude] = ipInfo.loc ? ipInfo.loc.split(',') : [null, null]; //.map(Number);
|
||||
const listingEvent: ListingEvent = {
|
||||
listingId: id,
|
||||
eventType,
|
||||
eventTimestamp: new Date(),
|
||||
userAgent: navigator.userAgent,
|
||||
email: userId,
|
||||
userIp: ipInfo.ip,
|
||||
locationCountry: ipInfo.country,
|
||||
locationCity: ipInfo.city,
|
||||
locationLat: latitude,
|
||||
locationLng: longitude,
|
||||
};
|
||||
lastValueFrom(this.http.post(`${this.apiBaseUrl}/bizmatch/event`, listingEvent));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user