Skip to content

Commit

Permalink
Merge pull request novuhq#58 from 2002Bishwajeet/feat-workflow-feature
Browse files Browse the repository at this point in the history
Feat: Workflow feature
  • Loading branch information
unicodeveloper authored Jan 8, 2024
2 parents 19654be + 4e072b4 commit 9303e84
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/.idea
*.swp
*.swo
.idea/
87 changes: 87 additions & 0 deletions src/Actions/ManagesWorkflow.php
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);
}
}
1 change: 1 addition & 0 deletions src/Novu.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Novu
Actions\ManagesTriggers,
Actions\ManagesNotificationGroups,
Actions\ManagesNotificationTemplates,
Actions\ManagesWorkflow,
Actions\ManagesBlueprints;

/**
Expand Down
131 changes: 131 additions & 0 deletions src/Resources/Workflow.php
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;
});
}
}
9 changes: 0 additions & 9 deletions tests/Feature.php

This file was deleted.

152 changes: 152 additions & 0 deletions tests/WorkflowTests.php
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);
}
}

0 comments on commit 9303e84

Please sign in to comment.