From 672bf65deb58fa019ae090ec81039c5a9591417a Mon Sep 17 00:00:00 2001 From: alexjpbanks14 Date: Tue, 27 Aug 2024 19:34:56 -0400 Subject: [PATCH] add refresh endpoint, modified create dock report routine to pull more info from classes (#12) Co-authored-by: Alex Banks --- .../DockReport/RefreshDockReportClasses.scala | 34 +++++++++++++ .../Staff/Rest/DockReport/GetDockReport.scala | 30 ++--------- .../DockhouseLogic/DockReportLogic.scala | 50 ++++++++++++++++++- conf/routes-build/src/10_boathouse | 3 +- 4 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 app/org/sailcbi/APIServer/Api/Endpoints/Staff/Dockhouse/DockReport/RefreshDockReportClasses.scala diff --git a/app/org/sailcbi/APIServer/Api/Endpoints/Staff/Dockhouse/DockReport/RefreshDockReportClasses.scala b/app/org/sailcbi/APIServer/Api/Endpoints/Staff/Dockhouse/DockReport/RefreshDockReportClasses.scala new file mode 100644 index 00000000..d4189b81 --- /dev/null +++ b/app/org/sailcbi/APIServer/Api/Endpoints/Staff/Dockhouse/DockReport/RefreshDockReportClasses.scala @@ -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]())))) + }) + }) + +} diff --git a/app/org/sailcbi/APIServer/Api/Endpoints/Staff/Rest/DockReport/GetDockReport.scala b/app/org/sailcbi/APIServer/Api/Endpoints/Staff/Rest/DockReport/GetDockReport.scala index a8f9c05f..fdecb7b1 100644 --- a/app/org/sailcbi/APIServer/Api/Endpoints/Staff/Rest/DockReport/GetDockReport.scala +++ b/app/org/sailcbi/APIServer/Api/Endpoints/Staff/Rest/DockReport/GetDockReport.scala @@ -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} @@ -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, @@ -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 diff --git a/app/org/sailcbi/APIServer/Logic/DockhouseLogic/DockReportLogic.scala b/app/org/sailcbi/APIServer/Logic/DockhouseLogic/DockReportLogic.scala index d1889c55..b304bb0e 100644 --- a/app/org/sailcbi/APIServer/Logic/DockhouseLogic/DockReportLogic.scala +++ b/app/org/sailcbi/APIServer/Logic/DockhouseLogic/DockReportLogic.scala @@ -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]) ={ @@ -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, @@ -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 = { diff --git a/conf/routes-build/src/10_boathouse b/conf/routes-build/src/10_boathouse index 7525fc55..12ed76ea 100644 --- a/conf/routes-build/src/10_boathouse +++ b/conf/routes-build/src/10_boathouse @@ -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() @@ -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)