2023-08-21 22:44:50 +12:00
< ? php
2023-08-22 04:45:15 +12:00
use Appwrite\Event\Event ;
use Appwrite\Extend\Exception ;
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-23 04:45:31 +12:00
use Utopia\Database\Query ;
use Utopia\Database\Validator\Authorization ;
2023-08-22 04:45:15 +12:00
use Utopia\Database\Validator\Datetime ;
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-21 22:44:50 +12:00
use Utopia\Validator\Text ;
2023-08-23 04:45:31 +12:00
App :: get ( '/v1/messaging/providers' )
-> desc ( 'List Providers' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'providers.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> 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 )
2023-08-24 08:23:52 +12:00
-> 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 )
2023-08-23 04:45:31 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( array $queries , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$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 -> find ( 'providers' , [
Query :: equal ( '$id' , [ $providerId ]),
Query :: limit ( 1 ),
]));
if ( empty ( $cursorDocument ) || $cursorDocument [ 0 ] -> isEmpty ()) {
throw new Exception ( Exception :: GENERAL_CURSOR_NOT_FOUND , " Provider ' { $providerId } ' for the 'cursor' value not found. " );
}
$cursor -> setValue ( $cursorDocument [ 0 ]);
}
$filterQueries = Query :: groupByType ( $queries )[ 'filters' ];
$response -> dynamic ( new Document ([
'total' => $dbForProject -> count ( 'providers' , $filterQueries , APP_LIMIT_COUNT ),
'indexes' => $dbForProject -> find ( 'providers' , $queries ),
]), Response :: MODEL_PROVIDER_LIST );
2023-08-23 04:45:31 +12:00
});
App :: get ( '/v1/messaging/providers/:id' )
-> desc ( 'Get Provider' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'providers.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> 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 )
-> param ( 'id' , null , new UID (), 'Provider ID.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-08-23 22:01:29 +12:00
-> action ( function ( string $id , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
$response -> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
2023-08-21 22:44:50 +12:00
/**
2023-08-23 00:51:30 +12:00
* Email Providers
2023-08-21 22:44:50 +12:00
*/
App :: post ( '/v1/messaging/providers/mailgun' )
-> desc ( 'Create Mailgun Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-21 22:44:50 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-08-23 04:45:31 +12:00
-> label ( 'sdk.method' , 'createProviderMailgun' )
2023-08-21 22:44:50 +12:00
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-mailgun.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-08-23 04:45:31 +12:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Mailgun API Key.' )
-> param ( 'domain' , '' , new Text ( 0 ), 'Mailgun Domain.' )
2023-08-21 22:44:50 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $apiKey , string $domain , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'mailgun' ,
'type' => 'email' ,
'credentials' => [
'apiKey' => $apiKey ,
'domain' => $domain ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/mailgun' )
-> desc ( 'Update Mailgun Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderMailgun' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-mailgun.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'mailgun' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
if ( $apiKey || $domain ) {
// Check if all five variables are present
if ( $apiKey && $domain ) {
$provider -> setAttribute ( 'credentials' , [
'apiKey' => $apiKey ,
'domain' => $domain
]);
} else {
// Not all credential params are present
throw new Exception ( Exception :: DOCUMENT_MISSING_DATA );
}
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: post ( '/v1/messaging/providers/sendgrid' )
-> desc ( 'Create Sendgrid Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderSendgrid' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-sendgrid.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Sendgrid API key.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $apiKey , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'sendgrid' ,
'type' => 'email' ,
'credentials' => [
'apiKey' => $apiKey ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/sendgrid' )
-> desc ( 'Update Sendgrid Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderSendgrid' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-sendgrid.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $providerAttr !== 'sendgrid' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $apiKey ) {
$provider -> setAttribute ( 'credentials' , [
'apiKey' => $apiKey
]);
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
/**
* SMS Providers
*/
App :: post ( '/v1/messaging/providers/msg91' )
-> desc ( 'Create Msg91 Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderMsg91' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-msg91.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> param ( 'senderId' , '' , new Text ( 0 ), 'Msg91 Sender ID.' )
-> param ( 'authKey' , '' , new Text ( 0 ), 'Msg91 Auth Key.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $senderId , string $authKey , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'msg91' ,
'type' => 'sms' ,
'credentials' => [
'senderId' => $senderId ,
'authKey' => $authKey ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/msg91' )
-> desc ( 'Update Msg91 Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderMsg91' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-msg91.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'msg91' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
if ( $senderId || $authKey ) {
// Check if all five variables are present
if ( $senderId && $authKey ) {
$provider -> setAttribute ( 'credentials' , [
'senderId' => $senderId ,
'authKey' => $authKey
]);
} else {
// Not all credential params are present
throw new Exception ( Exception :: DOCUMENT_MISSING_DATA );
}
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: post ( '/v1/messaging/providers/telesign' )
-> desc ( 'Create Telesign Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderTelesign' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-telesign.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> param ( 'username' , '' , new Text ( 0 ), 'Telesign username.' )
-> param ( 'password' , '' , new Text ( 0 ), 'Telesign password.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $username , string $password , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'telesign' ,
'type' => 'sms' ,
'credentials' => [
'username' => $username ,
'password' => $password ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/telesign' )
-> desc ( 'Update Telesign Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderTelesign' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-telesign.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'telesign' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
if ( $username || $password ) {
// Check if all five variables are present
if ( $username && $password ) {
$provider -> setAttribute ( 'credentials' , [
'username' => $username ,
'password' => $password
]);
} else {
// Not all credential params are present
throw new Exception ( Exception :: DOCUMENT_MISSING_DATA );
}
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: post ( '/v1/messaging/providers/textmagic' )
-> desc ( 'Create Textmagic Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderTextmagic' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-textmagic.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> param ( 'username' , '' , new Text ( 0 ), 'Textmagic username.' )
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Textmagic apiKey.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $username , string $apiKey , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'text-magic' ,
'type' => 'sms' ,
'credentials' => [
'username' => $username ,
'apiKey' => $apiKey ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-21 22:44:50 +12:00
});
2023-08-22 04:45:15 +12:00
2023-08-23 04:45:31 +12:00
App :: patch ( '/v1/messaging/providers/:id/textmagic' )
-> desc ( 'Update Textmagic Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderTextmagic' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-textmagic.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'text-magic' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
if ( $username || $apiKey ) {
// Check if all five variables are present
if ( $username && $apiKey ) {
$provider -> setAttribute ( 'credentials' , [
'username' => $username ,
'apiKey' => $apiKey
]);
} else {
// Not all credential params are present
throw new Exception ( Exception :: DOCUMENT_MISSING_DATA );
}
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: post ( '/v1/messaging/providers/twilio' )
-> desc ( 'Create Twilio Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderTwilio' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-twilio.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> param ( 'accountSid' , '' , new Text ( 0 ), 'Twilio account secret ID.' )
-> param ( 'authToken' , '' , new Text ( 0 ), 'Twilio authentication token.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $accountSid , string $authToken , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'twilio' ,
'type' => 'sms' ,
'credentials' => [
'accountSid' => $accountSid ,
'authToken' => $authToken ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/twilio' )
-> desc ( 'Update Twilio Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderTwilio' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-twilio.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'twilio' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
if ( $accountSid || $authToken ) {
// Check if all five variables are present
if ( $accountSid && $authToken ) {
$provider -> setAttribute ( 'credentials' , [
'accountSid' => $accountSid ,
'authToken' => $authToken ,
]);
} else {
// Not all credential params are present
throw new Exception ( Exception :: DOCUMENT_MISSING_DATA );
}
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: post ( '/v1/messaging/providers/vonage' )
-> desc ( 'Create Vonage Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderVonage' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-vonage.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Vonage API key.' )
-> param ( 'apiSecret' , '' , new Text ( 0 ), 'Vonage API secret.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $apiKey , string $apiSecret , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'vonage' ,
'type' => 'sms' ,
'credentials' => [
'apiKey' => $apiKey ,
'apiSecret' => $apiSecret ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/vonage' )
-> desc ( 'Update Vonage Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderVonage' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-vonage.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'vonage' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
if ( $apiKey || $apiSecret ) {
// Check if all five variables are present
if ( $apiKey && $apiSecret ) {
$provider -> setAttribute ( 'credentials' , [
'apiKey' => $apiKey ,
'apiSecret' => $apiSecret ,
]);
} else {
// Not all credential params are present
throw new Exception ( Exception :: DOCUMENT_MISSING_DATA );
}
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
/**
* Push Providers
*/
App :: post ( '/v1/messaging/providers/fcm' )
-> desc ( 'Create FCM Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderFCM' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-fcm.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> param ( 'serverKey' , '' , new Text ( 0 ), 'FCM Server Key.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $name , string $serverKey , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'fcm' ,
'type' => 'push' ,
'credentials' => [
'serverKey' => $serverKey ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/fcm' )
-> desc ( 'Update FCM Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderFCM' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-fcm.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $providerAttr !== 'fcm' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $serverKey ) {
$provider -> setAttribute ( 'credentials' , [ 'serverKey' => $serverKey ]);
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: post ( '/v1/messaging/providers/apns' )
-> desc ( 'Create APNS Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.create' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createProviderAPNS' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-provider-apns.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 ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
-> 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 $name , string $authKey , string $authKeyId , string $teamId , string $bundleId , string $endpoint , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> createDocument ( 'providers' , new Document ([
'name' => $name ,
'provider' => 'apns' ,
'type' => 'push' ,
'credentials' => [
'authKey' => $authKey ,
'authKeyId' => $authKeyId ,
'teamId' => $teamId ,
'bundleId' => $bundleId ,
'endpoint' => $endpoint ,
],
]));
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: patch ( '/v1/messaging/providers/:id/apns' )
-> desc ( 'Update APNS Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.update' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateProviderAPNS' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-provider-apns.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , 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 )
-> param ( 'bundleId' , '' , new Text ( 0 ), 'APNS bundle ID.' , true )
-> 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 ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'apns' ) {
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE . $providerAttr );
}
if ( $name ) {
$provider -> setAttribute ( 'name' , $name );
}
if ( $authKey || $authKeyId || $teamId || $bundleId || $endpoint ) {
// Check if all five variables are present
if ( $authKey && $authKeyId && $teamId && $bundleId && $endpoint ) {
$provider -> setAttribute ( 'credentials' , [
'authKey' => $authKey ,
'authKeyId' => $authKeyId ,
'teamId' => $teamId ,
'bundleId' => $bundleId ,
'endpoint' => $endpoint ,
]);
} else {
// Not all credential params are present
throw new Exception ( Exception :: DOCUMENT_MISSING_DATA );
}
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
2023-08-23 04:45:31 +12:00
});
App :: delete ( '/v1/messaging/providers/:id' )
-> desc ( 'Delete Provider' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.providers.delete' )
2023-08-23 04:45:31 +12:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'deleteProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/delete-provider.md' )
2023-08-23 22:01:29 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
2023-08-23 04:45:31 +12:00
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
-> param ( 'id' , '' , new UID (), 'Provider ID.' )
-> inject ( 'dbForProject' )
2023-08-23 22:01:29 +12:00
-> inject ( 'response' )
-> action ( function ( string $id , Database $dbForProject , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $id );
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
$dbForProject -> deleteCachedDocument ( 'providers' , $provider -> getId ());
$dbForProject -> deleteDocument ( 'providers' , $provider -> getId ());
2023-08-23 04:45:31 +12:00
2023-08-24 08:23:52 +12:00
$response -> noContent ();
2023-08-23 04:45:31 +12:00
});
2023-08-22 04:45:15 +12:00
App :: post ( '/v1/messaging/messages/email' )
-> desc ( 'Send an email.' )
-> groups ([ 'api' , 'messaging' ])
2023-08-23 22:01:29 +12:00
-> label ( 'audits.event' , 'messages.create' )
2023-08-22 04:45:15 +12:00
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-08-23 04:45:31 +12:00
-> label ( 'sdk.method' , 'sendEmail' )
2023-08-22 04:45:15 +12:00
-> 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 )
-> param ( 'providerId' , '' , new Text ( 128 ), 'Email Provider ID.' )
-> param ( 'to' , [], new ArrayList ( new Text ( 0 )), 'Email Recepient.' , true )
-> param ( 'subject' , null , new Text ( 0 ), 'Email Subject.' , true )
-> param ( 'content' , null , new Text ( 0 ), 'Email Content.' , true )
-> param ( 'from' , null , new Text ( 0 ), 'Email from.' , false )
-> param ( 'html' , null , new Text ( 0 ), 'Is content of type HTML' , false )
-> param ( 'deliveryTime' , null , new Datetime (), 'Delivery time of the message' , false )
-> inject ( 'dbForProject' )
-> inject ( 'events' )
-> inject ( 'response' )
-> action ( function ( string $providerId , string $to , string $subject , string $content , string $from , string $html , DateTime $deliveryTime , Database $dbForProject , Event $eventsInstance , Response $response ) {
2023-08-24 08:23:52 +12:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$message = $dbForProject -> createDocument ( 'messages' , new Document ([
'providerId' => $provider -> getId (),
'providerInternalId' => $provider -> getInternalId (),
'to' => $to ,
'data' => [
'subject' => $subject ,
'content' => $content ,
],
'deliveryTime' => $deliveryTime ,
'deliveryError' => null ,
'deliveredTo' => null ,
'delivered' => false ,
'search' => null ,
]));
$eventsInstance -> setParam ( 'messageId' , $message -> getId ());
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_MESSAGE );
2023-08-22 04:45:15 +12:00
});