diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 7e051ad828..f50d397e5c 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -3251,6 +3251,12 @@ App::patch('/v1/messaging/messages/email/:messageId') throw new Exception(Exception::MESSAGE_NOT_FOUND); } + if ($status !== MessageStatus::DRAFT && \count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) { + throw new Exception(Exception::MESSAGE_MISSING_TARGET); + } + + $currentScheduledAt = $message->getAttribute('scheduledAt'); + switch ($message->getAttribute('status')) { case MessageStatus::PROCESSING: throw new Exception(Exception::MESSAGE_ALREADY_PROCESSING); @@ -3258,12 +3264,51 @@ App::patch('/v1/messaging/messages/email/:messageId') throw new Exception(Exception::MESSAGE_ALREADY_SENT); case MessageStatus::FAILED: throw new Exception(Exception::MESSAGE_ALREADY_FAILED); + case MessageStatus::SCHEDULED: + if (\is_null($scheduledAt) && \is_null($currentScheduledAt)) { + throw new Exception(Exception::MESSAGE_MISSING_SCHEDULE); + } + break; } - if (!\is_null($message->getAttribute('scheduledAt')) && $message->getAttribute('scheduledAt') < new \DateTime()) { + if (!\is_null($currentScheduledAt) && $currentScheduledAt < new \DateTime()) { throw new Exception(Exception::MESSAGE_ALREADY_SCHEDULED); } + if (\is_null($currentScheduledAt && !\is_null($scheduledAt))) { + $schedule = $dbForConsole->createDocument('schedules', new Document([ + 'region' => App::getEnv('_APP_REGION', 'default'), + 'resourceType' => 'message', + 'resourceId' => $message->getId(), + 'resourceInternalId' => $message->getInternalId(), + 'resourceUpdatedAt' => DateTime::now(), + 'projectId' => $project->getId(), + 'schedule' => $scheduledAt, + 'active' => $status === MessageStatus::SCHEDULED, + ])); + + $message->setAttribute('scheduleId', $schedule->getId()); + } + + if (!\is_null($currentScheduledAt)) { + $schedule = $dbForConsole->getDocument('schedules', $message->getAttribute('scheduleId')); + + if ($schedule->isEmpty()) { + throw new Exception(Exception::SCHEDULE_NOT_FOUND); + } + + if (!\is_null($scheduledAt)) { + $schedule + ->setAttribute('resourceUpdatedAt', DateTime::now()) + ->setAttribute('schedule', $scheduledAt) + ->setAttribute('active', $status === MessageStatus::SCHEDULED); + } else { + $schedule->setAttribute('active', $status === MessageStatus::SCHEDULED); + } + + $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); + } + if (!\is_null($topics)) { $message->setAttribute('topics', $topics); } @@ -3304,38 +3349,6 @@ App::patch('/v1/messaging/messages/email/:messageId') $message->setAttribute('status', $status); } - if (!\is_null($scheduledAt)) { - if (\is_null($message->getAttribute(('scheduleId')))) { - $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => App::getEnv('_APP_REGION', 'default'), - 'resourceType' => 'message', - 'resourceId' => $message->getId(), - 'resourceInternalId' => $message->getInternalId(), - 'resourceUpdatedAt' => DateTime::now(), - 'projectId' => $project->getId(), - 'schedule' => $scheduledAt, - 'active' => $status === MessageStatus::SCHEDULED, - ])); - - $message->setAttribute('scheduleId', $schedule->getId()); - } else { - $schedule = $dbForConsole->getDocument('schedules', $message->getAttribute('scheduleId')); - - if ($schedule->isEmpty()) { - throw new Exception(Exception::SCHEDULE_NOT_FOUND); - } - - $schedule - ->setAttribute('resourceUpdatedAt', DateTime::now()) - ->setAttribute('schedule', $scheduledAt) - ->setAttribute('active', $status === MessageStatus::SCHEDULED); - - $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); - } - - $message->setAttribute('scheduleId', $schedule->getId()); - } - $message = $dbForProject->updateDocument('messages', $message->getId(), $message); if ($status === MessageStatus::PROCESSING) { @@ -3385,6 +3398,12 @@ App::patch('/v1/messaging/messages/sms/:messageId') throw new Exception(Exception::MESSAGE_NOT_FOUND); } + if ($status !== MessageStatus::DRAFT && \count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) { + throw new Exception(Exception::MESSAGE_MISSING_TARGET); + } + + $currentScheduledAt = $message->getAttribute('scheduledAt'); + switch ($message->getAttribute('status')) { case MessageStatus::PROCESSING: throw new Exception(Exception::MESSAGE_ALREADY_PROCESSING); @@ -3392,12 +3411,51 @@ App::patch('/v1/messaging/messages/sms/:messageId') throw new Exception(Exception::MESSAGE_ALREADY_SENT); case MessageStatus::FAILED: throw new Exception(Exception::MESSAGE_ALREADY_FAILED); + case MessageStatus::SCHEDULED: + if (\is_null($scheduledAt) && \is_null($message->getAttribute('scheduledAt'))) { + throw new Exception(Exception::MESSAGE_MISSING_SCHEDULE); + } + break; } - if (!is_null($message->getAttribute('scheduledAt')) && $message->getAttribute('scheduledAt') < new \DateTime()) { + if (!is_null($currentScheduledAt) && $currentScheduledAt < new \DateTime()) { throw new Exception(Exception::MESSAGE_ALREADY_SCHEDULED); } + if (\is_null($currentScheduledAt && !\is_null($scheduledAt))) { + $schedule = $dbForConsole->createDocument('schedules', new Document([ + 'region' => App::getEnv('_APP_REGION', 'default'), + 'resourceType' => 'message', + 'resourceId' => $message->getId(), + 'resourceInternalId' => $message->getInternalId(), + 'resourceUpdatedAt' => DateTime::now(), + 'projectId' => $project->getId(), + 'schedule' => $scheduledAt, + 'active' => $status === MessageStatus::SCHEDULED, + ])); + + $message->setAttribute('scheduleId', $schedule->getId()); + } + + if (!\is_null($currentScheduledAt)) { + $schedule = $dbForConsole->getDocument('schedules', $message->getAttribute('scheduleId')); + + if ($schedule->isEmpty()) { + throw new Exception(Exception::SCHEDULE_NOT_FOUND); + } + + if (!\is_null($scheduledAt)) { + $schedule + ->setAttribute('resourceUpdatedAt', DateTime::now()) + ->setAttribute('schedule', $scheduledAt) + ->setAttribute('active', $status === MessageStatus::SCHEDULED); + } else { + $schedule->setAttribute('active', $status === MessageStatus::SCHEDULED); + } + + $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); + } + if (!\is_null($topics)) { $message->setAttribute('topics', $topics); } @@ -3422,38 +3480,6 @@ App::patch('/v1/messaging/messages/sms/:messageId') $message->setAttribute('status', $status); } - if (!\is_null($scheduledAt)) { - if (\is_null($message->getAttribute(('scheduleId')))) { - $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => App::getEnv('_APP_REGION', 'default'), - 'resourceType' => 'message', - 'resourceId' => $message->getId(), - 'resourceInternalId' => $message->getInternalId(), - 'resourceUpdatedAt' => DateTime::now(), - 'projectId' => $project->getId(), - 'schedule' => $scheduledAt, - 'active' => $status === MessageStatus::SCHEDULED, - ])); - - $message->setAttribute('scheduleId', $schedule->getId()); - } else { - $schedule = $dbForConsole->getDocument('schedules', $message->getAttribute('scheduleId')); - - if ($schedule->isEmpty()) { - throw new Exception(Exception::SCHEDULE_NOT_FOUND); - } - - $schedule - ->setAttribute('resourceUpdatedAt', DateTime::now()) - ->setAttribute('schedule', $scheduledAt) - ->setAttribute('active', $status === MessageStatus::SCHEDULED); - - $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); - } - - $message->setAttribute('scheduleId', $schedule->getId()); - } - $message = $dbForProject->updateDocument('messages', $message->getId(), $message); if ($status === MessageStatus::PROCESSING) { @@ -3521,10 +3547,50 @@ App::patch('/v1/messaging/messages/push/:messageId') throw new Exception(Exception::MESSAGE_ALREADY_FAILED); } - if (!is_null($message->getAttribute('scheduledAt')) && $message->getAttribute('scheduledAt') < new \DateTime()) { + if ($status !== MessageStatus::DRAFT && \count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) { + throw new Exception(Exception::MESSAGE_MISSING_TARGET); + } + + $currentScheduledAt = $message->getAttribute('scheduledAt'); + + if (!is_null($currentScheduledAt) && $currentScheduledAt < new \DateTime()) { throw new Exception(Exception::MESSAGE_ALREADY_SCHEDULED); } + if (\is_null($currentScheduledAt && !\is_null($scheduledAt))) { + $schedule = $dbForConsole->createDocument('schedules', new Document([ + 'region' => App::getEnv('_APP_REGION', 'default'), + 'resourceType' => 'message', + 'resourceId' => $message->getId(), + 'resourceInternalId' => $message->getInternalId(), + 'resourceUpdatedAt' => DateTime::now(), + 'projectId' => $project->getId(), + 'schedule' => $scheduledAt, + 'active' => $status === MessageStatus::SCHEDULED, + ])); + + $message->setAttribute('scheduleId', $schedule->getId()); + } + + if (!\is_null($currentScheduledAt)) { + $schedule = $dbForConsole->getDocument('schedules', $message->getAttribute('scheduleId')); + + if ($schedule->isEmpty()) { + throw new Exception(Exception::SCHEDULE_NOT_FOUND); + } + + if (!\is_null($scheduledAt)) { + $schedule + ->setAttribute('resourceUpdatedAt', DateTime::now()) + ->setAttribute('schedule', $scheduledAt) + ->setAttribute('active', $status === MessageStatus::SCHEDULED); + } else { + $schedule->setAttribute('active', $status === MessageStatus::SCHEDULED); + } + + $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); + } + if (!\is_null($topics)) { $message->setAttribute('topics', $topics); } @@ -3613,38 +3679,6 @@ App::patch('/v1/messaging/messages/push/:messageId') $message->setAttribute('status', $status); } - if (!\is_null($scheduledAt)) { - if (\is_null($message->getAttribute(('scheduleId')))) { - $schedule = $dbForConsole->createDocument('schedules', new Document([ - 'region' => App::getEnv('_APP_REGION', 'default'), - 'resourceType' => 'message', - 'resourceId' => $message->getId(), - 'resourceInternalId' => $message->getInternalId(), - 'resourceUpdatedAt' => DateTime::now(), - 'projectId' => $project->getId(), - 'schedule' => $scheduledAt, - 'active' => $status === MessageStatus::SCHEDULED, - ])); - - $message->setAttribute('scheduleId', $schedule->getId()); - } else { - $schedule = $dbForConsole->getDocument('schedules', $message->getAttribute('scheduleId')); - - if ($schedule->isEmpty()) { - throw new Exception(Exception::SCHEDULE_NOT_FOUND); - } - - $schedule - ->setAttribute('resourceUpdatedAt', DateTime::now()) - ->setAttribute('schedule', $scheduledAt) - ->setAttribute('active', $status === MessageStatus::SCHEDULED); - - $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule); - } - - $message->setAttribute('scheduleId', $schedule->getId()); - } - $message = $dbForProject->updateDocument('messages', $message->getId(), $message); if ($status === MessageStatus::PROCESSING) {