Skip to content

Commit

Permalink
Refactor some seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrus committed Apr 13, 2017
1 parent 0f7bbfe commit a6cd713
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 51 deletions.
22 changes: 20 additions & 2 deletions phoenix-scala/app/models/promotion/Promotion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ case class Promotion(id: Int = 0,
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)
Expand All @@ -53,10 +56,25 @@ case class Promotion(id: Int = 0,

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

def applyType = column[Promotion.ApplyType]("apply_type")
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, updatedAt, createdAt, archivedAt) <> ((Promotion.apply _).tupled, Promotion.unapply)
(id,
scope,
contextId,
shadowId,
formId,
commitId,
applyType,
name,
activeFrom,
activeTo,
updatedAt,
createdAt,
archivedAt) <> ((Promotion.apply _).tupled, Promotion.unapply)
}

object Promotions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package models.promotion

import java.time.Instant

import models.customer.{CustomerGroup, CustomerGroups}
import models.objects.ObjectHeadLinks._
import shapeless._
import utils.db._
import utils.db.ExPostgresDriver.api._

case class PromotionCustomerGroupLink(id: Int = 0,
leftId: Int,
rightId: Int,
createdAt: Instant = Instant.now,
updatedAt: Instant = Instant.now)
extends FoxModel[PromotionCustomerGroupLink]

class PromotionCustomerGroupLinks(tag: Tag)
extends FoxTable[PromotionCustomerGroupLink](tag, "promotion_customer_group_links") {
// FIXME: what an awful lot of duplication @michalrus
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def leftId = column[Int]("left_id")
def rightId = column[Int]("right_id")
def createdAt = column[Instant]("created_at")
def updatedAt = column[Instant]("updated_at")

def * =
(id, leftId, rightId, createdAt, updatedAt) <> ((PromotionCustomerGroupLink.apply _).tupled, PromotionCustomerGroupLink.unapply)

def left = foreignKey(Promotions.tableName, leftId, Promotions)(_.id)
def right = foreignKey(CustomerGroups.tableName, rightId, CustomerGroups)(_.id)
}

object PromotionCustomerGroupLinks
extends FoxTableQuery[PromotionCustomerGroupLink, PromotionCustomerGroupLinks](
new PromotionCustomerGroupLinks(_))
with ReturningId[PromotionCustomerGroupLink, PromotionCustomerGroupLinks] {

val returningLens: Lens[PromotionCustomerGroupLink, Int] = lens[PromotionCustomerGroupLink].id

def build(left: Promotion, right: CustomerGroup) =
PromotionCustomerGroupLink(leftId = left.id, rightId = right.id)
}
30 changes: 16 additions & 14 deletions phoenix-scala/app/utils/seeds/PromotionSeeds.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package utils.seeds

import scala.concurrent.ExecutionContext.Implicits.global
import java.time.Instant

import scala.concurrent.ExecutionContext.Implicits.global
import models.account._
import models.objects._
import models.product.SimpleContext
Expand Down Expand Up @@ -38,15 +39,15 @@ trait PromotionSeeds {
for {
context * <~ ObjectContexts.mustFindById404(SimpleContext.id)
results * <~ discounts.map { discount
val payload = createPromotion(discount.title, Promotion.Coupon)
insertPromotion(payload, discount, context)
val payload = createPromotion(Promotion.Coupon)
insertPromotion(payload, discount, context, discount.title)
}
} yield results

def insertPromotion(payload: CreatePromotion, discount: BaseDiscount, context: ObjectContext)(
implicit db: DB,
ac: AC,
au: AU): DbResultT[BasePromotion] =
def insertPromotion(payload: CreatePromotion,
discount: BaseDiscount,
context: ObjectContext,
name: String)(implicit db: DB, ac: AC, au: AU): DbResultT[BasePromotion] =
for {
scope * <~ Scope.resolveOverride(payload.scope)
form * <~ ObjectForm(kind = Promotion.kind, attributes = payload.form.attributes)
Expand All @@ -58,19 +59,20 @@ trait PromotionSeeds {
applyType = payload.applyType,
formId = ins.form.id,
shadowId = ins.shadow.id,
commitId = ins.commit.id))
commitId = ins.commit.id,
name = name,
activeFrom = Some(Instant.now),
activeTo = None))
link * <~ PromotionDiscountLinks.create(
PromotionDiscountLink(leftId = promotion.id, rightId = discount.discountId))
} yield
BasePromotion(promotion.id, ins.form.id, ins.shadow.id, payload.applyType, discount.title)

