95 lines
5.9 KiB
PHP
95 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\InvestmentSpecificNeed;
|
|
use App\Models\InvestmentSubmission;
|
|
use App\Models\Opportunity;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class InvestmentWorkspace
|
|
{
|
|
/** @param array<string,mixed> $criteria
|
|
* @param array<string,mixed> $filters */
|
|
public function notifyFunding(int $userId, \stdClass $record, int $version, array $criteria, array $filters): string
|
|
{
|
|
$public = json_decode($record->payload, true, 512, JSON_THROW_ON_ERROR);
|
|
$event = 'public-funding-version:'.$record->version_id;
|
|
$id = (string) Str::uuid();
|
|
$windows = [];
|
|
foreach ($public['windows'] as $window) {
|
|
$windows[] = ['title' => $window['title'], 'state' => app(FundingProgrammes::class)->windowState($window), 'end_on' => $window['end_on'] ?? null];
|
|
}
|
|
DB::table('investment_notifications')->insertOrIgnore(['id' => $id, 'user_id' => $userId, 'submission_id' => null,
|
|
'funding_version_id' => $record->version_id, 'kind' => 'funding_match', 'event_key' => $event,
|
|
'payload' => Crypt::encryptString(json_encode(['title' => $public['title'], 'programme_id' => $record->programme_id,
|
|
'publication_version' => (int) $record->version, 'source' => 'Catálogo público de fundos e apoios do InvesteMoz',
|
|
'published_at' => CarbonImmutable::parse($record->published_at)->toIso8601String(), 'source_url' => $public['source_url'],
|
|
'source_reference' => $public['source_reference'], 'source_checked_on' => $public['source_checked_on'], 'window_states' => $windows,
|
|
'scope_explanation' => app(FundingProgrammes::class)->filterExplanation(['payload' => $public], $filters),
|
|
'preference_version' => $version, 'criteria' => $criteria], JSON_THROW_ON_ERROR)), 'created_at' => now()]);
|
|
|
|
return DB::table('investment_notifications')->where('user_id', $userId)->where('event_key', $event)->value('id');
|
|
}
|
|
|
|
/** @param array<string,mixed> $criteria */
|
|
public function notifyTender(int $userId, \stdClass $record, int $version, array $criteria): string
|
|
{
|
|
$public = json_decode($record->payload, true, 512, JSON_THROW_ON_ERROR);
|
|
$event = 'public-tender-version:'.$record->version_id;
|
|
$id = (string) Str::uuid();
|
|
DB::table('investment_notifications')->insertOrIgnore(['id' => $id, 'user_id' => $userId, 'submission_id' => null,
|
|
'tender_version_id' => $record->version_id, 'kind' => 'tender_match', 'event_key' => $event,
|
|
'payload' => Crypt::encryptString(json_encode(['title' => $public['title'], 'reference' => $public['reference'],
|
|
'tender_id' => $record->tender_id, 'publication_version' => (int) $record->version, 'act' => $record->act,
|
|
'source' => 'Catálogo público de concursos do InvesteMoz', 'published_at' => CarbonImmutable::parse($record->published_at)->toIso8601String(),
|
|
'origin_published_at' => $public['origin_published_at'] ?? null, 'preference_version' => $version,
|
|
'criteria' => $criteria], JSON_THROW_ON_ERROR)), 'created_at' => now()]);
|
|
|
|
return DB::table('investment_notifications')->where('user_id', $userId)->where('event_key', $event)->value('id');
|
|
}
|
|
|
|
/** @param array<string,mixed> $criteria */
|
|
public function notifyOpportunity(int $userId, Opportunity $opportunity, int $version, array $criteria): string
|
|
{
|
|
$event = 'public-opportunity:'.$opportunity->id;
|
|
$id = (string) Str::uuid();
|
|
DB::table('investment_notifications')->insertOrIgnore(['id' => $id, 'user_id' => $userId, 'submission_id' => null,
|
|
'opportunity_id' => $opportunity->id, 'kind' => 'opportunity_match', 'event_key' => $event,
|
|
'payload' => Crypt::encryptString(json_encode(['title' => $opportunity->title, 'slug' => $opportunity->slug,
|
|
'source' => 'Catálogo público do InvesteMoz', 'published_at' => $opportunity->published_at?->toIso8601String(),
|
|
'preference_version' => $version, 'criteria' => $criteria], JSON_THROW_ON_ERROR)), 'created_at' => now()]);
|
|
|
|
return DB::table('investment_notifications')->where('user_id', $userId)->where('event_key', $event)->value('id');
|
|
}
|
|
|
|
/** @param array<string,mixed> $payload */
|
|
public function notify(int $userId, InvestmentSubmission $submission, string $kind, string $eventKey, array $payload = []): void
|
|
{
|
|
DB::table('investment_notifications')->insertOrIgnore(['id' => (string) Str::uuid(), 'user_id' => $userId,
|
|
'submission_id' => $submission->id, 'kind' => $kind, 'event_key' => $eventKey,
|
|
'payload' => Crypt::encryptString(json_encode($payload, JSON_THROW_ON_ERROR)), 'created_at' => now()]);
|
|
}
|
|
|
|
public function snapshot(InvestmentSubmission $submission): void
|
|
{
|
|
$snapshot = ['payload' => $submission->payload, 'status' => $submission->status,
|
|
'zone_origin' => DB::table('investment_submission_zones')->where('submission_id', $submission->id)->first(['zone_id', 'zone_version', 'intent']),
|
|
'needs' => InvestmentSpecificNeed::where('submission_id', $submission->id)->get()->toArray(),
|
|
'attachment_ids' => DB::table('investment_submission_attachments')->where('submission_id', $submission->id)->pluck('id')->all()];
|
|
DB::table('investment_submission_revisions')->insert(['submission_id' => $submission->id, 'version' => $submission->version,
|
|
'snapshot' => Crypt::encryptString(json_encode($snapshot, JSON_THROW_ON_ERROR)), 'created_at' => now()]);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
public function account(int $userId): array
|
|
{
|
|
$row = DB::table('investment_accounts')->where('user_id', $userId)->first();
|
|
|
|
return ['version' => $row->version ?? 0, 'payload' => $row ? json_decode(Crypt::decryptString($row->payload), true, 512, JSON_THROW_ON_ERROR) : []];
|
|
}
|
|
}
|