Skip to content
This repository was archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
feat(front-end): Send employee location
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Feb 13, 2021
1 parent 05d284e commit 6c58249
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { EmployeeLocationService } from './employee-location.service';

describe('EmployeeLocationService', () => {
let service: EmployeeLocationService;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ]
});
service = TestBed.inject(EmployeeLocationService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Injectable } from '@angular/core';
import { Employee } from '@modules/employees/models/employee';
import { EmployeeLocation } from '@modules/employees/models/employee-location';
import { EmployeeSignalrService } from '@modules/employees/services/employee-signalr.service';

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

private employeeSendLocationInterval: NodeJS.Timeout;
private currentEmployee: Employee;

constructor(private employeeSignalr: EmployeeSignalrService) {
}

public startToSendEmployeeLocation(): void {
this.employeeSendLocationInterval = setInterval(() => {
if (!this.currentEmployee) {
this.currentEmployee = JSON.parse(localStorage.getItem('current_person'));
}

if (!this.currentEmployee) {
return;
}

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position => {
if (position) {
const employeeLocation: EmployeeLocation = {
id: this.currentEmployee.id,
employee: this.currentEmployee,
location: {
latitude: position.coords.latitude,
longitude: position.coords.longitude
}
};

this.employeeSignalr.sendNewEmployeeLocation(employeeLocation);
}
}));
}
}, 5000);
}

public endToSendEmployeeLocation(): void {
if (this.employeeSendLocationInterval) {
clearInterval(this.employeeSendLocationInterval);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { AuthenticationService } from '@core/authentication/authentication.service';
import { ADMINISTRATOR_ROLE, CLIENT_ROLE } from '@global/roles';
import { ADMINISTRATOR_ROLE, CLIENT_ROLE, TECHNICAL_EMPLOYEE_ROLE } from '@global/roles';
import { ClientService } from '@modules/clients/services/client.service';
import { EmployeeLocationService } from '@modules/employees/services/employee-location.service';
import { EmployeeService } from '@modules/employees/services/employee.service';
import { Person } from '@shared/models/person';

Expand All @@ -17,7 +18,8 @@ export class UserProfileComponent implements OnInit {
constructor(
private authService: AuthenticationService,
private clientService: ClientService,
private employeeService: EmployeeService
private employeeService: EmployeeService,
private employeeLocationService: EmployeeLocationService
) {}

ngOnInit(): void {
Expand All @@ -41,6 +43,10 @@ export class UserProfileComponent implements OnInit {
this.person = employee;
this.savePersonInLocalStorage();
});

if (userRole == TECHNICAL_EMPLOYEE_ROLE) {
this.employeeLocationService.startToSendEmployeeLocation();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { AuthenticationService } from '@core/authentication/authentication.service';
import { EmployeeLocationService } from '@modules/employees/services/employee-location.service';

@Component({
selector: 'app-nav-menu',
Expand All @@ -10,7 +11,7 @@ export class NavMenuComponent implements OnInit {
isLogged = false;
isLogout = false;

constructor(private authService: AuthenticationService) {}
constructor(private authService: AuthenticationService, private employeeLocationService: EmployeeLocationService) {}

ngOnInit(): void {
this.onCheckUser();
Expand All @@ -26,6 +27,7 @@ export class NavMenuComponent implements OnInit {
this.isLogout = true;
this.authService.logoutUser().subscribe(result => {
if (result) {
this.employeeLocationService.endToSendEmployeeLocation();
location.reload();
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatSidenav } from '@angular/material/sidenav';
import { Router } from '@angular/router';
import { DASHBOARDS_CARDS } from '@app/global/control-panel-cards';
import { AuthenticationService } from '@core/authentication/authentication.service';
import { DashboardCard } from '@core/models/dashboard-card';
import { EmployeeLocationService } from '@modules/employees/services/employee-location.service';
import { Observable } from 'rxjs';
import { map, shareReplay } from 'rxjs/operators';

Expand All @@ -27,7 +27,7 @@ export class DashboardLayoutComponent implements OnInit {
constructor(
private breakPointObserver: BreakpointObserver,
private authService: AuthenticationService,
private router: Router
private employeeLocationService: EmployeeLocationService
) {}

ngOnInit(): void {
Expand Down Expand Up @@ -55,6 +55,7 @@ export class DashboardLayoutComponent implements OnInit {
this.isLogout = true;
this.authService.logoutUser().subscribe(result => {
if (result) {
this.employeeLocationService.endToSendEmployeeLocation();
location.reload();
}
});
Expand Down

0 comments on commit 6c58249

Please sign in to comment.