This commit is contained in:
James Cole
2025-07-01 19:31:27 +02:00
parent ebfdeeedaa
commit 8e93af5cc7

View File

@@ -32,11 +32,14 @@ use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\Webhook; use FireflyIII\Models\Webhook;
use FireflyIII\Models\WebhookMessage; use FireflyIII\Models\WebhookMessage;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment;
use FireflyIII\Transformers\AccountTransformer; use FireflyIII\Transformers\AccountTransformer;
use FireflyIII\Transformers\TransactionGroupTransformer; use FireflyIII\Transformers\TransactionGroupTransformer;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
@@ -59,14 +62,14 @@ class StandardMessageGenerator implements MessageGeneratorInterface
public function generateMessages(): void public function generateMessages(): void
{ {
app('log')->debug(__METHOD__); Log::debug(__METHOD__);
// get the webhooks: // get the webhooks:
if (0 === $this->webhooks->count()) { if (0 === $this->webhooks->count()) {
$this->webhooks = $this->getWebhooks(); $this->webhooks = $this->getWebhooks();
} }
// do some debugging // do some debugging
app('log')->debug( Log::debug(
sprintf('StandardMessageGenerator will generate messages for %d object(s) and %d webhook(s).', $this->objects->count(), $this->webhooks->count()) sprintf('StandardMessageGenerator will generate messages for %d object(s) and %d webhook(s).', $this->objects->count(), $this->webhooks->count())
); );
$this->run(); $this->run();
@@ -79,13 +82,13 @@ class StandardMessageGenerator implements MessageGeneratorInterface
private function run(): void private function run(): void
{ {
app('log')->debug('Now in StandardMessageGenerator::run'); Log::debug('Now in StandardMessageGenerator::run');
/** @var Webhook $webhook */ /** @var Webhook $webhook */
foreach ($this->webhooks as $webhook) { foreach ($this->webhooks as $webhook) {
$this->runWebhook($webhook); $this->runWebhook($webhook);
} }
app('log')->debug('Done with StandardMessageGenerator::run'); Log::debug('Done with StandardMessageGenerator::run');
} }
/** /**
@@ -93,7 +96,7 @@ class StandardMessageGenerator implements MessageGeneratorInterface
*/ */
private function runWebhook(Webhook $webhook): void private function runWebhook(Webhook $webhook): void
{ {
app('log')->debug(sprintf('Now in runWebhook(#%d)', $webhook->id)); Log::debug(sprintf('Now in runWebhook(#%d)', $webhook->id));
/** @var Model $object */ /** @var Model $object */
foreach ($this->objects as $object) { foreach ($this->objects as $object) {
@@ -108,7 +111,7 @@ class StandardMessageGenerator implements MessageGeneratorInterface
{ {
$class = $model::class; $class = $model::class;
// Line is ignored because all of Firefly III's Models have an id property. // Line is ignored because all of Firefly III's Models have an id property.
app('log')->debug(sprintf('Now in generateMessage(#%d, %s#%d)', $webhook->id, $class, $model->id)); Log::debug(sprintf('Now in generateMessage(#%d, %s#%d)', $webhook->id, $class, $model->id));
$uuid = Uuid::uuid4(); $uuid = Uuid::uuid4();
$basicMessage = [ $basicMessage = [
@@ -125,7 +128,7 @@ class StandardMessageGenerator implements MessageGeneratorInterface
switch ($class) { switch ($class) {
default: default:
// Line is ignored because all of Firefly III's Models have an id property. // Line is ignored because all of Firefly III's Models have an id property.
app('log')->error( Log::error(
sprintf('Webhook #%d was given %s#%d to deal with but can\'t extract user ID from it.', $webhook->id, $class, $model->id) sprintf('Webhook #%d was given %s#%d to deal with but can\'t extract user ID from it.', $webhook->id, $class, $model->id)
); );
@@ -141,7 +144,7 @@ class StandardMessageGenerator implements MessageGeneratorInterface
// then depends on the response what to put in the message: // then depends on the response what to put in the message:
switch ($webhook->response) { switch ($webhook->response) {
default: default:
app('log')->error( Log::error(
sprintf('The response code for webhook #%d is "%d" and the message generator cant handle it. Soft fail.', $webhook->id, $webhook->response) sprintf('The response code for webhook #%d is "%d" and the message generator cant handle it. Soft fail.', $webhook->id, $webhook->response)
); );
@@ -159,10 +162,10 @@ class StandardMessageGenerator implements MessageGeneratorInterface
try { try {
$basicMessage['content'] = $transformer->transformObject($model); $basicMessage['content'] = $transformer->transformObject($model);
} catch (FireflyException $e) { } catch (FireflyException $e) {
app('log')->error( Log::error(
sprintf('The transformer could not include the requested transaction group for webhook #%d: %s', $webhook->id, $e->getMessage()) sprintf('The transformer could not include the requested transaction group for webhook #%d: %s', $webhook->id, $e->getMessage())
); );
app('log')->error($e->getTraceAsString()); Log::error($e->getTraceAsString());
return; return;
} }
@@ -172,6 +175,10 @@ class StandardMessageGenerator implements MessageGeneratorInterface
case WebhookResponse::ACCOUNTS->value: case WebhookResponse::ACCOUNTS->value:
/** @var TransactionGroup $model */ /** @var TransactionGroup $model */
$accounts = $this->collectAccounts($model); $accounts = $this->collectAccounts($model);
$enrichment = new AccountEnrichment();
$enrichment->setUser($model->user);
$enrichment->setNative(Amount::getNativeCurrencyByUserGroup($model->userGroup));
$accounts = $enrichment->enrich($accounts);
foreach ($accounts as $account) { foreach ($accounts as $account) {
$transformer = new AccountTransformer(); $transformer = new AccountTransformer();
$transformer->setParameters(new ParameterBag()); $transformer->setParameters(new ParameterBag());
@@ -210,7 +217,7 @@ class StandardMessageGenerator implements MessageGeneratorInterface
$webhookMessage->uuid = $message['uuid']; $webhookMessage->uuid = $message['uuid'];
$webhookMessage->message = $message; $webhookMessage->message = $message;
$webhookMessage->save(); $webhookMessage->save();
app('log')->debug(sprintf('Stored new webhook message #%d', $webhookMessage->id)); Log::debug(sprintf('Stored new webhook message #%d', $webhookMessage->id));
} }
public function setObjects(Collection $objects): void public function setObjects(Collection $objects): void