diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql index 9fe988a331b42260673cfe4ada1df274013b1e6d..aca81bc4b2ddcf6fb8e0970f86c5cc61ce6f5d76 100644 --- a/migration/19.12/1912.sql +++ b/migration/19.12/1912.sql @@ -121,7 +121,6 @@ CREATE TABLE custom_fields label character varying(256) NOT NULL, type character varying(256) NOT NULL, values jsonb, - default_value text, CONSTRAINT custom_fields_pkey PRIMARY KEY (id), CONSTRAINT custom_fields_unique_key UNIQUE (label) ) @@ -143,16 +142,16 @@ WITH (OIDS=FALSE); DROP TABLE IF EXISTS indexing_models_fields; DROP TYPE IF EXISTS indexing_models_fields_type; -CREATE TYPE indexing_models_fields_type AS ENUM ('standard', 'custom'); +CREATE TYPE indexing_models_fields_type AS ENUM ('string', 'integer', 'select', 'date', 'radio', 'checkbox'); CREATE TABLE indexing_models_fields ( id SERIAL NOT NULL, model_id INTEGER NOT NULL, type indexing_models_fields_type NOT NULL, - identifier INTEGER NOT NULL, + identifier text NOT NULL, mandatory BOOLEAN NOT NULL, - value text, - unit INTEGER, + default_value text, + unit text, CONSTRAINT indexing_models_fields_pkey PRIMARY KEY (id) ) WITH (OIDS=FALSE); diff --git a/sql/structure.sql b/sql/structure.sql index b62571b5bee304ab63d3c3b295fc623c9f1ad528..28c4d041acf8d14dbab1a8b2e5a24efd4416331b 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -1911,7 +1911,6 @@ CREATE TABLE custom_fields label character varying(256) NOT NULL, type character varying(256) NOT NULL, values jsonb, - default_value text, CONSTRAINT custom_fields_pkey PRIMARY KEY (id), CONSTRAINT custom_fields_unique_key UNIQUE (label) ) @@ -1928,17 +1927,16 @@ CREATE TABLE indexing_models ) WITH (OIDS=FALSE); -CREATE TYPE indexing_models_fields_type AS ENUM ('standard', 'custom'); - +CREATE TYPE indexing_models_fields_type AS ENUM ('string', 'integer', 'select', 'date', 'radio', 'checkbox'); CREATE TABLE indexing_models_fields ( id SERIAL NOT NULL, model_id INTEGER NOT NULL, type indexing_models_fields_type NOT NULL, - identifier INTEGER NOT NULL, + identifier text NOT NULL, mandatory BOOLEAN NOT NULL, - value text, - unit INTEGER, + default_value text, + unit text, CONSTRAINT indexing_models_fields_pkey PRIMARY KEY (id) ) WITH (OIDS=FALSE); diff --git a/src/app/customField/controllers/CustomFieldController.php b/src/app/customField/controllers/CustomFieldController.php index 153342a9051a8f2a703e4c1d354add0549dafab3..0bf4f31c69fb4ed7a13fa529db014d1dd869a5e6 100644 --- a/src/app/customField/controllers/CustomFieldController.php +++ b/src/app/customField/controllers/CustomFieldController.php @@ -61,8 +61,7 @@ class CustomFieldController $id = CustomFieldModel::create([ 'label' => $body['label'], 'type' => $body['type'], - 'values' => empty($body['values']) ? '[]' : json_encode($body['values']), - 'default_value' => $body['default_value'] + 'values' => empty($body['values']) ? '[]' : json_encode($body['values']) ]); return $response->withStatus(201)->withJson(['customFieldId' => $id]); @@ -99,8 +98,7 @@ class CustomFieldController CustomFieldModel::update([ 'set' => [ 'label' => $body['label'], - 'values' => empty($body['values']) ? '[]' : json_encode($body['values']), - 'default_value' => $body['default_value'] + 'values' => empty($body['values']) ? '[]' : json_encode($body['values']) ], 'where' => ['id = ?'], 'data' => [$args['id']] @@ -115,7 +113,7 @@ class CustomFieldController return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } - IndexingModelFieldModel::delete(['where' => ['type = ?', 'identifier = ?'], 'data' => ['custom', $args['id']]]); + IndexingModelFieldModel::delete(['where' => ['identifier = ?'], 'data' => [$args['id']]]); //TODO Suppression des valeurs liés aux courriers ? diff --git a/src/app/indexingModel/controllers/IndexingModelController.php b/src/app/indexingModel/controllers/IndexingModelController.php index 0b204d1eb33fa10a7281671fd48568e5427d0f15..3369fa3c5a9dd7659a4b3515fadd82533215dd3f 100644 --- a/src/app/indexingModel/controllers/IndexingModelController.php +++ b/src/app/indexingModel/controllers/IndexingModelController.php @@ -26,14 +26,14 @@ use Slim\Http\Response; class IndexingModelController { - const FIELDS_TYPES = ['standard', 'custom']; + const FIELDS_TYPES = ['string', 'integer', 'select', 'date', 'radio', 'checkbox']; public function get(Request $request, Response $response) { $models = IndexingModelModel::get(['where' => ['owner = ? OR private = ?'], 'data' => [$GLOBALS['id'], 'false']]); foreach ($models as $key => $model) { - $fields = IndexingModelFieldModel::get(['select' => ['type', 'identifier', 'mandatory', 'value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$model['id']]]); + $fields = IndexingModelFieldModel::get(['select' => ['type', 'identifier', 'mandatory', 'default_value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$model['id']]]); $models[$key]['fields'] = $fields; } @@ -47,9 +47,9 @@ class IndexingModelController return $response->withStatus(400)->withJson(['errors' => 'Model not found']); } elseif ($model['private'] && $model['owner'] != $GLOBALS['id']) { return $response->withStatus(400)->withJson(['errors' => 'Model out of perimeter']); - } + } - $fields = IndexingModelFieldModel::get(['select' => ['type', 'identifier', 'mandatory', 'value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$args['id']]]); + $fields = IndexingModelFieldModel::get(['select' => ['type', 'identifier', 'mandatory', 'default_value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$args['id']]]); $model['fields'] = $fields; return $response->withJson(['indexingModel' => $model]); @@ -65,7 +65,7 @@ class IndexingModelController foreach ($body['fields'] as $key => $field) { if (!Validator::stringType()->notEmpty()->validate($field['type']) || !in_array($field['type'], IndexingModelController::FIELDS_TYPES)) { return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] type is empty or not a validate type"]); - } elseif (!Validator::intVal()->notEmpty()->validate($field['identifier'])) { + } elseif (!Validator::stringType()->notEmpty()->validate($field['identifier'])) { return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] identifier is empty or not an integer"]); } } @@ -84,18 +84,13 @@ class IndexingModelController ]); foreach ($body['fields'] as $field) { - if ($field['type'] == 'custom') { - $unit = $field['unit'] ?? null; - } else { - $unit = null; - } IndexingModelFieldModel::create([ 'model_id' => $modelId, 'type' => $field['type'], 'identifier' => $field['identifier'], 'mandatory' => empty($field['mandatory']) ? 'false' : 'true', - 'value' => $field['value'] ?? null, - 'unit' => $unit + 'default_value' => $field['default_value'] ?? null, + 'unit' => $field['unit'] ?? null ]); } @@ -112,7 +107,7 @@ class IndexingModelController foreach ($body['fields'] as $key => $field) { if (!Validator::stringType()->notEmpty()->validate($field['type']) || !in_array($field['type'], IndexingModelController::FIELDS_TYPES)) { return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] type is empty or not a validate type"]); - } elseif (!Validator::intVal()->notEmpty()->validate($field['identifier'])) { + } elseif (!Validator::stringType()->notEmpty()->validate($field['identifier'])) { return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] identifier is empty or not an integer"]); } } @@ -137,18 +132,13 @@ class IndexingModelController IndexingModelFieldModel::delete(['where' => ['model_id = ?'], 'data' => [$args['id']]]); foreach ($body['fields'] as $field) { - if ($field['type'] == 'custom') { - $unit = $field['unit'] ?? null; - } else { - $unit = null; - } IndexingModelFieldModel::create([ 'model_id' => $args['id'], 'type' => $field['type'], 'identifier' => $field['identifier'], 'mandatory' => empty($field['mandatory']) ? 'false' : 'true', - 'value' => $field['value'] ?? null, - 'unit' => $unit + 'default_value' => $field['default_value'] ?? null, + 'unit' => $field['unit'] ?? null ]); } diff --git a/src/app/indexingModel/models/IndexingModelFieldModel.php b/src/app/indexingModel/models/IndexingModelFieldModel.php index 8cd391ec1353369a03dbe12634bfdabeb483838d..b5f150e4450ba695de05761566e4d7eb4edff683 100644 --- a/src/app/indexingModel/models/IndexingModelFieldModel.php +++ b/src/app/indexingModel/models/IndexingModelFieldModel.php @@ -39,8 +39,8 @@ class IndexingModelFieldModel public static function create(array $args) { ValidatorModel::notEmpty($args, ['model_id', 'type', 'identifier', 'mandatory']); - ValidatorModel::stringType($args, ['type', 'mandatory', 'value']); - ValidatorModel::intVal($args, ['model_id', 'identifier', 'unit']); + ValidatorModel::stringType($args, ['type', 'mandatory', 'default_value', 'identifier', 'unit']); + ValidatorModel::intVal($args, ['model_id']); DatabaseModel::insert([ 'table' => 'indexing_models_fields', @@ -49,7 +49,7 @@ class IndexingModelFieldModel 'type' => $args['type'], 'identifier' => $args['identifier'], 'mandatory' => $args['mandatory'], - 'value' => $args['value'], + 'default_value' => $args['default_value'], 'unit' => $args['unit'] ] ]); diff --git a/test/unitTests/app/indexingModel/IndexingModelControllerTest.php b/test/unitTests/app/indexingModel/IndexingModelControllerTest.php index 56be4af9423feeff8f2042d852f68bd6757935a9..d1610ab3f49607be43ee5c041d149fae735f1ce5 100644 --- a/test/unitTests/app/indexingModel/IndexingModelControllerTest.php +++ b/test/unitTests/app/indexingModel/IndexingModelControllerTest.php @@ -26,26 +26,26 @@ class IndexingModelControllerTest extends TestCase 'private' => true, 'fields' => [ [ - 'type' => 'standard', - 'identifier' => 1, + 'type' => 'string', + 'identifier' => 'subject', 'mandatory' => true, - 'value' => 'tika', + 'default_value' => 'tika', ], [ - 'type' => 'standard', - 'identifier' => 2, + 'type' => 'string', + 'identifier' => 'name', 'mandatory' => true, - 'value' => 'massala', + 'default_value' => 'massala', ] ] ]; $fullRequest = \httpRequestCustom::addContentInBody($args, $request); $response = $indexingModelController->create($fullRequest, new \Slim\Http\Response()); - $this->assertSame(200, $response->getStatusCode()); - $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(200, $response->getStatusCode()); + self::$id = $responseBody->id; // GET BY ID @@ -59,14 +59,14 @@ class IndexingModelControllerTest extends TestCase $this->assertSame('mon model d indexation', $responseBody->indexingModel->label); $this->assertSame(true, $responseBody->indexingModel->private); - $this->assertSame('standard', $responseBody->indexingModel->fields[0]->type); - $this->assertSame(1, $responseBody->indexingModel->fields[0]->identifier); + $this->assertSame('string', $responseBody->indexingModel->fields[0]->type); + $this->assertSame('subject', $responseBody->indexingModel->fields[0]->identifier); $this->assertSame(true, $responseBody->indexingModel->fields[0]->mandatory); - $this->assertSame('tika', $responseBody->indexingModel->fields[0]->value); - $this->assertSame('standard', $responseBody->indexingModel->fields[1]->type); - $this->assertSame(2, $responseBody->indexingModel->fields[1]->identifier); + $this->assertSame('tika', $responseBody->indexingModel->fields[0]->default_value); + $this->assertSame('string', $responseBody->indexingModel->fields[1]->type); + $this->assertSame('name', $responseBody->indexingModel->fields[1]->identifier); $this->assertSame(true, $responseBody->indexingModel->fields[1]->mandatory); - $this->assertSame('massala', $responseBody->indexingModel->fields[1]->value); + $this->assertSame('massala', $responseBody->indexingModel->fields[1]->default_value); // Errors @@ -95,16 +95,16 @@ class IndexingModelControllerTest extends TestCase 'label' => 'mon model d indexation modifié', 'fields' => [ [ - 'type' => 'standard', - 'identifier' => 4, + 'type' => 'string', + 'identifier' => 'subject', 'mandatory' => true, - 'value' => 'butter', + 'default_value' => 'butter', ], [ - 'type' => 'custom', - 'identifier' => 8, + 'type' => 'string', + 'identifier' => 'siret', 'mandatory' => false, - 'value' => 'chicken', + 'default_value' => 'chicken', ] ] ]; @@ -125,14 +125,14 @@ class IndexingModelControllerTest extends TestCase $this->assertSame('mon model d indexation modifié', $responseBody->indexingModel->label); $this->assertSame(true, $responseBody->indexingModel->private); - $this->assertSame('standard', $responseBody->indexingModel->fields[0]->type); - $this->assertSame(4, $responseBody->indexingModel->fields[0]->identifier); + $this->assertSame('string', $responseBody->indexingModel->fields[0]->type); + $this->assertSame('subject', $responseBody->indexingModel->fields[0]->identifier); $this->assertSame(true, $responseBody->indexingModel->fields[0]->mandatory); - $this->assertSame('butter', $responseBody->indexingModel->fields[0]->value); - $this->assertSame('custom', $responseBody->indexingModel->fields[1]->type); - $this->assertSame(8, $responseBody->indexingModel->fields[1]->identifier); + $this->assertSame('butter', $responseBody->indexingModel->fields[0]->default_value); + $this->assertSame('string', $responseBody->indexingModel->fields[1]->type); + $this->assertSame('siret', $responseBody->indexingModel->fields[1]->identifier); $this->assertSame(false, $responseBody->indexingModel->fields[1]->mandatory); - $this->assertSame('chicken', $responseBody->indexingModel->fields[1]->value); + $this->assertSame('chicken', $responseBody->indexingModel->fields[1]->default_value); // Errors