1
0
Fork 0
mirror of synced 2024-07-04 14:10:33 +12:00

adds provider type in target

This commit is contained in:
prateek banga 2023-11-14 18:14:07 +05:30
parent c5aaa670a9
commit 7ae614fe13
14 changed files with 202 additions and 70 deletions

View file

@ -1845,7 +1845,7 @@ $commonCollections = [
'filters' => [], 'filters' => [],
], ],
[ [
'$id' => ID::custom('providerId'), '$id' => ID::custom('providerType'),
'type' => Database::VAR_STRING, 'type' => Database::VAR_STRING,
'format' => '', 'format' => '',
'size' => Database::LENGTH_KEY, 'size' => Database::LENGTH_KEY,
@ -1855,13 +1855,24 @@ $commonCollections = [
'array' => false, 'array' => false,
'filters' => [], 'filters' => [],
], ],
[
'$id' => ID::custom('providerId'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[ [
'$id' => ID::custom('providerInternalId'), '$id' => ID::custom('providerInternalId'),
'type' => Database::VAR_STRING, 'type' => Database::VAR_STRING,
'format' => '', 'format' => '',
'size' => Database::LENGTH_KEY, 'size' => Database::LENGTH_KEY,
'signed' => true, 'signed' => true,
'required' => true, 'required' => false,
'default' => null, 'default' => null,
'array' => false, 'array' => false,
'filters' => [], 'filters' => [],

View file

@ -781,6 +781,11 @@ return [
'description' => 'Provider with the requested ID is of incorrect type: ', 'description' => 'Provider with the requested ID is of incorrect type: ',
'code' => 400, 'code' => 400,
], ],
Exception::PROVIDER_INTERNAL_UPDATE_DISABLED => [
'name' => Exception::PROVIDER_INTERNAL_UPDATE_DISABLED,
'description' => 'Provider with the requested ID cannot be disabled.',
'code' => 400,
],
/** Topic Errors */ /** Topic Errors */
Exception::TOPIC_NOT_FOUND => [ Exception::TOPIC_NOT_FOUND => [

View file

@ -1240,6 +1240,7 @@ App::post('/v1/account/sessions/phone')
Query::equal('internal', [true]), Query::equal('internal', [true]),
Query::equal('type', ['sms']) Query::equal('type', ['sms'])
])); ]));
if ($provider === false || $provider->isEmpty()) { if ($provider === false || $provider->isEmpty()) {
throw new Exception(Exception::GENERAL_PHONE_DISABLED, 'Phone provider not configured'); throw new Exception(Exception::GENERAL_PHONE_DISABLED, 'Phone provider not configured');
} }
@ -1335,8 +1336,7 @@ App::post('/v1/account/sessions/phone')
$target = $dbForProject->createDocument('targets', new Document([ $target = $dbForProject->createDocument('targets', new Document([
'userId' => $user->getId(), 'userId' => $user->getId(),
'userInternalId' => $user->getInternalId(), 'userInternalId' => $user->getInternalId(),
'providerId' => $provider->getId(), 'providerType' => 'sms',
'providerInternalId' => $provider->getInternalId(),
'identifier' => $phone, 'identifier' => $phone,
])); ]));
} }
@ -2959,8 +2959,7 @@ App::post('/v1/account/verification/phone')
$target = $dbForProject->createDocument('targets', new Document([ $target = $dbForProject->createDocument('targets', new Document([
'userId' => $user->getId(), 'userId' => $user->getId(),
'userInternalId' => $user->getInternalId(), 'userInternalId' => $user->getInternalId(),
'providerId' => $provider->getId(), 'providerType' => 'sms',
'providerInternalId' => $provider->getInternalId(),
'identifier' => $user->getAttribute('phone'), 'identifier' => $user->getAttribute('phone'),
])); ]));
} }

View file

