Skip to content

Commit

Permalink
[270] Code Event comme un enum. (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucien Pereira authored and jdauphant committed Jan 16, 2020
1 parent 042342f commit 196da6e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/UserController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import extentions.BooleanHelper.not
import extentions.Operators.{GroupOperators, UserOperators}
import extentions.{Time, UUIDHelper}
import javax.inject.{Inject, Singleton}
import models.{Area, User, UserGroup}
import models.{EventType, Area, User, UserGroup}
import org.joda.time.{DateTime, DateTimeZone}
import org.postgresql.util.PSQLException
import org.webjars.play.WebJarsUtil
Expand Down Expand Up @@ -211,7 +211,7 @@ case class UserController @Inject()(loginAction: LoginAction,
}, { Unit =>
users.foreach { user =>
notificationsService.newUser(user)
eventService.info("ADD_USER_DONE", s"Ajout de l'utilisateur ${user.name} ${user.email}", user = Some(user))
eventService.log(EventType.AddUserDone, s"Ajout de l'utilisateur ${user.name} ${user.email}", user = Some(user))
}
eventService.info("ADD_USERS_DONE", "Utilisateurs ajoutés")
Redirect(routes.GroupController.editGroup(groupId)).flashing("success" -> "Utilisateurs ajouté")
Expand Down
2 changes: 2 additions & 0 deletions app/helper/StringHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ object StringHelper {
def canonize: String =
StringUtils.stripAccents(string.toLowerCase().replaceAll("[-'’ +]", ""))
}

def camelToUnderscoresUpperCase(name: String) = "_?[A-Z][a-z\\d]+".r.findAllMatchIn(name).map(_.group(0).toLowerCase).mkString("_").toUpperCase()
}
24 changes: 24 additions & 0 deletions app/models/EventType.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package models

import helper.StringHelper

trait EventType {
val code = StringHelper.camelToUnderscoresUpperCase(this.getClass.getSimpleName)
val level: String
}

sealed trait Error extends EventType {
val level = "ERROR"
}

sealed trait Info extends EventType {
val level = "INFO"
}

sealed trait Warn extends EventType {
val level = "WARN"
}

object EventType {
object AddUserDone extends Info
}
3 changes: 3 additions & 0 deletions app/services/EventService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class EventService @Inject()(db: Database) {
"ip_address"
)

def log[A](event: EventType, description: String, application: Option[Application] = None, user: Option[User] = None)(implicit request: RequestWithUserData[A])
= register[A](event.level)(request.currentUser, request.currentArea, request.remoteAddress, event.code, description, application, user)

def info[A](code: String, description: String, application: Option[Application] = None, user: Option[User] = None)(implicit request: RequestWithUserData[A])
= register[A]("INFO")(request.currentUser, request.currentArea, request.remoteAddress, code, description, application, user)

Expand Down

0 comments on commit 196da6e

Please sign in to comment.