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.segment
21 
22 import org.make.api.feature.{ActiveFeatureServiceComponent, FeatureServiceComponent}
23 import org.make.core.RequestContext
24 import org.make.core.feature.FeatureSlug
25 import org.make.core.feature.FeatureSlug.{ConsultationDepartmentCompulsory, SequenceCustomDataSegment}
26 
27 import scala.concurrent.ExecutionContext.Implicits.global
28 import scala.concurrent.Future
29 
30 trait DefaultSegmentServiceComponent extends SegmentServiceComponent {
31   self: ActiveFeatureServiceComponent with FeatureServiceComponent =>
32 
33   override lazy val segmentService: DefaultSegmentService = new DefaultSegmentService
34 
35   class DefaultSegmentService extends SegmentService {
36     type SegmentResolver = RequestContext => Future[Option[String]]
37 
38     val defaultSegmentResolver: SegmentResolver = { _ =>
39       Future.successful(None)
40     }
41 
42     val segmentResolverFromDepartment: SegmentResolver = { requestContext =>
43       val customData = requestContext.customData
44       Future.successful(
45         customData
46           .get("declared_department")
47           .orElse(customData.get("detected_department"))
48       )
49     }
50 
51     val segmentResolverFromCustomData: SegmentResolver = { requestContext =>
52       Future.successful(requestContext.customData.get("segment").map(_.trim).filter(_.nonEmpty))
53     }
54 
55     val segmentResolvers: Map[FeatureSlug, SegmentResolver] = Map(
56       ConsultationDepartmentCompulsory -> segmentResolverFromDepartment,
57       SequenceCustomDataSegment -> segmentResolverFromCustomData
58     )
59 
60     override def resolveSegment(requestContext: RequestContext): Future[Option[String]] = {
61       requestContext.questionContext.questionId.map { _ =>
62         activeFeatureService
63           .find(maybeQuestionId = requestContext.questionContext.questionId.map(Seq(_)))
64           .flatMap { activeFeatures =>
65             featureService.findByFeatureIds(activeFeatures.map(_.featureId))
66           }
67           .flatMap { features =>
68             features
69               .flatMap(feature => segmentResolvers.get(feature.slug))
70               .headOption
71               .getOrElse(defaultSegmentResolver)(requestContext)
72           }
73       }.getOrElse(Future.successful(None))
74     }
75   }
76 
77 }
Line Stmt Id Pos Tree Symbol Tests Code
39 25856 1547 - 1570 Apply scala.concurrent.Future.successful scala.concurrent.Future.successful[None.type](scala.None)
39 28006 1565 - 1569 Select scala.None scala.None
43 24564 1678 - 1703 Select org.make.core.RequestContext.customData requestContext.customData
44 22984 1710 - 1850 Apply scala.concurrent.Future.successful scala.concurrent.Future.successful[Option[String]](customData.get("declared_department").orElse[String](customData.get("detected_department")))
46 22458 1763 - 1784 Literal <nosymbol> "declared_department"
47 26151 1804 - 1841 Apply scala.collection.MapOps.get customData.get("detected_department")
47 23899 1737 - 1842 Apply scala.Option.orElse customData.get("declared_department").orElse[String](customData.get("detected_department"))
52 24865 1941 - 2031 Apply scala.concurrent.Future.successful scala.concurrent.Future.successful[Option[String]](requestContext.customData.get("segment").map[String](((x$2: String) => x$2.trim())).filter(((x$3: String) => scala.Predef.augmentString(x$3).nonEmpty)))
52 28017 2019 - 2029 Select scala.collection.StringOps.nonEmpty scala.Predef.augmentString(x$3).nonEmpty
52 24198 2004 - 2010 Apply java.lang.String.trim x$2.trim()
52 26446 1989 - 1998 Literal <nosymbol> "segment"
52 25704 1959 - 2030 Apply scala.Option.filter requestContext.customData.get("segment").map[String](((x$2: String) => x$2.trim())).filter(((x$3: String) => scala.Predef.augmentString(x$3).nonEmpty))
55 28028 2101 - 2249 Apply scala.collection.MapFactory.apply org.make.api.segment.defaultsegmentservicecomponenttest scala.Predef.Map.apply[org.make.core.feature.FeatureSlug, DefaultSegmentService.this.SegmentResolver](scala.Predef.ArrowAssoc[org.make.core.feature.FeatureSlug.ConsultationDepartmentCompulsory.type](org.make.core.feature.FeatureSlug.ConsultationDepartmentCompulsory).->[DefaultSegmentService.this.SegmentResolver](DefaultSegmentService.this.segmentResolverFromDepartment), scala.Predef.ArrowAssoc[org.make.core.feature.FeatureSlug.SequenceCustomDataSegment.type](org.make.core.feature.FeatureSlug.SequenceCustomDataSegment).->[DefaultSegmentService.this.SegmentResolver](DefaultSegmentService.this.segmentResolverFromCustomData))
56 22521 2112 - 2144 Select org.make.core.feature.FeatureSlug.ConsultationDepartmentCompulsory org.make.api.segment.defaultsegmentservicecomponenttest org.make.core.feature.FeatureSlug.ConsultationDepartmentCompulsory
56 26082 2148 - 2177 Select org.make.api.segment.DefaultSegmentServiceComponent.DefaultSegmentService.segmentResolverFromDepartment org.make.api.segment.defaultsegmentservicecomponenttest DefaultSegmentService.this.segmentResolverFromDepartment
56 23910 2112 - 2177 Apply scala.Predef.ArrowAssoc.-> org.make.api.segment.defaultsegmentservicecomponenttest scala.Predef.ArrowAssoc[org.make.core.feature.FeatureSlug.ConsultationDepartmentCompulsory.type](org.make.core.feature.FeatureSlug.ConsultationDepartmentCompulsory).->[DefaultSegmentService.this.SegmentResolver](DefaultSegmentService.this.segmentResolverFromDepartment)
57 27564 2185 - 2210 Select org.make.core.feature.FeatureSlug.SequenceCustomDataSegment org.make.api.segment.defaultsegmentservicecomponenttest org.make.core.feature.FeatureSlug.SequenceCustomDataSegment
57 26744 2214 - 2243 Select org.make.api.segment.DefaultSegmentServiceComponent.DefaultSegmentService.segmentResolverFromCustomData org.make.api.segment.defaultsegmentservicecomponenttest DefaultSegmentService.this.segmentResolverFromCustomData
57 24405 2185 - 2243 Apply scala.Predef.ArrowAssoc.-> org.make.api.segment.defaultsegmentservicecomponenttest scala.Predef.ArrowAssoc[org.make.core.feature.FeatureSlug.SequenceCustomDataSegment.type](org.make.core.feature.FeatureSlug.SequenceCustomDataSegment).->[DefaultSegmentService.this.SegmentResolver](DefaultSegmentService.this.segmentResolverFromCustomData)
62 26000 2410 - 2430 Select org.make.api.feature.ActiveFeatureServiceComponent.activeFeatureService org.make.api.segment.defaultsegmentservicecomponenttest DefaultSegmentServiceComponent.this.activeFeatureService
63 26595 2442 - 2442 Select org.make.api.feature.ActiveFeatureService.find$default$4 org.make.api.segment.defaultsegmentservicecomponenttest qual$1.find$default$4
63 22444 2465 - 2518 Apply scala.Option.map org.make.api.segment.defaultsegmentservicecomponenttest requestContext.questionContext.questionId.map[Seq[org.make.core.question.QuestionId]](((x$5: org.make.core.question.QuestionId) => scala.`package`.Seq.apply[org.make.core.question.QuestionId](x$5)))
63 24127 2442 - 2442 Select org.make.api.feature.ActiveFeatureService.find$default$2 org.make.api.segment.defaultsegmentservicecomponenttest qual$1.find$default$2
63 24330 2442 - 2442 Select org.make.api.feature.ActiveFeatureService.find$default$6 org.make.api.segment.defaultsegmentservicecomponenttest qual$1.find$default$6
63 28164 2410 - 2519 Apply org.make.api.feature.ActiveFeatureService.find org.make.api.segment.defaultsegmentservicecomponenttest qual$1.find(x$2, x$3, x$4, x$5, x$1, x$6)
63 24877 2511 - 2517 Apply scala.collection.SeqFactory.Delegate.apply org.make.api.segment.defaultsegmentservicecomponenttest scala.`package`.Seq.apply[org.make.core.question.QuestionId](x$5)
63 26303 2442 - 2442 Select org.make.api.feature.ActiveFeatureService.find$default$1 org.make.api.segment.defaultsegmentservicecomponenttest qual$1.find$default$1
63 27858 2442 - 2442 Select org.make.api.feature.ActiveFeatureService.find$default$3 org.make.api.segment.defaultsegmentservicecomponenttest qual$1.find$default$3
64 26312 2539 - 2539 Select scala.concurrent.ExecutionContext.Implicits.global org.make.api.segment.defaultsegmentservicecomponenttest scala.concurrent.ExecutionContext.Implicits.global
65 26011 2622 - 2633 Select org.make.core.feature.ActiveFeature.featureId x$6.featureId
65 22452 2571 - 2635 Apply org.make.api.feature.FeatureService.findByFeatureIds DefaultSegmentServiceComponent.this.featureService.findByFeatureIds(activeFeatures.map[org.make.core.feature.FeatureId](((x$6: org.make.core.feature.ActiveFeature) => x$6.featureId)))
65 23737 2603 - 2634 Apply scala.collection.IterableOps.map activeFeatures.map[org.make.core.feature.FeatureId](((x$6: org.make.core.feature.ActiveFeature) => x$6.featureId))
67 27871 2667 - 2667 Select scala.concurrent.ExecutionContext.Implicits.global org.make.api.segment.defaultsegmentservicecomponenttest scala.concurrent.ExecutionContext.Implicits.global
67 26536 2410 - 2874 ApplyToImplicitArgs scala.concurrent.Future.flatMap org.make.api.segment.defaultsegmentservicecomponenttest { <artifact> val qual$1: org.make.api.feature.ActiveFeatureService = DefaultSegmentServiceComponent.this.activeFeatureService; <artifact> val x$1: Option[Seq[org.make.core.question.QuestionId]] @scala.reflect.internal.annotations.uncheckedBounds = requestContext.questionContext.questionId.map[Seq[org.make.core.question.QuestionId]](((x$5: org.make.core.question.QuestionId) => scala.`package`.Seq.apply[org.make.core.question.QuestionId](x$5))); <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; <artifact> val x$6: Option[Seq[org.make.core.feature.FeatureId]] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.find$default$6; qual$1.find(x$2, x$3, x$4, x$5, x$1, x$6) }.flatMap[Seq[org.make.core.feature.Feature]](((activeFeatures: Seq[org.make.core.feature.ActiveFeature]) => DefaultSegmentServiceComponent.this.featureService.findByFeatureIds(activeFeatures.map[org.make.core.feature.FeatureId](((x$6: org.make.core.feature.ActiveFeature) => x$6.featureId)))))(scala.concurrent.ExecutionContext.Implicits.global).flatMap[Option[String]](((features: Seq[org.make.core.feature.Feature]) => features.flatMap[DefaultSegmentService.this.SegmentResolver](((feature: org.make.core.feature.Feature) => DefaultSegmentService.this.segmentResolvers.get(feature.slug))).headOption.getOrElse[DefaultSegmentService.this.SegmentResolver](DefaultSegmentService.this.defaultSegmentResolver).apply(requestContext)))(scala.concurrent.ExecutionContext.Implicits.global)
71 23894 2693 - 2862 Apply scala.Function1.apply features.flatMap[DefaultSegmentService.this.SegmentResolver](((feature: org.make.core.feature.Feature) => DefaultSegmentService.this.segmentResolvers.get(feature.slug))).headOption.getOrElse[DefaultSegmentService.this.SegmentResolver](DefaultSegmentService.this.defaultSegmentResolver).apply(requestContext)
73 25783 2349 - 2917 Apply scala.Option.getOrElse org.make.api.segment.defaultsegmentservicecomponenttest requestContext.questionContext.questionId.map[scala.concurrent.Future[Option[String]]](((x$4: org.make.core.question.QuestionId) => { <artifact> val qual$1: org.make.api.feature.ActiveFeatureService = DefaultSegmentServiceComponent.this.activeFeatureService; <artifact> val x$1: Option[Seq[org.make.core.question.QuestionId]] @scala.reflect.internal.annotations.uncheckedBounds = requestContext.questionContext.questionId.map[Seq[org.make.core.question.QuestionId]](((x$5: org.make.core.question.QuestionId) => scala.`package`.Seq.apply[org.make.core.question.QuestionId](x$5))); <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; <artifact> val x$6: Option[Seq[org.make.core.feature.FeatureId]] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.find$default$6; qual$1.find(x$2, x$3, x$4, x$5, x$1, x$6) }.flatMap[Seq[org.make.core.feature.Feature]](((activeFeatures: Seq[org.make.core.feature.ActiveFeature]) => DefaultSegmentServiceComponent.this.featureService.findByFeatureIds(activeFeatures.map[org.make.core.feature.FeatureId](((x$6: org.make.core.feature.ActiveFeature) => x$6.featureId)))))(scala.concurrent.ExecutionContext.Implicits.global).flatMap[Option[String]](((features: Seq[org.make.core.feature.Feature]) => features.flatMap[DefaultSegmentService.this.SegmentResolver](((feature: org.make.core.feature.Feature) => DefaultSegmentService.this.segmentResolvers.get(feature.slug))).headOption.getOrElse[DefaultSegmentService.this.SegmentResolver](DefaultSegmentService.this.defaultSegmentResolver).apply(requestContext)))(scala.concurrent.ExecutionContext.Implicits.global))).getOrElse[scala.concurrent.Future[Option[String]]](scala.concurrent.Future.successful[None.type](scala.None))
73 28011 2893 - 2916 Apply scala.concurrent.Future.successful org.make.api.segment.defaultsegmentservicecomponenttest scala.concurrent.Future.successful[None.type](scala.None)
73 24344 2911 - 2915 Select scala.None org.make.api.segment.defaultsegmentservicecomponenttest scala.None