Skip to content

Commit

Permalink
Merge branch 'master' into task/upgrade-play-2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
niladic authored Nov 8, 2023
2 parents 496f9f3 + 098c0b0 commit 1f402e5
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "3.7.14"
version = "3.7.15"
runner.dialect = scala213
maxColumn = 100
spaces.afterKeywordBeforeParen = true
Expand Down
7 changes: 4 additions & 3 deletions app/controllers/ApplicationController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import helper.PlayFormHelper.formErrorsLog
import helper.ScalatagsHelpers.writeableOf_Modifier
import helper.StringHelper.{CanonizeString, NonEmptyTrimmedString}
import helper.Time.zonedDateTimeOrdering
import helper.{BusinessDaysCalculator, Hash, Time, UUIDHelper}
import helper.{Hash, Time, UUIDHelper}
import helper.TwirlImports.toHtml
import models.Answer.AnswerType
import models.EventType._
Expand Down Expand Up @@ -50,6 +50,7 @@ import scala.util.{Failure, Success, Try}
@Singleton
case class ApplicationController @Inject() (
applicationService: ApplicationService,
businessDaysService: BusinessDaysService,
config: AppConfig,
eventService: EventService,
fileService: FileService,
Expand Down Expand Up @@ -586,10 +587,10 @@ case class ApplicationController @Inject() (
)
.lastOption match {
case None =>
BusinessDaysCalculator
businessDaysService
.businessHoursBetween(application.creationDate, ZonedDateTime.now()) > (3 * 24)
case Some(lastAnswer) =>
BusinessDaysCalculator
businessDaysService
.businessHoursBetween(lastAnswer.creationDate, ZonedDateTime.now()) > (15 * 24)
}
}
Expand Down
17 changes: 11 additions & 6 deletions app/helper/BusinessDaysCalculator.scala
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
package helper

import java.time.{DayOfWeek, ZonedDateTime}
import java.time.{DayOfWeek, LocalDate, ZonedDateTime}
import java.time.temporal.ChronoUnit

