Skip to content

Commit ce3a18f

Browse files
committed
feat: publish sponsor update domain event when company is updated
1 parent 44c00d6 commit ce3a18f

File tree

6 files changed

+122
-7
lines changed

6 files changed

+122
-7
lines changed

app/Jobs/CompanyEventJob.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php namespace App\Jobs;
2+
/*
3+
* Copyright 2025 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use App\Events\SponsorServices\SponsorDomainEvents;
15+
use App\Models\Foundation\Summit\Repositories\ISponsorRepository;
16+
use App\Services\Model\Imp\Factories\RabbitPublisherFactory;
17+
use Illuminate\Bus\Queueable;
18+
use Illuminate\Contracts\Queue\ShouldQueue;
19+
use Illuminate\Foundation\Bus\Dispatchable;
20+
use Illuminate\Queue\InteractsWithQueue;
21+
use Illuminate\Queue\SerializesModels;
22+
use Illuminate\Support\Facades\Log;
23+
use models\main\Company;
24+
25+
class CompanyEventJob implements ShouldQueue
26+
{
27+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
28+
29+
public $tries = 5;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $op;
35+
36+
/**
37+
* @var int
38+
*/
39+
private $company_id;
40+
41+
/**
42+
* @var string|null
43+
*/
44+
private $company_name;
45+
46+
/**
47+
* @param Company $company
48+
* @param string $op
49+
*/
50+
public function __construct(Company $company, string $op){
51+
$this->op = $op;
52+
$this->company_id = $company->getId();
53+
$this->company_name = $company->getName();
54+
Log::debug(sprintf("CompanyEventJob::construct op %s company_id %s", $op, $this->company_id));
55+
}
56+
57+
/**
58+
* @param ISponsorRepository $sponsor_repository
59+
* @return void
60+
*/
61+
public function handle(ISponsorRepository $sponsor_repository){
62+
Log::debug(sprintf("CompanyEventJob::handle op %s company_id %s", $this->op, $this->company_id));
63+
$excerpt = $sponsor_repository->getSponsorsExcerptByCompanyID($this->company_id);
64+
$domain_event_publisher_service = RabbitPublisherFactory::make('domain_events_message_broker');
65+
Log::debug(sprintf("CompanyEventJob::handle excerpt %s", json_encode($excerpt)));
66+
foreach($excerpt as $entry) {
67+
$payload = [
68+
'id' => intval($entry['sponsor_id']),
69+
'company_name' => $this->company_name,
70+
];
71+
$routing_key = $this->op == 'UPDATE' ? SponsorDomainEvents::SponsorUpdated : SponsorDomainEvents::SponsorDeleted;
72+
$domain_event_publisher_service->publish($payload, $routing_key);
73+
}
74+
}
75+
76+
77+
public function failed(\Throwable $e): void
78+
{
79+
Log::error("CompanyEventJob::failed {$e->getMessage()}");
80+
}
81+
82+
}

app/Jobs/SponsorServices/PublishSponsorServiceDomainEventsJob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function handle(): void
4444
{
4545
try {
4646
Log::debug(sprintf("PublishSponsorServiceDomainEventsJob::handle payload %s event_type %s", json_encode($this->payload), $this->event_type));
47-
$sponsor_services_publisher = RabbitPublisherFactory::make('domain_events_message_broker');
48-
$sponsor_services_publisher->publish($this->payload, $this->event_type);
47+
$domain_event_publisher_service = RabbitPublisherFactory::make('domain_events_message_broker');
48+
$domain_event_publisher_service->publish($this->payload, $this->event_type);
4949
} catch (\Exception $ex) {
5050
Log::error($ex);
5151
throw $ex;

app/Models/Foundation/Summit/Repositories/ISponsorRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@
1818
*/
1919
interface ISponsorRepository extends IBaseRepository
2020
{
21-
21+
/**
22+
* @param int $company_id
23+
* @return array
24+
*/
25+
public function getSponsorsExcerptByCompanyID(int $company_id):array;
2226
}

app/Repositories/RepositoriesProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* limitations under the License.
1313
**/
1414

15-
use App\Jobs\Emails\Schedule\RSVP\RSVPInvitationExcerptEmail;
1615
use App\Models\Foundation\Elections\Election;
1716
use App\Models\Foundation\Elections\IElectionsRepository;
1817
use App\Models\Foundation\Main\Language;

app/Repositories/Summit/DoctrineSponsorRepository.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
1415
use App\Models\Foundation\Summit\Repositories\ISponsorRepository;
1516
use App\Repositories\SilverStripeDoctrineRepository;
17+
use Doctrine\DBAL\ParameterType;
1618
use Doctrine\ORM\QueryBuilder;
1719
use models\summit\Sponsor;
1820
use utils\DoctrineFilterMapping;
1921
use utils\DoctrineHavingFilterMapping;
20-
use utils\DoctrineJoinFilterMapping;
2122
use utils\DoctrineLeftJoinFilterMapping;
2223
use utils\Filter;
2324
use utils\Order;
@@ -99,4 +100,30 @@ protected function getBaseEntity()
99100
{
100101
return Sponsor::class;
101102
}
103+
104+
/**
105+
* @param int $company_id
106+
* @return array
107+
*/
108+
public function getSponsorsExcerptByCompanyID(int $company_id):array{
109+
try {
110+
$sql = <<<SQL
111+
SELECT Sponsor.ID AS sponsor_id, Sponsor.SummitID as summit_id FROM Sponsor
112+
WHERE Sponsor.CompanyID = :company_id
113+
SQL;
114+
115+
$bindings = ['company_id' => $company_id];
116+
$types = [
117+
'company_id' => ParameterType::INTEGER
118+
];
119+
120+
$stm = $this->getEntityManager()->getConnection()->executeQuery($sql, $bindings, $types);
121+
122+
return $stm->fetchAllAssociative();
123+
124+
} catch (\Exception $ex) {
125+
126+
}
127+
return [];
128+
}
102129
}

app/Services/Model/Imp/CompanyService.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* limitations under the License.
1313
**/
1414
use App\Http\Utils\IFileUploader;
15+
use App\Jobs\CompanyEventJob;
1516
use App\Models\Foundation\Main\Factories\CompanyFactory;
1617
use App\Services\Model\AbstractService;
1718
use App\Services\Model\ICompanyService;
@@ -86,7 +87,7 @@ public function addCompany(array $payload): Company
8687
*/
8788
public function updateCompany(int $company_id, array $payload): Company
8889
{
89-
return $this->tx_service->transaction(function() use($company_id, $payload){
90+
$company = $this->tx_service->transaction(function() use($company_id, $payload){
9091
$company = $this->repository->getById($company_id);
9192
if(is_null($company) || !$company instanceof Company)
9293
throw new EntityNotFoundException(sprintf("company %s not found.", $company_id));
@@ -100,6 +101,8 @@ public function updateCompany(int $company_id, array $payload): Company
100101

101102
return CompanyFactory::populate($company, $payload);
102103
});
104+
CompanyEventJob::dispatch($company, "UPDATE");
105+
return $company;
103106
}
104107

105108
/**
@@ -113,7 +116,7 @@ public function deleteCompany(int $company_id): void
113116
$company = $this->repository->getById($company_id);
114117
if(is_null($company))
115118
throw new EntityNotFoundException(sprintf("company %s not found.", $company_id));
116-
119+
CompanyEventJob::dispatch($company, "DELETE");
117120
$this->repository->delete($company);
118121
});
119122
}

0 commit comments

Comments
 (0)