diff --git a/.gitignore b/.gitignore index 4c49bd7..6ed48a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +node_modules diff --git a/docs/skills-api.postman_collection.json b/docs/skills-api.postman_collection.json index 12af166..cdec01c 100644 --- a/docs/skills-api.postman_collection.json +++ b/docs/skills-api.postman_collection.json @@ -1,6 +1,6 @@ { "info": { - "_postman_id": "8d22bd52-0b64-43a5-9db3-c8516cb3e371", + "_postman_id": "f4b1365c-fcee-492c-848a-c7c5a49a779f", "name": "skills-api", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, @@ -90,41 +90,7 @@ ], "body": { "mode": "raw", - "raw": "{\n \"name\": \"taxonomies_update12\",\n \"metadata\": {\n \"version\": \"1.0.0\"\n }\n}" - }, - "url": { - "raw": "{{HOST}}/taxonomies/{{taxonomyId}}", - "host": [ - "{{HOST}}" - ], - "path": [ - "taxonomies", - "{{taxonomyId}}" - ] - } - }, - "response": [] - }, - { - "name": "{{HOST}}/taxonomies/:id", - "request": { - "method": "PUT", - "header": [ - { - "key": "Authorization", - "type": "text", - "value": "Bearer {{token}}" - }, - { - "key": "Content-Type", - "name": "Content-Type", - "type": "text", - "value": "application/json" - } - ], - "body": { - "mode": "raw", - "raw": "{\n \"name\": \"taxonomies_update\",\n \"metadata\": {\n \"new_metadata_field\": \"value\"\n }\n}" + "raw": "{\n \"metadata\": {\n \"version\": \"1.0.0\"\n }\n}" }, "url": { "raw": "{{HOST}}/taxonomies/{{taxonomyId}}", @@ -319,41 +285,7 @@ ], "body": { "mode": "raw", - "raw": "{\n \"name\": \"skill_name_update23\",\n \"metadata\": {\n \"memberProminence\": \"0.75\",\n \"version\": \"0.1.0\"\n }\n}" - }, - "url": { - "raw": "{{HOST}}/skills/{{skillId}}", - "host": [ - "{{HOST}}" - ], - "path": [ - "skills", - "{{skillId}}" - ] - } - }, - "response": [] - }, - { - "name": "{{HOST}}/skills/:id", - "request": { - "method": "PUT", - "header": [ - { - "key": "Authorization", - "type": "text", - "value": "Bearer {{token}}" - }, - { - "key": "Content-Type", - "name": "Content-Type", - "type": "text", - "value": "application/json" - } - ], - "body": { - "mode": "raw", - "raw": "{\n\t\"taxonomyId\":\"{{taxonomyId}}\",\n\t\"name\":\"jump6\",\n \"metadata\": {\n \"challengeProminence\": \"0.2\"\n }\n}" + "raw": "{\n \"metadata\": {\n \"memberProminence\": \"0.75\",\n \"version\": \"0.1.0\"\n }\n}" }, "url": { "raw": "{{HOST}}/skills/{{skillId}}", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 02d4fbf..a9d8135 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -303,55 +303,7 @@ paths: name: "body" required: true schema: - $ref: "#/definitions/SkillUpdateRequestBody" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/Skill" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Skills" - - put: - tags: - - "Skills" - description: > - Fully update an existing skill with given id. - - - **PERMISSION** - - 1. permission `UPDATE_SKILL` is required to perform this operation - - 2. permission `ADD_SKILL_METADATA` is required if new metadata fields are provided. - - 3. permission `DELETE_SKILL_METADATA` is required if there is any existing metadata field in the skill. - operationId: "skillsSkillIdPUT" - parameters: - - name: "skillId" - in: "path" - description: "The skill id" - required: true - type: "string" - format: "UUID" - - in: "body" - name: "body" - required: true - schema: - $ref: "#/definitions/SkillRequestBody" + $ref: "#/definitions/SkillPatchRequestBody" responses: "200": description: "OK - the request was successful" @@ -611,54 +563,7 @@ paths: name: "body" required: true schema: - $ref: "#/definitions/TaxonomyUpdateRequestBody" - responses: - "200": - description: "OK - the request was successful" - schema: - $ref: "#/definitions/Taxonomy" - "400": - $ref: "#/definitions/BadRequest" - "401": - $ref: "#/definitions/Unauthorized" - "403": - $ref: "#/definitions/Forbidden" - "404": - $ref: "#/definitions/NotFound" - "409": - $ref: "#/definitions/Conflict" - "500": - $ref: "#/definitions/ServerError" - security: - - Bearer: [] - x-swagger-router-controller: "Taxonomy" - put: - tags: - - "Taxonomies" - description: > - Fully update an existing taxonomy with given id. - - - **PERMISSION** - - 1. permission `UPDATE_TAXONOMY` is required to perform this operation - - 2. permission `ADD_TAXONOMY_METADATA` is required if any metadata field is provided. - - 3. permission `DELETE_TAXONOMY_METADATA` is required if there is any existing metadata field in the taxonomy. - operationId: "skillstaxonomiestaxonomyIdPUT" - parameters: - - name: "taxonomyId" - in: "path" - description: "The taxonomy id" - required: true - type: "string" - format: "UUID" - - in: "body" - name: "body" - required: true - schema: - $ref: "#/definitions/TaxonomyRequestBody" + $ref: "#/definitions/TaxonomyPatchRequestBody" responses: "200": description: "OK - the request was successful" @@ -788,6 +693,23 @@ definitions: metadata: challengeProminence: "challengeProminence" memberProminence: "memberProminence" + SkillPatchRequestBody: + type: "object" + properties: + metadata: + type: "object" + description: "The metadata for the skill" + properties: + challengeProminence: + type: "string" + description: "The challenge prominence ranging from [0, 1]" + memberProminence: + type: "string" + description: "The member prominence ranging from [0, 1]" + example: + metadata: + challengeProminence: "challengeProminence" + memberProminence: "memberProminence" Taxonomy: allOf: - type: "object" @@ -831,6 +753,15 @@ definitions: name: "name" metadata: random_field_name_01: random_value_01 + TaxonomyPatchRequestBody: + type: "object" + properties: + metadata: + type: "object" + description: "The metadata of the taxonomy." + example: + metadata: + random_field_name_01: random_value_01 Unauthorized: type: "object" properties: diff --git a/src/modules/skill/route.js b/src/modules/skill/route.js index 2c07f3c..bd29bcb 100644 --- a/src/modules/skill/route.js +++ b/src/modules/skill/route.js @@ -30,11 +30,6 @@ module.exports = { auth: 'jwt', permission: 'skill.edit' }, - put: { - method: Controller.fullyUpdate, - auth: 'jwt', - permission: 'skill.edit' - }, delete: { method: Controller.remove, auth: 'jwt', diff --git a/src/modules/skill/service.js b/src/modules/skill/service.js index 2ebdb84..b087372 100644 --- a/src/modules/skill/service.js +++ b/src/modules/skill/service.js @@ -69,7 +69,7 @@ create.schema = { } /** - * Update skill by id. Used in functions patch and fullyUpdate. + * Update skill by id. Used in functions patch. * * @param instance the skill instance * @param updateData the data to be updated @@ -115,14 +115,6 @@ async function patch (id, entity, auth) { // check if the skill exists or not const instance = await dbHelper.get(Skill, id) - if (entity.taxonomyId && entity.taxonomyId !== instance.taxonomyId) { - // check if the taxonomy exists or not - await dbHelper.get(Taxonomy, entity.taxonomyId) - } - - // check if the skill has conflict or not - await dbHelper.makeSureUnique(Skill, { ...entity, id }, uniqueFields) - if (entity.metadata) { const inputFields = Object.keys(entity.metadata) const existingFields = Object.keys(instance.metadata) @@ -138,7 +130,7 @@ async function patch (id, entity, auth) { } } - const updateData = { ...instance, ...entity, metadata: { ...instance.metadata, ...entity.metadata } } + const updateData = { ...instance, metadata: { ...instance.metadata, ...entity.metadata } } return update(instance, updateData, auth) } @@ -158,56 +150,6 @@ patch.schema = { auth: joi.object() } -/** - * Fully update skill by id. - * Existing metadata fields would be entirely replace with the new ones. - * - * @param id the skill id - * @param entity the request skill entity - * @param auth the auth object - * @return the updated skill - */ -async function fullyUpdate (id, entity, auth) { - // check if the skill exists or not - const instance = await dbHelper.get(Skill, id) - - if (entity.taxonomyId !== instance.taxonomyId) { - // check if the taxonomy exists or not - await dbHelper.get(Taxonomy, entity.taxonomyId) - } - - // check if the skill has conflict or not - await dbHelper.makeSureUnique(Skill, { ...entity, id }, uniqueFields) - - if (Object.keys(entity.metadata).length) { - // check permission for adding new metadata fields - serviceHelper.hasPermission(PERMISSION.ADD_SKILL_METADATA, auth) - } - if (Object.keys(instance.metadata).length) { - // check permission for removing existing metadata fields - serviceHelper.hasPermission(PERMISSION.DELETE_SKILL_METADATA, auth) - } - - const updateData = entity - - return update(instance, updateData, auth) -} - -fullyUpdate.schema = { - id: joi.string().uuid().required(), - entity: joi.object().keys({ - taxonomyId: joi.string().uuid().required(), - name: joi.string().required(), - uri: joi.string().default(null), - externalId: joi.string().default(null), - metadata: joi.object().keys({ - challengeProminence: joi.prominence('challengeProminence'), - memberProminence: joi.prominence('memberProminence') - }).unknown(true).required() - }).required(), - auth: joi.object() -} - /** * get skill by id * @param id the skill id @@ -333,7 +275,6 @@ module.exports = { create, search, patch, - fullyUpdate, get, remove } diff --git a/src/modules/taxonomy/route.js b/src/modules/taxonomy/route.js index 64177e2..1586fa4 100644 --- a/src/modules/taxonomy/route.js +++ b/src/modules/taxonomy/route.js @@ -30,11 +30,6 @@ module.exports = { auth: 'jwt', permission: 'taxonomy.edit' }, - put: { - method: Controller.fullyUpdate, - auth: 'jwt', - permission: 'taxonomy.edit' - }, delete: { method: Controller.remove, auth: 'jwt', diff --git a/src/modules/taxonomy/service.js b/src/modules/taxonomy/service.js index 6defe8f..ee82b03 100644 --- a/src/modules/taxonomy/service.js +++ b/src/modules/taxonomy/service.js @@ -115,7 +115,7 @@ async function patch (id, entity, auth) { } } - const updateData = { ...instance, ...entity, metadata: { ...instance.metadata, ...entity.metadata } } + const updateData = { ...instance, metadata: { ...instance.metadata, ...entity.metadata } } return update(instance, updateData, auth) } @@ -129,44 +129,6 @@ patch.schema = { auth: joi.object() } -/** - * Fully update taxonomy by id. - * Existing metadata fields would be entirely replace with the new ones. - * - * @param id the taxonomy id - * @param entity the request taxonomy entity - * @param auth the auth object - * @param params the query params - * @return the updated taxonomy - */ -async function fullyUpdate (id, entity, auth) { - const instance = await dbHelper.get(Taxonomy, id) - - if (entity.metadata) { - if (Object.keys(entity.metadata).length) { - // check permission for adding new metadata fields - serviceHelper.hasPermission(PERMISSION.ADD_TAXONOMY_METADATA, auth) - } - if (Object.keys(instance.metadata).length) { - // check permission for removing existing metadata fields - serviceHelper.hasPermission(PERMISSION.DELETE_TAXONOMY_METADATA, auth) - } - } - - const updateData = entity - - return update(instance, updateData, auth) -} - -fullyUpdate.schema = { - id: joi.string().uuid().required(), - entity: joi.object().keys({ - name: joi.string().required(), - metadata: joi.object().default({}) - }).required(), - auth: joi.object() -} - /** * get taxonomy by id * @param id the taxonomy id @@ -261,7 +223,6 @@ module.exports = { create, search, patch, - fullyUpdate, get, remove }