Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less enlightenment for promos #1369

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
5 changes: 5 additions & 0 deletions phoenix-scala/app/payloads/PromotionPayloads.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package payloads

import java.time.Instant

import models.promotion.Promotion.ApplyType
import payloads.DiscountPayloads.CreateDiscount
import utils.aliases._
Expand All @@ -9,6 +11,9 @@ object PromotionPayloads {
case class UpdatePromoDiscount(id: Int, attributes: Map[String, Json])

case class CreatePromotion(applyType: ApplyType,
name: String,
activeFrom: Option[Instant],
activeTo: Option[Instant],
attributes: Map[String, Json],
discounts: Seq[CreateDiscount],
schema: Option[String] = None,
Expand Down
3 changes: 3 additions & 0 deletions phoenix-scala/app/services/promotion/PromotionManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ object PromotionManager {
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))
Expand Down
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))
}
}
35 changes: 15 additions & 20 deletions phoenix-scala/app/utils/seeds/generators/PromotionGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,21 @@ case class SimplePromotion(promotionId: Int = 0,
totalAmount: Int,
applyType: Promotion.ApplyType = Promotion.Auto)

case class SimplePromotionForm(percentOff: Percent, totalAmount: Int) {
case class SimplePromotionForm() {

val (keyMap, form) = ObjectUtils.createForm(parse(s"""
val (keyMap, form) = ObjectUtils.createForm(parse("""
{
"name" : "$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" : "${Instant.now}",
"activeTo" : null,
"tags" : []
}
}"""))
}

case class SimplePromotionShadow(f: SimplePromotionForm) {

val shadow = ObjectUtils.newShadow(
parse("""
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)
f.keyMap)
}

trait PromotionGenerator {
Expand All @@ -78,9 +64,11 @@ trait PromotionGenerator {
for {
context ← * <~ ObjectContexts.mustFindById404(SimpleContext.id)
promotions ← * <~ sourceData.map(source ⇒ {
val promotionForm = SimplePromotionForm(source.percentOff, source.totalAmount)
import source.{percentOff, totalAmount}

val promotionForm = SimplePromotionForm()
val promotionShadow = SimplePromotionShadow(promotionForm)
val discountForm = SimpleDiscountForm(source.percentOff, source.totalAmount)
val discountForm = SimpleDiscountForm(percentOff, totalAmount)
val discountShadow = SimpleDiscountShadow(discountForm)

def discountFS: FormAndShadow = {
Expand All @@ -94,6 +82,13 @@ trait PromotionGenerator {

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)))
Expand Down
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
23 changes: 1 addition & 22 deletions phoenix-scala/resources/object_schemas/promotion.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,7 @@
"properties": {
"attributes": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"customerGroupIds": {
"type": "array",
"items": {
"type": "number"
},
"uniqueItems": true
},
"activeFrom": {
"type": ["string", "null"],
"format": "date-time"
},
"activeTo": {
"type": ["string", "null"],
"format": "date-time"
}
},
"required": ["name"]
"properties": {}
},
"discounts": {
"type": "array",
Expand Down
82 changes: 82 additions & 0 deletions phoenix-scala/sql/V4.117__less_enlightened_promos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
create table promotion_customer_group_links (
id serial primary key
, left_id integer not null references promotions(id) on update restrict on delete restrict
, right_id integer not null references customer_groups(id) on update restrict on delete restrict
, created_at generic_timestamp
, updated_at generic_timestamp
, foreign key (left_id) references promotions(id) on update restrict on delete restrict
, foreign key (right_id) references customer_groups(id) on update restrict on delete restrict
);

create index promotion_customer_group_link_left_idx on promotion_customer_group_links (left_id);
create index promotion_customer_group_link_right_idx on promotion_customer_group_links (right_id);

insert into promotion_customer_group_links
( left_id
, right_id
, created_at
, updated_at
)
select
promotions.id
, jsonb_array_elements_text(
illuminate_obj(object_forms, object_shadows, 'customerGroupIds')
) :: integer
, now()
, now()
from
promotions
inner join object_forms on (object_forms.id = promotions.form_id)
inner join object_shadows on (object_shadows.id = promotions.shadow_id)
;

-------------------------------------------------------------------------------

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

update promotions
set
name = q.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_text(object_forms, object_shadows, 'activeFrom') :: timestamp as active_from
, illuminate_text(object_forms, object_shadows, 'activeTo') :: timestamp as active_to
from
promotions
inner join object_forms on (object_forms.id = promotions.form_id)
inner join object_shadows on (object_shadows.id = promotions.shadow_id)
) as q
where
promotions.id = q.id
;

update object_shadows
set
attributes = q.attributes
from
(select
object_shadows.id as id
, object_shadows.attributes
- 'name'
- 'details'
- 'description'
- 'storefrontName'
- 'customerGroupIds'
- 'activeFrom'
- 'activeTo'
as attributes
from
object_shadows
inner join promotions on (object_shadows.id = promotions.shadow_id)
) as q
where
object_shadows.id = q.id;
;
Loading