-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPromotionManager.scala
More file actions
219 lines (196 loc) · 9.26 KB
/
Copy pathPromotionManager.scala
File metadata and controls
219 lines (196 loc) · 9.26 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package services.promotion
import java.time.Instant
import failures.NotFoundFailure404
import failures.ObjectFailures._
import failures.PromotionFailures._
import models.account._
import models.coupon.Coupons
import models.discount._
import models.objects.ObjectUtils._
import models.objects._
import models.promotion._
import payloads.DiscountPayloads._
import payloads.PromotionPayloads._
import responses.PromotionResponses._
import services.LogActivity
import services.discount.DiscountManager
import services.objects.ObjectManager
import slick.driver.PostgresDriver.api._
import utils.aliases._
import utils.db._
object PromotionManager {
def create(payload: CreatePromotion, contextName: String, admin: Option[User])(
implicit ec: EC,
db: DB,
ac: AC,
au: AU): DbResultT[PromotionResponse.Root] = {
val formAndShadow =
FormAndShadow.fromPayload(kind = Promotion.kind, attributes = payload.attributes)
for {
scope ← * <~ Scope.resolveOverride(payload.scope)
context ← * <~ ObjectContexts
.filterByName(contextName)
.mustFindOneOr(ObjectContextNotFound(contextName))
(form, shadow) = IlluminatedPromotion
.validatePromotion(payload.applyType, formAndShadow)
.tupled
ins ← * <~ ObjectUtils.insert((form, shadow), payload.schema)
promotion ← * <~ Promotions.create(
Promotion(scope = scope,
contextId = context.id,
applyType = payload.applyType,
name = payload.name,
activeFrom = payload.activeFrom,
activeTo = payload.activeTo,
formId = ins.form.id,
shadowId = ins.shadow.id,
commitId = ins.commit.id))
discount ← * <~ createDiscounts(context, payload, ins.shadow)
response = PromotionResponse
.build(context, promotion, ins.form, ins.shadow, discount.forms.zip(discount.shadows))
_ ← * <~ LogActivity().withScope(scope).promotionCreated(response, admin)
} yield response
}
private case class DiscountsCreateResult(forms: Seq[ObjectForm], shadows: Seq[ObjectShadow])
private def createDiscounts(
context: ObjectContext,
payload: CreatePromotion,
promotionShadow: ObjectShadow)(implicit ec: EC, au: AU): DbResultT[DiscountsCreateResult] =
for {
newDiscounts ← * <~ payload.discounts.map { discount ⇒
createDiscount(context, discount, promotionShadow)
}
forms = newDiscounts.map(_.form)
shadows = newDiscounts.map(_.shadow)
} yield DiscountsCreateResult(forms, shadows)
private case class DiscountCreateResult(form: ObjectForm, shadow: ObjectShadow)
private def createDiscount(
context: ObjectContext,
createDiscount: CreateDiscount,
promotionShadow: ObjectShadow)(implicit ec: EC, au: AU): DbResultT[DiscountCreateResult] =
for {
promotion ← * <~ Promotions
.filterByContextAndShadowId(context.id, promotionShadow.id)
.mustFindOneOr(NotFoundFailure404(classOf[Promotion].getSimpleName,
"shadowId",
promotionShadow.id))
discount ← * <~ DiscountManager
.createInternal(CreateDiscount(attributes = createDiscount.attributes), context)
link ← * <~ PromotionDiscountLinks.create(
PromotionDiscountLink(leftId = promotion.id, rightId = discount.discount.id))
} yield DiscountCreateResult(discount.form, discount.shadow)
def update(id: Int, payload: UpdatePromotion, contextName: String, admin: Option[User])(
implicit ec: EC,
ac: AC,
db: DB): DbResultT[PromotionResponse.Root] = {
val formAndShadow = FormAndShadow.fromPayload(Promotion.kind, payload.attributes)
for {
context ← * <~ ObjectContexts
.filterByName(contextName)
.mustFindOneOr(ObjectContextNotFound(contextName))
promotion ← * <~ Promotions
.filterByContextAndFormId(context.id, id)
.mustFindOneOr(PromotionNotFoundForContext(id, contextName))
validated = IlluminatedPromotion
.validatePromotion(payload.applyType, (formAndShadow.form, formAndShadow.shadow))
updated ← * <~ ObjectUtils.update(promotion.formId,
promotion.shadowId,
validated.form.attributes,
validated.shadow.attributes)
discount ← * <~ updateDiscounts(context, payload)
commit ← * <~ ObjectUtils.commit(updated)
promotion ← * <~ updateHead(promotion, payload, updated.shadow, commit)
response = PromotionResponse.build(context,
promotion,
updated.form,
updated.shadow,
discount.forms.zip(discount.shadows))
_ ← * <~ LogActivity().promotionUpdated(response, admin)
} yield response
}
def archiveByContextAndId(contextName: String, formId: Int)(
implicit ec: EC,
db: DB): DbResultT[PromotionResponse.Root] =
for {
context ← * <~ ObjectContexts
.filterByName(contextName)
.mustFindOneOr(ObjectContextNotFound(contextName))
fullObject ← * <~ ObjectManager.getFullObject(
mustFindPromotionByContextAndFormId(context.id, formId))
model = fullObject.model
now = Some(Instant.now)
archiveResult ← * <~ Promotions.update(model, model.copy(archivedAt = now))
coupons ← * <~ Coupons.filterByContextAndPromotionId(context.id, archiveResult.id).result
_ ← * <~ coupons.map(coupon ⇒ Coupons.update(coupon, coupon.copy(archivedAt = now)))
discounts ← * <~ PromotionDiscountLinks.queryRightByLeft(archiveResult)
discountForms = discounts.map(_.form)
discountShadows = discounts.map(_.shadow)
} yield
PromotionResponse.build(context,
archiveResult,
fullObject.form,
fullObject.shadow,
discountForms.zip(discountShadows))
private def mustFindPromotionByContextAndFormId(contextId: Int, formId: Int)(
implicit ec: EC,
db: DB): DbResultT[Promotion] =
Promotions
.findOneByContextAndFormId(contextId, formId)
.mustFindOneOr(NotFoundFailure404(Promotion, formId))
private case class UpdateDiscountsResult(forms: Seq[ObjectForm], shadows: Seq[ObjectShadow])
private def updateDiscounts(context: ObjectContext, payload: UpdatePromotion)(
implicit ec: EC,
db: DB): DbResultT[UpdateDiscountsResult] = {
for {
updated ← * <~ payload.discounts.sortBy(_.id).map { discount ⇒
updateDiscount(context, discount)
}
forms = updated.map(_.form)
shadows = updated.map(_.shadow)
} yield UpdateDiscountsResult(forms, shadows)
}
private case class UpdateDiscountResult(form: ObjectForm, shadow: ObjectShadow)
private def updateDiscount(context: ObjectContext, updateDiscount: UpdatePromoDiscount)(
implicit ec: EC,
db: DB): DbResultT[UpdateDiscountResult] = {
for {
discount ← * <~ DiscountManager
.updateInternal(updateDiscount.id, updateDiscount.attributes, context)
} yield UpdateDiscountResult(discount.form, discount.shadow)
}
def getIlluminated(id: Int, contextName: String)(implicit ec: EC,
db: DB): DbResultT[PromotionResponse.Root] =
for {
context ← * <~ ObjectContexts
.filterByName(contextName)
.mustFindOneOr(ObjectContextNotFound(contextName))
promotion ← * <~ Promotions
.filter(_.contextId === context.id)
.filter(_.formId === id)
.mustFindOneOr(PromotionNotFound(id))
form ← * <~ ObjectForms.mustFindById404(promotion.formId)
shadow ← * <~ ObjectShadows.mustFindById404(promotion.shadowId)
discounts ← * <~ PromotionDiscountLinks.queryRightByLeft(promotion)
} yield
PromotionResponse.build(
promotion = IlluminatedPromotion.illuminate(context, promotion, form, shadow),
discounts =
discounts.map(d ⇒ IlluminatedDiscount.illuminate(form = d.form, shadow = d.shadow)),
promotion)
private def updateHead(
promotion: Promotion,
payload: UpdatePromotion,
shadow: ObjectShadow,
maybeCommit: Option[ObjectCommit])(implicit ec: EC): DbResultT[Promotion] =
maybeCommit match {
case Some(commit) ⇒
val updated =
promotion.copy(applyType = payload.applyType, shadowId = shadow.id, commitId = commit.id)
Promotions.update(promotion, updated)
case None ⇒
if (promotion.applyType != payload.applyType)
Promotions.update(promotion, promotion.copy(applyType = payload.applyType))
else
DbResultT.good(promotion)
}
}