Skip to content

Commit

Permalink
Separated cohortModules and modules endpoints (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommylau523 authored Nov 20, 2024
1 parent a7b37c1 commit 968edc2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.terraformation.backend.accelerator.api

import com.terraformation.backend.accelerator.db.CohortModuleStore
import com.terraformation.backend.accelerator.db.CohortStore
import com.terraformation.backend.accelerator.db.ModuleNotFoundException
import com.terraformation.backend.accelerator.model.CohortDepth
import com.terraformation.backend.accelerator.model.ExistingCohortModel
import com.terraformation.backend.accelerator.model.ModuleModel
import com.terraformation.backend.accelerator.model.NewCohortModel
import com.terraformation.backend.api.AcceleratorEndpoint
import com.terraformation.backend.api.ApiResponse200
Expand All @@ -12,15 +14,18 @@ import com.terraformation.backend.api.SimpleSuccessResponsePayload
import com.terraformation.backend.api.SuccessResponsePayload
import com.terraformation.backend.db.accelerator.CohortId
import com.terraformation.backend.db.accelerator.CohortPhase
import com.terraformation.backend.db.accelerator.EventType
import com.terraformation.backend.db.accelerator.ModuleId
import com.terraformation.backend.db.accelerator.ParticipantId
import com.terraformation.backend.db.default_schema.UserId
import com.terraformation.backend.i18n.TimeZones
import com.terraformation.backend.util.orNull
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.responses.ApiResponse
import jakarta.ws.rs.BadRequestException
import java.time.Instant
import java.time.InstantSource
import java.time.LocalDate
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -36,6 +41,7 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping("/api/v1/accelerator/cohorts")
@RestController
class CohortsController(
private val clock: InstantSource,
private val cohortModuleStore: CohortModuleStore,
private val cohortStore: CohortStore,
) {
Expand Down Expand Up @@ -100,6 +106,34 @@ class CohortsController(
return getCohort(cohortId)
}

@ApiResponse200
@ApiResponse404
@GetMapping("/{cohortId}/modules")
@Operation(summary = "List cohort modules.")
fun listCohortModules(
@PathVariable cohortId: CohortId?,
): ListCohortModulesResponsePayload {
val models = cohortModuleStore.fetch(cohortId = cohortId)
val today = LocalDate.ofInstant(clock.instant(), TimeZones.UTC)
return ListCohortModulesResponsePayload(
models.map { model -> CohortModulePayload(model, today) })
}

@ApiResponse200
@ApiResponse404
@GetMapping("/{cohortId}/modules/{moduleId}")
@Operation(summary = "Gets one cohort module.")
fun getCohortModule(
@PathVariable cohortId: CohortId?,
@PathVariable moduleId: ModuleId,
): GetCohortModuleResponsePayload {
val model =
cohortModuleStore.fetch(cohortId = cohortId, moduleId = moduleId).firstOrNull()
?: throw ModuleNotFoundException(moduleId)
val today = LocalDate.ofInstant(clock.instant(), TimeZones.UTC)
return GetCohortModuleResponsePayload(CohortModulePayload(model, today))
}

@ApiResponse200
@ApiResponse404
@Operation(
Expand Down Expand Up @@ -187,6 +221,43 @@ data class UpdateCohortRequestPayload(
}
}

data class CohortModulePayload(
val id: ModuleId,
val title: String,
val name: String,
val startDate: LocalDate,
val endDate: LocalDate,
val isActive: Boolean,
val additionalResources: String?,
val overview: String?,
val preparationMaterials: String?,
val eventDescriptions: Map<EventType, String>,
) {
constructor(
model: ModuleModel,
today: LocalDate,
) : this(
id = model.id,
title = model.title!!,
name = model.name,
startDate = model.startDate!!,
endDate = model.endDate!!,
isActive = today in model.startDate..model.endDate,
additionalResources = model.additionalResources,
overview = model.overview,
preparationMaterials = model.preparationMaterials,
eventDescriptions = model.eventDescriptions,
)
}

data class CohortResponsePayload(val cohort: CohortPayload) : SuccessResponsePayload

data class CohortListResponsePayload(val cohorts: List<CohortPayload>) : SuccessResponsePayload

data class GetCohortModuleResponsePayload(
val module: CohortModulePayload,
) : SuccessResponsePayload

data class ListCohortModulesResponsePayload(
val modules: List<CohortModulePayload>,
) : SuccessResponsePayload
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.terraformation.backend.accelerator.api

import com.terraformation.backend.accelerator.db.CohortModuleStore
import com.terraformation.backend.accelerator.db.DeliverableStore
import com.terraformation.backend.accelerator.db.ModuleNotFoundException
import com.terraformation.backend.accelerator.db.ModuleStore
import com.terraformation.backend.accelerator.db.ModulesImporter
import com.terraformation.backend.accelerator.model.ModuleDeliverableModel
import com.terraformation.backend.accelerator.model.ModuleModel
Expand All @@ -12,28 +11,21 @@ import com.terraformation.backend.api.ApiResponse404
import com.terraformation.backend.api.ResponsePayload
import com.terraformation.backend.api.SuccessOrError
import com.terraformation.backend.api.SuccessResponsePayload
import com.terraformation.backend.db.accelerator.CohortId
import com.terraformation.backend.db.accelerator.DeliverableCategory
import com.terraformation.backend.db.accelerator.DeliverableId
import com.terraformation.backend.db.accelerator.DeliverableType
import com.terraformation.backend.db.accelerator.EventType
import com.terraformation.backend.db.accelerator.ModuleId
import com.terraformation.backend.db.accelerator.ParticipantId
import com.terraformation.backend.db.default_schema.ProjectId
import com.terraformation.backend.i18n.TimeZones
import com.terraformation.backend.importer.CsvImportFailedException
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Encoding
import io.swagger.v3.oas.annotations.media.Schema
import java.time.InstantSource
import java.time.LocalDate
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RequestPart
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
Expand All @@ -42,58 +34,34 @@ import org.springframework.web.multipart.MultipartFile
@RequestMapping("/api/v1/accelerator/modules")
@RestController
class ModulesController(
private val clock: InstantSource,
private val cohortModuleStore: CohortModuleStore,
private val deliverableStore: DeliverableStore,
private val moduleStore: ModuleStore,
private val modulesImporter: ModulesImporter,
) {
@ApiResponse200
@ApiResponse404
@GetMapping
@Operation(summary = "List modules.")
fun listModules(
@RequestParam projectId: ProjectId?,
@RequestParam participantId: ParticipantId?,
@RequestParam cohortId: CohortId?,
): ListModulesResponsePayload {
val models =
cohortModuleStore.fetch(
projectId = projectId, participantId = participantId, cohortId = cohortId)
val today = LocalDate.ofInstant(clock.instant(), TimeZones.UTC)
return ListModulesResponsePayload(models.map { model -> ModulePayload(model, today) })
fun listModules(): ListModulesResponsePayload {
val modules = moduleStore.fetchAllModules()
return ListModulesResponsePayload(
modules.map { module ->
val deliverables = deliverableStore.fetchDeliverables(moduleId = module.id)
ModulePayload(module, deliverables.map { ModuleDeliverablePayload(it) })
})
}

@ApiResponse200
@ApiResponse404
@GetMapping("/{moduleId}")
@Operation(summary = "Gets one module.")
fun getModule(
@RequestParam projectId: ProjectId?,
@RequestParam participantId: ParticipantId?,
@RequestParam cohortId: CohortId?,
@PathVariable moduleId: ModuleId,
): GetModuleResponsePayload {
val model =
cohortModuleStore
.fetch(
projectId = projectId,
participantId = participantId,
cohortId = cohortId,
moduleId = moduleId)
.firstOrNull() ?: throw ModuleNotFoundException(moduleId)
val today = LocalDate.ofInstant(clock.instant(), TimeZones.UTC)
return GetModuleResponsePayload(ModulePayload(model, today))
}

@ApiResponse200
@ApiResponse404
@GetMapping("/{moduleId}/deliverables")
@Operation(summary = "List module deliverables.")
fun listModuleDeliverables(
@PathVariable moduleId: ModuleId,
): ListModuleDeliverablesResponsePayload {
val model = moduleStore.fetchOneById(moduleId)
val deliverables = deliverableStore.fetchDeliverables(moduleId = moduleId)
return ListModuleDeliverablesResponsePayload(deliverables.map { ModuleDeliverablePayload(it) })
return GetModuleResponsePayload(
ModulePayload(model, deliverables.map { ModuleDeliverablePayload(it) }))
}

@ApiResponse200
Expand All @@ -118,30 +86,24 @@ class ModulesController(

data class ModulePayload(
val id: ModuleId,
val title: String,
val name: String,
val startDate: LocalDate,
val endDate: LocalDate,
val isActive: Boolean,
val additionalResources: String?,
val overview: String?,
val preparationMaterials: String?,
val eventDescriptions: Map<EventType, String>,
val deliverables: List<ModuleDeliverablePayload>,
) {
constructor(
model: ModuleModel,
today: LocalDate,
deliverables: List<ModuleDeliverablePayload>,
) : this(
id = model.id,
title = model.title!!,
name = model.name,
startDate = model.startDate!!,
endDate = model.endDate!!,
isActive = today in model.startDate..model.endDate,
additionalResources = model.additionalResources,
overview = model.overview,
preparationMaterials = model.preparationMaterials,
eventDescriptions = model.eventDescriptions,
deliverables = deliverables,
)
}

Expand Down Expand Up @@ -180,8 +142,6 @@ data class ImportModuleResponsePayload(
val message: String? = null,
) : ResponsePayload

data class ListModuleDeliverablesResponsePayload(val deliverables: List<ModuleDeliverablePayload>)

data class GetModuleResponsePayload(
val module: ModulePayload,
) : SuccessResponsePayload
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.terraformation.backend.accelerator.db

import com.terraformation.backend.accelerator.model.ModuleModel
import com.terraformation.backend.auth.currentUser
import com.terraformation.backend.customer.model.requirePermissions
import com.terraformation.backend.db.accelerator.CohortPhase
import com.terraformation.backend.db.accelerator.ModuleId
Expand All @@ -14,13 +15,10 @@ class ModuleStore(
private val dslContext: DSLContext,
) {
fun fetchOneById(moduleId: ModuleId): ModuleModel {
requirePermissions { readModule(moduleId) }

return fetch(MODULES.ID.eq(moduleId)).firstOrNull() ?: throw ModuleNotFoundException(moduleId)
}

fun fetchAllModules(): List<ModuleModel> {
requirePermissions { manageModules() }
return fetch()
}

Expand All @@ -42,6 +40,7 @@ class ModuleStore(
.apply { condition?.let { where(it) } }
.orderBy(ID)
.fetch { ModuleModel.of(it) }
.filter { currentUser().canReadModule(it.id) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.security.access.AccessDeniedException

class ModuleStoreTest : DatabaseTest(), RunsAsUser {
override val user = mockUser()
Expand Down Expand Up @@ -119,14 +118,13 @@ class ModuleStoreTest : DatabaseTest(), RunsAsUser {
}

@Test
fun `throws exception if no permission to manage modules`() {
every { user.canManageModules() } returns false

assertThrows<AccessDeniedException> { store.fetchAllModules() }
fun `returns empty list of module if no permission to read any module`() {
every { user.canReadModule(any()) } returns false
assertEquals(emptyList<ModuleModel>(), store.fetchAllModules())
}

@Test
fun `returns list of modules with details`() {
fun `returns list of visible modules with details`() {
val moduleId =
insertModule(
additionalResources = "<b> Additional Resources </b>",
Expand All @@ -139,6 +137,15 @@ class ModuleStoreTest : DatabaseTest(), RunsAsUser {
workshopDescription = "Workshop ideas",
)

val invisibleModuleId =
insertModule(
additionalResources = "<b> Invisible Additional Resources </b>",
liveSessionDescription = "Invisible Live session lectures",
name = "Invisible Module",
)

every { user.canReadModule(invisibleModuleId) } returns false

assertEquals(
listOf(
ModuleModel(
Expand Down

0 comments on commit 968edc2

Please sign in to comment.