diff --git a/config/surveyhero.php b/config/surveyhero.php index 772a03f..75d27e3 100644 --- a/config/surveyhero.php +++ b/config/surveyhero.php @@ -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, diff --git a/database/migrations/create_surveyhero_tables.php b/database/migrations/create_surveyhero_tables.php index 9a715c2..0d6955a 100644 --- a/database/migrations/create_surveyhero_tables.php +++ b/database/migrations/create_surveyhero_tables.php @@ -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(); }); @@ -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']) @@ -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(); diff --git a/database/migrations/create_unique_surveyhero_id_indices.php b/database/migrations/create_unique_surveyhero_id_indices.php index 8c9269a..b81c520 100644 --- a/database/migrations/create_unique_surveyhero_id_indices.php +++ b/database/migrations/create_unique_surveyhero_id_indices.php @@ -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 }); } } diff --git a/src/Contracts/SurveyContract.php b/src/Contracts/SurveyContract.php index 6fc19f7..ae8d938 100644 --- a/src/Contracts/SurveyContract.php +++ b/src/Contracts/SurveyContract.php @@ -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 */ diff --git a/src/Http/SurveyheroClient.php b/src/Http/SurveyheroClient.php index b5f6fcd..3155167 100644 --- a/src/Http/SurveyheroClient.php +++ b/src/Http/SurveyheroClient.php @@ -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)); @@ -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; } /** @@ -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); } } diff --git a/src/Services/Info/ResponseImportInfo.php b/src/Services/Info/ResponseImportInfo.php index c4f5d6b..e241eff 100644 --- a/src/Services/Info/ResponseImportInfo.php +++ b/src/Services/Info/ResponseImportInfo.php @@ -51,7 +51,7 @@ public function addUnimportedQuestion(int $questionId, string $errorInfo): void $this->unimportedQuestions[$questionId] = $errorInfo; } - public function increateTotalResponses() + public function increaseTotalResponses() { $this->totalResponsesImported++; } diff --git a/src/Services/SurveyImportService.php b/src/Services/SurveyImportService.php index 670ab44..85aaeb8 100644 --- a/src/Services/SurveyImportService.php +++ b/src/Services/SurveyImportService.php @@ -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), ]); } } diff --git a/src/Services/SurveyResponseImportService.php b/src/Services/SurveyResponseImportService.php index 2b7ec81..e36a5a1 100644 --- a/src/Services/SurveyResponseImportService.php +++ b/src/Services/SurveyResponseImportService.php @@ -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);