Skip to content

Commit

Permalink
- fix response update condition
Browse files Browse the repository at this point in the history
- fix refresh response import bug
  • Loading branch information
sten committed Sep 21, 2022
1 parent 25d9776 commit 1c4b6d1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Commands/SurveyheroResponseImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function __construct(SurveyResponseImportService $surveyResponseImportSer

public function handle(): int
{
$truncateResponses = $this->option('fresh');
$refreshResponses = $this->option('fresh');

if ($truncateResponses) {
if ($refreshResponses) {
$this->deleteResponses();
}

Expand All @@ -50,6 +50,10 @@ public function handle(): int
foreach ($surveys as $survey) {
/* @var SurveyContract $survey */
try {
if($refreshResponses){
$survey->survey_last_imported = null;
$survey->save();
}
$importInfo->addInfo($this->importService->importSurveyResponses($survey));
} catch (SurveyNotMappedException $exception) {
$this->error("{$exception->getMessage()} Survey '$survey->name' with Surveyhero ID $survey->surveyhero_id");
Expand Down
7 changes: 7 additions & 0 deletions src/Contracts/SurveyContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public function surveyQuestions(): HasMany;
public function completedResponses(): Collection;

public function hasResponses(): bool;

/**
* Checks if the response timestamp is more recent than the last updated survey timestamp.
* @param string $responseLastUpdatedIsoTimestamp
* @return bool
*/
public function doesResponseNeedsToBeUpdated(string $responseLastUpdatedIsoTimestamp): bool;
}
11 changes: 11 additions & 0 deletions src/Models/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statikbe\Surveyhero\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -45,4 +46,14 @@ public function hasResponses(): bool
{
return $this->surveyResponses()->count() > 0;
}

/**
* @inheritDoc
*/
public function doesResponseNeedsToBeUpdated(string $responseLastUpdatedIsoTimestamp): bool {
if($this->survey_last_imported) {
return Carbon::parse($responseLastUpdatedIsoTimestamp)->gt($this->survey_last_imported);
}
else return true;
}
}
2 changes: 1 addition & 1 deletion src/Services/SurveyResponseImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function importSurveyResponse($responseId, SurveyContract $survey, array
}

$responseAnswers = $this->client->getSurveyResponseAnswers($survey->surveyhero_id, $responseId);
if ($responseAnswers && Carbon::parse($responseAnswers->last_updated_on)->gt($survey->survey_last_imported)) {
if ($responseAnswers && $survey->doesResponseNeedsToBeUpdated($responseAnswers->last_updated_on)) {
//do not import already imported data that is not updated.
/* @var SurveyResponseContract $existingResponseRecord */
$existingResponseRecord = app(SurveyheroRegistrar::class)->getSurveyResponseClass()::where('surveyhero_id', $responseId)->first();
Expand Down

0 comments on commit 1c4b6d1

Please sign in to comment.