appwrite/src/Appwrite/Deletes/Targets.php

64 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Deletes;
use Appwrite\Extend\Exception;
2026-01-11 06:13:39 +00:00
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
2026-01-11 06:13:39 +00:00
use Utopia\Database\Exception\Limit as LimitException;
2026-01-11 06:21:30 +00:00
use Utopia\Database\Query;
class Targets
{
public static function delete(Database $database, Query $query): void
{
$database->deleteDocuments(
'targets',
[
$query,
Query::orderAsc()
],
Database::DELETE_BATCH_SIZE,
fn (Document $target) => self::deleteSubscribers($database, $target)
);
}
public static function deleteSubscribers(Database $database, Document $target): void
{
$database->deleteDocuments(
'subscribers',
[
2025-05-26 05:42:11 +00:00
Query::equal('targetInternalId', [$target->getSequence()]),
Query::orderAsc(),
],
Database::DELETE_BATCH_SIZE,
function (Document $subscriber) use ($database, $target) {
$topicId = $subscriber->getAttribute('topicId');
$topicInternalId = $subscriber->getAttribute('topicInternalId');
$topic = $database->getDocument('topics', $topicId);
2025-05-26 05:42:11 +00:00
if (!$topic->isEmpty() && $topic->getSequence() === $topicInternalId) {
$totalAttribute = match ($target->getAttribute('providerType')) {
MESSAGE_TYPE_EMAIL => 'emailTotal',
MESSAGE_TYPE_SMS => 'smsTotal',
MESSAGE_TYPE_PUSH => 'pushTotal',
default => throw new Exception('Invalid target provider type'),
};
2026-01-11 06:13:39 +00:00
try {
$database->decreaseDocumentAttribute(
'topics',
$topicId,
$totalAttribute,
min: 0
);
2026-01-11 06:21:30 +00:00
} catch (LimitException $e) {
2026-01-11 06:30:52 +00:00
Console::error("Delete subscribers decreaseDocumentAttribute (topicId={$topicId}): {$e->getMessage()}");
2026-01-11 06:13:39 +00:00
}
}
}
);
}
}