@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\Sector;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function show(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'locale' => ['nullable', 'string', Rule::in(['pt', 'en'])],
|
||||
]);
|
||||
|
||||
$locale = $validated['locale'] ?? 'pt';
|
||||
|
||||
$data = Cache::remember("portal_home_v2_{$locale}", now()->addMinutes(10), function () {
|
||||
$sectors = Sector::withCount([
|
||||
'opportunities as opportunities_count' => fn ($q) => $q->published(),
|
||||
])->where('is_active', true)->orderBy('sort_order')->get();
|
||||
|
||||
$featured = Opportunity::with('sector:id,name,slug')
|
||||
->published()->where('is_featured', true)
|
||||
->latest('published_at')->limit(6)->get();
|
||||
|
||||
return [
|
||||
'sectors' => $sectors->map(fn ($s) => [
|
||||
'name' => $s->name,
|
||||
'slug' => $s->slug,
|
||||
'shortDescription' => $s->short_description,
|
||||
'opportunitiesCount' => $s->opportunities_count,
|
||||
'url' => "/sectores/{$s->slug}",
|
||||
])->values()->all(),
|
||||
'featuredOpportunities' => $featured->map(fn ($o) => [
|
||||
'reference' => $o->reference,
|
||||
'title' => $o->title,
|
||||
'sector' => $o->sector?->name,
|
||||
'province' => $o->province,
|
||||
'maturity' => $o->maturity_label,
|
||||
'estimatedInvestment' => [
|
||||
'amount' => $o->estimated_investment_amount,
|
||||
'currency' => $o->estimated_investment_currency,
|
||||
],
|
||||
'url' => "/oportunidades/{$o->slug}",
|
||||
])->values()->all(),
|
||||
'stats' => [
|
||||
'opportunities' => Opportunity::published()->count(),
|
||||
'sectors' => Sector::where('is_active', true)->count(),
|
||||
'provinces' => Opportunity::published()->distinct('province')->count('province'),
|
||||
],
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['success' => true, 'locale' => $locale, 'data' => $data]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\StoreInterestRequestRequest;
|
||||
use App\Jobs\NotifyInterestReceived;
|
||||
use App\Models\InterestRequest;
|
||||
use App\Models\Opportunity;
|
||||
use App\Services\IdempotencyConflict;
|
||||
use App\Services\InterestSubmission;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class InterestRequestController extends Controller
|
||||
{
|
||||
public function store(StoreInterestRequestRequest $request, InterestSubmission $submission): JsonResponse
|
||||
{
|
||||
$validated = $request->validated();
|
||||
$key = $request->header('Idempotency-Key');
|
||||
Validator::make(['idempotency_key' => $key], ['idempotency_key' => [$key === null ? 'nullable' : 'required', 'uuid']])->validate();
|
||||
|
||||
try {
|
||||
$interestRequest = $submission->receive($validated, $key, function () use ($request, $validated) {
|
||||
$opportunity = ! empty($validated['opportunityReference'])
|
||||
? Opportunity::published()->where('reference', $validated['opportunityReference'])->first()
|
||||
: null;
|
||||
if (! empty($validated['opportunityReference']) && ! $opportunity) {
|
||||
throw ValidationException::withMessages(['opportunityReference' => 'A oportunidade seleccionada já não está disponível.']);
|
||||
}
|
||||
|
||||
$interest = InterestRequest::create([
|
||||
'opportunity_id' => $opportunity?->id,
|
||||
'reference' => $this->generateReference(),
|
||||
'full_name' => trim($validated['fullName']),
|
||||
'email' => strtolower(trim($validated['email'])),
|
||||
'phone' => $validated['phone'] ?? null,
|
||||
'investor_type' => $validated['investorType'],
|
||||
'country' => $validated['country'],
|
||||
'sector_interest' => $validated['sectorInterest'],
|
||||
'opportunity_reference' => $validated['opportunityReference'] ?? null,
|
||||
'investment_range' => $validated['investmentRange'] ?? null,
|
||||
'message' => trim($validated['message']),
|
||||
'privacy_consent' => (bool) $validated['privacyConsent'],
|
||||
'consented_at' => now(),
|
||||
'status' => 'received',
|
||||
'source_page' => Str::limit($request->headers->get('referer', ''), 255, ''),
|
||||
'ip_address' => $request->ip(),
|
||||
'user_agent' => $request->userAgent(),
|
||||
]);
|
||||
if (config('portal.notifications_enabled')) {
|
||||
$connection = config('queue.connections.database.connection');
|
||||
if (config('queue.connections.database.after_commit') || ($connection && $connection !== config('database.default'))) {
|
||||
throw new \RuntimeException('Portal notifications require the transactional database queue.');
|
||||
}
|
||||
foreach (array_unique(config('portal.notification_recipients')) as $recipient) {
|
||||
if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
|
||||
Queue::connection('database')->push(new NotifyInterestReceived($interest->id, $recipient));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $interest;
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Manifestação de interesse registada com sucesso.',
|
||||
'data' => [
|
||||
'reference' => $interestRequest->reference,
|
||||
'status' => 'received',
|
||||
],
|
||||
], 201);
|
||||
} catch (IdempotencyConflict) {
|
||||
return response()->json(['success' => false, 'message' => 'A chave de envio já foi usada com dados diferentes.'], 409);
|
||||
} catch (ValidationException $exception) {
|
||||
throw $exception;
|
||||
} catch (\Throwable $exception) {
|
||||
Log::error('Erro ao registar manifestação de interesse', [
|
||||
'exception_type' => $exception::class,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Não foi possível registar o pedido. Tente novamente mais tarde.',
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function generateReference(): string
|
||||
{
|
||||
$year = now()->format('Y');
|
||||
|
||||
return 'IR-'.$year.'-'.Str::ulid();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Opportunity;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OpportunityController extends Controller
|
||||
{
|
||||
public function search(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'q' => ['nullable', 'string', 'max:120'],
|
||||
'sector' => ['nullable', 'string', 'max:80'],
|
||||
'province' => ['nullable', 'string', 'max:80'],
|
||||
'maturity_level' => ['nullable', 'integer', 'between:1,6'],
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:50'],
|
||||
'min_amount' => ['nullable', 'numeric', 'min:0', 'max:999999999999'],
|
||||
'max_amount' => ['nullable', 'numeric', 'min:0', 'max:999999999999', ...($request->filled('min_amount') ? ['gte:min_amount'] : [])],
|
||||
'currency' => ['nullable', 'required_with:min_amount,max_amount', 'in:MZN,USD,EUR,ZAR'],
|
||||
]);
|
||||
|
||||
$query = Opportunity::with('sector:id,name,slug')->published();
|
||||
foreach (['min_amount' => '>=', 'max_amount' => '<='] as $field => $operator) {
|
||||
if (isset($validated[$field])) {
|
||||
$query->where('estimated_investment_amount', $operator, $validated[$field]);
|
||||
}
|
||||
}
|
||||
if (! empty($validated['currency'])) {
|
||||
$query->where('estimated_investment_currency', $validated['currency']);
|
||||
}
|
||||
|
||||
if (! empty($validated['q'])) {
|
||||
$term = $validated['q'];
|
||||
$query->where(fn ($q) => $q
|
||||
->whereLike('title', "%{$term}%")
|
||||
->orWhereLike('summary', "%{$term}%")
|
||||
->orWhereLike('reference', "%{$term}%")
|
||||
->orWhereLike('province', "%{$term}%")
|
||||
->orWhereHas('sector', fn ($sectorQuery) => $sectorQuery
|
||||
->whereLike('name', "%{$term}%")
|
||||
->orWhereLike('slug', "%{$term}%")));
|
||||
}
|
||||
|
||||
if (! empty($validated['sector'])) {
|
||||
$sector = $validated['sector'];
|
||||
$query->whereHas('sector', fn ($q) => $q->where('slug', $sector));
|
||||
}
|
||||
|
||||
if (! empty($validated['province'])) {
|
||||
$query->where('province', $validated['province']);
|
||||
}
|
||||
|
||||
if (! empty($validated['maturity_level'])) {
|
||||
$query->where('maturity_level', $validated['maturity_level']);
|
||||
}
|
||||
|
||||
$results = $query->orderByDesc('is_featured')
|
||||
->orderByDesc('published_at')
|
||||
->paginate($validated['per_page'] ?? 12);
|
||||
|
||||
return response()->json($results);
|
||||
}
|
||||
|
||||
public function show(string $slug): JsonResponse
|
||||
{
|
||||
$opportunity = Opportunity::with('sector:id,name,slug')
|
||||
->where('slug', $slug)->published()->firstOrFail();
|
||||
|
||||
return response()->json(['success' => true, 'data' => $opportunity]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Sector;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class SectorController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$sectors = Sector::withCount([
|
||||
'opportunities as opportunities_count' => fn ($q) => $q->published(),
|
||||
])
|
||||
->where('is_active', true)
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
->map(fn ($s) => [
|
||||
'id' => $s->id,
|
||||
'name' => $s->name,
|
||||
'slug' => $s->slug,
|
||||
'shortDescription' => $s->short_description,
|
||||
'description' => $s->description,
|
||||
'icon' => $s->icon,
|
||||
'opportunitiesCount' => $s->opportunities_count,
|
||||
'url' => "/sectores/{$s->slug}",
|
||||
]);
|
||||
|
||||
return response()->json(['success' => true, 'data' => $sectors]);
|
||||
}
|
||||
|
||||
public function show(string $slug): JsonResponse
|
||||
{
|
||||
$sector = Sector::with(['opportunities' => fn ($q) => $q->published()])
|
||||
->where('slug', $slug)->firstOrFail();
|
||||
|
||||
return response()->json(['success' => true, 'data' => $sector]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user