Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 6, 2024
1 parent 5dbca96 commit c80be6d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 49 deletions.
15 changes: 6 additions & 9 deletions api/app/db/ApplicationsDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package db

import anorm._
import io.apibuilder.api.v0.models.{AppSortBy, Application, ApplicationForm, Error, MoveForm, Organization, SortOrder, User, Version, Visibility}
import io.apibuilder.task.v0.models.TaskType
import io.apibuilder.task.v0.models.{EmailDataApplicationCreated, TaskType}
import io.flow.postgresql.Query
import lib.{UrlKey, Validation}
import play.api.db._
import processor.EmailProcessorQueue

import java.util.UUID
import javax.inject.{Inject, Named, Singleton}
import javax.inject.{Inject, Singleton}

@Singleton
class ApplicationsDao @Inject() (
@Named("main-actor") mainActor: akka.actor.ActorRef,
@NamedDatabase("default") db: Database,
emailQueue: EmailProcessorQueue,
organizationsDao: OrganizationsDao,
tasksDao: InternalTasksDao,
) {
Expand Down Expand Up @@ -263,10 +264,9 @@ class ApplicationsDao @Inject() (
"created_by_guid" -> createdBy.guid,
"updated_by_guid" -> createdBy.guid
).execute()
emailQueue.queueWithConnection(c, EmailDataApplicationCreated(guid))
})

mainActor ! actors.MainActor.Messages.ApplicationCreated(guid)

findAll(Authorization.All, orgKey = Some(org.key), key = Some(key)).headOption.getOrElse {
sys.error("Failed to create application")
}
Expand All @@ -279,10 +279,7 @@ class ApplicationsDao @Inject() (
}

def canUserUpdate(user: User, app: Application): Boolean = {
findAll(Authorization.User(user.guid), key = Some(app.key)).headOption match {
case None => false
case Some(a) => true
}
findAll(Authorization.User(user.guid), key = Some(app.key)).nonEmpty
}

