diff --git a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/services/StatisticsServiceImpl.java b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/services/StatisticsServiceImpl.java index 48f3fc7fc..841fff8a7 100644 --- a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/services/StatisticsServiceImpl.java +++ b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/services/StatisticsServiceImpl.java @@ -1,6 +1,7 @@ package com.xpeho.yaki_admin_backend.data.services; import com.xpeho.yaki_admin_backend.data.sources.DeclarationJpaRepository; +import com.xpeho.yaki_admin_backend.domain.entities.LastActivityEntity; import com.xpeho.yaki_admin_backend.domain.entities.statistics.*; import com.xpeho.yaki_admin_backend.data.sources.TeamJpaRepository; import com.xpeho.yaki_admin_backend.data.sources.TeammateJpaRepository; @@ -173,4 +174,47 @@ public ArrayList getPerWeekDayStatistics(StatisticsR } return perWeekdayStatisticsEntities; } + + @Override + public List getLastTeamsActivityByCustomerId(Integer targetId) { + List results = declarationJpaRepository.getLastTeamActivityById( + targetId); + + ArrayList customerActivityData = new ArrayList<>(); + + for (Object[] result : results) { + String teamId = result[0].toString(); + LocalDateTime latestActivity = ((Timestamp) result[1]).toLocalDateTime(); + + LastActivityEntity teamActivityData = new LastActivityEntity( + teamId, + latestActivity); + + customerActivityData.add(teamActivityData); + } + + return customerActivityData; + } + + @Override + public List getLastTeamActivityById(Integer targetId) { + + List results = declarationJpaRepository.getLastTeamActivityById( + targetId); + + ArrayList teamActivityData = new ArrayList<>(); + + for (Object[] result : results) { + String teamId = result[0].toString(); + LocalDateTime latestActivity = ((Timestamp) result[1]).toLocalDateTime(); + + LastActivityEntity soleTeamActivityData = new LastActivityEntity( + teamId, + latestActivity); + + teamActivityData.add(soleTeamActivityData); + } + + return teamActivityData; + } } \ No newline at end of file diff --git a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/sources/DeclarationJpaRepository.java b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/sources/DeclarationJpaRepository.java index 390201937..e2d47d3b1 100644 --- a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/sources/DeclarationJpaRepository.java +++ b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/data/sources/DeclarationJpaRepository.java @@ -422,4 +422,21 @@ SELECT fd.week_day, count(*) as absence_count + PER_WEEKDAY_COUNTS + PER_WEEKDAY_MAIN_QUERY, nativeQuery = true) List getPerWeekDayStatisticsByCustomerId(LocalDate periodStart, LocalDate periodEnd, int customerId); -} + + @Query(value = """ + ---------------------------------- LATEST ACTIVITY IN A TEAM + SELECT declaration_team_id, MAX(declaration_date) + FROM declaration + WHERE declaration_team_id = ?1 + GROUP BY declaration_team_id""", nativeQuery = true) + List getLastTeamActivityById(Integer targetId); + + @Query(value = """ + ---------------------------------- LATEST ACTIVITY IN A TEAM + SELECT team.team_id, MAX(declaration_date) + FROM declaration + INNER JOIN team ON declaration.declaration_team_id = team.team_id + WHERE team.team_customer_id = ?1 + GROUP BY team.team_id""", nativeQuery = true) + List getLastTeamsActivityByCustomerId(Integer targetId); +} \ No newline at end of file diff --git a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/domain/entities/LastActivityEntity.java b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/domain/entities/LastActivityEntity.java new file mode 100644 index 000000000..0bf9d13f5 --- /dev/null +++ b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/domain/entities/LastActivityEntity.java @@ -0,0 +1,34 @@ +package com.xpeho.yaki_admin_backend.domain.entities; + +import java.time.LocalDateTime; + +public class LastActivityEntity { + String teamId; + LocalDateTime lastActivityDate; + + public LastActivityEntity() { + } + + public LastActivityEntity(String teamId, LocalDateTime lastActivityDate) { + this.teamId = teamId; + this.lastActivityDate = lastActivityDate; + } + + public String getTeamId() { + return teamId; + } + + public void setTeamId(String teamId) { + this.teamId = teamId; + } + + public LocalDateTime getLastActivityDate() { + return lastActivityDate; + } + + public void setLastActivityDate(LocalDateTime lastActivityDate) { + this.lastActivityDate = lastActivityDate; + } + +} + diff --git a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/domain/services/StatisticsService.java b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/domain/services/StatisticsService.java index 7f13cedcd..76931cf4c 100644 --- a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/domain/services/StatisticsService.java +++ b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/domain/services/StatisticsService.java @@ -1,8 +1,10 @@ package com.xpeho.yaki_admin_backend.domain.services; +import com.xpeho.yaki_admin_backend.domain.entities.LastActivityEntity; import com.xpeho.yaki_admin_backend.domain.entities.statistics.*; import java.util.ArrayList; +import java.util.List; public interface StatisticsService { BasicStatisticsEntity getBasicStatistics(StatisticsRequestEntity requestEntity); @@ -11,4 +13,6 @@ public interface StatisticsService { ArrayList getPerTeammateStatistics(StatisticsRequestEntity requestEntity); ArrayList getPerTeamStatistics(StatisticsRequestEntity requestEntity); ArrayList getPerWeekDayStatistics(StatisticsRequestEntity requestEntity); + List getLastTeamsActivityByCustomerId (Integer customerId); + List getLastTeamActivityById (Integer teamId); } diff --git a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/presentation/controllers/StatisticsController.java b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/presentation/controllers/StatisticsController.java index 92a3ce28b..458393d8d 100644 --- a/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/presentation/controllers/StatisticsController.java +++ b/yaki_admin_backend/src/main/java/com/xpeho/yaki_admin_backend/presentation/controllers/StatisticsController.java @@ -1,8 +1,11 @@ package com.xpeho.yaki_admin_backend.presentation.controllers; + +import com.xpeho.yaki_admin_backend.domain.entities.LastActivityEntity; import com.xpeho.yaki_admin_backend.domain.entities.statistics.*; import com.xpeho.yaki_admin_backend.domain.services.StatisticsService; import io.swagger.v3.oas.annotations.security.SecurityRequirement; + import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -10,6 +13,8 @@ import org.springframework.web.bind.annotation.*; import java.util.ArrayList; +import java.util.List; + @CrossOrigin @RestController @@ -23,6 +28,29 @@ public StatisticsController(StatisticsService statisticsService) { this.statisticsService = statisticsService; } + /** + * Returns the latest activity of a singular team, or of a customer's list of teams + * + * @param customerId : id of the customer + * @param teamId : id of the team, if a customerId is given, the teamId is ignored + * @return a list of objects {teamId: String, lastActivityDate: LocalDateTime}, or a code 400 if no parameter is given + */ + @GetMapping("lastActivity") + public ResponseEntity> getLatestTeamActivity( + @RequestParam(required = false) Integer customerId, + @RequestParam(required = false) Integer teamId) + { + if (customerId != null) { + return new ResponseEntity<>(statisticsService.getLastTeamsActivityByCustomerId(customerId), HttpStatus.OK); + } else if (teamId != null) { + return new ResponseEntity<>(statisticsService.getLastTeamActivityById(teamId), HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + } + + @PostMapping("basic") public BasicStatisticsEntity getBasicStatistics(@RequestBody StatisticsRequestEntity requestEntity) { checkRequestEntity(requestEntity);