1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2020 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.technical.graphql
21 
22 import java.time.ZonedDateTime
23 import cats.Id
24 import cats.data.NonEmptyList
25 import org.make.api.operation.{OperationOfQuestionServiceComponent, SearchOperationsOfQuestions}
26 import org.make.api.question.{QuestionServiceComponent, SimpleQuestionWordingResponse}
27 import org.make.core.question.QuestionId
28 import org.make.core.technical.MultilingualType
29 import org.make.core.reference.{Country, Language}
30 import zio.query.DataSource
31 
32 import scala.concurrent.{ExecutionContext, Future}
33 
34 trait GraphQLQuestionServiceComponent {
35   def questionDataSource: DataSource[Any, GetQuestion]
36 }
37 
38 trait DefaultGraphQLQuestionServiceComponent extends GraphQLQuestionServiceComponent {
39   this: QuestionServiceComponent with OperationOfQuestionServiceComponent =>
40 
41   override val questionDataSource: DataSource[Any, GetQuestion] = {
42     def findFromIds(
43       questionIds: Seq[QuestionId]
44     )(executionContext: ExecutionContext): Future[Map[QuestionId, GraphQLQuestion]] = {
45       implicit val ec: ExecutionContext = executionContext
46       for {
47         questions <- questionService.getQuestions(questionIds)
48         ooqs      <- operationOfQuestionService.find(request = SearchOperationsOfQuestions(questionIds = Some(questionIds)))
49       } yield {
50         val questionMap = questions.map(q => q.questionId -> q).toMap
51         val ooqMap = ooqs.map(ooq         => ooq.questionId -> ooq).toMap
52         questionMap.keys.toSeq.flatMap { questionId =>
53           for {
54             question <- questionMap.get(questionId)
55             ooq      <- ooqMap.get(questionId)
56           } yield questionId -> GraphQLQuestion(
57             questionId = questionId,
58             slug = question.slug,
59             wording = SimpleQuestionWordingResponse(
60               title = ooq.operationTitles.getTranslationUnsafe(question.defaultLanguage),
61               question = question.questions.getTranslationUnsafe(question.defaultLanguage)
62             ),
63             countries = question.countries,
64             defaultLanguage = question.defaultLanguage,
65             languages = question.languages,
66             startDate = ooq.startDate,
67             endDate = ooq.endDate
68           )
69         }.toMap
70       }
71     }
72 
73     DataSourceHelper.one("questions-datasource", findFromIds)
74   }
75 }
76 final case class GetQuestion(ids: QuestionId) extends IdsRequest[Id, QuestionId, GraphQLQuestion]
77 
78 final case class GraphQLQuestion(
79   questionId: QuestionId,
80   slug: String,
81   wording: SimpleQuestionWordingResponse,
82   countries: NonEmptyList[Country],
83   defaultLanguage: Language,
84   languages: NonEmptyList[Language],
85   startDate: ZonedDateTime,
86   endDate: ZonedDateTime
87 ) extends MultilingualType
Line Stmt Id Pos Tree Symbol Tests Code
47 45929 1806 - 2951 ApplyToImplicitArgs scala.concurrent.Future.flatMap DefaultGraphQLQuestionServiceComponent.this.questionService.getQuestions(questionIds).flatMap[scala.collection.immutable.Map[org.make.core.question.QuestionId,org.make.api.technical.graphql.GraphQLQuestion]](((questions: Seq[org.make.core.question.Question]) => { <artifact> val qual$1: org.make.api.operation.OperationOfQuestionService = DefaultGraphQLQuestionServiceComponent.this.operationOfQuestionService; <artifact> val x$1: org.make.api.operation.SearchOperationsOfQuestions = org.make.api.operation.SearchOperationsOfQuestions.apply(scala.Some.apply[Seq[org.make.core.question.QuestionId]](questionIds), org.make.api.operation.SearchOperationsOfQuestions.apply$default$2, org.make.api.operation.SearchOperationsOfQuestions.apply$default$3, org.make.api.operation.SearchOperationsOfQuestions.apply$default$4, org.make.api.operation.SearchOperationsOfQuestions.apply$default$5, org.make.api.operation.SearchOperationsOfQuestions.apply$default$6); <artifact> val x$2: org.make.core.technical.Pagination.Offset = qual$1.find$default$1; <artifact> val x$3: Option[org.make.core.technical.Pagination.End] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.find$default$2; <artifact> val x$4: Option[String] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.find$default$3; <artifact> val x$5: Option[org.make.core.Order] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.find$default$4; qual$1.find(x$2, x$3, x$4, x$5, x$1) }.map[scala.collection.immutable.Map[org.make.core.question.QuestionId,org.make.api.technical.graphql.GraphQLQuestion]](((ooqs: Seq[org.make.core.operation.OperationOfQuestion]) => { val questionMap: scala.collection.immutable.Map[org.make.core.question.QuestionId,org.make.core.question.Question] = questions.map[(org.make.core.question.QuestionId, org.make.core.question.Question)](((q: org.make.core.question.Question) => scala.Predef.ArrowAssoc[org.make.core.question.QuestionId](q.questionId).->[org.make.core.question.Question](q))).toMap[org.make.core.question.QuestionId, org.make.core.question.Question](scala.this.<:<.refl[(org.make.core.question.QuestionId, org.make.core.question.Question)]); val ooqMap: scala.collection.immutable.Map[org.make.core.question.QuestionId,org.make.core.operation.OperationOfQuestion] = ooqs.map[(org.make.core.question.QuestionId, org.make.core.operation.OperationOfQuestion)](((ooq: org.make.core.operation.OperationOfQuestion) => scala.Predef.ArrowAssoc[org.make.core.question.QuestionId](ooq.questionId).->[org.make.core.operation.OperationOfQuestion](ooq))).toMap[org.make.core.question.QuestionId, org.make.core.operation.OperationOfQuestion](scala.this.<:<.refl[(org.make.core.question.QuestionId, org.make.core.operation.OperationOfQuestion)]); questionMap.keys.toSeq.flatMap[(org.make.core.question.QuestionId, org.make.api.technical.graphql.GraphQLQuestion)](((questionId: org.make.core.question.QuestionId) => questionMap.get(questionId).flatMap[(org.make.core.question.QuestionId, org.make.api.technical.graphql.GraphQLQuestion)](((question: org.make.core.question.Question) => ooqMap.get(questionId).map[(org.make.core.question.QuestionId, org.make.api.technical.graphql.GraphQLQuestion)](((ooq: org.make.core.operation.OperationOfQuestion) => scala.Predef.ArrowAssoc[org.make.core.question.QuestionId](questionId).->[org.make.api.technical.graphql.GraphQLQuestion](GraphQLQuestion.apply(questionId, question.slug, org.make.api.question.SimpleQuestionWordingResponse.apply(ooq.operationTitles.getTranslationUnsafe(question.defaultLanguage), question.questions.getTranslationUnsafe(question.defaultLanguage)), question.countries, question.defaultLanguage, question.languages, ooq.startDate, ooq.endDate)))))))).toMap[org.make.core.question.QuestionId, org.make.api.technical.graphql.GraphQLQuestion](scala.this.<:<.refl[(org.make.core.question.QuestionId, org.make.api.technical.graphql.GraphQLQuestion)]) }))(ec)))(ec)
73 43040 2963 - 3020 Apply org.make.api.technical.graphql.DataSourceHelper.one DataSourceHelper.one[org.make.core.question.QuestionId, org.make.api.technical.graphql.GraphQLQuestion, org.make.api.technical.graphql.GetQuestion]("questions-datasource", ((questionIds: Seq[org.make.core.question.QuestionId]) => ((executionContext: scala.concurrent.ExecutionContext) => findFromIds(questionIds)(executionContext))))
73 37524 2984 - 3006 Literal <nosymbol> "questions-datasource"
73 50838 3008 - 3019 Apply org.make.api.technical.graphql.DefaultGraphQLQuestionServiceComponent.findFromIds findFromIds(questionIds)(executionContext)