From 3858ef58056b744b10fc037a1ca66db80e01271f Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Mon, 9 Oct 2023 13:23:37 +0530 Subject: [PATCH 1/4] feat: Add Workflows API --- .gitignore | 1 + src/Actions/ManagesWorkflow.php | 50 +++++++++++ src/Novu.php | 1 + src/Resources/Workflow.php | 131 +++++++++++++++++++++++++++ tests/Feature.php | 9 -- tests/NovuTest.php | 153 ++++++++++++++++++++++++++++++++ 6 files changed, 336 insertions(+), 9 deletions(-) create mode 100644 src/Actions/ManagesWorkflow.php create mode 100644 src/Resources/Workflow.php delete mode 100644 tests/Feature.php create mode 100644 tests/NovuTest.php diff --git a/.gitignore b/.gitignore index 487716c..7768a06 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /vendor/ *.swp *.swo +.idea/ diff --git a/src/Actions/ManagesWorkflow.php b/src/Actions/ManagesWorkflow.php new file mode 100644 index 0000000..2b6949e --- /dev/null +++ b/src/Actions/ManagesWorkflow.php @@ -0,0 +1,50 @@ +get('workflows'. '?' . http_build_query(['page' => $page, 'limit' => $limit]))['data']; + return new Workflow($response, $this); + } + + public function createWorkflow(array $data) + { + $response = $this->post('workflows', $data)['data']; + return new Workflow($response, $this); + + } + + public function getWorkflow(string $workflowId) + { + $response = $this->get("workflows/{$workflowId}")['data']; + return new Workflow($response, $this); + } + + public function updateWorkflow(string $workflowId, array $data) + { + $response = $this->put("workflows/{$workflowId}", $data)['data']; + return new Workflow($response, $this); + } + + /** + * @param string $workflowId + * @return boolean + */ + public function deleteWorkflow(string $workflowId) + { + $response = $this->delete("workflows/{$workflowId}")['data']; + return $response; + } + + public function updateWorkflowStatus(string $workflowId, array $data) + { + $response = $this->put("workflows/{$workflowId}/status", $data)['data']; + return new Workflow($response, $this); + } +} diff --git a/src/Novu.php b/src/Novu.php index 9d6e1f8..d902957 100644 --- a/src/Novu.php +++ b/src/Novu.php @@ -25,6 +25,7 @@ class Novu Actions\ManagesTriggers, Actions\ManagesNotificationGroups, Actions\ManagesNotificationTemplates, + Actions\ManagesWorkflow, Actions\ManagesBlueprints; /** diff --git a/src/Resources/Workflow.php b/src/Resources/Workflow.php new file mode 100644 index 0000000..082e246 --- /dev/null +++ b/src/Resources/Workflow.php @@ -0,0 +1,131 @@ +toBeTrue(); -}); diff --git a/tests/NovuTest.php b/tests/NovuTest.php new file mode 100644 index 0000000..68aec6b --- /dev/null +++ b/tests/NovuTest.php @@ -0,0 +1,153 @@ +novu = new Novu([getenv('NOVU_API_KEY')]); + $this->novu = new Novu('3d53880f8247032da02c207646d1ee4a'); + } + + 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); + } +} From a87e1297bd76338e1657f9137ac00fba92e47c5d Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Mon, 9 Oct 2023 13:24:25 +0530 Subject: [PATCH 2/4] chore(remove): API Key --- tests/NovuTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/NovuTest.php b/tests/NovuTest.php index 68aec6b..505e69f 100644 --- a/tests/NovuTest.php +++ b/tests/NovuTest.php @@ -15,8 +15,7 @@ class NovuTest extends Testcase protected function setUp(): void { - // $this->novu = new Novu([getenv('NOVU_API_KEY')]); - $this->novu = new Novu('3d53880f8247032da02c207646d1ee4a'); + $this->novu = new Novu([getenv('NOVU_API_KEY')]); } public function testCreateWorkflow() From 00d7695b4cb7394af3a9a1b81620d8f8caa83be6 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 1 Nov 2023 09:32:29 +0530 Subject: [PATCH 3/4] chore(add): codeblocks and lint issues --- src/Actions/ManagesWorkflow.php | 53 ++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/Actions/ManagesWorkflow.php b/src/Actions/ManagesWorkflow.php index 2b6949e..75ebb03 100644 --- a/src/Actions/ManagesWorkflow.php +++ b/src/Actions/ManagesWorkflow.php @@ -6,45 +6,82 @@ 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']; - $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); + $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']; + $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']; + $response = $this->put("workflows/{$workflowId}", $data)['data']; + return new Workflow($response, $this); } /** + * Delete Workflow. + * * @param string $workflowId - * @return boolean + * @return \Novu\SDK\Resources\Workflow */ public function deleteWorkflow(string $workflowId) { - $response = $this->delete("workflows/{$workflowId}")['data']; + $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']; + $response = $this->put("workflows/{$workflowId}/status", $data)['data']; + return new Workflow($response, $this); } } From 4e072b4b7c65819c60c3e649605781b59e53a2a3 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 1 Nov 2023 09:35:30 +0530 Subject: [PATCH 4/4] chore(fix): rename workflow tests --- src/Actions/ManagesWorkflow.php | 2 +- tests/{NovuTest.php => WorkflowTests.php} | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename tests/{NovuTest.php => WorkflowTests.php} (97%) diff --git a/src/Actions/ManagesWorkflow.php b/src/Actions/ManagesWorkflow.php index 75ebb03..bee95f6 100644 --- a/src/Actions/ManagesWorkflow.php +++ b/src/Actions/ManagesWorkflow.php @@ -62,7 +62,7 @@ public function updateWorkflow(string $workflowId, array $data) * Delete Workflow. * * @param string $workflowId - * @return \Novu\SDK\Resources\Workflow + * @return bool */ public function deleteWorkflow(string $workflowId) { diff --git a/tests/NovuTest.php b/tests/WorkflowTests.php similarity index 97% rename from tests/NovuTest.php rename to tests/WorkflowTests.php index 505e69f..0b71c3b 100644 --- a/tests/NovuTest.php +++ b/tests/WorkflowTests.php @@ -1,12 +1,12 @@ novu = new Novu([getenv('NOVU_API_KEY')]); + $this->novu = new Novu([getenv('NOVU_API_KEY')]); } public function testCreateWorkflow()