diff --git a/phoenix-scala/app/models/promotion/Promotion.scala b/phoenix-scala/app/models/promotion/Promotion.scala index bf2e06a75d..322240824a 100644 --- a/phoenix-scala/app/models/promotion/Promotion.scala +++ b/phoenix-scala/app/models/promotion/Promotion.scala @@ -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) @@ -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 diff --git a/phoenix-scala/app/models/promotion/PromotionCustomerGroupLink.scala b/phoenix-scala/app/models/promotion/PromotionCustomerGroupLink.scala new file mode 100644 index 0000000000..66d318f75d --- /dev/null +++ b/phoenix-scala/app/models/promotion/PromotionCustomerGroupLink.scala @@ -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) +} diff --git a/phoenix-scala/app/utils/seeds/PromotionSeeds.scala b/phoenix-scala/app/utils/seeds/PromotionSeeds.scala index 4fd95ff609..ac04758b12 100644 --- a/phoenix-scala/app/utils/seeds/PromotionSeeds.scala +++ b/phoenix-scala/app/utils/seeds/PromotionSeeds.scala @@ -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 @@ -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) @@ -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)) } } diff --git a/phoenix-scala/app/utils/seeds/package.scala b/phoenix-scala/app/utils/seeds/package.scala index e7589ba3d1..b77c98f047 100644 --- a/phoenix-scala/app/utils/seeds/package.scala +++ b/phoenix-scala/app/utils/seeds/package.scala @@ -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) diff --git a/phoenix-scala/sql/V4.117__less_enlightened_promos.sql b/phoenix-scala/sql/V4.117__less_enlightened_promos.sql index 9ec2f3b23b..605a60f5ba 100644 --- a/phoenix-scala/sql/V4.117__less_enlightened_promos.sql +++ b/phoenix-scala/sql/V4.117__less_enlightened_promos.sql @@ -34,9 +34,6 @@ 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 ; @@ -44,18 +41,12 @@ alter table promotions 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