diff --git a/app/config/errors.php b/app/config/errors.php index 8c3b08b96c..7932dcd3a9 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -880,6 +880,16 @@ return [ 'description' => 'Message with the requested ID has already been sent.', 'code' => 400, ], + Exception::MESSAGE_ALREADY_PROCESSING => [ + 'name' => Exception::MESSAGE_ALREADY_PROCESSING, + 'description' => 'Message with the requested ID is already being processed.', + 'code' => 400, + ], + Exception::MESSAGE_ALREADY_FAILED => [ + 'name' => Exception::MESSAGE_ALREADY_FAILED, + 'description' => 'Message with the requested ID has already failed.', + 'code' => 400, + ], Exception::MESSAGE_ALREADY_SCHEDULED => [ 'name' => Exception::MESSAGE_ALREADY_SCHEDULED, 'description' => 'Message with the requested ID has already been scheduled for delivery.', diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 39659fd2b3..94eda20d93 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -3136,8 +3136,13 @@ App::patch('/v1/messaging/messages/email/:messageId') throw new Exception(Exception::MESSAGE_NOT_FOUND); } - if ($message->getAttribute('status') === MessageStatus::SENT) { - throw new Exception(Exception::MESSAGE_ALREADY_SENT); + switch ($message->getAttribute('status')) { + case MessageStatus::PROCESSING: + throw new Exception(Exception::MESSAGE_ALREADY_PROCESSING); + case MessageStatus::SENT: + throw new Exception(Exception::MESSAGE_ALREADY_SENT); + case MessageStatus::FAILED: + throw new Exception(Exception::MESSAGE_ALREADY_FAILED); } if (!\is_null($message->getAttribute('scheduledAt')) && $message->getAttribute('scheduledAt') < new \DateTime()) { @@ -3195,7 +3200,7 @@ App::patch('/v1/messaging/messages/email/:messageId') 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), 'schedule' => $scheduledAt, - 'active' => $status === 'processing', + 'active' => $status === MessageStatus::SCHEDULED, ])); $message->setAttribute('scheduleId', $schedule->getId()); @@ -3209,7 +3214,7 @@ App::patch('/v1/messaging/messages/email/:messageId') $schedule ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('schedule', $scheduledAt) - ->setAttribute('active', $status === 'processing'); + ->setAttribute('active', $status === MessageStatus::SCHEDULED); $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); } @@ -3266,8 +3271,13 @@ App::patch('/v1/messaging/messages/sms/:messageId') throw new Exception(Exception::MESSAGE_NOT_FOUND); } - if ($message->getAttribute('status') === 'sent') { - throw new Exception(Exception::MESSAGE_ALREADY_SENT); + switch ($message->getAttribute('status')) { + case MessageStatus::PROCESSING: + throw new Exception(Exception::MESSAGE_ALREADY_PROCESSING); + case MessageStatus::SENT: + throw new Exception(Exception::MESSAGE_ALREADY_SENT); + case MessageStatus::FAILED: + throw new Exception(Exception::MESSAGE_ALREADY_FAILED); } if (!is_null($message->getAttribute('scheduledAt')) && $message->getAttribute('scheduledAt') < new \DateTime()) { @@ -3309,7 +3319,7 @@ App::patch('/v1/messaging/messages/sms/:messageId') 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), 'schedule' => $scheduledAt, - 'active' => $status === 'processing', + 'active' => $status === MessageStatus::SCHEDULED, ])); $message->setAttribute('scheduleId', $schedule->getId()); @@ -3323,7 +3333,7 @@ App::patch('/v1/messaging/messages/sms/:messageId') $schedule ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('schedule', $scheduledAt) - ->setAttribute('active', $status === 'processing'); + ->setAttribute('active', $status === MessageStatus::SCHEDULED); $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); } @@ -3333,7 +3343,7 @@ App::patch('/v1/messaging/messages/sms/:messageId') $message = $dbForProject->updateDocument('messages', $message->getId(), $message); - if ($status === 'processing' && \is_null($message->getAttribute('scheduledAt'))) { + if ($status === MessageStatus::PROCESSING) { $queueForMessaging ->setMessageId($message->getId()) ->trigger(); @@ -3388,8 +3398,13 @@ App::patch('/v1/messaging/messages/push/:messageId') throw new Exception(Exception::MESSAGE_NOT_FOUND); } - if ($message->getAttribute('status') === 'sent') { - throw new Exception(Exception::MESSAGE_ALREADY_SENT); + switch ($message->getAttribute('status')) { + case MessageStatus::PROCESSING: + throw new Exception(Exception::MESSAGE_ALREADY_PROCESSING); + case MessageStatus::SENT: + throw new Exception(Exception::MESSAGE_ALREADY_SENT); + case MessageStatus::FAILED: + throw new Exception(Exception::MESSAGE_ALREADY_FAILED); } if (!is_null($message->getAttribute('scheduledAt')) && $message->getAttribute('scheduledAt') < new \DateTime()) { @@ -3463,7 +3478,7 @@ App::patch('/v1/messaging/messages/push/:messageId') 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), 'schedule' => $scheduledAt, - 'active' => $status === 'processing', + 'active' => $status === MessageStatus::SCHEDULED, ])); $message->setAttribute('scheduleId', $schedule->getId()); @@ -3477,7 +3492,7 @@ App::patch('/v1/messaging/messages/push/:messageId') $schedule ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('schedule', $scheduledAt) - ->setAttribute('active', $status === 'processing'); + ->setAttribute('active', $status === MessageStatus::SCHEDULED); $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); } @@ -3487,7 +3502,7 @@ App::patch('/v1/messaging/messages/push/:messageId') $message = $dbForProject->updateDocument('messages', $message->getId(), $message); - if ($status === 'processing' && \is_null($message->getAttribute('scheduledAt'))) { + if ($status === MessageStatus::PROCESSING) { $queueForMessaging ->setMessageId($message->getId()) ->trigger(); diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 29eba3785b..98dee95c94 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -266,6 +266,8 @@ class Exception extends \Exception public const MESSAGE_NOT_FOUND = 'message_not_found'; public const MESSAGE_MISSING_TARGET = 'message_missing_target'; public const MESSAGE_ALREADY_SENT = 'message_already_sent'; + public const MESSAGE_ALREADY_PROCESSING = 'message_already_processing'; + public const MESSAGE_ALREADY_FAILED = 'message_already_failed'; public const MESSAGE_ALREADY_SCHEDULED = 'message_already_scheduled'; public const MESSAGE_TARGET_NOT_EMAIL = 'message_target_not_email'; public const MESSAGE_TARGET_NOT_SMS = 'message_target_not_sms';