From d14fbde9e70cc79e835c3e1a0d5b3bff4bc330ca Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Wed, 13 Sep 2023 00:40:51 +0530 Subject: [PATCH 1/3] removes provider sub query --- app/config/collections.php | 11 ----------- app/init.php | 18 ------------------ 2 files changed, 29 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index eebd6d31d5..3deb29636a 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1861,17 +1861,6 @@ $commonCollections = [ 'array' => false, 'filters' => [], ], - [ - '$id' => ID::custom('provider'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => ['subQueryProvider'], - ], [ '$id' => ID::custom('identifier'), 'type' => Database::VAR_STRING, diff --git a/app/init.php b/app/init.php index 072384919b..dde6d2a199 100644 --- a/app/init.php +++ b/app/init.php @@ -544,24 +544,6 @@ Database::addFilter( } ); -Database::addFilter( - 'subQueryProvider', - function (mixed $value) { - return null; - }, - function (mixed $value, Document $document, Database $database) { - $provider = Authorization::skip(fn () => $database - ->getDocument( - 'providers', - $document->getAttribute('providerId'), - )); - if (!$provider->isEmpty()) { - return $provider; - } - return null; - } -); - Database::addFilter( 'subQueryTopicTargets', function (mixed $value) { From 9b6fc440d944016d7102b10cfc485eafce07ffa4 Mon Sep 17 00:00:00 2001 From: prateek banga Date: Wed, 13 Sep 2023 16:57:59 +0530 Subject: [PATCH 2/3] Review changes and adds enabled in providers collection --- app/config/collections.php | 11 +++++++++++ src/Appwrite/Utopia/Response/Model/Message.php | 14 +++++++------- src/Appwrite/Utopia/Response/Model/Project.php | 2 +- src/Appwrite/Utopia/Response/Model/Provider.php | 11 +++++++---- src/Appwrite/Utopia/Response/Model/Subscriber.php | 5 ----- src/Appwrite/Utopia/Response/Model/Target.php | 11 ----------- src/Appwrite/Utopia/Response/Model/Topic.php | 5 ----- 7 files changed, 26 insertions(+), 33 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 6050afd95f..ed73c222a7 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1427,6 +1427,17 @@ $commonCollections = [ 'default' => false, 'array' => false, ], + [ + '$id' => ID::custom('enabled'), + 'type' => Database::VAR_BOOLEAN, + 'signed' => true, + 'size' => 0, + 'format' => '', + 'filters' => [], + 'required' => true, + 'default' => true, + 'array' => false, + ], [ '$id' => ID::custom('credentials'), 'type' => Database::VAR_STRING, diff --git a/src/Appwrite/Utopia/Response/Model/Message.php b/src/Appwrite/Utopia/Response/Model/Message.php index 150e837853..08d8af55ff 100644 --- a/src/Appwrite/Utopia/Response/Model/Message.php +++ b/src/Appwrite/Utopia/Response/Model/Message.php @@ -24,7 +24,7 @@ class Message extends Any ]) ->addRule('providerId', [ 'type' => self::TYPE_STRING, - 'description' => 'Provider Id for the message.', + 'description' => 'Provider ID for the message.', 'default' => '', 'example' => '5e5ea5c16897e', ]) @@ -40,26 +40,26 @@ class Message extends Any 'description' => 'Recipient of message.', 'required' => false, 'default' => DateTime::now(), - 'example' => DateTime::now(), + 'example' => self::TYPE_DATETIME_EXAMPLE, ]) - ->addRule('deliveryError', [ + ->addRule('deliveryErrors', [ 'type' => self::TYPE_STRING, - 'description' => 'Delivery error if any.', + 'description' => 'Delivery errors if any.', 'required' => false, 'default' => '', 'array' => true, - 'example' => 'Provider not valid.', + 'example' => 'Credentials not valid.', ]) ->addRule('deliveredTo', [ 'type' => self::TYPE_INTEGER, 'description' => 'Number of recipients the message was delivered to.', - 'default' => '', + 'default' => 0, 'example' => 1, ]) ->addRule('delivered', [ 'type' => self::TYPE_BOOLEAN, 'description' => 'Status of delivery.', - 'default' => '', + 'default' => false, 'example' => true, ]); } diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index ae199d77f7..807c1fb574 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -348,7 +348,7 @@ class Project extends Model ]); } - $document->setAttribute("authProviders", $projectProviders); + $document->setAttribute('authProviders', $projectProviders); return $document; } diff --git a/src/Appwrite/Utopia/Response/Model/Provider.php b/src/Appwrite/Utopia/Response/Model/Provider.php index 668db553fc..7921786394 100644 --- a/src/Appwrite/Utopia/Response/Model/Provider.php +++ b/src/Appwrite/Utopia/Response/Model/Provider.php @@ -7,9 +7,6 @@ use Appwrite\Utopia\Response\Model; class Provider extends Model { - /** - * @var bool - */ protected bool $public = false; public function __construct() @@ -36,7 +33,13 @@ class Provider extends Model ->addRule('default', [ 'type' => self::TYPE_BOOLEAN, 'description' => 'Is this a pre-configured provider instance?', - 'default' => '', + 'default' => false, + 'example' => true, + ]) + ->addRule('enabled', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Is provider enabled?', + 'default' => true, 'example' => true, ]) ->addRule('type', [ diff --git a/src/Appwrite/Utopia/Response/Model/Subscriber.php b/src/Appwrite/Utopia/Response/Model/Subscriber.php index 8e4ef74d74..2f34619cb4 100644 --- a/src/Appwrite/Utopia/Response/Model/Subscriber.php +++ b/src/Appwrite/Utopia/Response/Model/Subscriber.php @@ -7,11 +7,6 @@ use Appwrite\Utopia\Response\Model; class Subscriber extends Model { - /** - * @var bool - */ - protected bool $public = false; - public function __construct() { $this diff --git a/src/Appwrite/Utopia/Response/Model/Target.php b/src/Appwrite/Utopia/Response/Model/Target.php index 272c929e11..5750f57eba 100644 --- a/src/Appwrite/Utopia/Response/Model/Target.php +++ b/src/Appwrite/Utopia/Response/Model/Target.php @@ -7,11 +7,6 @@ use Appwrite\Utopia\Response\Model; class Target extends Model { - /** - * @var bool - */ - protected bool $public = false; - public function __construct() { $this @@ -34,12 +29,6 @@ class Target extends Model 'default' => '', 'example' => '259125845563242502', ]) - ->addRule('providerType', [ - 'type' => self::TYPE_STRING, - 'description' => 'The type of provider supported by this target.', - 'default' => '', - 'example' => 'sms', - ]) ->addRule('identifier', [ 'type' => self::TYPE_STRING, 'description' => 'The target identifier.', diff --git a/src/Appwrite/Utopia/Response/Model/Topic.php b/src/Appwrite/Utopia/Response/Model/Topic.php index c0996fcf55..3a6e832f5c 100644 --- a/src/Appwrite/Utopia/Response/Model/Topic.php +++ b/src/Appwrite/Utopia/Response/Model/Topic.php @@ -7,11 +7,6 @@ use Appwrite\Utopia\Response\Model; class Topic extends Model { - /** - * @var bool - */ - protected bool $public = false; - public function __construct() { $this From 0066216bef1757562282fe6852094e80e3ad3c7c Mon Sep 17 00:00:00 2001 From: prateek banga Date: Wed, 13 Sep 2023 17:47:38 +0530 Subject: [PATCH 3/3] adds enabled param for provider endpoints and review changes --- app/controllers/api/messaging.php | 124 +++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 27 deletions(-) diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 5355b56721..dbf149fd31 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -107,11 +107,12 @@ App::post('/v1/messaging/providers/mailgun') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('apiKey', '', new Text(0), 'Mailgun API Key.') ->param('domain', '', new Text(0), 'Mailgun Domain.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $apiKey, string $domain, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $apiKey, string $domain, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ @@ -120,6 +121,7 @@ App::post('/v1/messaging/providers/mailgun') 'provider' => 'mailgun', 'type' => 'email', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'apiKey' => $apiKey, 'domain' => $domain, @@ -161,11 +163,12 @@ App::patch('/v1/messaging/providers/:id/mailgun') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('apiKey', '', new Text(0), 'Mailgun API Key.', true) ->param('domain', '', new Text(0), 'Mailgun Domain.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $apiKey, string $domain, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $apiKey, string $domain, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -181,8 +184,12 @@ App::patch('/v1/messaging/providers/:id/mailgun') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($apiKey || $domain) { - // Check if all five variables are present + // Check if all credential variables are present if ($apiKey && $domain) { $provider->setAttribute('credentials', [ 'apiKey' => $apiKey, @@ -217,10 +224,11 @@ App::post('/v1/messaging/providers/sendgrid') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('apiKey', '', new Text(0), 'Sendgrid API key.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $apiKey, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $apiKey, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -228,6 +236,7 @@ App::post('/v1/messaging/providers/sendgrid') 'provider' => 'sendgrid', 'type' => 'email', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'apiKey' => $apiKey, ], @@ -268,10 +277,11 @@ App::patch('/v1/messaging/providers/:id/sendgrid') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('apiKey', '', new Text(0), 'Sendgrid API key.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $apiKey, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $apiKey, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -287,6 +297,10 @@ App::patch('/v1/messaging/providers/:id/sendgrid') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($apiKey) { $provider->setAttribute('credentials', [ 'apiKey' => $apiKey, @@ -319,11 +333,12 @@ App::post('/v1/messaging/providers/msg91') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('senderId', '', new Text(0), 'Msg91 Sender ID.') ->param('authKey', '', new Text(0), 'Msg91 Auth Key.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $senderId, string $authKey, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $senderId, string $authKey, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -331,6 +346,7 @@ App::post('/v1/messaging/providers/msg91') 'provider' => 'msg91', 'type' => 'sms', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'senderId' => $senderId, 'authKey' => $authKey, @@ -372,11 +388,12 @@ App::patch('/v1/messaging/providers/:id/msg91') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('senderId', '', new Text(0), 'Msg91 Sender ID.', true) ->param('authKey', '', new Text(0), 'Msg91 Auth Key.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $senderId, string $authKey, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $senderId, string $authKey, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -392,8 +409,12 @@ App::patch('/v1/messaging/providers/:id/msg91') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($senderId || $authKey) { - // Check if all five variables are present + // Check if all credential variables are present if ($senderId && $authKey) { $provider->setAttribute('credentials', [ 'senderId' => $senderId, @@ -428,11 +449,12 @@ App::post('/v1/messaging/providers/telesign') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('username', '', new Text(0), 'Telesign username.') ->param('password', '', new Text(0), 'Telesign password.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $username, string $password, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $username, string $password, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -440,6 +462,7 @@ App::post('/v1/messaging/providers/telesign') 'provider' => 'telesign', 'type' => 'sms', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'username' => $username, 'password' => $password, @@ -481,11 +504,12 @@ App::patch('/v1/messaging/providers/:id/telesign') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('username', '', new Text(0), 'Telesign username.', true) ->param('password', '', new Text(0), 'Telesign password.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $username, string $password, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $username, string $password, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -501,8 +525,12 @@ App::patch('/v1/messaging/providers/:id/telesign') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($username || $password) { - // Check if all five variables are present + // Check if all credential variables are present if ($username && $password) { $provider->setAttribute('credentials', [ 'username' => $username, @@ -537,11 +565,12 @@ App::post('/v1/messaging/providers/textmagic') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('username', '', new Text(0), 'Textmagic username.') ->param('apiKey', '', new Text(0), 'Textmagic apiKey.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $username, string $apiKey, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $username, string $apiKey, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -549,6 +578,7 @@ App::post('/v1/messaging/providers/textmagic') 'provider' => 'text-magic', 'type' => 'sms', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'username' => $username, 'apiKey' => $apiKey, @@ -590,11 +620,12 @@ App::patch('/v1/messaging/providers/:id/textmagic') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('username', '', new Text(0), 'Textmagic username.', true) ->param('apiKey', '', new Text(0), 'Textmagic apiKey.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $username, string $apiKey, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $username, string $apiKey, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -610,8 +641,12 @@ App::patch('/v1/messaging/providers/:id/textmagic') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($username || $apiKey) { - // Check if all five variables are present + // Check if all credential variables are present if ($username && $apiKey) { $provider->setAttribute('credentials', [ 'username' => $username, @@ -646,11 +681,12 @@ App::post('/v1/messaging/providers/twilio') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('accountSid', '', new Text(0), 'Twilio account secret ID.') ->param('authToken', '', new Text(0), 'Twilio authentication token.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $accountSid, string $authToken, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $accountSid, string $authToken, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -658,6 +694,7 @@ App::post('/v1/messaging/providers/twilio') 'provider' => 'twilio', 'type' => 'sms', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'accountSid' => $accountSid, 'authToken' => $authToken, @@ -699,11 +736,12 @@ App::patch('/v1/messaging/providers/:id/twilio') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('accountSid', null, new Text(0), 'Twilio account secret ID.', true) ->param('authToken', null, new Text(0), 'Twilio authentication token.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $accountSid, string $authToken, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $accountSid, string $authToken, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -719,8 +757,12 @@ App::patch('/v1/messaging/providers/:id/twilio') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($accountSid || $authToken) { - // Check if all five variables are present + // Check if all credential variables are present if ($accountSid && $authToken) { $provider->setAttribute('credentials', [ 'accountSid' => $accountSid, @@ -755,11 +797,12 @@ App::post('/v1/messaging/providers/vonage') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('apiKey', '', new Text(0), 'Vonage API key.') ->param('apiSecret', '', new Text(0), 'Vonage API secret.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $apiKey, string $apiSecret, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $apiKey, string $apiSecret, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -767,6 +810,7 @@ App::post('/v1/messaging/providers/vonage') 'provider' => 'vonage', 'type' => 'sms', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'apiKey' => $apiKey, 'apiSecret' => $apiSecret, @@ -808,11 +852,12 @@ App::patch('/v1/messaging/providers/:id/vonage') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('apiKey', '', new Text(0), 'Vonage API key.', true) ->param('apiSecret', '', new Text(0), 'Vonage API secret.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $apiKey, string $apiSecret, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $apiKey, string $apiSecret, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -828,8 +873,12 @@ App::patch('/v1/messaging/providers/:id/vonage') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($apiKey || $apiSecret) { - // Check if all five variables are present + // Check if all credential variables are present if ($apiKey && $apiSecret) { $provider->setAttribute('credentials', [ 'apiKey' => $apiKey, @@ -867,10 +916,11 @@ App::post('/v1/messaging/providers/fcm') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('serverKey', '', new Text(0), 'FCM Server Key.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $serverKey, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $serverKey, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -878,6 +928,7 @@ App::post('/v1/messaging/providers/fcm') 'provider' => 'fcm', 'type' => 'push', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'serverKey' => $serverKey, ], @@ -918,10 +969,11 @@ App::patch('/v1/messaging/providers/:id/fcm') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('serverKey', '', new Text(0), 'FCM Server Key.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $serverKey, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $serverKey, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -937,6 +989,10 @@ App::patch('/v1/messaging/providers/:id/fcm') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($serverKey) { $provider->setAttribute('credentials', ['serverKey' => $serverKey]); } @@ -964,6 +1020,7 @@ App::post('/v1/messaging/providers/apns') ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('authKey', '', new Text(0), 'APNS authentication key.') ->param('authKeyId', '', new Text(0), 'APNS authentication key ID.') ->param('teamId', '', new Text(0), 'APNS team ID.') @@ -971,7 +1028,7 @@ App::post('/v1/messaging/providers/apns') ->param('endpoint', '', new Text(0), 'APNS endpoint.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, string $authKey, string $authKeyId, string $teamId, string $bundleId, string $endpoint, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, string $authKey, string $authKeyId, string $teamId, string $bundleId, string $endpoint, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -979,6 +1036,7 @@ App::post('/v1/messaging/providers/apns') 'provider' => 'apns', 'type' => 'push', 'default' => $default, + 'enabled' => $enabled, 'credentials' => [ 'authKey' => $authKey, 'authKeyId' => $authKeyId, @@ -1023,6 +1081,7 @@ App::patch('/v1/messaging/providers/:id/apns') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('authKey', '', new Text(0), 'APNS authentication key.', true) ->param('authKeyId', '', new Text(0), 'APNS authentication key ID.', true) ->param('teamId', '', new Text(0), 'APNS team ID.', true) @@ -1030,7 +1089,7 @@ App::patch('/v1/messaging/providers/:id/apns') ->param('endpoint', '', new Text(0), 'APNS endpoint.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $authKey, string $authKeyId, string $teamId, string $bundleId, string $endpoint, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, string $authKey, string $authKeyId, string $teamId, string $bundleId, string $endpoint, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -1046,8 +1105,12 @@ App::patch('/v1/messaging/providers/:id/apns') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if ($authKey || $authKeyId || $teamId || $bundleId || $endpoint) { - // Check if all five variables are present + // Check if all credential variables are present if ($authKey && $authKeyId && $teamId && $bundleId && $endpoint) { $provider->setAttribute('credentials', [ 'authKey' => $authKey, @@ -1090,10 +1153,11 @@ App::post('/v1/messaging/providers/general') ->param('name', '', new Text(128), 'Provider name.') ->param('type', '', new WhiteList(['push', 'email', 'sms']), 'Provider type.') ->param('default', false, new Boolean(), 'Set as default provider.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('credentials', '', new JSON(), 'Provider credentials object.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $provider, string $name, string $type, bool $default, array $credentials, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $provider, string $name, string $type, bool $default, bool $enabled, array $credentials, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ '$id' => $providerId, @@ -1101,6 +1165,7 @@ App::post('/v1/messaging/providers/general') 'provider' => $provider, 'type' => $type, 'default' => $default, + 'enabled' => $enabled, 'credentials' => $credentials, ]); @@ -1139,10 +1204,11 @@ App::patch('/v1/messaging/providers/:id/general') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) + ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->param('credentials', '', new JSON(), 'Provider credentials.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, array $credentials, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, bool $enabled, array $credentials, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -1153,6 +1219,10 @@ App::patch('/v1/messaging/providers/:id/general') $provider->setAttribute('name', $name); } + if ($enabled === false) { + $provider->setAttribute('enabled', $enabled); + } + if (!empty($credentials)) { $provider->setAttribute('credentials', $credentials); }