Notification & Integrations refactoring (#346)
* Integrations Refactoring - WIP * integrations list & edit - WIP * Fix integration store binding issue * integrations refactor - WIP * Form integration - WIP * Form integration Edit - WIP * Integration Refactor - Slack - WIP * Integration Refactor - Discord - WIP * Integration Refactor - Webhook - WIP * Integration Refactor - Send Submission Confirmation - WIP * Integration Refactor - Backend handler - WIP * Form Integration Status field * Integration Refactor - Backend SubmissionConfirmation - WIP * IntegrationMigration Command * skip confirmation email test case * Small refactoring * FormIntegration status active/inactive * formIntegrationData to integrationData * Rename file name with Integration suffix for integration realted files * Loader on form integrations * WIP * form integration test case * WIP * Added Integration card - working on refactoring * change location for IntegrationCard and update package file * Form Integration Create/Edit in single Modal * Remove integration extra pages * crisp_help_page_slug for integration json * integration logic as collapse * UI improvements * WIP * Trying to debug vue devtools * WIP for integrations * getIntegrationHandler change namespace name * useForm for integration fields + validation structure * Integration Test case & apply validation rules * Apply useform changes to integration other files * validation rules for FormNotificationsMessageActions fields * Zapier integration as coming soon * Update FormCleaner * set default settings for confirmation integration * WIP * Finish validation for all integrations * Updated purify, added integration formatData * Fix testcase * Ran pint; working on integration errors * Handle integration events * command for Delete Old Integration Events * Display Past Events in Modal * on Integration event create with status error send email to form creator * Polish styling * Minor improvements * Finish badge and integration status * Fix tests and add an integration event test * Lint --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
186
app/Rules/IntegrationLogicRule.php
Normal file
186
app/Rules/IntegrationLogicRule.php
Normal file
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\DataAwareRule;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class IntegrationLogicRule implements DataAwareRule, Rule
|
||||
{
|
||||
private $isConditionCorrect = true;
|
||||
|
||||
private $conditionErrors = [];
|
||||
|
||||
private $field = [];
|
||||
|
||||
private $data = [];
|
||||
|
||||
private function checkBaseCondition($condition)
|
||||
{
|
||||
|
||||
if (!isset($condition['value'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition body';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['property_meta'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition property';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['property_meta']['type'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition property type';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['operator'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition operator';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($condition['value']['value'])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'missing condition value';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$typeField = $condition['value']['property_meta']['type'];
|
||||
$operator = $condition['value']['operator'];
|
||||
$value = $condition['value']['value'];
|
||||
|
||||
if (!isset(FormPropertyLogicRule::CONDITION_MAPPING[$typeField])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'configuration not found for condition type';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset(FormPropertyLogicRule::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'configuration not found for condition operator';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$type = FormPropertyLogicRule::CONDITION_MAPPING[$typeField]['comparators'][$operator]['expected_type'];
|
||||
|
||||
if (is_array($type)) {
|
||||
$foundCorrectType = false;
|
||||
foreach ($type as $subtype) {
|
||||
if ($this->valueHasCorrectType($subtype, $value)) {
|
||||
$foundCorrectType = true;
|
||||
}
|
||||
}
|
||||
if (!$foundCorrectType) {
|
||||
$this->isConditionCorrect = false;
|
||||
}
|
||||
} else {
|
||||
if (!$this->valueHasCorrectType($type, $value)) {
|
||||
$this->isConditionCorrect = false;
|
||||
$this->conditionErrors[] = 'wrong type of condition value';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function valueHasCorrectType($type, $value)
|
||||
{
|
||||
if (
|
||||
($type === 'string' && gettype($value) !== 'string') ||
|
||||
($type === 'boolean' && !is_bool($value)) ||
|
||||
($type === 'number' && !is_numeric($value)) ||
|
||||
($type === 'object' && !is_array($value))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function checkConditions($conditions)
|
||||
{
|
||||
if (array_key_exists('operatorIdentifier', $conditions)) {
|
||||
if (($conditions['operatorIdentifier'] !== 'and') && ($conditions['operatorIdentifier'] !== 'or')) {
|
||||
$this->conditionErrors[] = 'missing operator';
|
||||
$this->isConditionCorrect = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($conditions['operatorIdentifier']['children'])) {
|
||||
$this->conditionErrors[] = 'extra condition';
|
||||
$this->isConditionCorrect = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_array($conditions['children'])) {
|
||||
$this->conditionErrors[] = 'wrong sub-condition type';
|
||||
$this->isConditionCorrect = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($conditions['children'] as &$child) {
|
||||
$this->checkConditions($child);
|
||||
}
|
||||
} elseif (isset($conditions['identifier'])) {
|
||||
$this->checkBaseCondition($conditions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
if (isset($value)) {
|
||||
$this->checkConditions($value);
|
||||
}
|
||||
|
||||
return $this->isConditionCorrect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
$message = null;
|
||||
if (!$this->isConditionCorrect) {
|
||||
$message = 'The logic conditions are not complete.';
|
||||
}
|
||||
if (count($this->conditionErrors) > 0) {
|
||||
return $message . ' Error detail(s): ' . implode(', ', $this->conditionErrors);
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the data under validation.
|
||||
*
|
||||
* @param array $data
|
||||
* @return $this
|
||||
*/
|
||||
public function setData($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->isConditionCorrect = true;
|
||||
$this->conditionErrors = [];
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user