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.technical.crm
21 
22 import org.make.core.question.Question
23 import org.make.core.operation.OperationId
24 import org.make.core.RequestContext
25 import org.make.core.reference.Country
26 import org.make.core.reference.Language
27 
28 class QuestionResolver(questions: Seq[Question], operations: Map[String, OperationId]) {
29 
30   private val questionsWithOperation: Seq[Question] = questions.filter(_.operationId.isDefined)
31 
32   def findQuestionWithOperation(predicate: Question => Boolean): Option[Question] =
33     questionsWithOperation.find(predicate)
34 
35   def extractQuestionWithOperationFromRequestContext(requestContext: RequestContext): Option[Question] = {
36     requestContext.questionContext.questionId
37       .flatMap(questionId => questionsWithOperation.find(_.questionId == questionId))
38       .orElse {
39         requestContext.operationId.flatMap { operationId =>
40           questionsWithOperation.find(question => {
41             // In old operations, the header contained the slug and not the id
42             // also the old operations didn't all have a country or language
43             val reqLanguage = requestContext.languageContext.language.getOrElse(Language("fr"))
44             (question.operationId.contains(operationId) ||
45             question.operationId == operations.get(operationId.value)) &&
46             question.countries.toList.contains(requestContext.country.getOrElse(Country("FR"))) &&
47             question.languages.toList.contains(reqLanguage)
48           })
49         }
50       }
51   }
52 }
Line Stmt Id Pos Tree Symbol Tests Code
30 28371 1140 - 1163 Select scala.Option.isDefined org.make.api.technical.crm.crmservicecomponenttest x$1.operationId.isDefined
30 29739 1123 - 1164 Apply scala.collection.IterableOps.filter org.make.api.technical.crm.crmservicecomponenttest QuestionResolver.this.questions.filter(((x$1: org.make.core.question.Question) => x$1.operationId.isDefined))
33 29306 1254 - 1292 Apply scala.collection.IterableOnceOps.find org.make.api.technical.crm.crmservicecomponenttest QuestionResolver.this.questionsWithOperation.find(predicate)
37 28450 1504 - 1530 Apply java.lang.Object.== org.make.api.technical.crm.crmservicecomponenttest x$2.questionId.==(questionId)
37 29873 1476 - 1531 Apply scala.collection.IterableOnceOps.find org.make.api.technical.crm.crmservicecomponenttest QuestionResolver.this.questionsWithOperation.find(((x$2: org.make.core.question.Question) => x$2.questionId.==(questionId)))
38 29098 1405 - 2235 Apply scala.Option.orElse requestContext.questionContext.questionId.flatMap[org.make.core.question.Question](((questionId: org.make.core.question.QuestionId) => QuestionResolver.this.questionsWithOperation.find(((x$2: org.make.core.question.Question) => x$2.questionId.==(questionId))))).orElse[org.make.core.question.Question](requestContext.operationId.flatMap[org.make.core.question.Question](((operationId: org.make.core.operation.OperationId) => QuestionResolver.this.questionsWithOperation.find(((question: org.make.core.question.Question) => { val reqLanguage: org.make.core.reference.Language = requestContext.languageContext.language.getOrElse[org.make.core.reference.Language](org.make.core.reference.Language.apply("fr")); question.operationId.contains[org.make.core.operation.OperationId](operationId).||(question.operationId.==(QuestionResolver.this.operations.get(operationId.value))).&&(question.countries.toList.contains[org.make.core.reference.Country](requestContext.country.getOrElse[org.make.core.reference.Country](org.make.core.reference.Country.apply("FR")))).&&(question.languages.toList.contains[org.make.core.reference.Language](reqLanguage)) })))))
39 30030 1557 - 2227 Apply scala.Option.flatMap requestContext.operationId.flatMap[org.make.core.question.Question](((operationId: org.make.core.operation.OperationId) => QuestionResolver.this.questionsWithOperation.find(((question: org.make.core.question.Question) => { val reqLanguage: org.make.core.reference.Language = requestContext.languageContext.language.getOrElse[org.make.core.reference.Language](org.make.core.reference.Language.apply("fr")); question.operationId.contains[org.make.core.operation.OperationId](operationId).||(question.operationId.==(QuestionResolver.this.operations.get(operationId.value))).&&(question.countries.toList.contains[org.make.core.reference.Country](requestContext.country.getOrElse[org.make.core.reference.Country](org.make.core.reference.Country.apply("FR")))).&&(question.languages.toList.contains[org.make.core.reference.Language](reqLanguage)) }))))
40 30411 1619 - 2217 Apply scala.collection.IterableOnceOps.find QuestionResolver.this.questionsWithOperation.find(((question: org.make.core.question.Question) => { val reqLanguage: org.make.core.reference.Language = requestContext.languageContext.language.getOrElse[org.make.core.reference.Language](org.make.core.reference.Language.apply("fr")); question.operationId.contains[org.make.core.operation.OperationId](operationId).||(question.operationId.==(QuestionResolver.this.operations.get(operationId.value))).&&(question.countries.toList.contains[org.make.core.reference.Country](requestContext.country.getOrElse[org.make.core.reference.Country](org.make.core.reference.Country.apply("FR")))).&&(question.languages.toList.contains[org.make.core.reference.Language](reqLanguage)) }))
43 30410 1847 - 1912 Apply scala.Option.getOrElse requestContext.languageContext.language.getOrElse[org.make.core.reference.Language](org.make.core.reference.Language.apply("fr"))
43 29066 1897 - 1911 Apply org.make.core.reference.Language.apply org.make.api.technical.crm.crmservicecomponenttest org.make.core.reference.Language.apply("fr")
45 29134 2008 - 2041 Apply scala.collection.MapOps.get QuestionResolver.this.operations.get(operationId.value)
45 28373 1984 - 2041 Apply java.lang.Object.== question.operationId.==(QuestionResolver.this.operations.get(operationId.value))
45 29976 2023 - 2040 Select org.make.core.operation.OperationId.value operationId.value
46 28887 2093 - 2140 Apply scala.Option.getOrElse requestContext.country.getOrElse[org.make.core.reference.Country](org.make.core.reference.Country.apply("FR"))
46 28420 2058 - 2141 Apply scala.collection.immutable.List.contains question.countries.toList.contains[org.make.core.reference.Country](requestContext.country.getOrElse[org.make.core.reference.Country](org.make.core.reference.Country.apply("FR")))
46 29742 2126 - 2139 Apply org.make.core.reference.Country.apply org.make.api.technical.crm.crmservicecomponenttest org.make.core.reference.Country.apply("FR")
46 29000 1925 - 2204 Apply scala.Boolean.&& question.operationId.contains[org.make.core.operation.OperationId](operationId).||(question.operationId.==(QuestionResolver.this.operations.get(operationId.value))).&&(question.countries.toList.contains[org.make.core.reference.Country](requestContext.country.getOrElse[org.make.core.reference.Country](org.make.core.reference.Country.apply("FR")))).&&(question.languages.toList.contains[org.make.core.reference.Language](reqLanguage))
47 29836 2157 - 2204 Apply scala.collection.immutable.List.contains question.languages.toList.contains[org.make.core.reference.Language](reqLanguage)