forked from novuhq/novu-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request novuhq#58 from 2002Bishwajeet/feat-workflow-feature
Feat: Workflow feature
- Loading branch information
Showing
6 changed files
with
372 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
/.idea | ||
*.swp | ||
*.swo | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Novu\SDK\Actions; | ||
|
||
use Novu\SDK\Resources\Workflow; | ||
|
||
trait ManagesWorkflow | ||
{ | ||
/** | ||
* Get Workflows. | ||
* | ||
* @return \Novu\SDK\Resources\Workflow | ||
*/ | ||
public function getWorkflows(int $page = 1, int $limit = 10) | ||
{ | ||
$response = $this->get('workflows?' . http_build_query(['page' => $page, 'limit' => $limit]))['data']; | ||
|
||
return new Workflow($response, $this); | ||
} | ||
|
||
/** | ||
* Create Workflow. | ||
* | ||
* @param array $data | ||
* @return \Novu\SDK\Resources\Workflow | ||
*/ | ||
public function createWorkflow(array $data) | ||
{ | ||
$response = $this->post('workflows', $data)['data']; | ||
|
||
return new Workflow($response, $this); | ||
} | ||
|
||
/** | ||
* Get Workflow. | ||
* | ||
* @param string $workflowId | ||
* @return \Novu\SDK\Resources\Workflow | ||
*/ | ||
public function getWorkflow(string $workflowId) | ||
{ | ||
$response = $this->get("workflows/{$workflowId}")['data']; | ||
|
||
return new Workflow($response, $this); | ||
} | ||
|
||
/** | ||
* Update Workflow. | ||
* | ||
* @param string $workflowId | ||
* @param array $data | ||
* @return \Novu\SDK\Resources\Workflow | ||
*/ | ||
public function updateWorkflow(string $workflowId, array $data) | ||
{ | ||
$response = $this->put("workflows/{$workflowId}", $data)['data']; | ||
|
||
return new Workflow($response, $this); | ||
} | ||
|
||
/** | ||
* Delete Workflow. | ||
* | ||
* @param string $workflowId | ||
* @return bool | ||
*/ | ||
public function deleteWorkflow(string $workflowId) | ||
{ | ||
$response = $this->delete("workflows/{$workflowId}")['data']; | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Update Workflow Status. | ||
* | ||
* @param string $workflowId | ||
* @param array $data | ||
* @return \Novu\SDK\Resources\Workflow | ||
*/ | ||
public function updateWorkflowStatus(string $workflowId, array $data) | ||
{ | ||
$response = $this->put("workflows/{$workflowId}/status", $data)['data']; | ||
|
||
return new Workflow($response, $this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
namespace Novu\SDK\Resources; | ||
|
||
class Workflow extends Resource | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
public $active; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
public $draft; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
public $critical; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
public $isBlueprint; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $notificationGroupId; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $tags; | ||
|
||
|
||
/** | ||
* @var array | ||
*/ | ||
public $triggers; | ||
|
||
|
||
/** | ||
* @var array | ||
*/ | ||
public $steps; | ||
|
||
|
||
/** | ||
* @var object | ||
*/ | ||
public $preferenceSettings; | ||
|
||
|
||
/** | ||
* @var string | ||
*/ | ||
public $environmentId; | ||
|
||
|
||
/** | ||
* @var string | ||
*/ | ||
public $organizationId; | ||
|
||
|
||
/** | ||
* @var string | ||
*/ | ||
public $creatorId; | ||
|
||
|
||
/** | ||
* @var bool | ||
*/ | ||
public $deleted; | ||
|
||
|
||
/** | ||
* @var string | ||
*/ | ||
public $createdAt; | ||
|
||
|
||
/** | ||
* @var string | ||
*/ | ||
public $updatedAt; | ||
|
||
|
||
/** | ||
* @var string | ||
*/ | ||
public $v; | ||
|
||
|
||
/** | ||
* @var array | ||
*/ | ||
public $notificationGroup; | ||
|
||
|
||
/** | ||
* @var array | ||
*/ | ||
public $workflowIntegrationStatus; | ||
|
||
|
||
public function toArray(): array | ||
{ | ||
$publicProperties = get_object_vars($this); | ||
|
||
unset($publicProperties['attributes']); | ||
unset($publicProperties['novu']); | ||
|
||
return array_filter($publicProperties, function ($value) { | ||
return null !== $value; | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Novu\SDK\Novu; | ||
use Novu\SDK\Resources\Workflow; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class WorkflowTests extends Testcase | ||
{ | ||
/** | ||
* @var \Novu\SDK\Novu | ||
*/ | ||
private $novu; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->novu = new Novu([getenv('NOVU_API_KEY')]); | ||
} | ||
|
||
public function testCreateWorkflow() | ||
{ | ||
$workflow = $this->novu->createWorkflow( | ||
[ | ||
'name' => 'Onboarding Workflow', | ||
'notificationGroupId' => '5f5f5f5f5f5f5f5f5f5f5f5f', | ||
'steps' => [ | ||
[ | ||
'active' => true, | ||
'shouldStopOnFail' => false, | ||
'uuid' => '78ab8c72-46de-49e4-8464-257085960f9e', | ||
'name' => 'Chat', | ||
'filters' => [ | ||
[ | ||
'value' => 'AND', | ||
'children' => [ | ||
[ | ||
'field' => '{{chatContent}}', | ||
'value' => 'flag', | ||
'operator' => 'NOT_IN', | ||
'on' => 'PAYLOAD', | ||
], | ||
], | ||
], | ||
], | ||
'template' => [ | ||
'type' => 'chat', | ||
'active' => true, | ||
'subject' => '', | ||
'variables' => [ | ||
[ | ||
'name' => 'chatContent', | ||
'type' => 'STRING', | ||
'required' => true, | ||
], | ||
], | ||
'content' => '{{chatContent}}', | ||
'contentType' => 'editor', | ||
], | ||
], | ||
], | ||
'description' => 'Onboarding workflow to trigger after user sign up', | ||
// 'active' => true, | ||
// 'draft' => false, | ||
// 'critical' => false, | ||
] | ||
); | ||
$this->assertNotNull($workflow->id); | ||
$this->assertEquals('Onboarding Workflow', $workflow->name); | ||
$this->assertEquals(false, $workflow->active); | ||
$this->assertEquals(true, $workflow->draft); | ||
$this->assertEquals(false, $workflow->critical); | ||
|
||
return $workflow; | ||
} | ||
|
||
/** | ||
* @depends testCreateWorkflow | ||
*/ | ||
public function testGetWorkflow(Workflow $workflow) | ||
{ | ||
$workflow = $this->novu->getWorkflow($workflow->id); | ||
$this->assertNotNull($workflow->id); | ||
$this->assertEquals('Onboarding Workflow', $workflow->name); | ||
$this->assertFalse($workflow->active); | ||
$this->assertTrue($workflow->draft); | ||
$this->assertFalse($workflow->critical); | ||
|
||
return $workflow; | ||
} | ||
|
||
public function testGetWorkflows() | ||
{ | ||
$workflows = $this->novu->getWorkflows(); | ||
$this->assertNotNull($workflows); | ||
} | ||
|
||
/** | ||
* @depends testCreateWorkflow | ||
*/ | ||
public function testUpdateWorkflows(Workflow $workflow) | ||
{ | ||
$workflow = $this->novu->updateWorkflow($workflow->id, [ | ||
'name' => 'Test Workflow', | ||
'active' => true, | ||
'draft' => false, | ||
'critical' => false, | ||
'isBlueprint' => false, | ||
'notificationGroupId' => '5f5f5f5f5f5f5f5f5f5f5f5f', | ||
'tags' => ['test', 'test2'], | ||
'steps' => [ | ||
[ | ||
'type' => 'email', | ||
'name' => 'test', | ||
'email' => 'test@test.com', | ||
'active' => true, | ||
], | ||
], | ||
]); | ||
$this->assertNotNull($workflow->id); | ||
$this->assertEquals('Test Workflow', $workflow->name); | ||
$this->assertEquals(true, $workflow->active); | ||
$this->assertEquals(false, $workflow->draft); | ||
$this->assertEquals(false, $workflow->critical); | ||
} | ||
|
||
/** | ||
* @depends testCreateWorkflow | ||
*/ | ||
public function testUpdateWorkflowStatus(Workflow $workflow) | ||
{ | ||
$workflow = $this->novu->updateWorkflowStatus($workflow->id, [ | ||
'active' => true, | ||
'draft' => false, | ||
'critical' => false, | ||
]); | ||
$this->assertNotNull($workflow->id); | ||
$this->assertEquals('Test Workflow', $workflow->name); | ||
$this->assertTrue($workflow->active); | ||
$this->assertFalse($workflow->draft); | ||
$this->assertFalse($workflow->critical); | ||
} | ||
|
||
/** | ||
* @depends testCreateWorkflow | ||
*/ | ||
public function testDeleteWorkflow(Workflow $workflow) | ||
{ | ||
$workflow = $this->novu->deleteWorkflow($workflow->id); | ||
$this->assertTrue($workflow); | ||
} | ||
} |