Skip to content

Commit

Permalink
Merge pull request #116 from plentymarkets/fix/verified_guest
Browse files Browse the repository at this point in the history
Fix/verified guest
  • Loading branch information
felixgehrmann authored Mar 26, 2021
2 parents 2392f74 + 3999ea7 commit 2365524
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 47 deletions.
12 changes: 12 additions & 0 deletions meta/documents/changelog_de.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Release Notes für Feedback

## v3.6.3 (2021-03-26)

### TODO

- Zur Nutzung des Feedback-Plugins muss Ceres in Version 5.0 oder höher installiert sein.
- Nach dem Update auf Version 3.6.3 müssen Widgets im Menü **CMS » ShopBuilder** durch Klick auf **Inhalte neu generieren** aktualisiert werden.

### Behoben

- Bewertungen von Gastbestellungen konnten nicht verifiziert werden. Dies wurde behoben.
- Durch fehlerhaft gecachte Daten wurden Kundenbewertungen auf der Bestellbestätigung teilweise falsch zugeordnet.

## v3.6.2 (2021-03-03)

### TODO
Expand Down
14 changes: 14 additions & 0 deletions meta/documents/changelog_en.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Release Notes for Feedback

## v3.6.3 (2021-03-26)

### TODO

- This version of the feedback plugin is only compatible with Ceres v5.0 or higher.
- After updating the feedback plugin to v3.6.3, it is necessary to re-generate ShopBuilder widgets via the **Regenerate contents** button in the **CMS » ShopBuilder** menu.

### Fixed

- Feedbacks could not be verified, when written from a guests order confirmation. This has been fixed.
- Incorrectly cached data lead to wrongly linked feedbacks on the order confirmation.



## v3.6.2 (2021-03-03)

