2023-08-21 22:44:50 +12:00
< ? php
2023-09-06 22:10:56 +12:00
use Appwrite\Event\Messaging ;
2023-08-22 04:45:15 +12:00
use Appwrite\Extend\Exception ;
2023-09-06 22:10:56 +12:00
use Appwrite\Permission ;
use Appwrite\Role ;
2023-08-25 09:02:51 +12:00
use Appwrite\Utopia\Database\Validator\CustomId ;
2023-08-23 04:45:31 +12:00
use Appwrite\Utopia\Database\Validator\Queries\Providers ;
2023-08-21 22:44:50 +12:00
use Appwrite\Utopia\Response ;
use Utopia\App ;
use Utopia\Database\Database ;
use Utopia\Database\Document ;
2023-08-30 04:59:48 +12:00
use Utopia\Database\Exception\Duplicate as DuplicateException ;
2023-08-25 09:02:51 +12:00
use Utopia\Database\Helpers\ID ;
2023-08-23 04:45:31 +12:00
use Utopia\Database\Query ;
use Utopia\Database\Validator\Authorization ;
2023-09-06 22:10:56 +12:00
use Utopia\Database\Validator\Datetime as DatetimeValidator ;
2023-08-23 04:45:31 +12:00
use Utopia\Database\Validator\UID ;
2023-08-22 04:45:15 +12:00
use Utopia\Validator\ArrayList ;
2023-08-30 04:59:48 +12:00
use Utopia\Validator\Boolean ;
2023-09-06 05:10:48 +12:00
use Utopia\Validator\JSON ;
2023-08-21 22:44:50 +12:00
use Utopia\Validator\Text ;
2023-09-06 05:10:48 +12:00
use Utopia\Validator\WhiteList ;
2023-10-10 00:59:26 +13:00
use Utopia\Database\DateTime ;
2023-08-21 22:44:50 +12:00
App :: post ( '/v1/messaging/providers/mailgun' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Create Mailgun Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createMailgunProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-mailgun-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-08-30 04:59:48 +12:00
-> param ( 'default' , false , new Boolean (), 'Set as default provider.' , true )
2023-09-14 00:17:38 +12:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-09-26 20:42:44 +13:00
-> param ( 'isEuRegion' , false , new Boolean (), 'Set as EU region.' , true )
-> param ( 'from' , '' , new Text ( 256 ), 'Sender Email Address.' )
2023-08-24 21:01:22 +12:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Mailgun API Key.' )
-> param ( 'domain' , '' , new Text ( 0 ), 'Mailgun Domain.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-09-26 20:42:44 +13:00
-> action ( function ( string $providerId , string $name , bool $default , bool $enabled , bool $isEuRegion , string $from , string $apiKey , string $domain , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'mailgun' ,
'type' => 'email' ,
2023-08-30 04:59:48 +12:00
'default' => $default ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'mailgun' . ' ' . 'email' ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'apiKey' => $apiKey ,
'domain' => $domain ,
2023-09-19 22:06:40 +12:00
'isEuRegion' => $isEuRegion ,
2023-08-24 21:01:22 +12:00
],
2023-09-26 20:42:44 +13:00
'options' => [
'from' => $from ,
]
2023-08-30 04:59:48 +12:00
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'email' ])
2023-08-30 04:59:48 +12:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
App :: post ( '/v1/messaging/providers/sendgrid' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Create Sendgrid Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createSendgridProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-sengrid-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-08-30 04:59:48 +12:00
-> param ( 'default' , false , new Boolean (), 'Set as default provider.' , true )
2023-09-14 00:17:38 +12:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Sendgrid API key.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-09-14 00:17:38 +12:00
-> action ( function ( string $providerId , string $name , bool $default , bool $enabled , string $apiKey , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'sendgrid' ,
'type' => 'email' ,
2023-08-30 04:59:48 +12:00
'default' => $default ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-09-28 02:28:00 +13:00
'options' => [],
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'sendgrid' . ' ' . 'email' ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'apiKey' => $apiKey ,
],
2023-08-30 04:59:48 +12:00
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
App :: post ( '/v1/messaging/providers/msg91' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Create Msg91 Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createMsg91Provider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-msg91-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:16:15 +12:00
-> 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-08-30 04:59:48 +12:00
-> param ( 'default' , false , new Boolean (), 'Set as default provider.' , true )
2023-09-14 00:17:38 +12:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-09-28 02:28:00 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender Number.' )
2023-08-24 21:01:22 +12:00
-> param ( 'senderId' , '' , new Text ( 0 ), 'Msg91 Sender ID.' )
-> param ( 'authKey' , '' , new Text ( 0 ), 'Msg91 Auth Key.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-09-28 02:28:00 +13:00
-> action ( function ( string $providerId , string $name , bool $default , bool $enabled , string $from , string $senderId , string $authKey , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'msg91' ,
'type' => 'sms' ,
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'msg91' . ' ' . 'sms' ,
2023-08-30 04:59:48 +12:00
'default' => $default ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'senderId' => $senderId ,
'authKey' => $authKey ,
],
2023-09-28 02:28:00 +13:00
'options' => [
'from' => $from ,
]
2023-08-30 04:59:48 +12:00
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-04 23:45:59 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-06 00:27:48 +13:00
2023-08-23 04:45:31 +12:00
App :: post ( '/v1/messaging/providers/telesign' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Create Telesign Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createTelesignProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-telesign-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-08-30 04:59:48 +12:00
-> param ( 'default' , false , new Boolean (), 'Set as default provider.' , true )
2023-09-14 00:17:38 +12:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'username' , '' , new Text ( 0 ), 'Telesign username.' )
-> param ( 'password' , '' , new Text ( 0 ), 'Telesign password.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-09-14 00:17:38 +12:00
-> action ( function ( string $providerId , string $name , bool $default , bool $enabled , string $username , string $password , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'telesign' ,
'type' => 'sms' ,
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'telesign' . ' ' . 'sms' ,
2023-08-30 04:59:48 +12:00
'default' => $default ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'username' => $username ,
'password' => $password ,
],
2023-08-30 04:59:48 +12:00
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-04 23:45:59 +13:00
App :: post ( '/v1/messaging/providers/textmagic' )
-> desc ( 'Create Textmagic Provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-04 23:45:59 +13:00
-> label ( 'audits.event' , 'providers.create' )
2023-08-24 21:01:22 +12:00
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createTextmagicProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-textmagic-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-04 23:45:59 +13:00
-> 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.' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-04 23:45:59 +13:00
-> 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 ,
'name' => $name ,
'provider' => 'text-magic' ,
'type' => 'sms' ,
'search' => $providerId . ' ' . $name . ' ' . 'text-magic' . ' ' . 'sms' ,
'default' => $default ,
'enabled' => $enabled ,
'credentials' => [
'username' => $username ,
'apiKey' => $apiKey ,
],
]);
2023-08-24 21:01:22 +12:00
2023-10-04 23:45:59 +13:00
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'sms' ])
2023-10-04 23:45:59 +13:00
]))
) {
$provider -> setAttribute ( 'default' , true );
2023-09-14 00:17:38 +12:00
}
2023-10-04 23:45:59 +13:00
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-24 21:01:22 +12:00
}
2023-08-24 08:23:52 +12:00
2023-10-04 23:45:59 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
App :: post ( '/v1/messaging/providers/twilio' )
-> desc ( 'Create Twilio Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createTwilioProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-twilio-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> 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 , bool $enabled , string $accountSid , string $authToken , Database $dbForProject , Response $response ) {
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
$provider = new Document ([
'$id' => $providerId ,
'name' => $name ,
'provider' => 'twilio' ,
'type' => 'sms' ,
'search' => $providerId . ' ' . $name . ' ' . 'twilio' . ' ' . 'sms' ,
'default' => $default ,
'enabled' => $enabled ,
'credentials' => [
'accountSid' => $accountSid ,
'authToken' => $authToken ,
],
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'sms' ])
2023-10-04 23:45:59 +13:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-10-04 23:45:59 +13:00
}
2023-08-24 21:01:22 +12:00
$response
2023-10-04 23:45:59 +13:00
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
2023-08-24 21:01:22 +12:00
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-04 23:45:59 +13:00
App :: post ( '/v1/messaging/providers/vonage' )
-> desc ( 'Create Vonage Provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createVonageProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-vonage-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-08-30 04:59:48 +12:00
-> param ( 'default' , false , new Boolean (), 'Set as default provider.' , true )
2023-09-14 00:17:38 +12:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Vonage API key.' )
-> param ( 'apiSecret' , '' , new Text ( 0 ), 'Vonage API secret.' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-04 23:45:59 +13:00
-> action ( function ( string $providerId , string $name , bool $default , bool $enabled , string $apiKey , string $apiSecret , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-10-04 23:45:59 +13:00
'provider' => 'vonage' ,
2023-08-24 21:01:22 +12:00
'type' => 'sms' ,
2023-10-04 23:45:59 +13:00
'search' => $providerId . ' ' . $name . ' ' . 'vonage' . ' ' . 'sms' ,
2023-08-30 04:59:48 +12:00
'default' => $default ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'apiKey' => $apiKey ,
2023-10-04 23:45:59 +13:00
'apiSecret' => $apiSecret ,
2023-08-24 21:01:22 +12:00
],
2023-08-30 04:59:48 +12:00
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
2023-10-04 23:45:59 +13:00
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-22 04:45:15 +12:00
2023-10-04 23:45:59 +13:00
App :: post ( '/v1/messaging/providers/fcm' )
-> desc ( 'Create FCM Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'createFcmProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/create-fcm-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> 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 , bool $enabled , string $serverKey , Database $dbForProject , Response $response ) {
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
$provider = new Document ([
'$id' => $providerId ,
'name' => $name ,
'provider' => 'fcm' ,
'type' => 'push' ,
'search' => $providerId . ' ' . $name . ' ' . 'fcm' . ' ' . 'push' ,
'default' => $default ,
'enabled' => $enabled ,
'credentials' => [
'serverKey' => $serverKey ,
],
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'push' ])
2023-10-04 23:45:59 +13:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-10-04 23:45:59 +13:00
}
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
App :: post ( '/v1/messaging/providers/apns' )
-> desc ( 'Create APNS Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.create' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'createApnsProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/create-apns-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> 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.' )
-> param ( 'bundleId' , '' , new Text ( 0 ), 'APNS bundle ID.' )
-> param ( 'endpoint' , '' , new Text ( 0 ), 'APNS endpoint.' )
-> inject ( 'dbForProject' )
-> inject ( '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 ,
'name' => $name ,
'provider' => 'apns' ,
'type' => 'push' ,
'search' => $providerId . ' ' . $name . ' ' . 'apns' . ' ' . 'push' ,
'default' => $default ,
'enabled' => $enabled ,
'credentials' => [
'authKey' => $authKey ,
'authKeyId' => $authKeyId ,
'teamId' => $teamId ,
'bundleId' => $bundleId ,
'endpoint' => $endpoint ,
],
]);
// Check if a default provider exists, if not, set this one as default
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-06 00:27:48 +13:00
Query :: equal ( 'default' , [ true ]),
Query :: equal ( 'type' , [ 'push' ])
2023-10-04 23:45:59 +13:00
]))
) {
$provider -> setAttribute ( 'default' , true );
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-10-04 23:45:59 +13:00
}
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
App :: get ( '/v1/messaging/providers' )
-> desc ( 'List Providers' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'providers.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'listProviders' )
-> label ( 'sdk.description' , '/docs/references/messaging/list-providers.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER_LIST )
-> param ( 'queries' , [], new Providers (), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode ( ', ' , Providers :: ALLOWED_ATTRIBUTES ), true )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( array $queries , Database $dbForProject , Response $response ) {
$queries = Query :: parseQueries ( $queries );
// Get cursor document if there was a cursor query
$cursor = Query :: getByType ( $queries , [ Query :: TYPE_CURSORAFTER , Query :: TYPE_CURSORBEFORE ]);
$cursor = reset ( $cursor );
if ( $cursor ) {
$providerId = $cursor -> getValue ();
$cursorDocument = Authorization :: skip ( fn () => $dbForProject -> findOne ( 'providers' , [
Query :: equal ( '$id' , [ $providerId ]),
]));
2023-10-06 00:27:48 +13:00
if ( $cursorDocument === false || $cursorDocument -> isEmpty ()) {
2023-10-04 23:45:59 +13:00
throw new Exception ( Exception :: GENERAL_CURSOR_NOT_FOUND , " Provider ' { $providerId } ' for the 'cursor' value not found. " );
}
2023-10-07 02:53:46 +13:00
$cursor -> setValue ( $cursorDocument );
2023-10-04 23:45:59 +13:00
}
$filterQueries = Query :: groupByType ( $queries )[ 'filters' ];
$response -> dynamic ( new Document ([
'total' => $dbForProject -> count ( 'providers' , $filterQueries , APP_LIMIT_COUNT ),
'providers' => $dbForProject -> find ( 'providers' , $queries ),
]), Response :: MODEL_PROVIDER_LIST );
});
2023-10-06 11:00:47 +13:00
App :: get ( '/v1/messaging/providers/:providerId' )
2023-10-04 23:45:59 +13:00
-> desc ( 'Get Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'providers.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'getProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/get-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-06 11:00:47 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-06 11:00:47 +13:00
-> action ( function ( string $providerId , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$response -> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/mailgun/:providerId' )
2023-10-04 23:45:59 +13:00
-> desc ( 'Update Mailgun Provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateMailgunProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-mailgun-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'isEuRegion' , null , new Boolean (), 'Set as eu region.' , true )
-> param ( 'from' , '' , new Text ( 256 ), 'Sender Email Address.' , true )
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Mailgun API Key.' , true )
-> param ( 'domain' , '' , new Text ( 0 ), 'Mailgun Domain.' , true )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $isEuRegion , string $from , string $apiKey , string $domain , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
2023-08-24 08:23:52 +12:00
}
2023-08-24 21:01:22 +12:00
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-24 08:23:52 +12:00
2023-10-04 23:45:59 +13:00
if ( $providerAttr !== 'mailgun' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-08-24 08:23:52 +12:00
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'mailgun' . ' ' . 'email' );
}
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
if ( $isEuRegion === true || $isEuRegion === false ) {
$credentials [ 'isEuRegion' ] = $isEuRegion ;
}
if ( ! empty ( $apiKey )) {
$credentials [ 'apiKey' ] = $apiKey ;
}
if ( ! empty ( $domain )) {
$credentials [ 'domain' ] = $domain ;
2023-08-24 21:01:22 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
2023-08-24 21:01:22 +12:00
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/sendgrid/:providerId' )
2023-10-04 23:45:59 +13:00
-> desc ( 'Update Sendgrid Provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-04 23:45:59 +13:00
-> label ( 'audits.event' , 'providers.update' )
2023-08-24 21:01:22 +12:00
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateSendgridProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-sendgrid-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Sendgrid API key.' , true )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $apiKey , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'sendgrid' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-10-04 23:45:59 +13:00
}
if ( ! empty ( $name )) {
$provider -> setAttribute ( 'name' , $name );
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'sendgrid' . ' ' . 'email' );
}
if ( $enabled === true || $enabled === false ) {
$provider -> setAttribute ( 'enabled' , $enabled );
}
if ( ! empty ( $apiKey )) {
$provider -> setAttribute ( 'credentials' , [
'apiKey' => $apiKey ,
]);
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/msg91/:providerId' )
2023-10-04 23:45:59 +13:00
-> desc ( 'Update Msg91 Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateMsg91Provider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-msg91-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
-> param ( 'enabled' , null , 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' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $senderId , string $authKey , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'msg91' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-10-04 23:45:59 +13:00
}
if ( ! empty ( $name )) {
$provider -> setAttribute ( 'name' , $name );
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'msg91' . ' ' . 'sms' );
}
if ( $enabled === true || $enabled === false ) {
$provider -> setAttribute ( 'enabled' , $enabled );
}
$credentials = $provider -> getAttribute ( 'credentials' );
if ( ! empty ( $senderId )) {
$credentials [ 'senderId' ] = $senderId ;
}
if ( ! empty ( $authKey )) {
$credentials [ 'authKey' ] = $authKey ;
}
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/telesign/:providerId' )
2023-10-04 23:45:59 +13:00
-> desc ( 'Update Telesign Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateTelesignProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-telesign-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
-> param ( 'enabled' , null , 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' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $username , string $password , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'telesign' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-10-04 23:45:59 +13:00
}
if ( ! empty ( $name )) {
$provider -> setAttribute ( 'name' , $name );
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'telesign' . ' ' . 'sms' );
}
if ( $enabled === true || $enabled === false ) {
$provider -> setAttribute ( 'enabled' , $enabled );
}
$credentials = $provider -> getAttribute ( 'credentials' );
if ( ! empty ( $username )) {
$credentials [ 'username' ] = $username ;
}
if ( ! empty ( $password )) {
$credentials [ 'password' ] = $password ;
}
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/textmagic/:providerId' )
2023-10-04 23:45:59 +13:00
-> desc ( 'Update Textmagic Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateTextmagicProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-textmagic-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
-> param ( 'enabled' , null , 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' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $username , string $apiKey , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'text-magic' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-10-04 23:45:59 +13:00
}
if ( ! empty ( $name )) {
$provider -> setAttribute ( 'name' , $name );
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'textmagic' . ' ' . 'sms' );
}
2023-08-30 04:59:48 +12:00
2023-10-04 23:45:59 +13:00
if ( $enabled === true || $enabled === false ) {
$provider -> setAttribute ( 'enabled' , $enabled );
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
if ( ! empty ( $username )) {
$credentials [ 'username' ] = $username ;
}
if ( ! empty ( $apiKey )) {
$credentials [ 'apiKey' ] = $apiKey ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/twilio/:providerId' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Update Twilio Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateTwilioProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-twilio-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-08-24 21:01:22 +12:00
-> 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' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $accountSid , string $authToken , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
2023-08-24 08:23:52 +12:00
}
2023-08-24 21:01:22 +12:00
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-24 08:23:52 +12:00
2023-08-24 21:01:22 +12:00
if ( $providerAttr !== 'twilio' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'twilio' . ' ' . 'sms' );
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
2023-08-30 04:59:48 +12:00
2023-10-04 23:45:59 +13:00
if ( ! empty ( $accountSid )) {
$credentials [ 'accountSid' ] = $accountSid ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $authToken )) {
$credentials [ 'authToken' ] = $authToken ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/vonage/:providerId' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Update Vonage Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateVonageProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-vonage-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Vonage API key.' , true )
-> param ( 'apiSecret' , '' , new Text ( 0 ), 'Vonage API secret.' , true )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $apiKey , string $apiSecret , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'vonage' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 08:23:52 +12:00
}
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'vonage' . ' ' . 'sms' );
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
2023-08-30 04:59:48 +12:00
2023-10-04 23:45:59 +13:00
if ( ! empty ( $apiKey )) {
$credentials [ 'apiKey' ] = $apiKey ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $apiSecret )) {
$credentials [ 'apiSecret' ] = $apiSecret ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/fcm/:providerId' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Update FCM Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'updateFcmProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/update-fcm-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'serverKey' , '' , new Text ( 0 ), 'FCM Server Key.' , true )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $serverKey , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'fcm' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'fcm' . ' ' . 'push' );
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $serverKey )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'credentials' , [ 'serverKey' => $serverKey ]);
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/apns/:providerId' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Update APNS Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.update' )
-> label ( 'audits.resource' , 'providers/{response.$id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'updateApnsProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/update-apns-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-08-24 21:01:22 +12:00
-> 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 )
-> param ( 'bundleId' , '' , new Text ( 0 ), 'APNS bundle ID.' , true )
-> param ( 'endpoint' , '' , new Text ( 0 ), 'APNS endpoint.' , true )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , string $authKey , string $authKeyId , string $teamId , string $bundleId , string $endpoint , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
2023-08-24 08:23:52 +12:00
}
2023-08-24 21:01:22 +12:00
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-24 08:23:52 +12:00
2023-08-24 21:01:22 +12:00
if ( $providerAttr !== 'apns' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-08-24 08:23:52 +12:00
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'apns' . ' ' . 'push' );
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
if ( ! empty ( $authKey )) {
$credentials [ 'authKey' ] = $authKey ;
}
if ( ! empty ( $authKeyId )) {
$credentials [ 'authKeyId' ] = $authKeyId ;
}
if ( ! empty ( $teamId )) {
$credentials [ 'teamId' ] = $teamId ;
}
if ( ! empty ( $bundleId )) {
$credentials [ 'bundle' ] = $bundleId ;
}
if ( ! empty ( $endpoint )) {
$credentials [ 'endpoint' ] = $endpoint ;
2023-08-24 21:01:22 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
2023-08-24 21:01:22 +12:00
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-09-06 05:10:48 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-06 11:00:47 +13:00
App :: delete ( '/v1/messaging/providers/:providerId' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Delete Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'providers.delete' )
-> label ( 'audits.resource' , 'providers/{request.id}' )
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'deleteProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/delete-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2023-10-06 11:00:47 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-06 11:00:47 +13:00
-> action ( function ( string $providerId , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$dbForProject -> deleteDocument ( 'providers' , $provider -> getId ());
$response -> noContent ();
});
2023-08-23 04:45:31 +12:00
2023-08-22 04:45:15 +12:00
App :: post ( '/v1/messaging/messages/email' )
2023-08-24 21:01:22 +12:00
-> desc ( 'Send an email.' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'messages.create' )
-> label ( 'audits.resource' , 'messages/{response.$id}' )
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'sendEmail' )
-> label ( 'sdk.description' , '/docs/references/messaging/send-email.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE )
2023-09-22 03:47:15 +12:00
-> param ( 'messageId' , '' , new CustomId (), 'Message 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.' )
2023-10-04 23:45:59 +13:00
-> param ( 'providerId' , '' , new UID (), 'Email Provider ID.' )
2023-10-04 23:54:29 +13:00
-> param ( 'to' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY )), 'List of Topic IDs or List of User IDs or List of Target IDs.' )
2023-10-04 23:45:59 +13:00
-> param ( 'subject' , '' , new Text ( 998 ), 'Email Subject.' )
2023-09-28 02:28:00 +13:00
-> param ( 'description' , '' , new Text ( 256 ), 'Description for Message.' , true )
2023-10-06 00:27:48 +13:00
-> param ( 'content' , '' , new Text ( 64230 ), 'Email Content.' )
2023-10-07 02:53:46 +13:00
-> param ( 'status' , 'processing' , new WhiteList ([ 'draft' , 'processing' ]), 'Message Status.' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'html' , false , new Boolean (), 'Is content of type HTML' , true )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
2023-09-06 22:10:56 +12:00
-> inject ( 'project' )
-> inject ( 'messaging' )
2023-08-24 21:01:22 +12:00
-> inject ( 'response' )
2023-10-07 02:53:46 +13:00
-> action ( function ( string $messageId , string $providerId , array $to , string $subject , string $description , string $content , string $status , bool $html , Database $dbForProject , Document $project , Messaging $messaging , Response $response ) {
2023-09-22 03:47:15 +12:00
$messageId = $messageId == 'unique()' ? ID :: unique () : $messageId ;
2023-08-24 21:01:22 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$message = $dbForProject -> createDocument ( 'messages' , new Document ([
2023-09-22 03:47:15 +12:00
'$id' => $messageId ,
2023-08-24 21:01:22 +12:00
'providerId' => $provider -> getId (),
'providerInternalId' => $provider -> getInternalId (),
'to' => $to ,
'data' => [
'subject' => $subject ,
'content' => $content ,
2023-09-26 20:42:44 +13:00
'html' => $html ,
'description' => $description ,
2023-08-24 21:01:22 +12:00
],
2023-10-07 02:53:46 +13:00
'status' => $status ,
2023-09-28 02:28:00 +13:00
'search' => $messageId . ' ' . $description . ' ' . $subject ,
2023-08-24 21:01:22 +12:00
]));
2023-10-07 02:53:46 +13:00
if ( $status === 'processing' ) {
$messaging
-> setMessageId ( $message -> getId ())
-> setProject ( $project )
-> trigger ();
}
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
2023-09-06 23:00:46 +12:00
-> dynamic ( $message , Response :: MODEL_MESSAGE );
2023-08-24 21:01:22 +12:00
});
2023-10-04 23:45:59 +13:00
2023-10-07 02:53:46 +13:00
App :: get ( '/v1/messaging/messages' )
-> desc ( 'List Messages' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'messages.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'listMessages' )
-> label ( 'sdk.description' , '/docs/references/messaging/list-messages.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE_LIST )
-> param ( 'queries' , [], new Providers (), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode ( ', ' , Providers :: ALLOWED_ATTRIBUTES ), true )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( array $queries , Database $dbForProject , Response $response ) {
$queries = Query :: parseQueries ( $queries );
// Get cursor document if there was a cursor query
$cursor = Query :: getByType ( $queries , [ Query :: TYPE_CURSORAFTER , Query :: TYPE_CURSORBEFORE ]);
$cursor = reset ( $cursor );
if ( $cursor ) {
$messageId = $cursor -> getValue ();
$cursorDocument = Authorization :: skip ( fn () => $dbForProject -> findOne ( 'messages' , [
Query :: equal ( '$id' , [ $messageId ]),
]));
if ( $cursorDocument === false || $cursorDocument -> isEmpty ()) {
throw new Exception ( Exception :: GENERAL_CURSOR_NOT_FOUND , " Message ' { $messageId } ' for the 'cursor' value not found. " );
}
$cursor -> setValue ( $cursorDocument );
}
$filterQueries = Query :: groupByType ( $queries )[ 'filters' ];
$response -> dynamic ( new Document ([
'total' => $dbForProject -> count ( 'messages' , $filterQueries , APP_LIMIT_COUNT ),
'messages' => $dbForProject -> find ( 'messages' , $queries ),
]), Response :: MODEL_MESSAGE_LIST );
});
2023-10-06 11:00:47 +13:00
App :: get ( '/v1/messaging/messages/:messageId' )
2023-10-04 23:45:59 +13:00
-> desc ( 'Get Message' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'messages.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'getMessage' )
-> label ( 'sdk.description' , '/docs/references/messaging/get-message.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE )
2023-10-06 11:00:47 +13:00
-> param ( 'messageId' , '' , new UID (), 'Message ID.' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-06 11:00:47 +13:00
-> action ( function ( string $messageId , Database $dbForProject , Response $response ) {
$message = $dbForProject -> getDocument ( 'messages' , $messageId );
2023-10-04 23:45:59 +13:00
if ( $message -> isEmpty ()) {
throw new Exception ( Exception :: MESSAGE_NOT_FOUND );
}
$response -> dynamic ( $message , Response :: MODEL_MESSAGE );
});
2023-10-07 02:53:46 +13:00
App :: post ( '/v1/messaging/messages/email/:messageId' )
-> desc ( 'Update an email.' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'audits.event' , 'messages.update' )
-> label ( 'audits.resource' , 'messages/{response.$id}' )
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateEmail' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-email.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE )
-> param ( 'messageId' , '' , new UID (), 'Message ID.' )
-> param ( 'to' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY )), 'List of Topic IDs or List of User IDs or List of Target IDs.' , true )
-> param ( 'subject' , '' , new Text ( 998 ), 'Email Subject.' , true )
-> param ( 'description' , '' , new Text ( 256 ), 'Description for Message.' , true )
-> param ( 'content' , '' , new Text ( 64230 ), 'Email Content.' , true )
-> param ( 'status' , '' , new WhiteList ([ 'draft' , 'processing' ]), 'Message Status.' , true )
-> param ( 'html' , false , new Boolean (), 'Is content of type HTML' , true )
2023-10-10 00:59:26 +13:00
-> param ( 'deliveryTime' , DateTime :: now (), new DatetimeValidator (), 'Delivery time for message.' , true )
2023-10-07 02:53:46 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'project' )
-> inject ( 'messaging' )
-> inject ( 'response' )
2023-10-10 00:59:26 +13:00
-> action ( function ( string $messageId , array $to , string $subject , string $description , string $content , string $status , bool $html , string $deliveryTime , Database $dbForProject , Document $project , Messaging $messaging , Response $response ) {
2023-10-07 02:53:46 +13:00
$message = $dbForProject -> getDocument ( 'messages' , $messageId );
if ( $message -> isEmpty ()) {
throw new Exception ( Exception :: MESSAGE_NOT_FOUND );
}
if ( \count ( $to ) > 0 ) {
$message -> setAttribute ( 'to' , $to );
}
$data = $message -> getAttribute ( 'data' );
if ( ! empty ( $subject )) {
$data [ 'subject' ] = $subject ;
}
if ( ! empty ( $content )) {
$data [ 'content' ] = $content ;
}
if ( ! empty ( $description )) {
$data [ 'description' ] = $description ;
}
if ( ! empty ( $html )) {
$data [ 'html' ] = $html ;
}
$message -> setAttribute ( 'data' , $data );
$message -> setAttribute ( 'search' , $message -> getId () . ' ' . $data [ 'description' ] . ' ' . $data [ 'subject' ]);
if ( ! empty ( $status )) {
$message -> setAttribute ( 'status' , $status );
}
$message = $dbForProject -> updateDocument ( 'messages' , $message -> getId (), $message );
if ( $status === 'processing' ) {
$messaging
-> setMessageId ( $message -> getId ())
2023-10-10 00:59:26 +13:00
-> setDeliveryTime ( $deliveryTime )
2023-10-07 02:53:46 +13:00
-> setProject ( $project )
-> trigger ();
}
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $message , Response :: MODEL_MESSAGE );
});