Skip to content

Commit

Permalink
Add resume link + make model support longer labels
Browse files Browse the repository at this point in the history
  • Loading branch information
sten committed Apr 8, 2024
1 parent 8ea9ac8 commit 6401ea6
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions config/surveyhero.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
[
'survey_id' => 1234567,
'collectors' => [9876543], //optionally, see https://developer.surveyhero.com/api/#collector-api
'use_resume_link' => false,
'questions' => [
1000001 => [
'question_id' => 1000001,
Expand Down
4 changes: 3 additions & 1 deletion database/migrations/create_surveyhero_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function up()
$table->json('collector_ids')->nullable();
$table->json('question_mapping')->nullable();
$table->datetime('survey_last_imported')->nullable();
$table->boolean('use_resume_link')->default(false);

$table->timestamps();
});
Expand All @@ -35,6 +36,7 @@ public function up()
$table->string('survey_language')->nullable();
$table->boolean('survey_completed')->default(false);
$table->json('surveyhero_link_parameters')->nullable();
$table->string('resume_link')->nullable();

$table->foreignId($tableNames['surveys']['foreign_key'])
->constrained($tableNames['surveys']['name'])
Expand Down Expand Up @@ -63,7 +65,7 @@ public function up()
->constrained($tableNames['survey_questions']['name'])
->onDelete('cascade');
$table->bigInteger('surveyhero_answer_id')->nullable();
$table->string('converted_string_value')->nullable();
$table->text('converted_string_value')->nullable();
$table->integer('converted_int_value')->nullable();
$table->json('label')->nullable();
$table->timestamps();
Expand Down
3 changes: 1 addition & 2 deletions database/migrations/create_unique_surveyhero_id_indices.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function up()
});

Schema::table($tableNames['survey_question_responses']['name'], function (Blueprint $table) {
$table->unique(['survey_response_id', 'survey_question_id', 'survey_answer_id'])
->name('survey_question_responses_ternary_ids_idx'); //for some dbs the id name generated by laravel is too long
$table->unique(['survey_response_id', 'survey_question_id', 'survey_answer_id'], 'survey_question_responses_ternary_ids_idx'); //for some dbs the id name generated by laravel is too long
});
}
}
1 change: 1 addition & 0 deletions src/Contracts/SurveyContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @property Carbon|null $survey_last_imported
* @property Carbon $created_at
* @property Carbon $updated_at
* @property boolean $use_resume_link
* @property Collection $surveyResponses
* @property Collection $surveyQuestions
*/
Expand Down
15 changes: 11 additions & 4 deletions src/Http/SurveyheroClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function getSurveyLanguages(string|int $surveyId): array
return $languages ? $languages->languages : [];
}

public function getResumeLink(string|int $surveyId, string|int $responseId): ?string
{
$resumeData = $this->fetchFromSurveyHero("surveys/$surveyId/responses/$responseId/resume");

return $resumeData->successful() ? json_decode($resumeData->body())->url : null;
}

public function listWebhooks(string|int $surveyId): ?array
{
$webhookData = $this->fetchFromSurveyHero(sprintf('surveys/%s/webhooks', $surveyId));
Expand Down Expand Up @@ -145,11 +152,11 @@ private function postToSurveyHero(string $urlPath, array $queryStringArgs = []):

$this->updateThrottle();

if ($response->successful()) {
return $response;
if (!$response->successful()) {
throw new \Exception($response->body());
}

throw new \Exception($response->body());
return $response;
}

/**
Expand Down Expand Up @@ -178,7 +185,7 @@ private function preventThrottle(): void
if (Cache::has(self::CACHE_LATEST_REQUEST_TIME_KEY)) {
//usleep is in microseconds, 1000000 is 1s.
//Surveyhero only allows 2 requests per second.
$sleepTime = 1000000 - (Carbon::now()->getTimestampMs() - Cache::get(self::CACHE_LATEST_REQUEST_TIME_KEY)) * 1000;
$sleepTime = abs(1000000 - (Carbon::now()->getTimestampMs() - Cache::get(self::CACHE_LATEST_REQUEST_TIME_KEY)) * 1000);
usleep($sleepTime);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Info/ResponseImportInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function addUnimportedQuestion(int $questionId, string $errorInfo): void
$this->unimportedQuestions[$questionId] = $errorInfo;
}

public function increateTotalResponses()
public function increaseTotalResponses()
{
$this->totalResponsesImported++;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Services/SurveyImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ public function importSurveys(?Collection $surveyIdsToImport): array

public function updateOrCreateSurvey(\stdClass $surveyheroSurvey): SurveyContract
{
//check if the config has settings for this survey:
$questionMapping = collect(config('surveyhero.question_mapping'));
$surveyConfig = $questionMapping->filter(function ($elem) use ($surveyheroSurvey) {
return $elem['survey_id'] == $surveyheroSurvey->survey_id;
})->first();

return app(SurveyheroRegistrar::class)->getSurveyClass()::updateOrCreate([
'surveyhero_id' => $surveyheroSurvey->survey_id,
], [
'name' => $surveyheroSurvey->title,
'use_resume_link' => data_get($surveyConfig, 'use_resume_link', false),
]);
}
}
2 changes: 1 addition & 1 deletion src/Services/SurveyResponseImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function importSurveyResponse($responseId, SurveyContract $survey, ?array
}
}

$importInfo->increateTotalResponses();
$importInfo->increaseTotalResponses();

//increase survey last updated timestamp:
$responseLastUpdatedOn = $this->client->transformAPITimestamp($responseAnswers->last_updated_on);
Expand Down

0 comments on commit 6401ea6

Please sign in to comment.