Skip to content

Commit

Permalink
DEAR-131
Browse files Browse the repository at this point in the history
* DEAR-131: add dashboard dto

* DEAR-131: fix count of happiness days

* DEAR-131: add teamName to workKind for dashboard
  • Loading branch information
baurnick authored Aug 6, 2024
1 parent 27cfd01 commit 95319bb
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import ch.fhnw.deardevbackend.dto.SubmitWorkKindSurveyDTO;
import ch.fhnw.deardevbackend.entities.EmotionSurvey;
import ch.fhnw.deardevbackend.entities.HappinessSurvey;
import ch.fhnw.deardevbackend.entities.User;
import ch.fhnw.deardevbackend.entities.WorkKindSurvey;
import ch.fhnw.deardevbackend.services.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;


Expand All @@ -19,18 +22,13 @@ public class DashboardController {
@Autowired
private DashboardService dashboardService;

@GetMapping("/{userId}")
public ResponseEntity<DashboardDTO> getDashboardDataByUserId(@PathVariable Integer userId) {
DashboardDTO dashboardDTO = dashboardService.getDashboardDataByUserId(userId);
@GetMapping("/data")
public ResponseEntity<DashboardDTO> getDashboardData() {
Integer userId = getCurrentUserFromContext().getId();
DashboardDTO dashboardDTO = dashboardService.getDashboardData(userId);
return ResponseEntity.ok().body(dashboardDTO);
}

@GetMapping("/happiness/average/{userId}")
public ResponseEntity<Integer> getAverageScoreByUserId(@PathVariable Integer userId) {
Integer averageScore = dashboardService.getAverageScoreByUserId(userId);
return ResponseEntity.ok().body(averageScore);
}

@PostMapping("/survey/happiness")
public ResponseEntity<HappinessSurvey> submitHappinessSurvey(@RequestBody SubmitHappinessSurveyDTO request) {
HappinessSurvey data = dashboardService.saveHappinessSurvey(request);
Expand All @@ -48,4 +46,9 @@ public ResponseEntity<EmotionSurvey> submitEmotionSurvey(@RequestBody SubmitEmot
EmotionSurvey data = dashboardService.saveEmotionSurvey(request);
return ResponseEntity.ok().body(data);
}

private User getCurrentUserFromContext() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return (User) authentication.getPrincipal();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package ch.fhnw.deardevbackend.controller;

import ch.fhnw.deardevbackend.dto.WorkKindAndTeamDTO;
import ch.fhnw.deardevbackend.entities.User;
import ch.fhnw.deardevbackend.entities.WorkKind;
import ch.fhnw.deardevbackend.services.WorkKindService;
import ch.fhnw.deardevbackend.services.TeamService;
import ch.fhnw.deardevbackend.services.WorkKindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -29,4 +33,16 @@ public ResponseEntity<List<WorkKind>> getAllWorkKindsByUserAndTeam(@PathVariable
List<WorkKind> workKinds = workKindService.getWorkKindsForTeams(teamIds);
return ResponseEntity.ok(workKinds);
}

@GetMapping("/team")
public ResponseEntity<List<WorkKindAndTeamDTO>> getWorkKindsAndTeams() {
Integer userId = getCurrentUser().getId();
List<WorkKindAndTeamDTO> workKindAndTeams = workKindService.getWorkKindsAndTeams(userId);
return ResponseEntity.ok(workKindAndTeams);
}

private User getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return (User) authentication.getPrincipal();
}
}
16 changes: 12 additions & 4 deletions src/main/java/ch/fhnw/deardevbackend/dto/DashboardDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DashboardDTO {
private MostVotedWorkKindDTO mostVotedWorkKind;
private Integer averageScore;


private LocalDate lastSubmissionDateOfHappiness;
private LocalDate activeSprintEndDate;
private List<String> mostTrackedEmotions;
private String mostTrackedWorkKind;
private Integer numberOfDaysWithHappinessSurvey;
private Integer numberOfHappinessSurveysToday;
private Integer numberOfTeams;
private Integer numberOfTeamMembers;
private Double averageHappinessScore;
}
10 changes: 10 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/dto/WorkKindAndTeamDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.fhnw.deardevbackend.dto;

import ch.fhnw.deardevbackend.entities.WorkKind;
import lombok.Data;

@Data
public class WorkKindAndTeamDTO {
private WorkKind workKind;
private String teamName;
}
18 changes: 0 additions & 18 deletions src/main/java/ch/fhnw/deardevbackend/mapper/DashboardMapper.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.fhnw.deardevbackend.mapper;

import ch.fhnw.deardevbackend.dto.WorkKindAndTeamDTO;
import ch.fhnw.deardevbackend.entities.WorkKind;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper(componentModel = "spring", unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE)
public interface WorkKindAndTeamMapper {

WorkKindAndTeamMapper INSTANCE = Mappers.getMapper(WorkKindAndTeamMapper.class);

@Mapping(target = "workKind", source = "workKind")
@Mapping(target = "teamName", source = "teamName")
WorkKindAndTeamDTO toDTO(WorkKind workKind, String teamName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

import ch.fhnw.deardevbackend.entities.EmotionSurvey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EmotionSurveyRepository extends JpaRepository<EmotionSurvey, Integer> {
@Query(value = "SELECT e.name FROM emotion_survey es " +
"JOIN emotion e ON es.emotion_id = e.id " +
"WHERE es.user_id = :userId " +
"GROUP BY e.name " +
"ORDER BY COUNT(e.name) DESC " +
"LIMIT 2",
nativeQuery = true)
List<String> findTopTwoTrackedEmotions(Integer userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
@Repository
public interface HappinessSurveyRepository extends JpaRepository<HappinessSurvey, Integer> {

@Query("SELECT o FROM HappinessSurvey o WHERE o.userId = :userId")
List<HappinessSurvey> findByUserId(@Param("userId") Integer userId);

@Query("SELECT AVG(o.score) FROM HappinessSurvey o WHERE o.userId = :userId")
Double findAverageScoreByUserId(@Param("userId") Integer userId);

@Query("SELECT CAST(s.submitted AS date) AS day, AVG(s.score) AS dailyAverage FROM HappinessSurvey s WHERE s.userId = :userId GROUP BY day")
List<Object[]> findDailyAveragesByUserId(Integer userId);

@Query("SELECT MAX(h.submitted) FROM HappinessSurvey h WHERE h.userId = :userId")
LocalDateTime findLastSubmissionDateByUserId(Integer userId);

@Query("SELECT COUNT(DISTINCT(CAST(h.submitted AS date))) FROM HappinessSurvey h WHERE h.userId = :userId")
int countDaysWithHappinessSurveyThisYear(@Param("userId") Integer userId);

@Query("SELECT COUNT(h) FROM HappinessSurvey h WHERE h.userId = :userId AND CAST(h.submitted AS date) = CURRENT_DATE")
int numberOfHappinessSurveysToday(@Param("userId") Integer userId);

@Query("SELECT AVG(h.score) FROM HappinessSurvey h WHERE h.userId = :userId")
Double findAverageHappinessScore(@Param("userId") Integer userId);

@Query("SELECT CAST(s.submitted AS date) AS day, AVG(s.score) AS dailyAverage " +
"FROM HappinessSurvey s " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.fhnw.deardevbackend.entities.SprintStatus;
import ch.fhnw.deardevbackend.entities.Team;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
Expand All @@ -23,4 +24,12 @@ public interface SprintConfigRepository extends JpaRepository<SprintConfig, Inte

List<SprintConfig> findByTeamAndStatusIn(Team team, List<SprintStatus> statuses);


@Query(value = "SELECT s.end_date FROM sprint_config s " +
"WHERE s.team_id IN (SELECT tm.team_id FROM team_member tm WHERE tm.user_id = :userId) " +
"AND s.status = 'IN_PROGRESS' " +
"ORDER BY s.end_date " +
"LIMIT 1",
nativeQuery = true)
Optional<LocalDate> findClosestActiveSprintEndDateByUserId(Integer userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public interface TeamMemberRepository extends JpaRepository<TeamMember, Integer>

List<TeamMember> findByUserId(Integer userId);

@Query("SELECT COUNT(tm) FROM TeamMember tm WHERE tm.userId = :userId AND tm.active = true")
int countTeamsUserIsIn(@Param("userId") Integer userId);

@Query("SELECT COUNT(DISTINCT tm2.userId) FROM TeamMember tm JOIN TeamMember tm2 ON tm.teamId = tm2.teamId " +
"WHERE tm.userId = :userId AND tm.active = TRUE AND tm2.active = TRUE")
int countTotalTeamMembersForUser(@Param("userId") Integer userId);

@Query("SELECT tm.teamId FROM TeamMember tm WHERE tm.userId = :userId AND tm.active = true")
List<Integer> findTeamIdByUserId(@Param("userId") Integer userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface WorkKindRepository extends JpaRepository<WorkKind, Integer> {
List<WorkKind> findByTeamIdsOrNoTeam(@Param("teamIds") List<Integer> teamIds);

Optional<WorkKind> findByNameAndTeamId(String name, Integer teamId);

List<WorkKind> findByTeamId(Integer id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
import ch.fhnw.deardevbackend.entities.WorkKindSurvey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;


@Repository
public interface WorkKindSurveyRepository extends JpaRepository<WorkKindSurvey, Integer> {

@Query("SELECT w.workKindId, COUNT(w.workKindId) as voteCount " +
"FROM WorkKindSurvey w " +
"WHERE w.userId = :userId " +
"GROUP BY w.workKindId " +
"ORDER BY voteCount DESC")
List<Object[]> findWorkKindCountByUserId(Integer userId);

@Query("SELECT AVG(w.score) " +
"FROM WorkKindSurvey w " +
"WHERE w.workKindId = :workKindId AND w.userId = :userId")
Optional<Integer> findAverageHappinessScoreByWorkKindIdAndUserId(Integer workKindId, Integer userId);
@Query(value = "SELECT wk.name FROM work_kind_survey ws " +
"JOIN work_kind wk ON ws.work_kind_id = wk.id " +
"WHERE ws.user_id = :userId " +
"GROUP BY wk.name " +
"ORDER BY COUNT(wk.name) DESC " +
"LIMIT 1", nativeQuery = true)
String findMostTrackedWorkType(@Param("userId") Integer userId);

@Query("SELECT COUNT(DISTINCT wk.id) FROM WorkKind wk WHERE wk.teamId = :teamId")
int findDistinctWorkKindCountByTeamId(@Param("teamId") Integer teamId);
}


Loading

0 comments on commit 95319bb

Please sign in to comment.