-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPromotionGenerator.scala
More file actions
101 lines (84 loc) · 3.89 KB
/
PromotionGenerator.scala
File metadata and controls
101 lines (84 loc) · 3.89 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
package utils.seeds.generators
import java.time.Instant
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Random
import models.objects._
import models.objects.ObjectUtils._
import models.product.SimpleContext
import models.promotion._
import org.json4s._
import org.json4s.jackson.JsonMethods._
import payloads.DiscountPayloads._
import payloads.PromotionPayloads._
import services.promotion.PromotionManager
import utils.aliases._
import utils.db._
import utils.seeds.generators.SimplePromotion._
object SimplePromotion {
type Percent = Int
}
case class SimplePromotion(promotionId: Int = 0,
formId: Int = 0,
shadowId: Int = 0,
percentOff: Percent,
totalAmount: Int,
applyType: Promotion.ApplyType = Promotion.Auto)
case class SimplePromotionForm() {
val (keyMap, form) = ObjectUtils.createForm(parse("""
{
"tags" : []
}"""))
}
case class SimplePromotionShadow(f: SimplePromotionForm) {
val shadow = ObjectUtils.newShadow(parse("""
{
"tags" : {"type": "tags", "ref": "tags"}
}"""),
f.keyMap)
}
trait PromotionGenerator {
val percents = Seq(10, 15, 20, 25, 30, 45)
val amounts = Seq(10, 15, 20, 25, 30, 45, 50)
def generatePromotion(applyType: Promotion.ApplyType = Promotion.Auto): SimplePromotion = {
val percent = percents(Random.nextInt(percents.length))
val totalAmount = amounts(Random.nextInt(amounts.length))
SimplePromotion(applyType = applyType, percentOff = percent, totalAmount = totalAmount)
}
def generatePromotions(sourceData: Seq[SimplePromotion])(
implicit db: DB,
ac: AC,
au: AU): DbResultT[Seq[SimplePromotion]] =
for {
context ← * <~ ObjectContexts.mustFindById404(SimpleContext.id)
promotions ← * <~ sourceData.map(source ⇒ {
import source.{percentOff, totalAmount}
val promotionForm = SimplePromotionForm()
val promotionShadow = SimplePromotionShadow(promotionForm)
val discountForm = SimpleDiscountForm(percentOff, totalAmount)
val discountShadow = SimpleDiscountShadow(discountForm)
def discountFS: FormAndShadow = {
(ObjectForm(kind = Promotion.kind, attributes = discountForm.form),
ObjectShadow(attributes = discountShadow.shadow))
}
val promotionFS: FormAndShadow = {
(ObjectForm(kind = Promotion.kind, attributes = promotionForm.form),
ObjectShadow(attributes = promotionShadow.shadow))
}
val payload =
CreatePromotion(applyType = source.applyType,
name =
s"$percentOff% off after spending $totalAmount dollars",
// "storefrontName" : "$percentOff% off after spending $totalAmount dollars",
// "description" : "$percentOff% off full order after spending $totalAmount dollars",
// "details" : "This offer applies after you spend over $totalAmount dollars",
activeFrom = Some(Instant.now),
activeTo = None,
attributes = promotionFS.toPayload,
discounts =
Seq(CreateDiscount(attributes = discountFS.toPayload)))
PromotionManager.create(payload, context.name, None).map { newPromo ⇒
source.copy(promotionId = newPromo.id)
}
})
} yield promotions
}