Skip to content

Commit

Permalink
Merge pull request #3 from ThomasDaSilva/feat/site_reviews
Browse files Browse the repository at this point in the history
Add Pagination for loop + Site Review import + loop and improve reviews import
  • Loading branch information
zawaze authored Feb 16, 2024
2 parents 57c3538 + 1ba1bc7 commit 5181e9f
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 31 deletions.
21 changes: 17 additions & 4 deletions Command/GetProductReviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,36 @@ public function configure(): void

public function execute(InputInterface $input, OutputInterface $output): int
{
$productReviewsAdded = 0;
$rowsTreated = 0;

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

$output->write("Product Review synchronization start \n");
foreach ($products as $product)
{
$productReviews = $this->client->getReviewsFromApi($product->getId());

if ($productReviews !== [])
{
$this->productReviewService->addGuaranteedOpinionProductReviews($productReviews, $product->getId());
if ($productReviews !== []) {
foreach ($productReviews as $productRow) {
if ($rowsTreated % 100 === 0) {
$output->write("Rows treated : " . $rowsTreated . "\n");
}
if ($this->productReviewService->addGuaranteedOpinionProductRow($productRow, $product->getId())) {
$productReviewsAdded ++;
}
$rowsTreated++;
}
}
}

} catch (Exception $exception) {
$output->write($exception->getMessage());
}

$output->write("End of Product Review synchronization\n");
$output->write("Product Reviews Added : " .$productReviewsAdded. "\n");

return 1;
}
}
54 changes: 54 additions & 0 deletions Command/GetSiteReviewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace GuaranteedOpinion\Command;

use Exception;
use GuaranteedOpinion\Api\GuaranteedOpinionClient;
use GuaranteedOpinion\Service\SiteReviewService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Thelia\Command\ContainerAwareCommand;

class GetSiteReviewCommand extends ContainerAwareCommand
{
public function __construct(
protected GuaranteedOpinionClient $client,
protected SiteReviewService $siteReviewService
) {
parent::__construct();
}

public function configure(): void
{
$this
->setName('module:GuaranteedOpinion:GetSiteReview')
->setDescription('Get site review from API Avis-Garantis');
}

public function execute(InputInterface $input, OutputInterface $output): int
{
$siteReviewsAdded = 0;

try {
$siteReviews = $this->client->getReviewsFromApi();

$output->write("Site Review synchronization start \n");

foreach ($siteReviews as $key => $siteRow) {
if ($key % 100 === 0) {
$output->write("Rows treated : " . $key . "\n");
}
if ($this->siteReviewService->addGuaranteedOpinionSiteRow($siteRow)) {
$siteReviewsAdded ++;
}
}
} catch (Exception $exception) {
$output->write($exception->getMessage());
}

$output->write("End of Site Review synchronization\n");
$output->write("Site Review Added : " .$siteReviewsAdded. "\n");

return 1;
}
}
22 changes: 22 additions & 0 deletions Config/TheliaMain.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,27 @@ CREATE TABLE `guaranteed_opinion_order_queue`
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- guaranteed_opinion_site_review
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `guaranteed_opinion_site_review`;

CREATE TABLE `guaranteed_opinion_site_review`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`site_review_id` INTEGER NOT NULL,
`name` VARCHAR(255),
`rate` DECIMAL(2,1) DEFAULT 0,
`review` VARBINARY(10000),
`review_date` DATETIME,
`order_id` VARCHAR(255),
`order_date` DATETIME,
`reply` VARCHAR(255),
`reply_date` DATETIME,
PRIMARY KEY (`id`,`site_review_id`),
UNIQUE INDEX `guaranteed_opinion_site_review_id_unique` (`site_review_id`)
) ENGINE=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
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.1</version>
<version>1.0.2</version>
<authors>
<author>
<name>Chabreuil Antoine</name>
Expand Down
17 changes: 17 additions & 0 deletions Config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,22 @@
<column name="status" type="INTEGER" />
</table>

