Skip to content

Commit

Permalink
Merge pull request #18 from NicolasBarbey/main
Browse files Browse the repository at this point in the history
remove deleted product review, fix reply field
  • Loading branch information
NicolasBarbey authored Feb 17, 2025
2 parents 78552a9 + afb92e0 commit 3cac0ef
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 44 deletions.
25 changes: 24 additions & 1 deletion Command/GetProductReviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use GuaranteedOpinion\Api\GuaranteedOpinionClient;
use GuaranteedOpinion\Event\ProductReviewEvent;
use GuaranteedOpinion\Event\GuaranteedOpinionEvents;
use GuaranteedOpinion\Model\GuaranteedOpinionProductReview;
use GuaranteedOpinion\Model\GuaranteedOpinionProductReviewQuery;
use GuaranteedOpinion\Service\ProductReviewService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -34,14 +36,16 @@ public function execute(InputInterface $input, OutputInterface $output): int
$rowsTreated = 0;

try {
$products = ProductQuery::create()->findByVisible(1);
$products = ProductQuery::create()->filterById(6365)->find();

$output->write("Product Review synchronization start \n");
foreach ($products as $product)
{
$addProductReviewEvent = new ProductReviewEvent($product);
$this->getDispatcher()->dispatch($addProductReviewEvent, GuaranteedOpinionEvents::ADD_PRODUCT_REVIEW_EVENT);

$productReviews = GuaranteedOpinionProductReviewQuery::create()->filterByProductId($product->getId())->find();

$apiResponse = $this->client->getReviewsFromApi($addProductReviewEvent->getGuaranteedOpinionProductId());

if ($apiResponse['reviews'] !== []) {
Expand All @@ -57,6 +61,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
$rowsTreated++;
}
}

$this->removeDeletedReview($productReviews, $apiResponse['reviews']);
}
} catch (Exception $exception) {
$output->write($exception->getMessage());
Expand All @@ -67,4 +73,21 @@ public function execute(InputInterface $input, OutputInterface $output): int

return 1;
}

private function removeDeletedReview($productReviews, $apiResultReviews)
{
/** @var GuaranteedOpinionProductReview $productReview */
foreach ($productReviews as $productReview) {
$exist = false;
foreach ($apiResultReviews as $apiResultReview) {
if ((int)$apiResultReview['id'] === (int)$productReview->getProductReviewId()) {
$exist = true;
break;
}
}
if (!$exist){
$productReview->delete();
}
}
}
}
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.16</version>
<version>1.0.17</version>
<authors>
<author>
<name>Chabreuil Antoine</name>
Expand Down
4 changes: 2 additions & 2 deletions Config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<column name="product_id" type="INTEGER"/>
<column name="order_id" type="VARCHAR" size="255"/>
<column name="order_date" type="TIMESTAMP"/>
<column name="reply" type="VARCHAR" size="255"/>
<column name="reply" size="10000" sqlType="VARBINARY(10000)" type="VARCHAR"/>
<column name="reply_date" type="TIMESTAMP"/>

<unique name="guaranteed_opinion_product_review_id_unique">
Expand All @@ -37,7 +37,7 @@
<column name="review_date" type="TIMESTAMP"/>
<column name="order_id" type="VARCHAR" size="255"/>
<column name="order_date" type="TIMESTAMP"/>
<column name="reply" type="VARCHAR" size="255"/>
<column name="reply" size="10000" sqlType="VARBINARY(10000)" type="VARCHAR"/>
<column name="reply_date" type="TIMESTAMP"/>

<unique name="guaranteed_opinion_site_review_id_unique">
Expand Down
12 changes: 12 additions & 0 deletions Config/update/1.0.17.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SET FOREIGN_KEY_CHECKS = 0;

-- ---------------------------------------------------------------------
-- guaranteed_opinion_product_rating
-- ---------------------------------------------------------------------

ALTER TABLE guaranteed_opinion_product_review MODIFY reply VARBINARY(10000);

ALTER TABLE guaranteed_opinion_site_review MODIFY reply VARBINARY(10000);

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
39 changes: 18 additions & 21 deletions Service/ProductReviewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,33 @@ public function addGuaranteedOpinionProductReviews(array $productReviews, int $p

public function addGuaranteedOpinionProductRow($row, int $productId): bool
{
$review = GuaranteedOpinionProductReviewQuery::create()
->findOneByProductReviewId($row['id']);

if (null !== $review) {
return false;
}

$review = new GuaranteedOpinionProductReview();

try {
$review
->setProductReviewId($row['id'])
->setName($row['c'])
->setReview($row['txt'])
->setReviewDate($row['date'])
->setRate($row['r'])
->setOrderId($row['o'])
->setOrderDate($row['odate'])
->setProductId($productId)
;
$review = GuaranteedOpinionProductReviewQuery::create()
->findOneByProductReviewId($row['id']);

if (null === $review) {
$review = new GuaranteedOpinionProductReview();
$review
->setProductReviewId($row['id'])
->setName($row['c'])
->setReview($row['txt'])
->setReviewDate($row['date'])
->setRate($row['r'])
->setOrderId($row['o'])
->setOrderDate($row['odate'])
->setProductId($productId)
;
$review->save();
}

if ($row['reply'] !== "" && $row['rdate'] !== "") {
$review
->setReply($row['reply'])
->setReplyDate($row['rdate'])
;
$review->save();
}

$review->save();

} catch (PropelException $e) {
GuaranteedOpinion::log($e->getMessage());
return false;
Expand Down
35 changes: 16 additions & 19 deletions Service/SiteReviewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,32 @@ public function addGuaranteedOpinionSiteReviews($siteReviews): void
*/
public function addGuaranteedOpinionSiteRow($row): bool
{
$review = GuaranteedOpinionSiteReviewQuery::create()
->findOneBySiteReviewId($row["id"]);

if (null !== $review) {
return false;
}

try {
$review = new GuaranteedOpinionSiteReview();
$review = GuaranteedOpinionSiteReviewQuery::create()
->findOneBySiteReviewId($row["id"]);

$review
->setSiteReviewId($row["id"])
->setName($row["c"])
->setReview($row["txt"])
->setReviewDate($row["date"])
->setRate($row["r"])
->setOrderId($row["o"])
->setOrderDate($row["odate"])
;
if (null === $review) {
$review = new GuaranteedOpinionSiteReview();
$review
->setSiteReviewId($row["id"])
->setName($row["c"])
->setReview($row["txt"])
->setReviewDate($row["date"])
->setRate($row["r"])
->setOrderId($row["o"])
->setOrderDate($row["odate"])
;
$review->save();
}

if ($row["reply"] !== "" && $row["rdate"] !== "") {
$review
->setReply($row["reply"])
->setReplyDate($row["rdate"])
;
$review->save();
}

$review->save();

} catch (PropelException $e) {
GuaranteedOpinion::log($e->getMessage());
return false;
Expand Down

0 comments on commit 3cac0ef

Please # to comment.