Skip to content

Commit 0d710b8

Browse files
committed
Refactor some seeds
1 parent 969d6f8 commit 0d710b8

File tree

5 files changed

+84
-51
lines changed

5 files changed

+84
-51
lines changed

phoenix-scala/app/models/promotion/Promotion.scala

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ case class Promotion(id: Int = 0,
4040
formId: Int,
4141
commitId: Int,
4242
applyType: Promotion.ApplyType = Promotion.Auto,
43+
name: String,
44+
activeFrom: Option[Instant],
45+
activeTo: Option[Instant],
4346
updatedAt: Instant = Instant.now,
4447
createdAt: Instant = Instant.now,
4548
archivedAt: Option[Instant] = None)
@@ -53,10 +56,25 @@ case class Promotion(id: Int = 0,
5356

5457
class Promotions(tag: Tag) extends ObjectHeads[Promotion](tag, "promotions") {
5558

56-
def applyType = column[Promotion.ApplyType]("apply_type")
59+
def applyType = column[Promotion.ApplyType]("apply_type")
60+
def name = column[String]("name")
61+
def activeFrom = column[Option[Instant]]("active_from")
62+
def activeTo = column[Option[Instant]]("active_to")
5763

5864
def * =
59-
(id, scope, contextId, shadowId, formId, commitId, applyType, updatedAt, createdAt, archivedAt) <> ((Promotion.apply _).tupled, Promotion.unapply)
65+
(id,
66+
scope,
67+
contextId,
68+
shadowId,
69+
formId,
70+
commitId,
71+
applyType,
72+
name,
73+
activeFrom,
74+
activeTo,
75+
updatedAt,
76+
createdAt,
77+
archivedAt) <> ((Promotion.apply _).tupled, Promotion.unapply)
6078
}
6179

6280
object Promotions
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package models.promotion
2+
3+
import java.time.Instant
4+
5+
import models.customer.{CustomerGroup, CustomerGroups}
6+
import models.objects.ObjectHeadLinks._
7+
import shapeless._
8+
import utils.db._
9+
import utils.db.ExPostgresDriver.api._
10+
11+
case class PromotionCustomerGroupLink(id: Int = 0,
12+
leftId: Int,
13+
rightId: Int,
14+
createdAt: Instant = Instant.now,
15+
updatedAt: Instant = Instant.now)
16+
extends FoxModel[PromotionCustomerGroupLink]
17+
18+
class PromotionCustomerGroupLinks(tag: Tag)
19+
extends FoxTable[PromotionCustomerGroupLink](tag, "promotion_customer_group_links") {
20+
// FIXME: what an awful lot of duplication @michalrus
21+
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
22+
def leftId = column[Int]("left_id")
23+
def rightId = column[Int]("right_id")
24+
def createdAt = column[Instant]("created_at")
25+
def updatedAt = column[Instant]("updated_at")
26+
27+
def * =
28+
(id, leftId, rightId, createdAt, updatedAt) <> ((PromotionCustomerGroupLink.apply _).tupled, PromotionCustomerGroupLink.unapply)
29+
30+
def left = foreignKey(Promotions.tableName, leftId, Promotions)(_.id)
31+
def right = foreignKey(CustomerGroups.tableName, rightId, CustomerGroups)(_.id)
32+
}
33+
34+
object PromotionCustomerGroupLinks
35+
extends FoxTableQuery[PromotionCustomerGroupLink, PromotionCustomerGroupLinks](
36+
new PromotionCustomerGroupLinks(_))
37+
with ReturningId[PromotionCustomerGroupLink, PromotionCustomerGroupLinks] {
38+
39+
val returningLens: Lens[PromotionCustomerGroupLink, Int] = lens[PromotionCustomerGroupLink].id
40+
41+
def build(left: Promotion, right: CustomerGroup) =
42+
PromotionCustomerGroupLink(leftId = left.id, rightId = right.id)
43+
}

phoenix-scala/app/utils/seeds/PromotionSeeds.scala

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package utils.seeds
22

3-
import scala.concurrent.ExecutionContext.Implicits.global
3+
import java.time.Instant
44

5+
import scala.concurrent.ExecutionContext.Implicits.global
56
import models.account._
67
import models.objects._
78
import models.product.SimpleContext
@@ -38,15 +39,15 @@ trait PromotionSeeds {
3839
for {
3940
context * <~ ObjectContexts.mustFindById404(SimpleContext.id)
4041
results * <~ discounts.map { discount
41-
val payload = createPromotion(discount.title, Promotion.Coupon)
42-
insertPromotion(payload, discount, context)
42+
val payload = createPromotion(Promotion.Coupon)
43+
insertPromotion(payload, discount, context, discount.title)
4344
}
4445
} yield results
4546

46-
def insertPromotion(payload: CreatePromotion, discount: BaseDiscount, context: ObjectContext)(
47-
implicit db: DB,
48-
ac: AC,
49-
au: AU): DbResultT[BasePromotion] =
47+
def insertPromotion(payload: CreatePromotion,
48+
discount: BaseDiscount,
49+
context: ObjectContext,
50+
name: String)(implicit db: DB, ac: AC, au: AU): DbResultT[BasePromotion] =
5051
for {
5152
scope * <~ Scope.resolveOverride(payload.scope)
5253
form * <~ ObjectForm(kind = Promotion.kind, attributes = payload.form.attributes)
@@ -58,19 +59,20 @@ trait PromotionSeeds {
5859
applyType = payload.applyType,
5960
formId = ins.form.id,
6061
shadowId = ins.shadow.id,
61-
commitId = ins.commit.id))
62+
commitId = ins.commit.id,
63+
name = name,
64+
activeFrom = Some(Instant.now),
65+
activeTo = None))
6266
link * <~ PromotionDiscountLinks.create(
6367
PromotionDiscountLink(leftId = promotion.id, rightId = discount.discountId))
6468
} yield
6569
BasePromotion(promotion.id, ins.form.id, ins.shadow.id, payload.applyType, discount.title)
6670

