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

Make pending product reviews private #2318

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ object ProductReviewFailures {
override def description: String = s"Cannot update deleted review: $id"
}

case class ProductReviewUserMismatch(id: ProductReview#Id) extends Failure {
case class UpdateProductReviewUserMismatch(id: ProductReview#Id) extends Failure {
override def description: String =
s"Cannot update review $id: Only user who created the review can modify it."
s"Cannot update review $id: Only the user who created the review can modify it."
}

case class FetchProductReviewUserMismatch(id: ProductReview#Id) extends Failure {
override def description: String =
s"Cannot fetch review $id: You can only fetch your own reviews."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import core.utils.Validation
import phoenix.utils.aliases.Json
import core.db._
import core.db.ExPostgresDriver.api._
import phoenix.models.account.User
import phoenix.models.inventory.{Sku, Skus}

case class ProductReview(id: Int = 0,
scope: LTree,
Expand Down Expand Up @@ -41,8 +43,11 @@ object ProductReviews
extends FoxTableQuery[ProductReview, ProductReviews](new ProductReviews(_))
with ReturningId[ProductReview, ProductReviews] {

def findOneByUserAndSku(userId: Int, skuId: Int): DBIO[Option[ProductReview]] =
def findOneByUserAndSku(userId: User#Id, skuId: Sku#Id): DBIO[Option[ProductReview]] =
filter(_.userId === userId).filter(_.skuId === skuId).result.headOption

def findAllWithSkusByUser(userId: User#Id): DBIO[Seq[(ProductReview, Sku)]] =
filter(_.userId === userId).join(Skus).on(_.skuId === _.id).result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's actually account.id according to source from routes


val returningLens: Lens[ProductReview, Int] = lens[ProductReview].id
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package phoenix.responses
import phoenix.models.review.ProductReview
import phoenix.utils.aliases.Json

object ProductReviewResponses {
case class ProductReviewResponse(id: Int, sku: String, userId: Int, attributes: Json) extends ResponseItem

object ProductReviewResponse {
def build(review: ProductReview, skuCode: String): ProductReviewResponse =
ProductReviewResponse(review.id, skuCode, review.userId, review.content)
}

case class ProductReviewResponse(id: Int, sku: String, userId: Int, attributes: Json) extends ResponseItem
case class ProductReviewsResponse(reviews: Seq[ProductReviewResponse]) extends ResponseItem

object ProductReviewsResponse {
def build(reviews: Seq[ProductReviewResponse]): ProductReviewsResponse =
ProductReviewsResponse(reviews)
}
10 changes: 10 additions & 0 deletions phoenix-scala/phoenix/app/phoenix/routes/Customer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ object Customer {
ProductReviewManager.createProductReview(auth.account.id, payload)
}
} ~
(get & pathEnd) {
getOrFailures {
ProductReviewManager.getAllReviewsForCustomer(auth.account.id)
}
} ~
(get & path(IntNumber) & pathEnd) { reviewId ⇒
getOrFailures {
ProductReviewManager.getReviewForCustomer(auth.account.id, reviewId)
}
} ~
(path(IntNumber) & patch & entity(as[UpdateProductReviewPayload]) & pathEnd) {
(reviewId, payload) ⇒
mutateOrFailures {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package phoenix.services.review

import java.time.Instant

import core.db._
import phoenix.failures.ProductReviewFailures._
import phoenix.models.account.{Scope, User}
import phoenix.models.inventory.Skus
import phoenix.models.review.{ProductReview, ProductReviews}
import phoenix.payloads.ProductReviewPayloads._
import phoenix.responses.ProductReviewResponses
import phoenix.responses.ProductReviewResponses.ProductReviewResponse
import phoenix.responses.{ProductReviewResponse, ProductReviewsResponse}
import phoenix.utils.aliases._
import core.db._

object ProductReviewManager {

Expand All @@ -19,7 +18,25 @@ object ProductReviewManager {
for {
review ← * <~ ProductReviews.mustFindById404(reviewId)
sku ← * <~ Skus.mustFindById404(review.skuId)
} yield ProductReviewResponses.build(review, sku.code)
} yield ProductReviewResponse.build(review, sku.code)

def getReviewForCustomer(
userId: User#Id,
reviewId: ProductReview#Id)(implicit ec: EC, oc: OC, db: DB): DbResultT[ProductReviewResponse] =
for {
review ← * <~ ProductReviews.mustFindById404(reviewId)
sku ← * <~ Skus.mustFindById404(review.skuId)
_ ← * <~ failIf(userId != review.userId, FetchProductReviewUserMismatch(reviewId))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check this right after you get review (before getting SKU).

} yield ProductReviewResponse.build(review, sku.code)

def getAllReviewsForCustomer(
userId: User#Id)(implicit ec: EC, oc: OC, db: DB): DbResultT[ProductReviewsResponse] =
for {
reviewsWithSkus ← * <~ ProductReviews.findAllWithSkusByUser(userId)
reviewResponses = reviewsWithSkus.map {
case (review, sku) ⇒ ProductReviewResponse.build(review, sku.code)
}
} yield ProductReviewsResponse(reviewResponses)

def createProductReview(userId: User#Id, payload: CreateProductReviewPayload)(
implicit ec: EC,
Expand All @@ -29,16 +46,16 @@ object ProductReviewManager {
for {
scope ← * <~ Scope.resolveOverride(payload.scope)
sku ← * <~ Skus.mustFindByCode(payload.sku)
productReview ← * <~ ProductReviews
.findOneByUserAndSku(userId, sku.id)
.findOrCreate(
ProductReviews.create(
ProductReview(scope = scope,
content = payload.attributes,
userId = userId,
skuId = sku.id)))
sku ← * <~ Skus.mustFindById404(productReview.skuId)
} yield ProductReviewResponses.build(productReview, sku.code)
review ← * <~ ProductReviews
.findOneByUserAndSku(userId, sku.id)
.findOrCreate(
ProductReviews.create(
ProductReview(scope = scope,
content = payload.attributes,
userId = userId,
skuId = sku.id)))
sku ← * <~ Skus.mustFindById404(review.skuId)
} yield ProductReviewResponse.build(review, sku.code)

def updateProductReview(reviewId: ProductReview#Id, payload: UpdateProductReviewPayload)(
implicit ec: EC,
Expand All @@ -48,12 +65,12 @@ object ProductReviewManager {
for {
review ← * <~ ProductReviews.mustFindById404(reviewId)
_ ← * <~ failIf(review.archivedAt.isDefined, ProductReviewIsArchived(reviewId))
_ ← * <~ failIf(au.account.id != review.userId, ProductReviewUserMismatch(reviewId))
_ ← * <~ failIf(au.account.id != review.userId, UpdateProductReviewUserMismatch(reviewId))
newValue ← * <~ ProductReviews.update(
review,
review.copy(content = payload.attributes, updatedAt = Instant.now))
sku ← * <~ Skus.mustFindById404(review.skuId)
} yield ProductReviewResponses.build(newValue, sku.code)
} yield ProductReviewResponse.build(newValue, sku.code)

def archiveByContextAndId(reviewId: ProductReview#Id)(implicit ec: EC, oc: OC, db: DB): DbResultT[Unit] =
for {
Expand Down