-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPromotion.scala
More file actions
104 lines (84 loc) · 2.99 KB
/
Promotion.scala
File metadata and controls
104 lines (84 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package models.promotion
import java.time.Instant
import com.pellucid.sealerate
import models.objects._
import shapeless._
import slick.ast.BaseTypedType
import slick.jdbc.JdbcType
import utils.db.ExPostgresDriver.api._
import utils.db._
import utils.{ADT, Validation}
import com.github.tminglei.slickpg._
object Promotion {
val kind = "promotion"
sealed trait ApplyType
case object Auto extends ApplyType
case object Coupon extends ApplyType
object ApplyType extends ADT[ApplyType] {
def types = sealerate.values[ApplyType]
}
implicit val stateColumnType: JdbcType[ApplyType] with BaseTypedType[ApplyType] =
ApplyType.slickColumn
}
/**
* A Promotion is a way to bundle several discounts into a presentable form.
* ObjectLinks are used to connect a promotion to several discounts.
*/
case class Promotion(id: Int = 0,
scope: LTree,
contextId: Int,
shadowId: Int,
formId: Int,
commitId: Int,
applyType: Promotion.ApplyType = Promotion.Auto,
name: String,
activeFrom: Option[Instant],
activeTo: Option[Instant],
updatedAt: Instant = Instant.now,
createdAt: Instant = Instant.now,
archivedAt: Option[Instant] = None)
extends FoxModel[Promotion]
with Validation[Promotion]
with ObjectHead[Promotion] {
def withNewShadowAndCommit(shadowId: Int, commitId: Int): Promotion =
this.copy(shadowId = shadowId, commitId = commitId)
}
class Promotions(tag: Tag) extends ObjectHeads[Promotion](tag, "promotions") {
def applyType = column[Promotion.ApplyType]("apply_type")
def name = column[String]("name")
def activeFrom = column[Option[Instant]]("active_from")
def activeTo = column[Option[Instant]]("active_to")
def * =
(id,
scope,
contextId,
shadowId,
formId,
commitId,
applyType,
name,
activeFrom,
activeTo,
updatedAt,
createdAt,
archivedAt) <> ((Promotion.apply _).tupled, Promotion.unapply)
}
object Promotions
extends ObjectHeadsQueries[Promotion, Promotions](new Promotions(_))
with ReturningId[Promotion, Promotions] {
val returningLens: Lens[Promotion, Int] = lens[Promotion].id
def filterByContext(contextId: Int): QuerySeq =
filter(_.contextId === contextId)
def filterByContextAndFormId(contextId: Int, formId: Int): QuerySeq =
filter(_.contextId === contextId).filter(_.formId === formId)
def filterByContextAndShadowId(contextId: Int, shadowId: Int): QuerySeq =
filter(_.contextId === contextId).filter(_.shadowId === shadowId)
object scope {
implicit class PromotionQuerySeqConversions(q: QuerySeq) {
def autoApplied: QuerySeq =
q.filter(_.applyType === (Promotion.Auto: Promotion.ApplyType))
def couponOnly: QuerySeq =
q.filter(_.applyType === (Promotion.Coupon: Promotion.ApplyType))
}
}
}