@ -806,14 +806,21 @@ App::patch('/v1/messaging/providers/mailgun/:providerId')
]); ]);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');
if ($isEuRegion === true || $isEuRegion === false) { if ($isEuRegion === true || $isEuRegion === false) {
@ -893,14 +900,17 @@ App::patch('/v1/messaging/providers/sendgrid/:providerId')
]); ]);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
if (!empty($apiKey)) { if (!empty($apiKey)) {
$provider->setAttribute('credentials', [ $provider->setAttribute('credentials', [
'apiKey' => $apiKey, 'apiKey' => $apiKey,
@ -971,14 +981,17 @@ App::patch('/v1/messaging/providers/msg91/:providerId')
]); ]);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');
if (!empty($senderId)) { if (!empty($senderId)) {
@ -1055,14 +1068,17 @@ App::patch('/v1/messaging/providers/telesign/:providerId')
]); ]);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');
if (!empty($username)) { if (!empty($username)) {
@ -1139,14 +1155,17 @@ App::patch('/v1/messaging/providers/textmagic/:providerId')
]); ]);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_UPDATE_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');
if (!empty($username)) { if (!empty($username)) {
@ -1223,14 +1242,17 @@ App::patch('/v1/messaging/providers/twilio/:providerId')
]); ]);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');
if (!empty($accountSid)) { if (!empty($accountSid)) {
@ -1307,14 +1329,17 @@ App::patch('/v1/messaging/providers/vonage/:providerId')
]); ]);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');
if (!empty($apiKey)) { if (!empty($apiKey)) {
@ -1383,14 +1408,17 @@ App::patch('/v1/messaging/providers/fcm/:providerId')
$provider->setAttribute('name', $name); $provider->setAttribute('name', $name);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
if (!empty($serverKey)) { if (!empty($serverKey)) {
$provider->setAttribute('credentials', ['serverKey' => $serverKey]); $provider->setAttribute('credentials', ['serverKey' => $serverKey]);
} }
@ -1456,14 +1484,17 @@ App::patch('/v1/messaging/providers/apns/:providerId')
$provider->setAttribute('name', $name); $provider->setAttribute('name', $name);
} }
if ($enabled === true || $enabled === false) {
$provider->setAttribute('enabled', $enabled);
}
if ($internal === true) { if ($internal === true) {
$provider->setAttribute('internal', $internal); $provider->setAttribute('internal', $internal);
} }
if ($enabled === true || $enabled === false) {
if ($provider->getAttribute('internal') === true && $enabled === false) {
throw new Exception(Exception::PROVIDER_INTERNAL_DISABLED);
}
$provider->setAttribute('enabled', $enabled);
}
$credentials = $provider->getAttribute('credentials'); $credentials = $provider->getAttribute('credentials');
if (!empty($authKey)) { if (!empty($authKey)) {

View file

@ -650,8 +650,7 @@ App::post('/v1/teams/:teamId/memberships')
$target = $dbForProject->createDocument('targets', new Document([ $target = $dbForProject->createDocument('targets', new Document([
'userId' => $invitee->getId(), 'userId' => $invitee->getId(),
'userInternalId' => $invitee->getInternalId(), 'userInternalId' => $invitee->getInternalId(),
'providerId' => $provider->getId(), 'providerType' => 'sms',
'providerInternalId' => $provider->getInternalId(),
'identifier' => $phone, 'identifier' => $phone,
])); ]));

View file

@ -394,18 +394,25 @@ App::post('/v1/users/:userId/targets')
->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_TARGET) ->label('sdk.response.model', Response::MODEL_TARGET)
->param('targetId', '', new CustomId(), 'Target 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('userId', '', new UID(), 'User ID.') ->param('userId', '', new UID(), 'User ID.')
->param('targetId', '', new UID(), 'Target ID.') ->param('providerType', '', new WhiteList(['email', 'sms', 'push']), 'The target provider type. Can be one of the following: `email`, `sms` or `push`.')
->param('providerId', '', new UID(), 'Provider ID.')
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)') ->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)')
->param('providerId', '', new UID(), 'Provider ID. Message will be sent to this target from the specified provider ID. If no provider ID is set the first setup provider will be used.', true)
->inject('queueForEvents') ->inject('queueForEvents')
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->action(function (string $userId, string $targetId, string $providerId, string $identifier, Event $queueForEvents, Response $response, Database $dbForProject) { ->action(function (string $targetId, string $userId, string $providerType, string $identifier, string $providerId, Event $queueForEvents, Response $response, Database $dbForProject) {
$provider = $dbForProject->getDocument('providers', $providerId); $targetId = $targetId == 'unique()' ? ID::unique() : $targetId;
if ($provider->isEmpty()) { $provider = new Document();
throw new Exception(Exception::PROVIDER_NOT_FOUND);
if ($providerType === 'push') {
$provider = $dbForProject->getDocument('providers', $providerId);
if ($provider->isEmpty()) {
throw new Exception(Exception::PROVIDER_NOT_FOUND);
}
} }
$user = $dbForProject->getDocument('users', $userId); $user = $dbForProject->getDocument('users', $userId);
@ -423,8 +430,9 @@ App::post('/v1/users/:userId/targets')
try { try {
$target = $dbForProject->createDocument('targets', new Document([ $target = $dbForProject->createDocument('targets', new Document([
'$id' => $targetId, '$id' => $targetId,
'providerId' => $providerId, 'providerId' => $providerId ?? null,
'providerInternalId' => $provider->getInternalId(), 'providerInternalId' => $provider->getInternalId() ?? null,
'providerType' => $providerType,
'userId' => $userId, 'userId' => $userId,
'userInternalId' => $user->getInternalId(), 'userInternalId' => $user->getInternalId(),
'identifier' => $identifier, 'identifier' => $identifier,
@ -1223,8 +1231,8 @@ App::patch('/v1/users/:userId/prefs')
$response->dynamic(new Document($prefs), Response::MODEL_PREFERENCES); $response->dynamic(new Document($prefs), Response::MODEL_PREFERENCES);
}); });
App::patch('/v1/users/:userId/targets/:targetId/identifier') App::patch('/v1/users/:userId/targets/:targetId')
->desc('Update user target\'s identifier') ->desc('Update User target')
->groups(['api', 'users']) ->groups(['api', 'users'])
->label('audits.event', 'target.update') ->label('audits.event', 'target.update')
->label('audits.resource', 'target/{response.$id}') ->label('audits.resource', 'target/{response.$id}')
@ -1232,19 +1240,19 @@ App::patch('/v1/users/:userId/targets/:targetId/identifier')
->label('scope', 'targets.write') ->label('scope', 'targets.write')
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_ADMIN]) ->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'users') ->label('sdk.namespace', 'users')
->label('sdk.method', 'updateTargetIdentifier') ->label('sdk.method', 'updateTarget')
->label('sdk.description', '/docs/references/users/update-target-identifier.md') ->label('sdk.description', '/docs/references/users/update-target.md')
->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_TARGET) ->label('sdk.response.model', Response::MODEL_TARGET)
->param('userId', '', new UID(), 'User ID.') ->param('userId', '', new UID(), 'User ID.')
->param('targetId', '', new UID(), 'Target ID.') ->param('targetId', '', new UID(), 'Target ID.')
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)') ->param('providerId', '', new UID(), 'Provider ID. Message will be sent to this target from the specified provider ID. If no provider ID is set the first setup provider will be used.', true)
->param('identifier', '', new Text(Database::LENGTH_KEY), 'The target identifier (token, email, phone etc.)', true)
->inject('queueForEvents') ->inject('queueForEvents')
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->action(function (string $userId, string $targetId, string $identifier, Event $queueForEvents, Response $response, Database $dbForProject) { ->action(function (string $userId, string $targetId, string $providerId, string $identifier, Event $queueForEvents, Response $response, Database $dbForProject) {
$user = $dbForProject->getDocument('users', $userId); $user = $dbForProject->getDocument('users', $userId);
if ($user->isEmpty()) { if ($user->isEmpty()) {
@ -1261,7 +1269,20 @@ App::patch('/v1/users/:userId/targets/:targetId/identifier')
throw new Exception(Exception::USER_TARGET_NOT_FOUND); throw new Exception(Exception::USER_TARGET_NOT_FOUND);
} }
$target->setAttribute('identifier', $identifier); if ($identifier) {
$target->setAttribute('identifier', $identifier);
}
if ($providerId) {
$provider = $dbForProject->getDocument('providers', $providerId);
if ($provider->isEmpty()) {
throw new Exception(Exception::PROVIDER_NOT_FOUND);
}
$target->setAttribute('providerId', $provider->getId());
$target->setAttribute('providerInternalId', $provider->getInternalId());
}
$target = $dbForProject->updateDocument('targets', $target->getId(), $target); $target = $dbForProject->updateDocument('targets', $target->getId(), $target);
$dbForProject->deleteCachedDocument('users', $user->getId()); $dbForProject->deleteCachedDocument('users', $user->getId());

View file

@ -236,6 +236,7 @@ class Exception extends \Exception
public const PROVIDER_NOT_FOUND = 'provider_not_found'; public const PROVIDER_NOT_FOUND = 'provider_not_found';
public const PROVIDER_ALREADY_EXISTS = 'provider_already_exists'; public const PROVIDER_ALREADY_EXISTS = 'provider_already_exists';
public const PROVIDER_INCORRECT_TYPE = 'provider_incorrect_type'; public const PROVIDER_INCORRECT_TYPE = 'provider_incorrect_type';
public const PROVIDER_INTERNAL_UPDATE_DISABLED = 'provider_internal_update_disabled';
/** Topic */ /** Topic */
public const TOPIC_NOT_FOUND = 'topic_not_found'; public const TOPIC_NOT_FOUND = 'topic_not_found';

View file

@ -98,6 +98,11 @@ class Messaging extends Action
$recipients = \array_merge($recipients, $targets); $recipients = \array_merge($recipients, $targets);
} }
$internalProvider = $dbForProject->findOne('providers', [
Query::equal('internal', [true]),
Query::equal('type', [$recipients[0]->getAttribute('providerType')]),
]);
/** /**
* @var array<string, array<string>> $identifiersByProviderId * @var array<string, array<string>> $identifiersByProviderId
*/ */
@ -109,6 +114,11 @@ class Messaging extends Action
$providers = []; $providers = [];
foreach ($recipients as $recipient) { foreach ($recipients as $recipient) {
$providerId = $recipient->getAttribute('providerId'); $providerId = $recipient->getAttribute('providerId');
if (!$providerId) {
$providerId = $internalProvider->getId();
}
if (!isset($identifiersByProviderId[$providerId])) { if (!isset($identifiersByProviderId[$providerId])) {
$identifiersByProviderId[$providerId] = []; $identifiersByProviderId[$providerId] = [];
} }
@ -118,17 +128,26 @@ class Messaging extends Action
/** /**
* @var array[] $results * @var array[] $results
*/ */
$results = batch(\array_map(function ($providerId) use ($identifiersByProviderId, $providers, $message, $dbForProject) { $results = batch(\array_map(function ($providerId) use ($identifiersByProviderId, $providers, $internalProvider, $message, $dbForProject) {
return function () use ($providerId, $identifiersByProviderId, $providers, $message, $dbForProject) { return function () use ($providerId, $identifiersByProviderId, $providers, $internalProvider, $message, $dbForProject) {
$provider = $dbForProject->getDocument('providers', $providerId); $provider = new Document();
if ($internalProvider->getId() === $providerId) {
$provider = $internalProvider;
} else {
$provider = $dbForProject->getDocument('providers', $providerId);
}
$providers[] = $provider; $providers[] = $provider;
$identifiers = $identifiersByProviderId[$providerId]; $identifiers = $identifiersByProviderId[$providerId];
$adapter = match ($provider->getAttribute('type')) { $adapter = match ($provider->getAttribute('type')) {
'sms' => $this->sms($provider), 'sms' => $this->sms($provider),
'push' => $this->push($provider), 'push' => $this->push($provider),
'email' => $this->email($provider), 'email' => $this->email($provider),
default => throw new Exception(Exception::PROVIDER_INCORRECT_TYPE) default => throw new Exception(Exception::PROVIDER_INCORRECT_TYPE)
}; };
$maxBatchSize = $adapter->getMaxMessagesPerRequest(); $maxBatchSize = $adapter->getMaxMessagesPerRequest();
$batches = \array_chunk($identifiers, $maxBatchSize); $batches = \array_chunk($identifiers, $maxBatchSize);
$batchIndex = 0; $batchIndex = 0;
@ -139,12 +158,14 @@ class Messaging extends Action
$deliveryErrors = []; $deliveryErrors = [];
$messageData = clone $message; $messageData = clone $message;
$messageData->setAttribute('to', $batch); $messageData->setAttribute('to', $batch);
$data = match ($provider->getAttribute('type')) { $data = match ($provider->getAttribute('type')) {
'sms' => $this->buildSMSMessage($messageData, $provider), 'sms' => $this->buildSMSMessage($messageData, $provider),
'push' => $this->buildPushMessage($messageData), 'push' => $this->buildPushMessage($messageData),
'email' => $this->buildEmailMessage($messageData, $provider), 'email' => $this->buildEmailMessage($messageData, $provider),
default => throw new Exception(Exception::PROVIDER_INCORRECT_TYPE) default => throw new Exception(Exception::PROVIDER_INCORRECT_TYPE)
}; };
try { try {
$adapter->send($data); $adapter->send($data);
$deliveredTotal += \count($batch); $deliveredTotal += \count($batch);
@ -168,10 +189,12 @@ class Messaging extends Action
$deliveredTotal = 0; $deliveredTotal = 0;
$deliveryErrors = []; $deliveryErrors = [];
foreach ($results as $result) { foreach ($results as $result) {
$deliveredTotal += $result['deliveredTotal']; $deliveredTotal += $result['deliveredTotal'];
$deliveryErrors = \array_merge($deliveryErrors, $result['deliveryErrors']); $deliveryErrors = \array_merge($deliveryErrors, $result['deliveryErrors']);
} }
$message->setAttribute('deliveryErrors', $deliveryErrors); $message->setAttribute('deliveryErrors', $deliveryErrors);
if (\count($message->getAttribute('deliveryErrors')) > 0) { if (\count($message->getAttribute('deliveryErrors')) > 0) {
@ -179,6 +202,7 @@ class Messaging extends Action
} else { } else {
$message->setAttribute('status', 'sent'); $message->setAttribute('status', 'sent');
} }
$message->removeAttribute('to'); $message->removeAttribute('to');
foreach ($providers as $provider) { foreach ($providers as $provider) {

View file

@ -41,6 +41,12 @@ class Target extends Model
'default' => '', 'default' => '',
'example' => '259125845563242502', 'example' => '259125845563242502',
]) ])
->addRule('providerType', [
'type' => self::TYPE_STRING,
'description' => 'The target provider type. Can be one of the following: `email`, `sms` or `push`.',
'default' => '',
'example' => 'email',
])
->addRule('identifier', [ ->addRule('identifier', [
'type' => self::TYPE_STRING, 'type' => self::TYPE_STRING,
'description' => 'The target identifier.', 'description' => 'The target identifier.',

View file

@ -934,10 +934,11 @@ trait Base
} }
}'; }';
case self::$CREATE_USER_TARGET: case self::$CREATE_USER_TARGET:
return 'mutation createUserTarget($userId: String!, $targetId: String!, $providerId: String!, $identifier: String!){ return 'mutation createUserTarget($userId: String!, $targetId: String!, $providerType: String!, $identifier: String! $providerId: String){
usersCreateTarget(userId: $userId, targetId: $targetId, providerId: $providerId, identifier: $identifier) { usersCreateTarget(userId: $userId, targetId: $targetId, providerType: $providerType, identifier: $identifier, providerId: $providerId) {
_id _id
userId userId
providerType
providerId providerId
identifier identifier
} }
@ -949,6 +950,7 @@ trait Base
targets { targets {
_id _id
userId userId
providerType
providerId providerId
identifier identifier
} }
@ -959,15 +961,17 @@ trait Base
usersGetTarget(userId: $userId, targetId: $targetId) { usersGetTarget(userId: $userId, targetId: $targetId) {
_id _id
userId userId
providerType
providerId providerId
identifier identifier
} }
}'; }';
case self::$UPDATE_USER_TARGET: case self::$UPDATE_USER_TARGET:
return 'mutation updateUserTarget($userId: String!, $targetId: String!, $identifier: String!){ return 'mutation updateUserTarget($userId: String!, $targetId: String!, $providerId: String, $identifier: String){
usersUpdateTargetIdentifier(userId: $userId, targetId: $targetId, identifier: $identifier) { usersUpdateTarget(userId: $userId, targetId: $targetId, providerId: $providerId, identifier: $identifier) {
_id _id
userId userId
providerType
providerId providerId
identifier identifier
} }

View file

@ -395,6 +395,7 @@ class MessagingTest extends Scope
'query' => $query, 'query' => $query,
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'email',
'userId' => $userId, 'userId' => $userId,
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => 'token', 'identifier' => 'token',
@ -604,6 +605,7 @@ class MessagingTest extends Scope
'query' => $query, 'query' => $query,
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'email',
'userId' => $user['body']['data']['usersCreate']['_id'], 'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => $to, 'identifier' => $to,
@ -755,6 +757,7 @@ class MessagingTest extends Scope
'query' => $query, 'query' => $query,
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'email',
'userId' => $user['body']['data']['usersCreate']['_id'], 'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => $to, 'identifier' => $to,
@ -916,6 +919,7 @@ class MessagingTest extends Scope
'query' => $query, 'query' => $query,
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'sms',
'userId' => $user['body']['data']['usersCreate']['_id'], 'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => $to, 'identifier' => $to,
@ -1063,6 +1067,7 @@ class MessagingTest extends Scope
'query' => $query, 'query' => $query,
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'sms',
'userId' => $user['body']['data']['usersCreate']['_id'], 'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => $to, 'identifier' => $to,
@ -1219,6 +1224,7 @@ class MessagingTest extends Scope
'query' => $query, 'query' => $query,
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'push',
'userId' => $user['body']['data']['usersCreate']['_id'], 'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => $to, 'identifier' => $to,
@ -1363,6 +1369,7 @@ class MessagingTest extends Scope
'query' => $query, 'query' => $query,
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'push',
'userId' => $user['body']['data']['usersCreate']['_id'], 'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => $to, 'identifier' => $to,

View file

@ -78,6 +78,7 @@ class UsersTest extends Scope
'variables' => [ 'variables' => [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'userId' => $user['_id'], 'userId' => $user['_id'],
'providerType' => 'email',
'providerId' => $providerId, 'providerId' => $providerId,
'identifier' => 'identifier', 'identifier' => 'identifier',
] ]
@ -479,7 +480,7 @@ class UsersTest extends Scope
], $this->getHeaders()), $graphQLPayload); ], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $target['headers']['status-code']); $this->assertEquals(200, $target['headers']['status-code']);
$this->assertEquals('newidentifier', $target['body']['data']['usersUpdateTargetIdentifier']['identifier']); $this->assertEquals('newidentifier', $target['body']['data']['usersUpdateTarget']['identifier']);
} }
public function testDeleteUserSessions() public function testDeleteUserSessions()

View file

@ -317,6 +317,7 @@ trait MessagingBase
'x-appwrite-key' => $this->getProject()['apiKey'], 'x-appwrite-key' => $this->getProject()['apiKey'],
]), [ ]), [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'email',
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'identifier' => 'my-token', 'identifier' => 'my-token',
]); ]);
@ -558,6 +559,7 @@ trait MessagingBase
'isEuRegion' => filter_var($isEuRegion, FILTER_VALIDATE_BOOLEAN), 'isEuRegion' => filter_var($isEuRegion, FILTER_VALIDATE_BOOLEAN),
'from' => $from 'from' => $from
]); ]);
$this->assertEquals(201, $provider['headers']['status-code']); $this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic // Create Topic
@ -570,6 +572,7 @@ trait MessagingBase
'name' => 'topic1', 'name' => 'topic1',
'description' => 'Test Topic' 'description' => 'Test Topic'
]); ]);
$this->assertEquals(201, $topic['headers']['status-code']); $this->assertEquals(201, $topic['headers']['status-code']);
// Create User // Create User
@ -593,6 +596,7 @@ trait MessagingBase
'x-appwrite-key' => $this->getProject()['apiKey'], 'x-appwrite-key' => $this->getProject()['apiKey'],
], [ ], [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'email',
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'identifier' => $to, 'identifier' => $to,
]); ]);
@ -682,6 +686,7 @@ trait MessagingBase
'isEuRegion' => filter_var($isEuRegion, FILTER_VALIDATE_BOOLEAN), 'isEuRegion' => filter_var($isEuRegion, FILTER_VALIDATE_BOOLEAN),
'from' => $from 'from' => $from
]); ]);
$this->assertEquals(201, $provider['headers']['status-code']); $this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic // Create Topic
@ -694,6 +699,7 @@ trait MessagingBase
'name' => 'topic1', 'name' => 'topic1',
'description' => 'Test Topic' 'description' => 'Test Topic'
]); ]);
$this->assertEquals(201, $topic['headers']['status-code']); $this->assertEquals(201, $topic['headers']['status-code']);
// Create User // Create User
@ -717,6 +723,7 @@ trait MessagingBase
'x-appwrite-key' => $this->getProject()['apiKey'], 'x-appwrite-key' => $this->getProject()['apiKey'],
], [ ], [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'email',
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'identifier' => $to, 'identifier' => $to,
]); ]);
@ -801,6 +808,7 @@ trait MessagingBase
'authKey' => $authKey, 'authKey' => $authKey,
'from' => $from 'from' => $from
]); ]);
$this->assertEquals(201, $provider['headers']['status-code']); $this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic // Create Topic
@ -813,6 +821,7 @@ trait MessagingBase
'name' => 'topic1', 'name' => 'topic1',
'description' => 'Test Topic' 'description' => 'Test Topic'
]); ]);
$this->assertEquals(201, $topic['headers']['status-code']); $this->assertEquals(201, $topic['headers']['status-code']);
// Create User // Create User
@ -836,6 +845,7 @@ trait MessagingBase
'x-appwrite-key' => $this->getProject()['apiKey'], 'x-appwrite-key' => $this->getProject()['apiKey'],
], [ ], [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'sms',
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'identifier' => $to, 'identifier' => $to,
]); ]);
@ -922,6 +932,7 @@ trait MessagingBase
'authKey' => $authKey, 'authKey' => $authKey,
'from' => $from 'from' => $from
]); ]);
$this->assertEquals(201, $provider['headers']['status-code']); $this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic // Create Topic
@ -934,6 +945,7 @@ trait MessagingBase
'name' => 'topic1', 'name' => 'topic1',
'description' => 'Test Topic' 'description' => 'Test Topic'
]); ]);
$this->assertEquals(201, $topic['headers']['status-code']); $this->assertEquals(201, $topic['headers']['status-code']);
// Create User // Create User
@ -957,6 +969,7 @@ trait MessagingBase
'x-appwrite-key' => $this->getProject()['apiKey'], 'x-appwrite-key' => $this->getProject()['apiKey'],
], [ ], [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'sms',
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'identifier' => $to, 'identifier' => $to,
]); ]);
@ -1036,6 +1049,7 @@ trait MessagingBase
'name' => 'FCM-1', 'name' => 'FCM-1',
'serverKey' => $serverKey, 'serverKey' => $serverKey,
]); ]);
$this->assertEquals(201, $provider['headers']['status-code']); $this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic // Create Topic
@ -1048,6 +1062,7 @@ trait MessagingBase
'name' => 'topic1', 'name' => 'topic1',
'description' => 'Test Topic' 'description' => 'Test Topic'
]); ]);
$this->assertEquals(201, $topic['headers']['status-code']); $this->assertEquals(201, $topic['headers']['status-code']);
// Create User // Create User
@ -1071,6 +1086,7 @@ trait MessagingBase
'x-appwrite-key' => $this->getProject()['apiKey'], 'x-appwrite-key' => $this->getProject()['apiKey'],
], [ ], [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'push',
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'identifier' => $to, 'identifier' => $to,
]); ]);
@ -1154,6 +1170,7 @@ trait MessagingBase
'name' => 'FCM-2', 'name' => 'FCM-2',
'serverKey' => $serverKey, 'serverKey' => $serverKey,
]); ]);
$this->assertEquals(201, $provider['headers']['status-code']); $this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic // Create Topic
@ -1166,6 +1183,7 @@ trait MessagingBase
'name' => 'topic1', 'name' => 'topic1',
'description' => 'Test Topic' 'description' => 'Test Topic'
]); ]);
$this->assertEquals(201, $topic['headers']['status-code']); $this->assertEquals(201, $topic['headers']['status-code']);
// Create User // Create User
@ -1189,6 +1207,7 @@ trait MessagingBase
'x-appwrite-key' => $this->getProject()['apiKey'], 'x-appwrite-key' => $this->getProject()['apiKey'],
], [ ], [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerType' => 'push',
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'identifier' => $to, 'identifier' => $to,
]); ]);

