Skip to content

Commit

Permalink
Feedback dao+service updated removing unnecessary company id from dat…
Browse files Browse the repository at this point in the history
…a classes (#48)

* Update Response dao - removing unnecessary company id from data classes

* Update Response service - adding company id as another parameter

* Update Response unit tests
  • Loading branch information
RonAzar authored Sep 26, 2024
1 parent 97435d9 commit c3776f7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class FeedbackDao @Autowired constructor(private val sql: DSLContext) {
)
}

fun getFeedbacksUsingFilter(filter: FeedbackFilter): List<FeedbackData> {
var allFeedbacksByCompanyId = selectFeedbacksByCompanyId(filter.companyId)
fun getFeedbacksUsingFilter(filter: FeedbackFilter, companyId: Long): List<FeedbackData> {
var allFeedbacksByCompanyId = selectFeedbacksByCompanyId(companyId)

filter.isAnonymous?.let {
allFeedbacksByCompanyId = allFeedbacksByCompanyId.and(feedback.isAnonymous.eq(it))
Expand All @@ -49,11 +49,11 @@ class FeedbackDao @Autowired constructor(private val sql: DSLContext) {
?: throw IllegalArgumentException("Feedback not found: Incorrect feedbackId provided")
}

fun updateFeedbackStatus(updateFeedback: UpdateFeedbackStatus): Int {
fun updateFeedbackStatus(updateFeedback: UpdateFeedbackStatus, companyId: Long): Int {
val rowsAffected = sql.update(feedback)
.set(feedback.status, updateFeedback.status)
.where(feedback.id.eq(updateFeedback.feedbackId))
.and(feedback.companyId.eq(updateFeedback.companyId))
.and(feedback.companyId.eq(companyId))
.execute()

return rowsAffected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.hibob.academy.employee_feedback_feature.dao

import java.time.LocalDateTime

data class UpdateFeedbackStatus(val companyId: Long, val feedbackId: Long, val status: Boolean)
data class UpdateFeedbackStatus(val feedbackId: Long, val status: Boolean)
data class SearchedFeedback(val companyId: Long, val feedbackId: Long)
data class FeedbackFilter(val companyId: Long, val createdAt: LocalDateTime?, val department: String?, val isAnonymous: Boolean?)
data class FeedbackFilter(val createdAt: LocalDateTime?, val department: String?, val isAnonymous: Boolean?)
data class FeedbackRequest(val feedbackText: String, val isAnonymous: Boolean, val department: String)
data class FeedbackSubmission(val employeeId: Long?, val companyId: Long, val feedbackText: String, val isAnonymous: Boolean, val department: String)
data class FeedbackData(val feedbackId: Long, val employeeId: Long?, val companyId: Long, val feedbackText: String, val isAnonymous: Boolean, val department: String, val createdAt: LocalDateTime, val status: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class FeedbackService @Autowired constructor(private val feedbackDao: FeedbackDa
return feedbackDao.getAllFeedbacks(companyId)
}

fun getFeedbacksUsingFilter(filter: FeedbackFilter): List<FeedbackData> {
return feedbackDao.getFeedbacksUsingFilter(filter)
fun getFeedbacksUsingFilter(filter: FeedbackFilter, companyId: Long): List<FeedbackData> {
return feedbackDao.getFeedbacksUsingFilter(filter, companyId)
}

fun getFeedbackStatus(searchedFeedback: SearchedFeedback): Boolean {
return feedbackDao.getFeedbackStatus(searchedFeedback).status
}

fun updateFeedbackStatus(updateFeedback: UpdateFeedbackStatus): String {
val rowsAffected = feedbackDao.updateFeedbackStatus(updateFeedback)
fun updateFeedbackStatus(updateFeedback: UpdateFeedbackStatus, companyId: Long): String {
val rowsAffected = feedbackDao.updateFeedbackStatus(updateFeedback, companyId)
val result = if (rowsAffected > 0) {
"Feedback status updated!"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class FeedbackDaoTest @Autowired constructor(private val sql: DSLContext) {
dao.submitFeedback(testFeedbackSales)
dao.submitFeedback(testFeedbackHR)

val filter = FeedbackFilter(companyId = companyId, isAnonymous = true, createdAt = null, department = null)
val feedbacks = dao.getFeedbacksUsingFilter(filter)
val filter = FeedbackFilter(isAnonymous = true, createdAt = null, department = null)
val feedbacks = dao.getFeedbacksUsingFilter(filter, companyId)

assertNotNull(feedbacks)
assertEquals(1, feedbacks.size)
Expand All @@ -47,8 +47,8 @@ class FeedbackDaoTest @Autowired constructor(private val sql: DSLContext) {
dao.submitFeedback(testFeedbackSales)
dao.submitFeedback(testFeedbackHR)

val filter = FeedbackFilter(companyId, isAnonymous = null, createdAt = null, department = departmentSales)
val feedbacks = dao.getFeedbacksUsingFilter(filter)
val filter = FeedbackFilter(isAnonymous = null, createdAt = null, department = departmentSales)
val feedbacks = dao.getFeedbacksUsingFilter(filter, companyId)

assertNotNull(feedbacks)
assertEquals(1, feedbacks.size)
Expand All @@ -61,8 +61,8 @@ class FeedbackDaoTest @Autowired constructor(private val sql: DSLContext) {
dao.submitFeedback(testFeedbackHR)

val filter =
FeedbackFilter(companyId, createdAt = createdAt.minusDays(1), department = null, isAnonymous = null)
val feedbacks = dao.getFeedbacksUsingFilter(filter)
FeedbackFilter(createdAt = createdAt.minusDays(1), department = null, isAnonymous = null)
val feedbacks = dao.getFeedbacksUsingFilter(filter, companyId)

assertNotNull(feedbacks)
assertEquals(2, feedbacks.size)
Expand Down Expand Up @@ -94,8 +94,8 @@ class FeedbackDaoTest @Autowired constructor(private val sql: DSLContext) {
fun `Update feedback status - success`() {
val submittedFeedbackId = dao.submitFeedback(testFeedback)

val updateFeedback = UpdateFeedbackStatus(companyId, submittedFeedbackId, status = false)
val rowsAffected = dao.updateFeedbackStatus(updateFeedback)
val updateFeedback = UpdateFeedbackStatus(submittedFeedbackId, status = false)
val rowsAffected = dao.updateFeedbackStatus(updateFeedback, companyId)

assertEquals(1, rowsAffected)

Expand All @@ -105,8 +105,8 @@ class FeedbackDaoTest @Autowired constructor(private val sql: DSLContext) {

@Test
fun `Update feedback status - feedback does not exist`() {
val updateFeedback = UpdateFeedbackStatus(companyId, -1L, status = true)
val rowsAffected = dao.updateFeedbackStatus(updateFeedback)
val updateFeedback = UpdateFeedbackStatus(-1L, status = true)
val rowsAffected = dao.updateFeedbackStatus(updateFeedback, companyId)

assertEquals(0, rowsAffected)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class FeedbackServiceTest {
@Test
fun `getFeedbacksUsingFilter should return feedbacks when filters are applied`() {
val feedbackFilter = FeedbackFilter(
companyId = companyId,
isAnonymous = true,
department = "Sales",
createdAt = LocalDateTime.now().minusDays(1)
Expand All @@ -32,9 +31,9 @@ class FeedbackServiceTest {
FeedbackData(1L, employeeId, companyId, feedbackText, true, department, LocalDateTime.now(), true)
)

whenever(feedbackDao.getFeedbacksUsingFilter(feedbackFilter)).thenReturn(expectedFeedbacks)
whenever(feedbackDao.getFeedbacksUsingFilter(feedbackFilter, companyId)).thenReturn(expectedFeedbacks)

val result = feedbackService.getFeedbacksUsingFilter(feedbackFilter)
val result = feedbackService.getFeedbacksUsingFilter(feedbackFilter, companyId)

assertEquals(expectedFeedbacks, result)
}
Expand Down Expand Up @@ -66,22 +65,22 @@ class FeedbackServiceTest {

@Test
fun `updateFeedbackStatus should return success message when status is updated`() {
val updateFeedbackStatus = UpdateFeedbackStatus(companyId = companyId, feedbackId = 1L, status = true)
val updateFeedbackStatus = UpdateFeedbackStatus(feedbackId = 1L, status = true)

whenever(feedbackDao.updateFeedbackStatus(updateFeedbackStatus)).thenReturn(1)
whenever(feedbackDao.updateFeedbackStatus(updateFeedbackStatus, companyId)).thenReturn(1)

val result = feedbackService.updateFeedbackStatus(updateFeedbackStatus)
val result = feedbackService.updateFeedbackStatus(updateFeedbackStatus, companyId)

assertEquals("Feedback status updated!", result)
}

@Test
fun `updateFeedbackStatus should return failure message when status is not updated`() {
val updateFeedbackStatus = UpdateFeedbackStatus(companyId = companyId, feedbackId = 1L, status = true)
val updateFeedbackStatus = UpdateFeedbackStatus(feedbackId = 1L, status = true)

whenever(feedbackDao.updateFeedbackStatus(updateFeedbackStatus)).thenReturn(0)
whenever(feedbackDao.updateFeedbackStatus(updateFeedbackStatus, companyId)).thenReturn(0)

val result = feedbackService.updateFeedbackStatus(updateFeedbackStatus)
val result = feedbackService.updateFeedbackStatus(updateFeedbackStatus, companyId)

assertEquals("Feedback status not updated!", result)
}
Expand Down

0 comments on commit c3776f7

Please sign in to comment.