67-
def createPromotion(name: String, applyType: Promotion.ApplyType): CreatePromotion = {
68-
val promotionForm = BasePromotionForm(name, applyType)
69-
val promotionShadow = BasePromotionShadow(promotionForm)
70-
71+
def createPromotion(applyType: Promotion.ApplyType): CreatePromotion = {
7172
CreatePromotion(
7273
applyType = applyType,
73-
form = CreatePromotionForm(attributes = promotionForm.form, discounts = Seq.empty),
74-
shadow = CreatePromotionShadow(attributes = promotionShadow.shadow, discounts = Seq.empty))
74+
form = CreatePromotionForm(attributes = BasePromotionForm.form, discounts = Seq.empty),
75+
shadow =
76+
CreatePromotionShadow(attributes = BasePromotionShadow.shadow, discounts = Seq.empty))
7577
}
7678
}

phoenix-scala/app/utils/seeds/package.scala

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,14 @@ package object seeds {
5353
applyType: Promotion.ApplyType,
5454
title: String)
5555

56-
case class BasePromotionForm(name: String, applyType: Promotion.ApplyType) {
57-
58-
val (keyMap, form) = ObjectUtils.createForm(parse(s"""
59-
{
60-
"name" : "$name",
61-
"storefrontName" : "$name",
62-
"description" : "$name",
63-
"details" : "",
64-
"activeFrom" : "${Instant.now}",
65-
"activeTo" : null,
66-
"tags" : []
67-
}
68-
}"""))
56+
case object BasePromotionForm {
57+
val (keyMap, form) = ObjectUtils.createForm(parse(s"""{ "tags" : [] }"""))
6958
}
7059

71-
case class BasePromotionShadow(f: BasePromotionForm) {
60+
case object BasePromotionShadow {
7261

73-
val shadow = ObjectUtils.newShadow(
74-
parse("""
75-
{
76-
"name" : {"type": "string", "ref": "name"},
77-
"storefrontName" : {"type": "richText", "ref": "storefrontName"},
78-
"description" : {"type": "text", "ref": "description"},
79-
"details" : {"type": "richText", "ref": "details"},
80-
"activeFrom" : {"type": "date", "ref": "activeFrom"},
81-
"activeTo" : {"type": "date", "ref": "activeTo"},
82-
"tags" : {"type": "tags", "ref": "tags"}
83-
}"""),
84-
f.keyMap)
62+
val shadow = ObjectUtils
63+
.newShadow(parse("""{"tags" : {"type": "tags", "ref": "tags"}}"""), BasePromotionForm.keyMap)
8564
}
8665

8766
case class BaseCoupon(formId: Int = 0, shadowId: Int = 0, promotionId: Int)

phoenix-scala/sql/V4.117__less_enlightened_promos.sql

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,19 @@ insert into promotion_customer_group_links
3434

3535
alter table promotions
3636
add column name text
37-
, add column details text
38-
, add column description text
39-
, add column storefront_name text
4037
, add column active_from timestamp
4138
, add column active_to timestamp
4239
;
4340

4441
update promotions
4542
set
4643
name = q.name
47-
, details = q.details
48-
, description = q.description
49-
, storefront_name = q.storefront_name
5044
, active_from = q.active_from
5145
, active_to = q.active_to
5246
from
5347
(select
5448
promotions.id
5549
, illuminate_obj(object_forms, object_shadows, 'name') as name
56-
, illuminate_obj(object_forms, object_shadows, 'details') as details
57-
, illuminate_obj(object_forms, object_shadows, 'description') as description
58-
, illuminate_obj(object_forms, object_shadows, 'storefrontName') as storefront_name
5950
, illuminate_text(object_forms, object_shadows, 'activeFrom') :: timestamp as active_from
6051
, illuminate_text(object_forms, object_shadows, 'activeTo') :: timestamp as active_to
6152
from

0 commit comments

Comments
 (0)