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..aa66587 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,22 @@ package com.mutualmobile.praxisspringboot.repositories.orgs 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 java.util.* interface UserWorkRepository : JpaRepository { - fun findAllByWorkDateBetweenAndUserId(startDate: Date, endDate: Date, userId: String): List + + @Query( + "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 + ): List // TODO : Move getAllProjectIdsForUserId & getAllUserIdsForProjectId to their respective Repository (they don't belong here) @Query( @@ -14,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/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..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 @@ -3,12 +3,13 @@ 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 -import java.util.Date import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service +import java.util.* @Service class UserWorkServiceImpl : UserWorkService { @@ -21,7 +22,8 @@ class UserWorkServiceImpl : UserWorkService { override fun getWorkLogsForDateRange( startDate: Date, endDate: Date, - userIds: List? + userIds: List?, + workType: WorkType? ): ApiResponse> { return try { val listOfWork = mutableListOf() @@ -30,7 +32,8 @@ class UserWorkServiceImpl : UserWorkService { userWorkRepository.findAllByWorkDateBetweenAndUserId( startDate = startDate, endDate = endDate, - userId = userId + userId = userId, + workType = workType?.type?:"%" ).map { it.toHarvestUserWork() } ) } 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';