From e667e0fe82381141c92553e6f47d10af89f309fe Mon Sep 17 00:00:00 2001 From: nehaagrawalmm <94346484+nehaagrawalmm@users.noreply.github.com> Date: Thu, 16 Jun 2022 13:00:56 +0530 Subject: [PATCH 1/3] ADDED Worktype filtering parameter in get work logs --- .../controllers/orgs/impl/UserWorkApiImpl.kt | 7 ++++++- .../data/models/projects/DateRangeWorkRequest.kt | 4 +++- .../data/models/projects/HarvestUserWork.kt | 2 ++ .../repositories/orgs/UserWorkRepository.kt | 10 ++++++++-- .../praxisspringboot/services/orgs/UserWorkService.kt | 4 +++- .../services/orgs/impl/UserProjectServiceImpl.kt | 11 +++++++---- .../services/orgs/impl/UserWorkServiceImpl.kt | 9 ++++++--- 7 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/controllers/orgs/impl/UserWorkApiImpl.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/controllers/orgs/impl/UserWorkApiImpl.kt index 7011e6c..96dbade 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/controllers/orgs/impl/UserWorkApiImpl.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/controllers/orgs/impl/UserWorkApiImpl.kt @@ -4,6 +4,7 @@ import com.mutualmobile.praxisspringboot.controllers.orgs.UserWorkApi import com.mutualmobile.praxisspringboot.data.ApiResponse import com.mutualmobile.praxisspringboot.data.models.projects.DateRangeWorkRequest import com.mutualmobile.praxisspringboot.data.models.projects.HarvestUserWork +import com.mutualmobile.praxisspringboot.data.models.projects.WorkType import com.mutualmobile.praxisspringboot.services.orgs.UserWorkService import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpStatus @@ -22,7 +23,11 @@ class UserWorkApiImpl : UserWorkApi { val result = userWorkService.getWorkLogsForDateRange( startDate = dateRangeWorkRequest.startDate, endDate = dateRangeWorkRequest.endDate, - userIds = dateRangeWorkRequest.userIds + userIds = dateRangeWorkRequest.userIds, + workType = dateRangeWorkRequest.workType?.let { + WorkType.valueOf(it) + } + ) if (result.data == null) { ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(result) diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/DateRangeWorkRequest.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/DateRangeWorkRequest.kt index b0d8c40..f329dc8 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/DateRangeWorkRequest.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/DateRangeWorkRequest.kt @@ -6,5 +6,7 @@ data class DateRangeWorkRequest( val startDate: Date, val endDate: Date, // To be used as a filter for the output (if exists) - val userIds: List? = null + val userIds: List? = null, + val workType: String ? = null + ) diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/HarvestUserWork.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/HarvestUserWork.kt index c2fa4a3..fe063b6 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/HarvestUserWork.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/data/models/projects/HarvestUserWork.kt @@ -14,6 +14,8 @@ data class HarvestUserWork( val note: String? = null ) + + enum class WorkType(val type: String) { BILLABLE("1"), NONBILLABLE("2") diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt index e1123df..0276f01 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt @@ -1,12 +1,18 @@ package com.mutualmobile.praxisspringboot.repositories.orgs +import com.mutualmobile.praxisspringboot.data.models.projects.WorkType import com.mutualmobile.praxisspringboot.entities.projects.DBUserWork -import java.util.Date import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.query.Param +import java.util.* interface UserWorkRepository : JpaRepository { - fun findAllByWorkDateBetweenAndUserId(startDate: Date, endDate: Date, userId: String): List + + @Query("SELECT u FROM user_work AS u WHERE u.work_date BETWEEN :startDate AND :endDate AND u.user_id=:userId AND u.work_type =:workType", + nativeQuery = true + ) + fun findAllByWorkDateBetweenAndUserIdAAndWorkType(startDate: Date, endDate: Date, userId: String,workType: String?): List // TODO : Move getAllProjectIdsForUserId & getAllUserIdsForProjectId to their respective Repository (they don't belong here) @Query( diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/UserWorkService.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/UserWorkService.kt index ee0efa3..2818f78 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/UserWorkService.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/UserWorkService.kt @@ -3,13 +3,15 @@ package com.mutualmobile.praxisspringboot.services.orgs import com.mutualmobile.praxisspringboot.data.ApiResponse import com.mutualmobile.praxisspringboot.data.models.orgs.OrganizationProject import com.mutualmobile.praxisspringboot.data.models.projects.HarvestUserWork +import com.mutualmobile.praxisspringboot.data.models.projects.WorkType import java.util.Date interface UserWorkService { fun getWorkLogsForDateRange( startDate: Date, endDate: Date, - userIds: List? = null + userIds: List? = null, + workType: WorkType?=null ): ApiResponse> fun getUserAssignedProjects(userId: String): ApiResponse> diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserProjectServiceImpl.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserProjectServiceImpl.kt index 34e30e1..94dace6 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserProjectServiceImpl.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserProjectServiceImpl.kt @@ -3,6 +3,7 @@ package com.mutualmobile.praxisspringboot.services.orgs.impl import com.mutualmobile.praxisspringboot.data.ApiResponse import com.mutualmobile.praxisspringboot.data.models.orgs.OrganizationProject import com.mutualmobile.praxisspringboot.data.models.projects.HarvestUserWork +import com.mutualmobile.praxisspringboot.data.models.projects.WorkType import com.mutualmobile.praxisspringboot.data.user.HarvestUserProjectAssignment import com.mutualmobile.praxisspringboot.entities.orgs.DBOrgProjects import com.mutualmobile.praxisspringboot.entities.projects.DBUserWork @@ -46,9 +47,9 @@ class UserProjectServiceImpl : UserProjectService { Pair(projectsRepository.findByIdOrNull(projectId), userRepository.findByIdOrNull(userId)) if (!errorProjectAssignmentIds.contains(errorPair)) { - errorPair.first?.let { project-> - errorPair.second?.let { user-> - errorProjectAssignmentIds.add(Pair(project,user)) + errorPair.first?.let { project -> + errorPair.second?.let { user -> + errorProjectAssignmentIds.add(Pair(project, user)) } } @@ -127,7 +128,9 @@ fun DBUserWork.toHarvestUserWork() = HarvestUserWork( userId = userId, workDate = workDate, workHours = workHours, - workType = workType, + workType = WorkType.values().find { + it.type == workType + }?.type ?: WorkType.NONBILLABLE.type, note = note ) diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt index 1e44a0c..6d34e0a 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt @@ -3,6 +3,7 @@ package com.mutualmobile.praxisspringboot.services.orgs.impl import com.mutualmobile.praxisspringboot.data.ApiResponse import com.mutualmobile.praxisspringboot.data.models.orgs.OrganizationProject import com.mutualmobile.praxisspringboot.data.models.projects.HarvestUserWork +import com.mutualmobile.praxisspringboot.data.models.projects.WorkType import com.mutualmobile.praxisspringboot.repositories.orgs.OrgProjectsRepository import com.mutualmobile.praxisspringboot.repositories.orgs.UserWorkRepository import com.mutualmobile.praxisspringboot.services.orgs.UserWorkService @@ -21,16 +22,18 @@ class UserWorkServiceImpl : UserWorkService { override fun getWorkLogsForDateRange( startDate: Date, endDate: Date, - userIds: List? + userIds: List?, + workType: WorkType? ): ApiResponse> { return try { val listOfWork = mutableListOf() userIds?.forEach { userId -> listOfWork.addAll( - userWorkRepository.findAllByWorkDateBetweenAndUserId( + userWorkRepository.findAllByWorkDateBetweenAndUserIdAAndWorkType( startDate = startDate, endDate = endDate, - userId = userId + userId = userId, + workType = workType?.type ).map { it.toHarvestUserWork() } ) } From 9dc982d8be12d5c9764f1d5882bf18314ca62a1a Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Thu, 16 Jun 2022 13:26:46 +0530 Subject: [PATCH 2/3] Fixed `findAllByWorkDateBetweenAndUserId` not working --- .../repositories/orgs/UserWorkRepository.kt | 13 +++++++++---- .../services/orgs/impl/UserWorkServiceImpl.kt | 4 ++-- src/main/resources/data.sql | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt index 0276f01..5ace045 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt @@ -1,18 +1,22 @@ package com.mutualmobile.praxisspringboot.repositories.orgs -import com.mutualmobile.praxisspringboot.data.models.projects.WorkType import com.mutualmobile.praxisspringboot.entities.projects.DBUserWork import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query -import org.springframework.data.repository.query.Param import java.util.* interface UserWorkRepository : JpaRepository { - @Query("SELECT u FROM user_work AS u WHERE u.work_date BETWEEN :startDate AND :endDate AND u.user_id=:userId AND u.work_type =:workType", + @Query( + "SELECT * FROM user_work AS u WHERE u.work_date BETWEEN :startDate AND :endDate AND u.user_id=:userId AND u.work_type =:workType", nativeQuery = true ) - fun findAllByWorkDateBetweenAndUserIdAAndWorkType(startDate: Date, endDate: Date, userId: String,workType: String?): List + fun findAllByWorkDateBetweenAndUserId( + startDate: Date, + endDate: Date, + userId: String, + workType: String? + ): List // TODO : Move getAllProjectIdsForUserId & getAllUserIdsForProjectId to their respective Repository (they don't belong here) @Query( @@ -20,6 +24,7 @@ interface UserWorkRepository : JpaRepository { nativeQuery = true ) fun getAllProjectIdsForUserId(userId: String): List // List + @Query( value = "SELECT upa.user_id FROM user_project_assignment upa WHERE upa.project_id = :projectId", nativeQuery = true diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt index 6d34e0a..d24f68e 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt @@ -7,9 +7,9 @@ import com.mutualmobile.praxisspringboot.data.models.projects.WorkType import com.mutualmobile.praxisspringboot.repositories.orgs.OrgProjectsRepository import com.mutualmobile.praxisspringboot.repositories.orgs.UserWorkRepository import com.mutualmobile.praxisspringboot.services.orgs.UserWorkService -import java.util.Date import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service +import java.util.* @Service class UserWorkServiceImpl : UserWorkService { @@ -29,7 +29,7 @@ class UserWorkServiceImpl : UserWorkService { val listOfWork = mutableListOf() userIds?.forEach { userId -> listOfWork.addAll( - userWorkRepository.findAllByWorkDateBetweenAndUserIdAAndWorkType( + userWorkRepository.findAllByWorkDateBetweenAndUserId( startDate = startDate, endDate = endDate, userId = userId, diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 3eaf62a..5a59f25 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -165,4 +165,4 @@ create table if not exists user_work alter table user_work owner to harvestapi; -alter table user_work add column if not exists workType varchar(1) not null default '1'; +alter table user_work add column if not exists work_type varchar(1) not null default '1'; From aa7bd5539b8642b56f263d9adde1f1eef827339a Mon Sep 17 00:00:00 2001 From: nehaagrawalmm <94346484+nehaagrawalmm@users.noreply.github.com> Date: Thu, 16 Jun 2022 14:16:24 +0530 Subject: [PATCH 3/3] WHEN NO options provided for worktype show all --- .../praxisspringboot/repositories/orgs/UserWorkRepository.kt | 4 ++-- .../services/orgs/impl/UserWorkServiceImpl.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt index 5ace045..aa66587 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/repositories/orgs/UserWorkRepository.kt @@ -8,14 +8,14 @@ import java.util.* interface UserWorkRepository : JpaRepository { @Query( - "SELECT * FROM user_work AS u WHERE u.work_date BETWEEN :startDate AND :endDate AND u.user_id=:userId AND u.work_type =:workType", + "SELECT * FROM user_work AS u WHERE u.work_date BETWEEN :startDate AND :endDate AND u.user_id=:userId AND u.work_type LIKE :workType", nativeQuery = true ) fun findAllByWorkDateBetweenAndUserId( startDate: Date, endDate: Date, userId: String, - workType: String? + workType: String ): List // TODO : Move getAllProjectIdsForUserId & getAllUserIdsForProjectId to their respective Repository (they don't belong here) diff --git a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt index d24f68e..c1a3fc0 100644 --- a/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt +++ b/src/main/kotlin/com/mutualmobile/praxisspringboot/services/orgs/impl/UserWorkServiceImpl.kt @@ -33,7 +33,7 @@ class UserWorkServiceImpl : UserWorkService { startDate = startDate, endDate = endDate, userId = userId, - workType = workType?.type + workType = workType?.type?:"%" ).map { it.toHarvestUserWork() } ) }