Skip to content

Commit

Permalink
feat: Add support for the Notifications API
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Bruneel committed Oct 1, 2023
1 parent 811b26f commit 09d1527
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/CrowdinApiClient/Api/Enterprise/NotificationApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace CrowdinApiClient\Api\Enterprise;

use CrowdinApiClient\Api\AbstractApi;

/**
* Send notifications
*
* @package Crowdin\Api\Entreprise
*/
class NotificationApi extends AbstractApi
{

/**
* Send Notification To Organization Members
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.notify.post API Documentation Enterprise
*
* @param array $data
* string $data[message] required<br>
* string $data[role] Enum: "owner" "admin"<br>
* integer[] $data[userIds]
* @return mixed
*/
public function sendNotificationToOrganizationMembers(array $data)
{
$path = 'notify';

$options = [
'body' => json_encode($data),
'headers' => ['Content-Type' => 'application/json']
];

return $this->client->apiRequest('post', $path, null, $options);
}
}
55 changes: 55 additions & 0 deletions src/CrowdinApiClient/Api/NotificationApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace CrowdinApiClient\Api;

/**
* Send notifications
*
* @package Crowdin\Api
*/
class NotificationApi extends AbstractApi
{
/**
* Send Notification to Authenticated User
* @link https://developer.crowdin.com/api/v2/#operation/api.notify.post API Documentation
*
* @param array $data
* string $data[message] required
* @return mixed
*/
public function sendToAuthenticatedUser(array $data)
{
$path = 'notify';

$options = [
'body' => json_encode($data),
'headers' => ['Content-Type' => 'application/json']
];

return $this->client->apiRequest('post', $path, null, $options);

}

/**
* Send Notification To Project Members
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.notify.post API Documentation
*
* @param int $projectId
* @param array $data
* string $data[message] required<br>
* string $data[role] Enum: "owner" "manager"<br>
* integer[] $data[userIds]
* @return mixed
*/
public function sendNotificationToProjectMembers(int $projectId, array $data)
{
$path = sprintf('projects/%s/notify', $projectId);

$options = [
'body' => json_encode($data),
'headers' => ['Content-Type' => 'application/json']
];

return $this->client->apiRequest('post', $path, null, $options);
}
}
7 changes: 5 additions & 2 deletions src/CrowdinApiClient/Crowdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @property \CrowdinApiClient\Api\Enterprise\TeamApi team
* @property \CrowdinApiClient\Api\Enterprise\TeamMemberApi teamMember
* @property \CrowdinApiClient\Api\BundleApi bundle
* @property \CrowdinApiClient\Api\NotificationApi|\CrowdinApiClient\Api\Enterprise\NotificationApi notification
*/
class Crowdin
{
Expand Down Expand Up @@ -106,7 +107,8 @@ class Crowdin
'translation',
'translationStatus',
'distribution',
'bundle'
'bundle',
'notification'
];

protected $servicesEnterprise = [
Expand Down Expand Up @@ -139,7 +141,8 @@ class Crowdin
'distribution',
'team',
'teamMember',
'bundle'
'bundle',
'notification'
];

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/CrowdinApiClient/Api/Enterprise/NotificationApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace CrowdinApiClient\Tests\Api\Enterprise;

class NotificationApiTest extends AbstractTestApi
{

public function testSendNotificationToOrganizationMembers()
{
$params = [
'message' => 'New notification message',
'userIds' => [1]
];

$this->mockRequest([
'method' => 'post',
'uri' => 'https://organization_domain.crowdin.com/api/v2/notify',
'body' => $params,
'response' => null
]);

$this->crowdin->notification->sendNotificationToOrganizationMembers($params);
}
}
40 changes: 40 additions & 0 deletions tests/CrowdinApiClient/Api/NotificationApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace CrowdinApiClient\Tests\Api;

class NotificationApiTest extends AbstractTestApi
{

public function testSendToAuthenticatedUser()
{
$params = [
'message' => 'New notification message',
];

$this->mockRequest([
'method' => 'post',
'uri' => 'https://api.crowdin.com/api/v2/notify',
'body' => $params,
'response' => null
]);

$this->crowdin->notification->sendToAuthenticatedUser($params);
}

public function testSendNotificationToProjectMembers()
{
$params = [
'message' => 'New notification message',
'userIds' => [1]
];

$this->mockRequest([
'method' => 'post',
'uri' => 'https://api.crowdin.com/api/v2/projects/2/notify',
'body' => $params,
'response' => null
]);

$this->crowdin->notification->sendNotificationToProjectMembers(2, $params);
}
}

0 comments on commit 09d1527

Please sign in to comment.