object BusinessDaysCalculator {

def isBusinessDay(date: ZonedDateTime): Boolean = {
def isBusinessDay(date: ZonedDateTime, holidays: Set[LocalDate]): Boolean = {
val localDate = date.toLocalDate
val dayOfWeek = date.getDayOfWeek
!Set(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY).contains(dayOfWeek)
!Set(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY).contains(dayOfWeek) && !holidays.contains(localDate)
}

def businessHoursBetween(startTime: ZonedDateTime, endTime: ZonedDateTime): Int = {
def businessHoursBetween(
startTime: ZonedDateTime,
endTime: ZonedDateTime,
holidays: Set[LocalDate] = Set.empty
): Int = {
val (start, end) =
if (endTime.isAfter(startTime)) (startTime, endTime) else (endTime, startTime)

// scalastyle:ignore var.local
var totalHours: Int = 0

if (start.truncatedTo(ChronoUnit.DAYS).isEqual(end.truncatedTo(ChronoUnit.DAYS))) {
if (isBusinessDay(start)) {
if (isBusinessDay(start, holidays)) {
totalHours = end.getHour - start.getHour
}
} else {
// scalastyle:ignore var.local
var current = start.truncatedTo(ChronoUnit.DAYS)
// scalastyle:ignore while
while (!current.isAfter(end)) {
if (isBusinessDay(current)) {
if (isBusinessDay(current, holidays)) {
if (current.isEqual(start.truncatedTo(ChronoUnit.DAYS))) {
// For the start day, count hours from start time to end of day
totalHours += 24 - start.getHour
Expand Down
2 changes: 1 addition & 1 deletion app/models/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ case class User(
newsletterAcceptationDate.map(Time.adminsFormatter.format).getOrElse("<vide>")

lazy val firstLoginDateLog: String =
firstLoginDate.map(Time.adminsFormatter.format).getOrElse("<vide>")
firstLoginDate.map(Time.formatForAdmins).getOrElse("<vide>")

lazy val phoneNumberLog: String = phoneNumber.map(withQuotes).getOrElse("<vide>")
lazy val observableOrganisationIdsLog: String = observableOrganisationIds.map(_.id).mkString(", ")
Expand Down
22 changes: 22 additions & 0 deletions app/services/BusinessDaysService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package services

import anorm._
import anorm.SqlParser.scalar
import helper.BusinessDaysCalculator
import java.time.{LocalDate, ZonedDateTime}
import javax.inject.{Inject, Singleton}
import play.api.db.Database

@Singleton
class BusinessDaysService @Inject() (db: Database) {

val publicHolidays: List[LocalDate] = db.withTransaction { implicit connection =>
SQL("SELECT holiday_date FROM public_holidays").as(scalar[LocalDate].*)
}

private val publicHolidaySet: Set[LocalDate] = publicHolidays.toSet

def businessHoursBetween(startTime: ZonedDateTime, endTime: ZonedDateTime): Int =
BusinessDaysCalculator.businessHoursBetween(startTime, endTime, publicHolidaySet)

}
2 changes: 1 addition & 1 deletion app/views/myApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object myApplications {
)(frag())(
div(
cls := "mdl-cell mdl-cell--12-col mdl-grid--no-spacing",
if (filters.allGroupsOpenCount <= 0)
if (filters.allGroupsOpenCount <= 0 && filters.allGroupsClosedCount <= 0)
noApplications(currentUser, currentUserRights)
else
openApplications(currentUser, currentUserRights, applications, groups, filters),
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ libraryDependencies ++= Seq(
"org.postgresql" % "postgresql" % "42.6.0",
anormDependency,
"com.typesafe.play" %% "play-mailer" % "9.0.0",
"com.typesafe.play" %% "play-json" % "2.10.1",
"com.typesafe.play" %% "play-json" % "2.10.3",
"com.sun.mail" % "javax.mail" % "1.6.2",
"net.jcazevedo" %% "moultingyaml" % "0.4.2",
"com.github.tototoshi" %% "scala-csv" % "1.3.10",
Expand All @@ -112,7 +112,7 @@ libraryDependencies ++= Seq(
)

// Crash
libraryDependencies += "io.sentry" % "sentry-logback" % "6.32.0"
libraryDependencies += "io.sentry" % "sentry-logback" % "6.33.1"

// Overrides
dependencyOverrides += "org.apache.commons" % "commons-text" % "1.10.0"
Expand Down
146 changes: 146 additions & 0 deletions conf/evolutions/default/69.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
-- !Ups

CREATE TABLE public_holidays (
holiday_date date PRIMARY KEY
);

-- https://www.data.gouv.fr/fr/datasets/jours-feries-en-france/
INSERT INTO public_holidays (holiday_date) VALUES
('2017-01-01'),
('2017-04-17'),
('2017-05-01'),
('2017-05-08'),
('2017-05-25'),
('2017-06-05'),
('2017-07-14'),
('2017-08-15'),
('2017-11-01'),
('2017-11-11'),
('2017-12-25'),
('2018-01-01'),
('2018-04-02'),
('2018-05-01'),
('2018-05-08'),
('2018-05-10'),
('2018-05-21'),
('2018-07-14'),
('2018-08-15'),
('2018-11-01'),
('2018-11-11'),
('2018-12-25'),
('2019-01-01'),
('2019-04-22'),
('2019-05-01'),
('2019-05-08'),
('2019-05-30'),
('2019-06-10'),
('2019-07-14'),
('2019-08-15'),
('2019-11-01'),
('2019-11-11'),
('2019-12-25'),
('2020-01-01'),
('2020-04-13'),
('2020-05-01'),
('2020-05-08'),
('2020-05-21'),
('2020-06-01'),
('2020-07-14'),
('2020-08-15'),
('2020-11-01'),
('2020-11-11'),
('2020-12-25'),
('2021-01-01'),
('2021-04-05'),
('2021-05-01'),
('2021-05-08'),
('2021-05-13'),
('2021-05-24'),
('2021-07-14'),
('2021-08-15'),
('2021-11-01'),
('2021-11-11'),
('2021-12-25'),
('2022-01-01'),
('2022-04-18'),
('2022-05-01'),
('2022-05-08'),
('2022-05-26'),
('2022-06-06'),
('2022-07-14'),
('2022-08-15'),
('2022-11-01'),
('2022-11-11'),
('2022-12-25'),
('2023-01-01'),
('2023-04-10'),
('2023-05-01'),
('2023-05-08'),
('2023-05-18'),
('2023-05-29'),
('2023-07-14'),
('2023-08-15'),
('2023-11-01'),
('2023-11-11'),
('2023-12-25'),
('2024-01-01'),
('2024-04-01'),
('2024-05-01'),
('2024-05-08'),
('2024-05-09'),
('2024-05-20'),
('2024-07-14'),
('2024-08-15'),
('2024-11-01'),
('2024-11-11'),
('2024-12-25'),
('2025-01-01'),
('2025-04-21'),
('2025-05-01'),
('2025-05-08'),
('2025-05-29'),
('2025-06-09'),
('2025-07-14'),
('2025-08-15'),
('2025-11-01'),
('2025-11-11'),
('2025-12-25'),
('2026-01-01'),
('2026-04-06'),
('2026-05-01'),
('2026-05-08'),
('2026-05-14'),
('2026-05-25'),
('2026-07-14'),
('2026-08-15'),
('2026-11-01'),
('2026-11-11'),
('2026-12-25'),
('2027-01-01'),
('2027-03-29'),
('2027-05-01'),
('2027-05-06'),
('2027-05-08'),
('2027-05-17'),
('2027-07-14'),
('2027-08-15'),
('2027-11-01'),
('2027-11-11'),
('2027-12-25'),
('2028-01-01'),
('2028-04-17'),
('2028-05-01'),
('2028-05-08'),
('2028-05-25'),
('2028-06-05'),
('2028-07-14'),
('2028-08-15'),
('2028-11-01'),
('2028-11-11'),
('2028-12-25')
;


-- !Downs

DROP TABLE public_holidays;
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.6
sbt.version=1.9.7

0 comments on commit 1f402e5

Please sign in to comment.