Skip to content

Commit

Permalink
option to return multiple plans (#31)
Browse files Browse the repository at this point in the history
* VSLIV30-2578

option for multiple matches.
requires update of asseco-voice/laravel-inbox

* Apply fixes from StyleCI

---------

Co-authored-by: ngasparic <Nikola.Gasparic@asseco-see.hr>
Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
3 people authored Feb 15, 2024
1 parent a77d3ab commit 6431ff1
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 45 deletions.
10 changes: 5 additions & 5 deletions config/asseco-plan-router.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* Model bindings.
*/
'models' => [
'rule' => Rule::class,
'plan' => Plan::class,
'rule' => Rule::class,
'plan' => Plan::class,
'plan_model_value' => PlanModelValue::class,
],

Expand All @@ -30,7 +30,7 @@
/**
* UUIDs as primary keys.
*/
'uuid' => false,
'uuid' => false,

/**
* Timestamp types.
Expand All @@ -43,13 +43,13 @@
* Should the package run the migrations. Set to false if you're publishing
* and changing default migrations.
*/
'run' => true,
'run' => true,
],

'plan_event_topic' => env('PLAN_EVENT_TOPIC', 'plan_changed'),

'routes' => [
'prefix' => 'api',
'prefix' => 'api',
'middleware' => ['api'],
],
];
2 changes: 1 addition & 1 deletion src/App/Http/Requests/PlanRuleDeleteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function authorize()
public function rules()
{
return [
'rule_ids' => 'required|array',
'rule_ids' => 'required|array',
'rule_ids.*' => 'exists:rules,id',
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/App/Http/Requests/PlanRuleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function authorize()
public function rules()
{
return [
'rules' => 'array|required',
'rules' => 'array|required',
'rules.*.rule_id' => 'required_with:rules|exists:rules,id',
'rules.*.regex' => 'string|required_with:rules',
'rules.*.regex' => 'string|required_with:rules',
];
}
}
8 changes: 4 additions & 4 deletions src/App/Models/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Plan extends Model implements \Asseco\PlanRouter\App\Contracts\Plan
protected $fillable = ['name', 'description', 'order', 'match_either', 'priority'];

protected $casts = [
'match_either' => 'boolean',
'match_either' => 'boolean',
];

protected static function newFactory()
Expand Down Expand Up @@ -62,9 +62,9 @@ public static function getWithRelations(): Collection
public static function getValidationRules(): array
{
return [
'name' => 'required|string',
'description' => 'nullable|string',
'priority' => 'integer',
'name' => 'required|string',
'description' => 'nullable|string',
'priority' => 'integer',
'match_either' => 'boolean',
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/App/Models/PlanModelValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function model(): MorphTo
public static function getValidationRules(): array
{
return [
'plan_id' => 'required|exists:plans,id',
'plan_id' => 'required|exists:plans,id',
'attribute' => 'required|string',
'value' => 'required|string',
'value' => 'required|string',
];
}
}
2 changes: 1 addition & 1 deletion src/App/Models/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function plans(): BelongsToMany
public static function getValidationRules(): array
{
return [
'name' => 'required|string',
'name' => 'required|string',
'label' => 'required|string',
];
}
Expand Down
50 changes: 36 additions & 14 deletions src/App/Services/InboxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,51 @@ public function __construct(Plan $plan)

/**
* @param CanMatch $canMatch
* @return Plan|null
* @param bool|null $multipleMatches
* @return Plan|array|Plan[]|null
*
* @throws Exception
*/
public function match(CanMatch $canMatch): ?Plan
public function match(CanMatch $canMatch, ?bool $multipleMatches = false)
{
$this->canMatch = $canMatch;

$this->registerInboxes();

$this->registerFallback();

/** @var Inbox $matchedInbox */
// Interested in first match only as we're not utilizing
// continuous matching from Inbox package.
$matchedInbox = Arr::get(InboxGroup::run($canMatch), '0');

$planId = Arr::get($matchedInbox->getMeta(), 'plan_id');

/** @var Plan $plan */
$plan = $this->plan::query()->find($planId);

return $plan;
if (!$multipleMatches) {
// Interested in first match only as we're not utilizing
// continuous matching from Inbox package.
/** @var Inbox $matchedInbox */
$matchedInbox = Arr::get(InboxGroup::run($canMatch), '0');
$planId = Arr::get($matchedInbox->getMeta(), 'plan_id');

/** @var Plan $plan */
$plan = $this->plan::query()->find($planId);

return $plan;
} else {
// MOD: VSLIV30-2578
// multiple plans could be matched
$plans = [];
$matchedInboxes = InboxGroup::run($canMatch, $multipleMatches);

if (!empty($matchedInboxes)) {
/** @var Inbox $inbox */
foreach ($matchedInboxes as $inbox) {
$planId = Arr::get($inbox->getMeta(), 'plan_id');
if ($planId) {
/** @var Plan $plan */
$plan = $this->plan::query()->find($planId);
if ($plan) {
$plans[$plan->id] = $plan;
}
}
}
}

return $plans;
}
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Database/Factories/PlanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class PlanFactory extends Factory
public function definition()
{
$data = [
'name' => $this->faker->word,
'description' => $this->faker->word,
'priority' => $this->faker->numberBetween(0, 100),
'name' => $this->faker->word,
'description' => $this->faker->word,
'priority' => $this->faker->numberBetween(0, 100),
'match_either' => $this->faker->boolean,
'created_at' => now(),
'updated_at' => now(),
'created_at' => now(),
'updated_at' => now(),
];

if (config('asseco-plan-router.migrations.uuid')) {
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Factories/PlanModelValueFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class PlanModelValueFactory extends Factory
public function definition()
{
$data = [
'plan_id' => Plan::factory(),
'attribute' => $this->faker->word,
'value' => $this->faker->word,
'plan_id' => Plan::factory(),
'attribute' => $this->faker->word,
'value' => $this->faker->word,
'created_at' => now(),
'updated_at' => now(),
];
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Factories/RuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RuleFactory extends Factory
public function definition()
{
$data = [
'name' => $this->faker->unique()->word,
'name' => $this->faker->unique()->word,
'label' => $this->faker->word,
];

Expand Down
10 changes: 5 additions & 5 deletions src/Database/Seeders/RuleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ public function run()
{
$data = [
[
'name' => 'from',
'name' => 'from',
'label' => 'From',
],
[
'name' => 'to',
'name' => 'to',
'label' => 'To',
],
[
'name' => 'cc',
'name' => 'cc',
'label' => 'Cc',
],
[
'name' => 'bcc',
'name' => 'bcc',
'label' => 'Bcc',
],
[
'name' => 'subject',
'name' => 'subject',
'label' => 'Subject', ],
];

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Http/Controllers/PlanControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function can_create_a_new_plan_if_it_doesnt_exist()
public function can_update_an_existing_plan()
{
$data = [
'name' => 'test',
'name' => 'test',
'description' => 'test',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function can_update_an_existing_plan_model_value()

$data = [
'attribute' => 'test',
'value' => 'test',
'value' => 'test',
];

$this
Expand Down

0 comments on commit 6431ff1

Please sign in to comment.