Skip to content

Commit 2664f6b

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

5 files changed

Lines changed: 108 additions & 6 deletions

File tree

app/Jobs/CompanyEventJob.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
24+
class CompanyEventJob implements ShouldQueue
25+
{
26+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
27+
28+
public $tries = 5;
29+
30+
private $op;
31+
32+
private $company_id;
33+
34+
/**
35+
* @param int $company_id
36+
* @param string $op
37+
*/
38+
public function __construct(int $company_id, string $op){
39+
$this->op = $op;
40+
$this->company_id = $company_id;
41+
Log::debug(sprintf("CompanyEventJob::construct op %s company_id %s", $op, $this->company_id));
42+
}
43+
44+
/**
45+
* @param ISponsorRepository $sponsor_repository
46+
* @return void
47+
*/
48+
public function handle(ISponsorRepository $sponsor_repository){
49+
Log::debug(sprintf("CompanyEventJob::handle op %s company_id %s", $this->op, $this->company_id));
50+
$excerpt = $sponsor_repository->getSponsorsExcerptByCompanyID($this->company_id);
51+
$domain_event_publisher_service = RabbitPublisherFactory::make('domain_events_message_broker');
52+
Log::debug(sprintf("CompanyEventJob::handle excerpt %s", json_encode($excerpt)));
53+
foreach($excerpt as $entry) {
54+
$payload = [
55+
'id' => intval($entry['sponsor_id'])
56+
];
57+
$routing_key = $this->op == 'UPDATE' ? SponsorDomainEvents::SponsorUpdated : SponsorDomainEvents::SponsorDeleted;
58+
$domain_event_publisher_service->publish($payload, $routing_key);
59+
}
60+
}
61+
62+
63+
public function failed(\Throwable $e): void
64+
{
65+
Log::error("CompanyEventJob::failed {$e->getMessage()}");
66+
}
67+
68+
}

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/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_id, "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_id, "DELETE");
117120
$this->repository->delete($company);
118121
});
119122
}

0 commit comments

Comments
 (0)