From 4d2aa752c6c15ee1ea6107e52a51029fe5139f97 Mon Sep 17 00:00:00 2001
From: micnori <micnori@users.noreply.github.com>
Date: Mon, 16 Sep 2024 12:47:27 +0200
Subject: [PATCH 1/2] delete location - check employee

---
 .../pgazienda/repository/EmployeeRepository.java      |  2 +-
 .../pgazienda/service/CompanyService.java             | 11 ++++++++---
 .../pgazienda/service/TrackingDataService.java        |  2 +-
 .../pgazienda/web/rest/LocationResource.java          |  3 ++-
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/backend/src/main/java/it/smartcommunitylab/pgazienda/repository/EmployeeRepository.java b/backend/src/main/java/it/smartcommunitylab/pgazienda/repository/EmployeeRepository.java
index b6dbad6..6d9894b 100644
--- a/backend/src/main/java/it/smartcommunitylab/pgazienda/repository/EmployeeRepository.java
+++ b/backend/src/main/java/it/smartcommunitylab/pgazienda/repository/EmployeeRepository.java
@@ -39,7 +39,7 @@ public interface EmployeeRepository  extends MongoRepository<Employee, String> {
 
 	public List<Employee> findByCampaigns(String campaignId);
 
-	public List<Employee> findByCompanyIdAndLocation(String id, String location);
+	public List<Employee> findByCompanyIdAndLocationIgnoreCase(String id, String location);
 
 	public List<Employee> findByCompanyId(String companyId);
 
diff --git a/backend/src/main/java/it/smartcommunitylab/pgazienda/service/CompanyService.java b/backend/src/main/java/it/smartcommunitylab/pgazienda/service/CompanyService.java
index 1629228..af4d84f 100644
--- a/backend/src/main/java/it/smartcommunitylab/pgazienda/service/CompanyService.java
+++ b/backend/src/main/java/it/smartcommunitylab/pgazienda/service/CompanyService.java
@@ -329,13 +329,18 @@ public CompanyLocation updateLocation(String companyId, CompanyLocation location
 	 * @param companyId
 	 * @param locationId
 	 */
-	public void deleteLocation(String companyId, String locationId) {
-		companyRepo.findById(companyId).ifPresent(company -> {
+	public void deleteLocation(String companyId, String locationId) throws InconsistentDataException {
+		Company company = companyRepo.findById(companyId).orElse(null);
+		if(company != null) {
+			List<Employee> list = employeeRepo.findByCompanyIdAndLocationIgnoreCase(companyId, locationId);
+			if(list.size() > 0) {
+				throw new InconsistentDataException("Location has employees", "INVALID_LOCATION_DATA_EMPLOYEE");
+			}
 			if (company.getLocations() != null) {
 				company.getLocations().removeIf(l -> l.getId() == null && locationId == null || l.getId().equalsIgnoreCase(locationId));
 				companyRepo.save(company);
 			}
-		});		
+		}		
 	}
 	/**
 	 * @param companyId
diff --git a/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java b/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java
index 777f51c..6f199ac 100644
--- a/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java
+++ b/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java
@@ -516,7 +516,7 @@ public List<DayStat> statistics(
 		} else if (location != null && companyId != null) {
 			Company company = companyRepo.findById(companyId).orElse(null);
 			if (company == null) throw new InconsistentDataException("Invalid company: " + companyId, "NO_COMPANY");
-			Set<String> employeeKeys = employeeRepo.findByCompanyIdAndLocation(companyId, location).stream().map(e -> e.getCode()).collect(Collectors.toSet());
+			Set<String> employeeKeys = employeeRepo.findByCompanyIdAndLocationIgnoreCase(companyId, location).stream().map(e -> e.getCode()).collect(Collectors.toSet());
 			List<String> users = userRepo.findByCampaignAndCompanyAndEmployeeCode(campaignId, company.getCode(), employeeKeys).stream().map(u -> u.getPlayerId()).collect(Collectors.toList());
 			criteria = criteria.and("playerId").in(users);
 		}
diff --git a/backend/src/main/java/it/smartcommunitylab/pgazienda/web/rest/LocationResource.java b/backend/src/main/java/it/smartcommunitylab/pgazienda/web/rest/LocationResource.java
index 9477d67..155cb3e 100644
--- a/backend/src/main/java/it/smartcommunitylab/pgazienda/web/rest/LocationResource.java
+++ b/backend/src/main/java/it/smartcommunitylab/pgazienda/web/rest/LocationResource.java
@@ -89,10 +89,11 @@ public ResponseEntity<CompanyLocation> updateLocation(@PathVariable String compa
      * @param companyId
      * @param locationId
      * @return
+     * @throws InconsistentDataException 
      */
     @DeleteMapping("/companies/{companyId}/locations/{locationId:.*}")
     //@PreAuthorize("hasAnyAuthority(\"" + Constants.ROLE_ADMIN + "\", \""+Constants.ROLE_COMPANY_ADMIN  +"\")")
-	public ResponseEntity<Void> deleteLocation(@PathVariable String companyId, @PathVariable String locationId) {
+	public ResponseEntity<Void> deleteLocation(@PathVariable String companyId, @PathVariable String locationId) throws InconsistentDataException {
     	log.debug("Deleting a location {} / {}", companyId, locationId);
     	if (!userService.isInCompanyRole(companyId, Constants.ROLE_TERRITORY_MANAGER, Constants.ROLE_MOBILITY_MANAGER)) throw new SecurityException("Insufficient rights");
     	companyService.deleteLocation(companyId, locationId);

From 4b27cb569e77daed651306362fdb3ca3bb8684bf Mon Sep 17 00:00:00 2001
From: Matteo Chini <matteo.chini@gmail.com>
Date: Mon, 16 Sep 2024 14:56:22 +0200
Subject: [PATCH 2/2] fixed issue with string and location

---
 .../src/components/modal/specific-modals/LocationFormModal.vue  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/console_aziendale/src/components/modal/specific-modals/LocationFormModal.vue b/console_aziendale/src/components/modal/specific-modals/LocationFormModal.vue
index 1a953bd..3f41c36 100644
--- a/console_aziendale/src/components/modal/specific-modals/LocationFormModal.vue
+++ b/console_aziendale/src/components/modal/specific-modals/LocationFormModal.vue
@@ -232,7 +232,7 @@
                     <p>E’ possibile impostare la posizione della sede manualmente oppure automaticamente in base all’indirizzo inserito.</p>
                   </div>
 
-                <p v-if="!showErrorLocation ">
+                <p v-if="!showErrorLocation && !addresIsValid ">
                   Per poter impostare una posizione è necessario indicare l’indirizzo
                   della sede.
                 </p>