Skip to content

Commit

Permalink
Merge pull request #1 from ThomasDaSilva/api
Browse files Browse the repository at this point in the history
Add api connection for site_review product_review and export order
  • Loading branch information
zawaze authored Jan 26, 2024
2 parents 71f56a6 + c86a425 commit c67dcc0
Show file tree
Hide file tree
Showing 49 changed files with 1,116 additions and 1,702 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: "Auto Release"
on:
push:
branches: [ master, main ]
jobs:
release:
uses: thelia-modules/ReusableWorkflow/.github/workflows/auto_release.yml@main
2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

81 changes: 49 additions & 32 deletions Api/GuaranteedOpinionClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@

namespace GuaranteedOpinion\Api;

use GuaranteedOpinion\GuaranteedOpinion as GuaranteedOpinionModule;
use Thelia\Model\Base\Product;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Customer;
use Thelia\Model\OrderProduct;
use GuaranteedOpinion\GuaranteedOpinion;
use Thelia\Model\Order;

/**
* Class GuaranteedOpinionClient
Expand All @@ -25,37 +22,57 @@
*/
class GuaranteedOpinionClient
{
private const URL_API = "https://api.guaranteed-reviews.com/";
private const URL_API_REVIEW = "public/v3/reviews";
private const URL_API_ORDER = "private/v3/orders";

const URL_API = "https://www.societe-des-avis-garantis.fr/";
const SAGAPIENDPOINT = "wp-content/plugins/ag-core/api/";

function getLast(int $howMany, ?Product $product = null) {
//todo: call guaranteed-opinions api to get the last $howMany reviews by product or global
// $url = "https://www.guaranteed-opinions.com/api/last/$howMany";
// if ($product) {
// $url .= "?product_id=" . $product->getId();
// }
// $curl = curl_init($url);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// $response = curl_exec($curl);
// curl_close($curl);
// return json_decode($response);
}
/**
* Call API Avis-Garantis
* Return all the reviews of the store or productId
*
* @param string $scope 'site' or productId
* @return array
* @throws \JsonException
*/
public function getReviewsFromApi(string $scope = 'site'): array
{
$url = self::URL_API . "/" . self::URL_API_REVIEW . "/" . GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_REVIEW) . "/" . $scope;

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close($ch);

function postOrder(Customer $customer, OrderProduct $orderProduct) {
//todo send this new thelia order to guaranteed-opinions
return json_decode($response, false, 512, JSON_THROW_ON_ERROR)->reviews;
}

function tokenCheck(){
$domainUrl = $this::URL_API;
$apiKey = ConfigQuery::read(GuaranteedOpinionModule::CONFIG_API_SECRET);
$url = $domainUrl . $this::SAGAPIENDPOINT . "checkToken.php?token=" . $token . "&apiKey=" . $apiKey;
/**
* @throws \JsonException
*/
public function sendOrder($jsonOrder)
{
$url = self::URL_API . "/" . self::URL_API_ORDER;

$request = [
'api_key' => GuaranteedOpinion::getConfigValue(GuaranteedOpinion::CONFIG_API_ORDER),
'orders' => $jsonOrder
];

// Prepare CURL request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
return curl_exec($ch);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

// Execute CURL request
$response = curl_exec($ch);

// Close the connection, release resources used
curl_close($ch);

return json_decode($response, false, 512, JSON_THROW_ON_ERROR);
}
}
50 changes: 50 additions & 0 deletions Command/GetProductReviewCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace GuaranteedOpinion\Command;

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

class GetProductReviewCommand extends ContainerAwareCommand
{
public function __construct(
protected GuaranteedOpinionClient $client,
protected ProductReviewService $productReviewService
) {
parent::__construct();
}

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

public function execute(InputInterface $input, OutputInterface $output): int
{
try {
$products = ProductQuery::create()->findByVisible(1);

foreach ($products as $product)
{
$productReviews = $this->client->getReviewsFromApi($product->getId());

if ($productReviews !== [])
{
$this->productReviewService->addGuaranteedOpinionProductReviews($productReviews, $product->getId());
}
}

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

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

namespace GuaranteedOpinion\Command;

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

class SendOrderCommand extends ContainerAwareCommand
{
public function __construct(
protected GuaranteedOpinionClient $client,
protected OrderService $orderService
) {
parent::__construct();
}

public function configure(): void
{
$this
->setName('module:GuaranteedOpinion:SendOrder')
->setDescription('Send orders to API Avis-Garantis');
}

public function execute(InputInterface $input, OutputInterface $output): int
{
try {
$order = $this->orderService->prepareOrderRequest();

$response = $this->client->sendOrder($order);

if ($response->success === 1)
{
$output->write("Orders sent with success\n");
}

if ($response->success === 0)
{
$output->write("Error\n");
}

$output->write("Orders imported : " . $response->orders_count ."\n");
$output->write("Products imported : " . $response->products_imported ."\n");
$output->write("Message : " . $response->message ."\n");

if ($response->success === 1)
{
$this->orderService->clearOrderQueueTable();
$output->write("Order Queue is now empty\n");
}
} catch (Exception $exception) {
$output->write($exception->getMessage());
}

return 1;
}
}
45 changes: 45 additions & 0 deletions Config/TheliaMain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

# 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;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
45 changes: 0 additions & 45 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,4 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">

<forms>
<form name="guaranteed_opinion.configuration" class="GuaranteedOpinion\Form\GuaranteedOpinionConfigurationForm" />
</forms>

<services>
<service id="guaranteed_opinion.order.listener" class="GuaranteedOpinion\EventListeners\OrderListener">
<tag name="kernel.event_subscriber" />
</service>
<service id="guaranteed_opinion.order.manager" class="GuaranteedOpinion\Service\OrderManager">
<argument type="service" id="event_dispatcher"/>
<!-- <argument type="service" id="request"/>-->
</service>
<service id="guaranteed_opinion.product_review.service" class="GuaranteedOpinion\Service\ProductReviewManager">
</service>
<service id="guaranteed_opinion.site_review.service" class="GuaranteedOpinion\Service\SiteReviewManager">
</service>
<!-- <service id="guaranteed_opinion.product_review.smarty.plugin" class="GuaranteedOpinion\Smarty\ProductReviews">-->
<!-- <tag name="thelia.parser.register_plugin"/>-->
<!-- <argument type="service" id="guaranteed_opinion.product_review.service"/>-->
<!-- </service>-->
<!-- <service id="guaranteed_opinion.site_review.smarty.plugin" class="GuaranteedOpinion\Smarty\SiteReviews">-->
<!-- <tag name="thelia.parser.register_plugin"/>-->
<!-- <argument type="service" id="guaranteed_opinion.site_review.service"/>-->
<!-- </service>-->
</services>

<hooks>
<hook id="guaranteed_opinion.hook.front" class="GuaranteedOpinion\Hook\FrontHook">
<!-- <tag name="hook.event_listener" event="main.footer-bottom" type="front" method="displayFooterLink" />-->
<!-- <tag name="hook.event_listener" event="main.body-top" type="front" method="displaySiteWidget" />-->
<!-- <tag name="hook.event_listener" event="product.additional" type="front" method="displayProductTabReview" />-->

<!-- <tag name="hook.event_listener" event="guaranteed_opinion.tagmanager" type="front" method="displayTag" />-->
<!-- <tag name="hook.event_listener" event="guaranteed_opinion.site.widget" type="front" method="displaySiteWidget" />-->
<!-- <tag name="hook.event_listener" event="guaranteed_opinion.product.iframe" type="front" method="displayProductIframe" />-->
<!-- <tag name="hook.event_listener" event="guaranteed_opinion.footer.link" type="front" method="displayFooterLink" />-->

<argument type="service" id="guaranteed_opinion.order.manager"/>
</hook>

<!-- <hook id="guaranteed_opinion.hook.back" class="GuaranteedOpinion\Hook\BackHook">-->
<!-- <tag name="hook.event_listener" event="order-edit.bottom" type="back" method="onOrderEditBottom" />-->
<!-- </hook>-->
</hooks>

</config>
22 changes: 15 additions & 7 deletions Config/module.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="http://thelia.net/schema/dic/module"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/module http://thelia.net/schema/dic/module/module-2_1.xsd">
xsi:schemaLocation="http://thelia.net/schema/dic/module http://thelia.net/schema/dic/module/module-2_2.xsd">
<fullnamespace>GuaranteedOpinion\GuaranteedOpinion</fullnamespace>
<descriptive locale="en_US">
<title>GuaranteedOpinion</title>
</descriptive>
<descriptive locale="fr_FR">
<title>GuaranteedOpinion</title>
<title>Avis Garantis</title>
</descriptive>
<languages>
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.0</version>
<author>
<name>Chabreuil Antoine</name>
<email>achabreuil@openstudio.fr, thelia@cqfdev.fr</email>
</author>
<authors>
<author>
<name>Chabreuil Antoine</name>
<email>achabreuil@openstudio.fr, thelia@cqfdev.fr</email>
</author>
<author>
<name>DA SILVA MENDONCA Thomas</name>
<email>tdasilva@openstudio.fr</email>
</author>
</authors>
<type>classic</type>
<thelia>2.5.2</thelia>
<thelia>2.5.0</thelia>
<stability>other</stability>
<mandatory>0</mandatory>
<hidden>0</hidden>
</module>
4 changes: 2 additions & 2 deletions Config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<!-- Thelia 2.0 backward compatibility -->
</routes>

</routes>
Loading

0 comments on commit c67dcc0

Please # to comment.