<table name="guaranteed_opinion_site_review" namespace="GuaranteedOpinion\Model">
<column autoIncrement="true" name="id" primaryKey="true" type="INTEGER" required="true" />
<column name="site_review_id" primaryKey="true" type="INTEGER" required="true" />
<column name="name" type="VARCHAR" size="255" />
<column defaultValue="0" name="rate" scale="1" size="2" type="DECIMAL"/>
<column name="review" size="10000" sqlType="VARBINARY(10000)" type="VARCHAR"/>
<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_date" type="TIMESTAMP"/>

<unique name="guaranteed_opinion_site_review_id_unique">
<unique-column name="site_review_id"/>
</unique>
</table>

<external-schema filename="local/config/schema.xml" referenceOnly="true" />
</database>
66 changes: 66 additions & 0 deletions Config/update/1.0.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This is a fix for InnoDB in MySQL >= 4.1.x
# It "suspends judgement" for fkey relationships until are tables are set.
SET FOREIGN_KEY_CHECKS = 0;

-- ---------------------------------------------------------------------
-- guaranteed_opinion_product_review
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `guaranteed_opinion_product_review`;

CREATE TABLE `guaranteed_opinion_product_review`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`product_review_id` VARCHAR(55) NOT NULL,
`name` VARCHAR(255),
`rate` DECIMAL(2,1) DEFAULT 0,
`review` VARBINARY(10000),
`review_date` DATETIME,
`product_id` INTEGER,
`order_id` VARCHAR(255),
`order_date` DATETIME,
`reply` VARCHAR(255),
`reply_date` DATETIME,
PRIMARY KEY (`id`),
UNIQUE INDEX `guaranteed_opinion_product_review_id_unique` (`product_review_id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- guaranteed_opinion_order_queue
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `guaranteed_opinion_order_queue`;

CREATE TABLE `guaranteed_opinion_order_queue`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`order_id` INTEGER NOT NULL,
`treated_at` DATETIME,
`status` INTEGER,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- guaranteed_opinion_site_review
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `guaranteed_opinion_site_review`;

CREATE TABLE `guaranteed_opinion_site_review`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`site_review_id` INTEGER NOT NULL,
`name` VARCHAR(255),
`rate` DECIMAL(2,1) DEFAULT 0,
`review` VARBINARY(10000),
`review_date` DATETIME,
`order_id` VARCHAR(255),
`order_date` DATETIME,
`reply` VARCHAR(255),
`reply_date` DATETIME,
PRIMARY KEY (`id`,`site_review_id`),
UNIQUE INDEX `guaranteed_opinion_site_review_id_unique` (`site_review_id`)
) ENGINE=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
4 changes: 2 additions & 2 deletions Form/ConfigurationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function buildForm(): void
"site_review_hook_display",
TextType::class,
[
"data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY) ?: "main.content-bottom",
"data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::SITE_REVIEW_HOOK_DISPLAY),
"label"=>$translator->trans("Site review hook display", array(), GuaranteedOpinion::DOMAIN_NAME),
"required" => false,
]
Expand All @@ -123,7 +123,7 @@ protected function buildForm(): void
"product_review_hook_display",
TextType::class,
[
"data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY) ?: "product.bottom",
"data" => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::PRODUCT_REVIEW_HOOK_DISPLAY),
"label"=>$translator->trans("Product review hook display", array(), GuaranteedOpinion::DOMAIN_NAME),
"required" => false,
]
Expand Down
6 changes: 6 additions & 0 deletions GuaranteedOpinion.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ public static function log($msg): void
);
$logger->addAlert("MESSAGE => " . print_r($msg, true));
}

public function postActivation(ConnectionInterface $con = null): void
{
self::setConfigValue(self::SITE_REVIEW_HOOK_DISPLAY, 'main.content-bottom');
self::setConfigValue(self::PRODUCT_REVIEW_HOOK_DISPLAY, 'product.bottom');
}
}
13 changes: 9 additions & 4 deletions Loop/GuaranteedProductLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
class GuaranteedProductLoop extends BaseLoop implements PropelSearchLoopInterface
{

public function parseResults(LoopResult $loopResult): LoopResult
{
foreach ($loopResult->getResultDataCollection() as $review) {
Expand Down Expand Up @@ -50,21 +49,27 @@ public function buildModelCriteria(): GuaranteedOpinionProductReviewQuery|ModelC
$search = GuaranteedOpinionProductReviewQuery::create();

if (null !== $productId = $this->getProduct()) {
$search->filterByProductId($productId, Criteria::IN);
$search->filterByProductId($productId);
}

if (null !== $minRate = $this->getMinRate()) {
$search->filterByRate($minRate, Criteria::GREATER_EQUAL);
}

if ((null !== $page = $this->getPage()) && (null !== $limit = $this->getLimit())) {
$search->paginate($page, $limit);
}

return $search;
}

protected function getArgDefinitions(): ArgumentCollection
{
return new ArgumentCollection(
Argument::createIntListTypeArgument('product'),
Argument::createIntListTypeArgument('min_rate')
Argument::createIntTypeArgument('product'),
Argument::createIntTypeArgument('min_rate'),
Argument::createIntTypeArgument('limit', 5),
Argument::createIntTypeArgument('page', 0)
);
}
}
71 changes: 71 additions & 0 deletions Loop/GuaranteedSiteLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace GuaranteedOpinion\Loop;

use GuaranteedOpinion\Model\GuaranteedOpinionProductReviewQuery;
use GuaranteedOpinion\Model\GuaranteedOpinionSiteReviewQuery;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;

/**
* @method getMinRate()
* @method getPage()
* @method getLimit()
*/
class GuaranteedSiteLoop extends BaseLoop implements PropelSearchLoopInterface
{
public function parseResults(LoopResult $loopResult): LoopResult
{
foreach ($loopResult->getResultDataCollection() as $review) {
$loopResultRow = new LoopResultRow($review);

$loopResultRow
->set('ID', $review->getId())
->set('PRODUCT_REVIEW_ID', $review->getSiteReviewId())
->set('NAME', $review->getName())
->set('RATE', $review->getRate())
->set('REVIEW', $review->getReview())
->set('REVIEW_DATE', $review->getReviewDate()?->format('Y-m-d'))
->set('ORDER_ID', $review->getOrderId())
->set('ORDER_DATE', $review->getOrderDate()?->format('Y-m-d'))
->set('REPLY', $review->getReply())
->set('REPLY_DATE', $review->getReplyDate()?->format('Y-m-d'))
;
$this->addOutputFields($loopResultRow, $review);

$loopResult->addRow($loopResultRow);
}

return $loopResult;
}

public function buildModelCriteria(): GuaranteedOpinionProductReviewQuery|ModelCriteria
{
$search = GuaranteedOpinionSiteReviewQuery::create();

if (null !== $minRate = $this->getMinRate()) {
$search->filterByRate($minRate, Criteria::GREATER_EQUAL);
}

if ((null !== $page = $this->getPage()) && (null !== $limit = $this->getLimit())) {
$search->paginate($page, $limit);
}

return $search;
}

protected function getArgDefinitions(): ArgumentCollection
{
return new ArgumentCollection(
Argument::createIntTypeArgument('min_rate'),
Argument::createIntTypeArgument('limit', 5),
Argument::createIntTypeArgument('page', 0)
);
}
}
19 changes: 19 additions & 0 deletions Model/GuaranteedOpinionSiteReview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace GuaranteedOpinion\Model;

use GuaranteedOpinion\Model\Base\GuaranteedOpinionSiteReview as BaseGuaranteedOpinionSiteReview;

/**
* Skeleton subclass for representing a row from the 'guaranteed_opinion_site_review' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*/
class GuaranteedOpinionSiteReview extends BaseGuaranteedOpinionSiteReview
{

}
Loading

0 comments on commit 5181e9f

Please # to comment.