private[db] def findByOrganizationAndName(authorization: Authorization, org: Organization, name: String): Option[Application] = {
Expand Down
18 changes: 10 additions & 8 deletions api/app/db/EmailVerificationsDao.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package db

import anorm.JodaParameterMetaData._
import anorm._
import io.apibuilder.api.v0.models.User
import io.apibuilder.task.v0.models.EmailDataEmailVerificationCreated
import io.flow.postgresql.Query
import lib.{Role, TokenGenerator}
import anorm._
import anorm.JodaParameterMetaData._
import javax.inject.{Inject, Named, Singleton}
import org.joda.time.DateTime
import play.api.db._
import processor.EmailProcessorQueue

import java.util.UUID
import org.joda.time.DateTime
import javax.inject.{Inject, Singleton}

case class EmailVerification(
guid: UUID,
Expand All @@ -20,8 +23,8 @@ case class EmailVerification(

@Singleton
class EmailVerificationsDao @Inject() (
@Named("main-actor") mainActor: akka.actor.ActorRef,
@NamedDatabase("default") db: Database,
emailQueue: EmailProcessorQueue,
emailVerificationConfirmationsDao: EmailVerificationConfirmationsDao,
membershipRequestsDao: MembershipRequestsDao,
organizationsDao: OrganizationsDao
Expand Down Expand Up @@ -56,7 +59,7 @@ class EmailVerificationsDao @Inject() (

def create(createdBy: User, user: User, email: String): EmailVerification = {
val guid = UUID.randomUUID
db.withConnection { implicit c =>
db.withTransaction { implicit c =>
SQL(InsertQuery).on(
"guid" -> guid,
"user_guid" -> user.guid,
Expand All @@ -65,10 +68,9 @@ class EmailVerificationsDao @Inject() (
"expires_at" -> DateTime.now.plusHours(HoursUntilTokenExpires),
"created_by_guid" -> createdBy.guid
).execute()
emailQueue.queueWithConnection(c, EmailDataEmailVerificationCreated(guid))
}

mainActor ! actors.MainActor.Messages.EmailVerificationCreated(guid)

findByGuid(guid).getOrElse {
sys.error("Failed to create email verification")
}
Expand Down
26 changes: 13 additions & 13 deletions api/app/db/MembershipRequestsDao.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package db

import anorm._
import io.apibuilder.api.v0.models.{MembershipRequest, Organization, User}
import io.apibuilder.task.v0.models.{EmailDataMembershipRequestAccepted, EmailDataMembershipRequestCreated, EmailDataMembershipRequestDeclined}
import io.flow.postgresql.Query
import lib.Role
import anorm._
import javax.inject.{Inject, Named, Singleton}
import play.api.db._
import processor.EmailProcessorQueue

import java.util.UUID
import javax.inject.{Inject, Singleton}

@Singleton
class MembershipRequestsDao @Inject() (
@Named("main-actor") mainActor: akka.actor.ActorRef,
@NamedDatabase("default") db: Database,
emailQueue: EmailProcessorQueue,
membershipsDao: MembershipsDao,
organizationLogsDao: OrganizationLogsDao
) {
Expand Down Expand Up @@ -59,18 +62,17 @@ class MembershipRequestsDao @Inject() (

private[db] def create(createdBy: User, organization: Organization, user: User, role: Role): MembershipRequest = {
val guid = UUID.randomUUID
db.withConnection { implicit c =>
db.withTransaction { implicit c =>
SQL(InsertQuery).on(
"guid" -> guid,
"organization_guid" -> organization.guid,
"user_guid" -> user.guid,
"role" -> role.key,
"created_by_guid" -> createdBy.guid
).execute()
emailQueue.queueWithConnection(c, EmailDataMembershipRequestCreated(guid))
}

mainActor ! actors.MainActor.Messages.MembershipRequestCreated(guid)

findByGuid(Authorization.All, guid).getOrElse {
sys.error("Failed to create membership_request")
}
Expand Down Expand Up @@ -101,13 +103,12 @@ class MembershipRequestsDao @Inject() (
sys.error(s"Invalid role[${request.role}]")
}

db.withTransaction { implicit conn =>
db.withTransaction { implicit c =>
organizationLogsDao.create(createdBy, request.organization, message)
dbHelpers.delete(conn, createdBy, request.guid)
dbHelpers.delete(c, createdBy, request.guid)
membershipsDao.upsert(createdBy, request.organization, request.user, r)
emailQueue.queueWithConnection(c, EmailDataMembershipRequestAccepted(request.organization.guid, request.user.guid, r.toString))
}

mainActor ! actors.MainActor.Messages.MembershipRequestAccepted(request.organization.guid, request.user.guid, r)
}

/**
Expand All @@ -121,12 +122,11 @@ class MembershipRequestsDao @Inject() (
}

val message = s"Declined membership request for ${request.user.email} to join as ${r.name}"
db.withTransaction { implicit conn =>
db.withTransaction { implicit c =>
organizationLogsDao.create(createdBy.guid, request.organization, message)
softDelete(createdBy, request)
emailQueue.queueWithConnection(c, EmailDataMembershipRequestDeclined(request.organization.guid, request.user.guid, r.toString))
}

mainActor ! actors.MainActor.Messages.MembershipRequestDeclined(request.organization.guid, request.user.guid, r)
}

private[this] def assertUserCanReview(user: User, request: MembershipRequest): Unit = {
Expand Down
15 changes: 9 additions & 6 deletions api/app/db/MembershipsDao.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package db

import anorm._
import io.apibuilder.api.v0.models.{Membership, Organization, User}
import io.apibuilder.common.v0.models.{Audit, ReferenceGuid}
import io.apibuilder.task.v0.models.EmailDataMembershipCreated
import io.flow.postgresql.Query
import lib.Role
import anorm._
import javax.inject.{Inject, Named, Singleton}
import org.joda.time.DateTime
import play.api.db._
import processor.EmailProcessorQueue

import java.util.UUID
import org.joda.time.DateTime
import javax.inject.{Inject, Singleton}

@Singleton
class MembershipsDao @Inject() (
@Named("main-actor") mainActor: akka.actor.ActorRef,
@NamedDatabase("default") db: Database,
emailQueue: EmailProcessorQueue,
subscriptionsDao: SubscriptionsDao
) {

Expand Down Expand Up @@ -67,7 +70,7 @@ class MembershipsDao @Inject() (
}

private[db] def create(createdBy: UUID, organization: Organization, user: User, role: Role): Membership = {
db.withConnection { implicit c =>
db.withTransaction { implicit c =>
create(c, createdBy, organization, user, role)
}
}
Expand All @@ -94,7 +97,7 @@ class MembershipsDao @Inject() (
"created_by_guid" -> createdBy
).execute()

mainActor ! actors.MainActor.Messages.MembershipCreated(membership.guid)
emailQueue.queueWithConnection(c, EmailDataMembershipCreated(membership.guid))

membership
}
Expand Down
23 changes: 12 additions & 11 deletions api/app/db/PasswordResetRequestsDao.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package db

import io.apibuilder.api.v0.models.User
import lib.TokenGenerator
import anorm._
import anorm.JodaParameterMetaData._
import java.util.UUID
import javax.inject.{Inject, Named, Singleton}

import play.api.db._
import anorm._
import io.apibuilder.api.v0.models.User
import io.apibuilder.task.v0.models.EmailDataPasswordResetRequestCreated
import io.flow.postgresql.Query
import lib.TokenGenerator
import org.joda.time.DateTime
import play.api.db._
import processor.EmailProcessorQueue

import java.util.UUID
import javax.inject.{Inject, Singleton}

case class PasswordReset(
guid: UUID,
Expand All @@ -20,8 +22,8 @@ case class PasswordReset(

@Singleton
class PasswordResetRequestsDao @Inject() (
@Named("main-actor") mainActor: akka.actor.ActorRef,
@NamedDatabase("default") db: Database,
emailQueue: EmailProcessorQueue,
userPasswordsDao: UserPasswordsDao,
usersDao: UsersDao
) {
Expand All @@ -48,18 +50,17 @@ class PasswordResetRequestsDao @Inject() (

def create(createdBy: Option[User], user: User): PasswordReset = {
val guid = UUID.randomUUID
db.withConnection { implicit c =>
db.withTransaction { implicit c =>
SQL(InsertQuery).on(
"guid" -> guid,
"user_guid" -> user.guid,
"token" -> TokenGenerator.generate(TokenLength),
"expires_at" -> DateTime.now.plusHours(HoursUntilTokenExpires),
"created_by_guid" -> createdBy.getOrElse(user).guid
).execute()
emailQueue.queueWithConnection(c, EmailDataPasswordResetRequestCreated(guid))
}

mainActor ! actors.MainActor.Messages.PasswordResetRequestCreated(guid)

findByGuid(guid).getOrElse {
sys.error("Failed to create password reset")
}
Expand Down
12 changes: 11 additions & 1 deletion api/app/processor/EmailProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ package processor
import actors.Emails
import cats.data.ValidatedNec
import cats.implicits._
import db.{Authorization, OrganizationsDao, UsersDao}
import db.{Authorization, InternalTasksDao, OrganizationsDao, UsersDao}
import io.apibuilder.api.v0.models.Publication
import io.apibuilder.task.v0.models._
import io.apibuilder.task.v0.models.json._
import lib.{AppConfig, EmailUtil, Person, Role}
import play.api.libs.json.Json

import java.sql.Connection
import java.util.UUID
import javax.inject.Inject

class EmailProcessorQueue @Inject() (
internalTasksDao: InternalTasksDao
) {
def queueWithConnection(c: Connection, data: EmailData): Unit = {
val dataJson = Json.toJson(data)
internalTasksDao.queue(TaskType.Email, id = Json.asciiStringify(dataJson), data = dataJson)
}
}

class EmailProcessor @Inject()(
args: TaskProcessorArgs,
Expand Down
4 changes: 3 additions & 1 deletion api/app/processor/TaskActorCompanion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class TaskActorCompanion @Inject() (
migrateVersion: MigrateVersionProcessor,
userCreated: UserCreatedProcessor,
scheduleSyncGeneratorServices: ScheduleSyncGeneratorServicesProcessor,
syncGeneratorService: SyncGeneratorServiceProcessor
syncGeneratorService: SyncGeneratorServiceProcessor,
email: EmailProcessor
) {

def process(typ: TaskType): Unit = {
Expand All @@ -30,6 +31,7 @@ class TaskActorCompanion @Inject() (
case UserCreated => userCreated
case ScheduleSyncGeneratorServices => scheduleSyncGeneratorServices
case SyncGeneratorService => syncGeneratorService
case Email => email
case UNDEFINED(_) => sys.error(s"Undefined task type '$typ")
}
}
Expand Down

0 comments on commit c80be6d

Please sign in to comment.