View file

@ -1232,7 +1232,7 @@ trait UsersBase
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [ ], $this->getHeaders()), [
'providerId' => 'unique()', 'providerId' => ID::unique(),
'name' => 'Sengrid1', 'name' => 'Sengrid1',
'apiKey' => 'my-apikey', 'apiKey' => 'my-apikey',
'from' => 'from@domain.com', 'from' => 'from@domain.com',
@ -1244,6 +1244,7 @@ trait UsersBase
], $this->getHeaders()), [ ], $this->getHeaders()), [
'targetId' => ID::unique(), 'targetId' => ID::unique(),
'providerId' => $provider['body']['$id'], 'providerId' => $provider['body']['$id'],
'providerType' => 'email',
'identifier' => 'my-token', 'identifier' => 'my-token',
]); ]);
$this->assertEquals(201, $response['headers']['status-code']); $this->assertEquals(201, $response['headers']['status-code']);
@ -1257,7 +1258,7 @@ trait UsersBase
*/ */
public function testUpdateUserTarget(array $data): array public function testUpdateUserTarget(array $data): array
{ {
$response = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/targets/' . $data['$id'] . '/identifier', array_merge([ $response = $this->client->call(Client::METHOD_PATCH, '/users/' . $data['userId'] . '/targets/' . $data['$id'], array_merge([
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [ ], $this->getHeaders()), [
@ -1303,11 +1304,14 @@ trait UsersBase
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders())); ], $this->getHeaders()));
$this->assertEquals(204, $response['headers']['status-code']); $this->assertEquals(204, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'] . '/targets', array_merge([ $response = $this->client->call(Client::METHOD_GET, '/users/' . $data['userId'] . '/targets', array_merge([
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders())); ], $this->getHeaders()));
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(0, $response['body']['total']); $this->assertEquals(0, $response['body']['total']);
} }