Add routes to check calendar events

This commit is contained in:
Eike Kettner
2020-04-20 01:02:45 +02:00
parent 9b30542974
commit 3524904faf
4 changed files with 91 additions and 1 deletions

View File

@ -76,7 +76,8 @@ object RestServer {
"email/send" -> MailSendRoutes(restApp.backend, token),
"email/settings" -> MailSettingsRoutes(restApp.backend, token),
"email/sent" -> SentMailRoutes(restApp.backend, token),
"usertask/notifydueitems" -> NotifyDueItemsRoutes(restApp.backend, token)
"usertask/notifydueitems" -> NotifyDueItemsRoutes(restApp.backend, token),
"calevent/check" -> CalEventCheckRoutes()
)
def openRoutes[F[_]: Effect](cfg: Config, restApp: RestApp[F]): HttpRoutes[F] =

View File

@ -0,0 +1,40 @@
package docspell.restserver.routes
import cats.effect._
import cats.implicits._
import org.http4s._
import org.http4s.dsl.Http4sDsl
import org.http4s.circe.CirceEntityEncoder._
import org.http4s.circe.CirceEntityDecoder._
import com.github.eikek.calev.CalEvent
import docspell.common._
import docspell.restapi.model._
object CalEventCheckRoutes {
def apply[F[_]: Effect](): HttpRoutes[F] = {
val dsl = new Http4sDsl[F] {}
import dsl._
HttpRoutes.of {
case req @ POST -> Root =>
for {
data <- req.as[CalEventCheck]
res <- testEvent(data.event)
resp <- Ok(res)
} yield resp
}
}
def testEvent[F[_]: Sync](str: String): F[CalEventCheckResult] =
Timestamp.current[F].map { now =>
CalEvent.parse(str) match {
case Right(ev) =>
val next = ev.nextElapse(now.toUtcDateTime).map(Timestamp.atUtc)
CalEventCheckResult(true, "Valid.", ev.some, next)
case Left(err) =>
CalEventCheckResult(false, err, None, None)
}
}
}