Skip to content

Commit

Permalink
add refresh endpoint, modified create dock report routine to pull mor…
Browse files Browse the repository at this point in the history
…e info from classes (#12)

Co-authored-by: Alex Banks <[email protected]>
  • Loading branch information
alexjpbanks14 and Alex Banks authored Aug 27, 2024
1 parent a560091 commit 672bf65
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.sailcbi.APIServer.Api.Endpoints.Staff.Dockhouse.DockReport

import com.coleji.neptune.Core.{ParsedRequest, PermissionsAuthority}
import org.sailcbi.APIServer.Entities.EntityDefinitions.{DockReport, DockReportApClass}
import org.sailcbi.APIServer.Entities.dto.PutDockReportApClassDto
import org.sailcbi.APIServer.Logic.DockhouseLogic.DockReportLogic
import org.sailcbi.APIServer.UserTypes.StaffRequestCache
import play.api.libs.json.Json
import play.api.mvc.{Action, AnyContent, InjectedController}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class RefreshDockReportClasses @Inject()(implicit val exec: ExecutionContext) extends InjectedController {

def post()(implicit PA: PermissionsAuthority): Action[AnyContent] = Action.async(req => {
val parsedRequest = ParsedRequest(req)
PA.withRequestCache(StaffRequestCache)(None, parsedRequest, rc => {
Future(Ok(Json.toJson(rc.getObjectsByFilters(DockReport, List(DockReport.fields.reportDate.alias.isDateConstant(PA.now().toLocalDate)), Set(
DockReport.fields.dockReportId
)).headOption.map(dockReportToday => {
val currentClasses = DockReportLogic.getDockReportClasses(rc, dockReportToday)

DockReportLogic.refreshDockReportClasses(rc, dockReportToday, id => {
currentClasses.find(c => {
c.values.apInstanceId.get == id
}).getOrElse(new DockReportApClass())

})
}).getOrElse(List[PutDockReportApClassDto]()))))
})
})

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package org.sailcbi.APIServer.Api.Endpoints.Staff.Rest.DockReport

import com.coleji.neptune.API.RestController
import com.coleji.neptune.Core.{ParsedRequest, PermissionsAuthority, UnlockedRequestCache}
import com.coleji.neptune.Storable.StorableQuery.QueryBuilder
import org.sailcbi.APIServer.Entities.EntityDefinitions._
import org.sailcbi.APIServer.Entities.dto.{PutDockReportApClassDto, PutDockReportDto, PutDockReportHullCountDto}
import org.sailcbi.APIServer.Entities.dto.{PutDockReportDto, PutDockReportHullCountDto}
import org.sailcbi.APIServer.Logic.DockhouseLogic.DockReportLogic.refreshDockReportClasses
import org.sailcbi.APIServer.UserTypes.StaffRequestCache
import play.api.libs.json.Json
import play.api.mvc.{Action, AnyContent, InjectedController}
Expand All @@ -16,7 +16,7 @@ import scala.concurrent.{ExecutionContext, Future}
class GetDockReport @Inject()(implicit val exec: ExecutionContext) extends RestController(DockReport) with InjectedController {
def get()(implicit PA: PermissionsAuthority): Action[AnyContent] = Action.async(req => {
PA.withRequestCache(StaffRequestCache)(None, ParsedRequest(req), rc => {
val dockReport = getByFilters(rc, List(DockReport.fields.reportDate.alias.isDateConstant(LocalDate.now)), Set(
val dockReport = getByFilters(rc, List(DockReport.fields.reportDate.alias.isDateConstant(PA.now().toLocalDate)), Set(
DockReport.fields.dockReportId,
DockReport.fields.reportDate,
DockReport.fields.sunsetDatetime,
Expand Down Expand Up @@ -60,29 +60,9 @@ class GetDockReport @Inject()(implicit val exec: ExecutionContext) extends RestC
rc.commitObjectToDatabase(newDockReport)

// today's ap classes
val apClassesQb = QueryBuilder
.from(ApClassSession)
.innerJoin(ApClassInstance, ApClassInstance.fields.instanceId.alias equalsField ApClassSession.fields.instanceId.alias)
.innerJoin(ApClassFormat, ApClassFormat.fields.formatId.alias equalsField ApClassInstance.fields.formatId.alias)
.innerJoin(ApClassType, ApClassType.fields.typeId.alias equalsField ApClassFormat.fields.typeId.alias)
.where(ApClassSession.fields.sessionDatetime.alias isDateConstant LocalDate.now())
.select(List(
ApClassType.fields.typeName.alias,
ApClassInstance.fields.instanceId.alias,
ApClassSession.fields.sessionDatetime.alias
))

val apClassesQbrrs = rc.executeQueryBuilder(apClassesQb)

val dockReportClasses = apClassesQbrrs.map(qbrr => {
val dockRptClass = new DockReportApClass
dockRptClass.values.dockReportId.update(newDockReport.values.dockReportId.get)
dockRptClass.values.className.update(qbrr.getValue(ApClassType.alias)(_.typeName))
dockRptClass.values.classDatetime.update(qbrr.getValue(ApClassSession.alias)(_.sessionDatetime))
dockRptClass.values.apInstanceId.update(qbrr.getValue(ApClassInstance.alias)(_.instanceId))
rc.commitObjectToDatabase(dockRptClass)
dockRptClass.defaultAllUnsetNullableFields()
PutDockReportApClassDto(dockRptClass)
val dockReportClasses = refreshDockReportClasses(rc, newDockReport, (num) => {
new DockReportApClass()
})

// copy in svc hull counts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.sailcbi.APIServer.Logic.DockhouseLogic

import com.coleji.neptune.Core.UnlockedRequestCache
import com.coleji.neptune.Storable.StorableQuery.QueryBuilder
import org.sailcbi.APIServer.Entities.EntityDefinitions._
import org.sailcbi.APIServer.Entities.dto.PutDockReportApClassDto

import java.time.LocalDate

object DockReportLogic {
def getSubobjects(dockReport: DockReport, rc: UnlockedRequestCache): (List[DockReportWeather], List[DockReportStaff], List[DockReportUapAppt], List[DockReportHullCount], List[DockReportApClass]) ={
Expand Down Expand Up @@ -41,7 +45,12 @@ object DockReportLogic {
DockReportHullCount.fields.inService,
DockReportHullCount.fields.staffTally,
))
val apClasses = rc.getObjectsByFilters(DockReportApClass, List(DockReportApClass.fields.dockReportId.alias.equalsConstant(dockReport.values.dockReportId.get)), Set(
val apClasses = getDockReportClasses(rc, dockReport)
(weather, dockstaff, uapAppts, hullCounts, apClasses)
}

def getDockReportClasses(rc: UnlockedRequestCache, dockReport: DockReport): List[DockReportApClass] = {
rc.getObjectsByFilters(DockReportApClass, List(DockReportApClass.fields.dockReportId.alias.equalsConstant(dockReport.values.dockReportId.get)), Set(
DockReportApClass.fields.dockReportApClassId,
DockReportApClass.fields.dockReportId,
DockReportApClass.fields.apInstanceId,
Expand All @@ -51,7 +60,44 @@ object DockReportLogic {
DockReportApClass.fields.instructor,
DockReportApClass.fields.attend,
))
(weather, dockstaff, uapAppts, hullCounts, apClasses)
}

def refreshDockReportClasses(rc: UnlockedRequestCache, dockReport: DockReport, findOrMakeDockReportClass: Int => DockReportApClass): List[PutDockReportApClassDto] = {
val apClassesQb = QueryBuilder
.from(ApClassSession)
.innerJoin(ApClassInstance, ApClassInstance.fields.instanceId.alias equalsField ApClassSession.fields.instanceId.alias)
.innerJoin(ApClassFormat, ApClassFormat.fields.formatId.alias equalsField ApClassInstance.fields.formatId.alias)
.innerJoin(ApClassType, ApClassType.fields.typeId.alias equalsField ApClassFormat.fields.typeId.alias)
.outerJoin(Person.aliasOuter, Person.fields.personId.alias equalsField ApClassInstance.fields.instructorId.alias)
.where(ApClassSession.fields.sessionDatetime.alias isDateConstant LocalDate.now())
.select(List(
ApClassType.fields.typeName.alias,
ApClassInstance.fields.instanceId.alias,
ApClassSession.fields.sessionDatetime.alias,
ApClassInstance.fields.locationString.alias,
Person.fields.nameFirst.alias,
Person.fields.nameLast.alias
))

val apClassesQbrrs = rc.executeQueryBuilder(apClassesQb)

val dockReportClasses = apClassesQbrrs.map(qbrr => {
val dockRptClass = findOrMakeDockReportClass(qbrr.getValue(ApClassInstance.alias)(_.instanceId))
dockRptClass.values.dockReportId.update(dockReport.getID)
dockRptClass.values.className.update(qbrr.getValue(ApClassType.alias)(_.typeName))
dockRptClass.values.classDatetime.update(qbrr.getValue(ApClassSession.alias)(_.sessionDatetime))
dockRptClass.values.apInstanceId.update(qbrr.getValue(ApClassInstance.alias)(_.instanceId))
dockRptClass.values.location.update(qbrr.getValue(ApClassInstance.alias)(_.locationString))
dockRptClass.values.instructor.update(qbrr.getValue(Person.alias)(_.nameFirst)
.map(v => v + qbrr.getValue(Person.alias)(_.nameLast)
.map(v2 => " " + v2).getOrElse("")))
rc.commitObjectToDatabase(dockRptClass)
dockRptClass.defaultAllUnsetNullableFields()
PutDockReportApClassDto(dockRptClass)
})

dockReportClasses

}

def deleteSubobjects(dockReport: DockReport, rc: UnlockedRequestCache): Unit = {
Expand Down
3 changes: 2 additions & 1 deletion conf/routes-build/src/10_boathouse
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ DELETE /rest/signout-crew org.sailcbi.APIServer.Api.Endpoints.Staff.Rest.Sig
GET /rest/dock-report org.sailcbi.APIServer.Api.Endpoints.Staff.Rest.DockReport.GetDockReport.get()
POST /rest/dock-report org.sailcbi.APIServer.Api.Endpoints.Staff.Rest.DockReport.PutDockReport.post()

POST /staff/dockhouse/dock-report/refresh-classes org.sailcbi.APIServer.Api.Endpoints.Staff.Dockhouse.DockReport.RefreshDockReportClasses.post()

GET /rest/membership-sale org.sailcbi.APIServer.Api.Endpoints.Staff.Rest.MembershipSale.GetMembershipSale.get(calendarYear: Int)

GET /staff/user-permissions org.sailcbi.APIServer.Api.Endpoints.Staff.UserPermissions.get()
Expand All @@ -71,7 +73,6 @@ POST /auth/set-staff-pw org.sailcbi.APIServer.Api.Endpoints.Secu

# POST /staff/slack-sync-users org.sailcbi.APIServer.Api.Endpoints.Staff.slacksyncusers.SlackSyncUsers.post()


### DOCKHOUSE

#GET /staff/dockhouse/scan-card org.sailcbi.APIServer.Api.Endpoints.Staff.Dockhouse.ScanCard.ScanCard.get(cardNumber: String)
Expand Down

0 comments on commit 672bf65

Please sign in to comment.