def createPromotion(name: String, applyType: Promotion.ApplyType): CreatePromotion = {
val promotionForm = BasePromotionForm(name, applyType)
val promotionShadow = BasePromotionShadow(promotionForm)

def createPromotion(applyType: Promotion.ApplyType): CreatePromotion = {
CreatePromotion(
applyType = applyType,
form = CreatePromotionForm(attributes = promotionForm.form, discounts = Seq.empty),
shadow = CreatePromotionShadow(attributes = promotionShadow.shadow, discounts = Seq.empty))
form = CreatePromotionForm(attributes = BasePromotionForm.form, discounts = Seq.empty),
shadow =
CreatePromotionShadow(attributes = BasePromotionShadow.shadow, discounts = Seq.empty))
}
}
31 changes: 5 additions & 26 deletions phoenix-scala/app/utils/seeds/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,14 @@ package object seeds {
applyType: Promotion.ApplyType,
title: String)

case class BasePromotionForm(name: String, applyType: Promotion.ApplyType) {

val (keyMap, form) = ObjectUtils.createForm(parse(s"""
{
"name" : "$name",
"storefrontName" : "$name",
"description" : "$name",
"details" : "",
"activeFrom" : "${Instant.now}",
"activeTo" : null,
"tags" : []
}
}"""))
case object BasePromotionForm {
val (keyMap, form) = ObjectUtils.createForm(parse(s"""{ "tags" : [] }"""))
}

case class BasePromotionShadow(f: BasePromotionForm) {
case object BasePromotionShadow {

val shadow = ObjectUtils.newShadow(
parse("""
{
"name" : {"type": "string", "ref": "name"},
"storefrontName" : {"type": "richText", "ref": "storefrontName"},
"description" : {"type": "text", "ref": "description"},
"details" : {"type": "richText", "ref": "details"},
"activeFrom" : {"type": "date", "ref": "activeFrom"},
"activeTo" : {"type": "date", "ref": "activeTo"},
"tags" : {"type": "tags", "ref": "tags"}
}"""),
f.keyMap)
val shadow = ObjectUtils
.newShadow(parse("""{"tags" : {"type": "tags", "ref": "tags"}}"""), BasePromotionForm.keyMap)
}

case class BaseCoupon(formId: Int = 0, shadowId: Int = 0, promotionId: Int)
Expand Down
9 changes: 0 additions & 9 deletions phoenix-scala/sql/V4.117__less_enlightened_promos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,19 @@ insert into promotion_customer_group_links

alter table promotions
add column name text
, add column details text
, add column description text
, add column storefront_name text
, add column active_from timestamp
, add column active_to timestamp
;

update promotions
set
name = q.name
, details = q.details
, description = q.description
, storefront_name = q.storefront_name
, active_from = q.active_from
, active_to = q.active_to
from
(select
promotions.id
, illuminate_obj(object_forms, object_shadows, 'name') as name
, illuminate_obj(object_forms, object_shadows, 'details') as details
, illuminate_obj(object_forms, object_shadows, 'description') as description
, illuminate_obj(object_forms, object_shadows, 'storefrontName') as storefront_name
, illuminate_text(object_forms, object_shadows, 'activeFrom') :: timestamp as active_from
, illuminate_text(object_forms, object_shadows, 'activeTo') :: timestamp as active_to
from
Expand Down

0 comments on commit a6cd713

Please sign in to comment.