1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.user
21 
22 import org.make.api.extensions.MakeDBExecutionContextComponent
23 import org.make.api.technical.DatabaseTransactions.RichDatabase
24 import org.make.api.technical.ShortenedNames
25 import org.make.api.user.PersistentCrmSynchroUserService.{CrmSynchroUser, MakeUser}
26 import org.make.core.technical.Pagination
27 import scalikejdbc._
28 import org.make.api.technical.ScalikeSupport._
29 
30 import scala.concurrent.Future
31 
32 trait DefaultPersistentCrmSynchroUserServiceComponent extends PersistentCrmSynchroUserServiceComponent {
33   this: MakeDBExecutionContextComponent =>
34 
35   override lazy val persistentCrmSynchroUserService: PersistentCrmSynchroUserService =
36     new DefaultPersistentCrmSynchroUserService
37 
38   final class DefaultPersistentCrmSynchroUserService extends PersistentCrmSynchroUserService with ShortenedNames {
39 
40     private val synchroUser = SQLSyntaxSupportFactory[MakeUser]()
41     private val u = synchroUser.syntax
42 
43     override def findUsersForCrmSynchro(
44       optIn: Option[Boolean],
45       hardBounce: Option[Boolean],
46       offset: Pagination.Offset,
47       limit: Pagination.Limit
48     ): Future[Seq[CrmSynchroUser]] = {
49       implicit val cxt: EC = readExecutionContext
50       Future(NamedDB("READ").retryableTx { implicit session =>
51         withSQL {
52           select
53             .from(synchroUser.as(u))
54             .where(
55               sqls.toAndConditionOpt(
56                 hardBounce.map(sqls.eq(u.isHardBounce, _)),
57                 optIn.map(sqls.eq(u.optInNewsletter, _)),
58                 Some(sqls.notLike(u.email, "yopmail+%@make.org")),
59                 Some(sqls.notLike(u.email, "%@example.com"))
60               )
61             )
62             .orderBy(u.createdAt.asc, u.email.asc)
63             .limit(limit.extractInt)
64             .offset(offset.extractInt)
65         }.map(synchroUser.apply(u.resultName)).list()
66       })
67     }
68   }
69 }
Line Stmt Id Pos Tree Symbol Tests Code
41 20405 1657 - 1675 Select scalikejdbc.SQLSyntaxSupportFeature.SQLSyntaxSupport.syntax org.scalatest.testsuite DefaultPersistentCrmSynchroUserService.this.synchroUser.syntax
49 21977 1914 - 1934 Select org.make.api.extensions.MakeDBExecutionContextComponent.readExecutionContext org.scalatest.testsuite DefaultPersistentCrmSynchroUserServiceComponent.this.readExecutionContext
50 20901 1941 - 2593 ApplyToImplicitArgs scala.concurrent.Future.apply org.scalatest.testsuite scala.concurrent.Future.apply[List[org.make.api.user.PersistentCrmSynchroUserService.MakeUser]]({ <artifact> val qual$1: org.make.api.technical.DatabaseTransactions.RichDatabase = org.make.api.technical.DatabaseTransactions.RichDatabase({ <artifact> val x$1: String("READ") = "READ"; <artifact> val x$2: scalikejdbc.SettingsProvider = scalikejdbc.NamedDB.apply$default$2; <artifact> val x$3: scalikejdbc.ConnectionPoolContext = scalikejdbc.NamedDB.apply$default$3("READ", x$2); scalikejdbc.NamedDB.apply("READ", x$2)(x$3) }); <artifact> val x$7: scalikejdbc.DBSession => List[org.make.api.user.PersistentCrmSynchroUserService.MakeUser] @scala.reflect.internal.annotations.uncheckedBounds = ((implicit session: scalikejdbc.DBSession) => { <synthetic> <stable> <artifact> val stabilizer$1: scalikejdbc.SQLToList[org.make.api.user.PersistentCrmSynchroUserService.MakeUser,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = scalikejdbc.`package`.withSQL.apply[Nothing](scalikejdbc.`package`.select.from[Nothing](DefaultPersistentCrmSynchroUserService.this.synchroUser.as(DefaultPersistentCrmSynchroUserService.this.u)).where(scalikejdbc.`package`.sqls.toAndConditionOpt(hardBounce.map[scalikejdbc.interpolation.SQLSyntax](((x$1: Boolean) => scalikejdbc.`package`.sqls.eq[Boolean]((DefaultPersistentCrmSynchroUserService.this.u.field("isHardBounce"): scalikejdbc.interpolation.SQLSyntax), x$1)(scalikejdbc.this.ParameterBinderFactory.booleanParameterBinderFactory))), optIn.map[scalikejdbc.interpolation.SQLSyntax](((x$2: Boolean) => scalikejdbc.`package`.sqls.eq[Boolean]((DefaultPersistentCrmSynchroUserService.this.u.field("optInNewsletter"): scalikejdbc.interpolation.SQLSyntax), x$2)(scalikejdbc.this.ParameterBinderFactory.booleanParameterBinderFactory))), scala.Some.apply[scalikejdbc.interpolation.SQLSyntax](scalikejdbc.`package`.sqls.notLike((DefaultPersistentCrmSynchroUserService.this.u.field("email"): scalikejdbc.interpolation.SQLSyntax), "yopmail+%@make.org")), scala.Some.apply[scalikejdbc.interpolation.SQLSyntax](scalikejdbc.`package`.sqls.notLike((DefaultPersistentCrmSynchroUserService.this.u.field("email"): scalikejdbc.interpolation.SQLSyntax), "%@example.com")))).orderBy((DefaultPersistentCrmSynchroUserService.this.u.field("createdAt"): scalikejdbc.interpolation.SQLSyntax).asc, (DefaultPersistentCrmSynchroUserService.this.u.field("email"): scalikejdbc.interpolation.SQLSyntax).asc).limit(limit.extractInt).offset(offset.extractInt)).map[org.make.api.user.PersistentCrmSynchroUserService.MakeUser](((rs: scalikejdbc.WrappedResultSet) => DefaultPersistentCrmSynchroUserService.this.synchroUser.apply(DefaultPersistentCrmSynchroUserService.this.u.resultName)(rs))).list; { <artifact> val x$4: scalikejdbc.DBSession = session; <artifact> val x$5: scalikejdbc.SQL[org.make.api.user.PersistentCrmSynchroUserService.MakeUser,scalikejdbc.HasExtractor] =:= scalikejdbc.SQL[org.make.api.user.PersistentCrmSynchroUserService.MakeUser,scalikejdbc.HasExtractor] @scala.reflect.internal.annotations.uncheckedBounds = GeneralizedTypeConstraintsForWithExtractor.this.=:=.tpEquals[scalikejdbc.SQL[org.make.api.user.PersistentCrmSynchroUserService.MakeUser,scalikejdbc.HasExtractor]]; <artifact> val x$6: scalikejdbc.ConnectionPoolContext = stabilizer$1.apply$default$2(); stabilizer$1.apply()(x$4, x$6, x$5) } }); <artifact> val x$8: scalikejdbc.TxBoundary[List[org.make.api.user.PersistentCrmSynchroUserService.MakeUser]] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.retryableTx$default$2[List[org.make.api.user.PersistentCrmSynchroUserService.MakeUser]](x$7); qual$1.retryableTx[List[org.make.api.user.PersistentCrmSynchroUserService.MakeUser]](x$7)(x$8) })(cxt)