### TODO
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.6.2",
"version": "3.6.3",
"name": "Feedback",
"marketplaceName": {
"de": "Kunden-Feedback",
Expand Down
4 changes: 2 additions & 2 deletions resources/views/Widgets/FeedbackOrderWidget.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
:item-images="{{ Twig.print("data.itemImages | json_encode") }}"
:options="{{ options | json_encode }}"
:split-item-bundles="{{ Twig.print("splitItemBundle | json_encode") }}"
access-key="{{ request.get("accessKey") }}"
order-id="{{ request.get("orderId") }}">
access-key="{{ Twig.print(Twig.call("feedbackServices.feedback.getOrderAccessKey", [Twig.var("data.order.id")])) }}"
order-id="{{ Twig.print("data.order.id | json_encode") }}">
</feedback-order-container>
</div>
{{ Twig.elseif("#{ isPreview | json_encode } and not urls.is('confirmation')") }}
Expand Down
109 changes: 65 additions & 44 deletions src/Services/FeedbackService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function __construct(
FeedbackAverageRepositoryContract $feedbackAverageRepository,
AccountService $accountService,
LocalizationRepositoryContract $localizationRepository
) {
)
{
$this->request = $request;
$this->coreHelper = $coreHelper;
$this->feedbackRepository = $feedbackRepository;
Expand Down Expand Up @@ -133,22 +134,22 @@ function () use ($accountService) {
return $accountService->getAccountContactId();
}
);
$order = null;

// Check if accessKey for order is available
if ($creatorContactId <= 0)
{
if ($creatorContactId <= 0) {
$orderId = $this->request->input('orderId');
$accessKey = $this->request->input('accessKey');

if(strlen($orderId) && strlen($accessKey))
{
$creatorContactId = $this->getUserIdFromOrder($orderId, $accessKey);
if ($orderId != '' && $accessKey != '') {
$order = $this->getOrder($orderId, $accessKey);
$creatorContactId = $this->getUserIdFromOrder($order);
}
}

$allowGuestFeedbacks = $this->coreHelper->configValueAsBool(FeedbackCoreHelper::KEY_ALLOW_GUEST_FEEDBACKS);

if (!$allowGuestFeedbacks && $creatorContactId == 0) {
if (!$allowGuestFeedbacks && $creatorContactId == 0 && $order == null) {
return 'Guests are not allowed to write feedbacks';
}

Expand Down Expand Up @@ -183,8 +184,8 @@ function () use ($accountService) {
}

// The following checks cannot be applied to guests
if ($creatorContactId != 0) {
$hasPurchased = $this->hasPurchasedVariation($creatorContactId, $this->request->input('targetId'), $allowFeedbacksOnlyIfPurchased);
if ($creatorContactId != 0 || !is_null($order)) {
$hasPurchased = !is_null($order) || $this->hasPurchasedVariation($creatorContactId, $this->request->input('targetId'));

if ($allowFeedbacksOnlyIfPurchased && !$hasPurchased) {
return 'Not allowed to create review without purchasing the item first';
Expand All @@ -196,22 +197,22 @@ function () use ($accountService) {
'feedbackRelationSourceId' => $options['feedbackRelationTargetId']
];
}
}

if (!empty($numberOfFeedbacks) && $numberOfFeedbacks != 0) {
// Get the feedbacks that this user created on this item
$countFeedbacksOfUserPerItem = $this->listFeedbacks(
1,
50,
[],
[
'sourceId' => $creatorContactId,
'targetId' => $options['feedbackRelationTargetId']
]
)->getTotalCount();

if ($countFeedbacksOfUserPerItem >= $numberOfFeedbacks) {
return 'Too many reviews';
}
if (!empty($numberOfFeedbacks) && $numberOfFeedbacks != 0 && $creatorContactId != 0) {
// Get the feedbacks that this user created on this item
$countFeedbacksOfUserPerItem = $this->listFeedbacks(
1,
1,
[],
[
'sourceId' => $creatorContactId,
'targetId' => $options['feedbackRelationTargetId']
]
)->getTotalCount();

if ($countFeedbacksOfUserPerItem >= $numberOfFeedbacks) {
return 'Too many reviews';
}
}

Expand Down Expand Up @@ -394,15 +395,14 @@ public function getAuthenticatedUserMulti($itemIds = [], $variationIds = [])

$contactId = $this->accountService->getAccountContactId();

if(!$contactId)
{
if (!$contactId) {
// Check for accessKey
$accessKey = $this->request->input("accessKey");
$orderId = $this->request->input("orderId");

if(strlen($orderId) && strlen($accessKey))
{
$contactId = $this->getUserIdFromOrder($orderId, $accessKey);
if (strlen($orderId) && strlen($accessKey)) {
$order = $this->getOrder($orderId, $accessKey);
$contactId = $this->getUserIdFromOrder($order);
}
}

Expand All @@ -414,7 +414,7 @@ public function getAuthenticatedUserMulti($itemIds = [], $variationIds = [])
if (count($variationIds)) {
if ($isLoggedIn && $allowFeedbacksOnlyIfPurchased) {
foreach ($variationIds as $variationId) {
$hasPurchased[$variationId] = $this->hasPurchasedVariation($contactId, $variationId, $allowFeedbacksOnlyIfPurchased);
$hasPurchased[$variationId] = $this->hasPurchasedVariation($contactId, $variationId);
}
} else {
foreach ($variationIds as $variationId) {
Expand Down Expand Up @@ -482,6 +482,20 @@ public function getAuthenticatedUser($itemId, $variationId)
return $result;
}

public function getOrderAccessKey($orderId)
{
$orderRepository = pluginApp(OrderRepositoryContract::class);
$authHelper = pluginApp(AuthHelper::class);

$accessKey = $authHelper->processUnguarded(
function () use ($orderRepository, $orderId) {
return $orderRepository->generateAccessKey($orderId);
}
);

return $accessKey;
}

/**
* Calculate if the user has reached the maximum amount of feedbacks for the given itemId
* @param $itemId
Expand Down Expand Up @@ -518,24 +532,22 @@ private function isFeedbackLimitReached($itemId, $contactId, $numberOfFeedbacks)
* @param $mandatoryPurchase
* @return bool
*/
private function hasPurchasedVariation($contactId, $variationId, $mandatoryPurchase)
private function hasPurchasedVariation($contactId, $variationId)
{
$hasPurchased = false;

if ($mandatoryPurchase) {
$orderRepository = pluginApp(OrderRepositoryContract::class);
$orders = $orderRepository->allOrdersByContact($contactId);
$purchasedVariations = [];
$orderRepository = pluginApp(OrderRepositoryContract::class);
$orders = $orderRepository->allOrdersByContact($contactId);
$purchasedVariations = [];

foreach ($orders->getResult() as $order) {
foreach ($order->orderItems as $orderItem) {
$purchasedVariations[] = $orderItem->itemVariationId;
}
foreach ($orders->getResult() as $order) {
foreach ($order->orderItems as $orderItem) {
$purchasedVariations[] = $orderItem->itemVariationId;
}

$hasPurchased = in_array($variationId, $purchasedVariations);
}

$hasPurchased = in_array($variationId, $purchasedVariations);

return $hasPurchased;
}

Expand Down Expand Up @@ -592,14 +604,23 @@ private function determineVisibility($releaseLevel, $creatorId = 1)
|| $releaseLevel === self::RELEASE_LEVEL_ALL;
}

private function getUserIdFromOrder($orderId, $accessKey)
/**
* @param $orderId
* @param $accessKey
* @return \Order|null
*/
private function getOrder($orderId, $accessKey)
{
/** @var OrderRepositoryContract $orderRepository */
$orderRepository = pluginApp(OrderRepositoryContract::class);
$order = $orderRepository->findOrderByAccessKey($orderId, $accessKey);

if ($order !== null)
{
return $order;
}

private function getUserIdFromOrder($order)
{
if ($order !== null) {
foreach ($order->relations as $relation) {
if ($relation['referenceType'] === 'contact' && (int)$relation['referenceId'] > 0) {
return $relation['referenceId'];
Expand Down

0 comments on commit 2365524

Please sign in to comment.