Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form Templates for self hosted #546

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions api/app/Http/Controllers/Forms/TemplateController.php
Original file line number Diff line number Diff line change
@@ -7,10 +7,34 @@
use App\Models\Template;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\Controller;

class TemplateController extends Controller
{
private function getProdTemplates($slug = false)
{
if (!config('app.self_hosted')) {
return [];
}

$prodTemplates = \Cache::remember('prod_templates', 3600, function () {
$response = Http::get('https://api.opnform.com/templates');
if ($response->successful()) {
return collect($response->json())->map(function ($item) {
$item['from_prod'] = true;
return $item;
})->toArray();
}
});
if ($slug) {
return collect($prodTemplates)->filter(function ($item) use ($slug) {
return $item['slug'] === $slug;
})->toArray();
}
return $prodTemplates;
}
Comment on lines +15 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review the new getProdTemplates method for potential improvements.

The method getProdTemplates is well-implemented with caching and error handling. However, consider the following improvements:

  • Security: Ensure that the API endpoint https://api.opnform.com/templates is secure and trusted.
  • Error Handling: Add more robust error handling for cases where the API call fails or returns an unexpected response.
  • Performance: Verify the performance implications of this method, especially under high load, to ensure that the caching mechanism is effective.

Consider adding more detailed logging to help with debugging and monitoring the API calls.


public function index(Request $request)
{
$limit = (int) $request->get('limit', 0);
@@ -37,7 +61,10 @@ public function index(Request $request)

$templates = $query->orderByDesc('created_at')->get();

return FormTemplateResource::collection($templates);
return response()->json(array_merge(
FormTemplateResource::collection($templates)->toArray($request),
$this->getProdTemplates()
));
}

public function create(FormTemplateRequest $request)
@@ -83,8 +110,7 @@ public function destroy($id)

public function show(string $slug)
{
return new FormTemplateResource(
Template::whereSlug($slug)->firstOrFail()
);
$template = Template::whereSlug($slug)->first();
return ($template) ? new FormTemplateResource($template) : $this->getProdTemplates($slug);
}
}
1 change: 1 addition & 0 deletions client/pages/templates/[slug].vue
Original file line number Diff line number Diff line change
@@ -334,6 +334,7 @@ const canEditTemplate = computed(
() =>
authStore.check &&
template.value &&
template.value?.from_prod !== true &&
(authStore.user.admin ||
authStore.user.template_editor ||
template.value.creator_id === authStore.user.id),