Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 36d0772

Browse files
committedAug 17, 2021
new PUT endpoints & metadata endpoints removed
1 parent 3380164 commit 36d0772

File tree

14 files changed

+289
-902
lines changed

14 files changed

+289
-902
lines changed
 

‎docs/skills-api.postman_collection.json

Lines changed: 60 additions & 212 deletions
Large diffs are not rendered by default.

‎docs/swagger.yaml

Lines changed: 28 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@ basePath: "/api/1.0"
3030
tags:
3131
- name: "Skills"
3232
description: "Skills registered in the system"
33-
- name: "Skill Metadata"
34-
description: "Metadata for Skills registered in the system"
3533
- name: "Taxonomies"
3634
description: "Taxonomies registered in the system"
37-
- name: "Taxonomy Metadata"
38-
description: "Metadata for Taxonomies registered in the system"
3935
schemes:
4036
- "https"
4137
consumes:
@@ -285,16 +281,16 @@ paths:
285281
tags:
286282
- "Skills"
287283
description: >
288-
Update an existing skill with given id.
284+
Partically update an existing skill with given id.
289285
290286
291287
**PERMISSION**
292288
293289
1. permission `UPDATE_SKILL` is required to perform this operation
294290
295-
2. permission `ADD_SKILL_METADATA` is required if any metadata field is provided.
291+
2. permission `ADD_SKILL_METADATA` is required if any new metadata field is provided.
296292
297-
3. permission `DELETE_SKILL_METADATA` is required if there is any existing metadata field in the skill.
293+
3. permission `UPDATE_SKILL_METADATA` is required if any metadata field provided already exist in the skill.
298294
operationId: "skillsSkillIdPATCH"
299295
parameters:
300296
- name: "skillId"
@@ -329,105 +325,21 @@ paths:
329325
- Bearer: []
330326
x-swagger-router-controller: "Skills"
331327

332-
/skills/{skillId}/metadata:
333328
put:
334329
tags:
335-
- "Skill Metadata"
336-
description: >
337-
Fully update the metadata of an existing skill with given id.
338-
339-
Used to entirely overwrite existing metadata.
340-
341-
342-
**PERMISSION**
343-
344-
1. permission `ADD_SKILL_METADATA` is required if any metadata field is provided.
345-
346-
2. permission `DELETE_SKILL_METADATA` is required if there is any existing metadata field in the skill.
347-
parameters:
348-
- name: "skillId"
349-
in: "path"
350-
description: "The skill id"
351-
required: true
352-
type: "string"
353-
format: "UUID"
354-
- in: "body"
355-
name: "body"
356-
required: true
357-
schema:
358-
$ref: "#/definitions/SkillMetadataUpdateRequestBody"
359-
responses:
360-
"200":
361-
description: "OK - the request was successful"
362-
schema:
363-
$ref: "#/definitions/Skill"
364-
"400":
365-
$ref: "#/definitions/BadRequest"
366-
"401":
367-
$ref: "#/definitions/Unauthorized"
368-
"403":
369-
$ref: "#/definitions/Forbidden"
370-
"404":
371-
$ref: "#/definitions/NotFound"
372-
"500":
373-
$ref: "#/definitions/ServerError"
374-
security:
375-
- Bearer: []
376-
patch:
377-
tags:
378-
- "Skill Metadata"
330+
- "Skills"
379331
description: >
380-
Partically update the metadata of an existing skill with given id.
381-
382-
Used to update existing fields in metadata and to add new fields to metadata.
332+
Fully update an existing skill with given id.
383333
384334
385335
**PERMISSION**
386336
387-
1. permission `ADD_SKILL_METADATA` is required if any new metadata field is provided.
388-
389-
2. permission `UPDATE_SKILL_METADATA` is required if any metadata field provided already exist in the skill.
390-
parameters:
391-
- name: "skillId"
392-
in: "path"
393-
description: "The skill id"
394-
required: true
395-
type: "string"
396-
format: "UUID"
397-
- in: "body"
398-
name: "body"
399-
required: true
400-
schema:
401-
$ref: "#/definitions/SkillMetadataUpdateRequestBody"
402-
responses:
403-
"200":
404-
description: "OK - the request was successful"
405-
schema:
406-
$ref: "#/definitions/Skill"
407-
"400":
408-
$ref: "#/definitions/BadRequest"
409-
"401":
410-
$ref: "#/definitions/Unauthorized"
411-
"403":
412-
$ref: "#/definitions/Forbidden"
413-
"404":
414-
$ref: "#/definitions/NotFound"
415-
"500":
416-
$ref: "#/definitions/ServerError"
417-
security:
418-
- Bearer: []
419-
delete:
420-
tags:
421-
- "Skill Metadata"
422-
description: >
423-
Remove fields from the metadata of an existing skill with given id.
424-
425-
404 error will be raised if one or more of the provided fields is missing in the existing metadata.
426-
337+
1. permission `UPDATE_SKILL` is required to perform this operation
427338
428-
**PERMISSION**
339+
2. permission `ADD_SKILL_METADATA` is required if new metadata fields are provided.
429340
430-
Permission `DELETE_SKILL_METADATA` is required to perform this operation.
341+
3. permission `DELETE_SKILL_METADATA` is required if there is any existing metadata field in the skill.
342+
operationId: "skillsSkillIdPUT"
431343
parameters:
432344
- name: "skillId"
433345
in: "path"
@@ -439,7 +351,7 @@ paths:
439351
name: "body"
440352
required: true
441353
schema:
442-
$ref: "#/definitions/SkillMetadataDeleteRequestBody"
354+
$ref: "#/definitions/SkillRequestBody"
443355
responses:
444356
"200":
445357
description: "OK - the request was successful"
@@ -453,10 +365,13 @@ paths:
453365
$ref: "#/definitions/Forbidden"
454366
"404":
455367
$ref: "#/definitions/NotFound"
368+
"409":
369+
$ref: "#/definitions/Conflict"
456370
"500":
457371
$ref: "#/definitions/ServerError"
458372
security:
459373
- Bearer: []
374+
x-swagger-router-controller: "Skills"
460375

461376
/taxonomies:
462377
get:
@@ -674,16 +589,16 @@ paths:
674589
tags:
675590
- "Taxonomies"
676591
description: >
677-
Update an existing taxonomy with given id.
592+
Partically update an existing taxonomy with given id.
678593
679594
680595
**PERMISSION**
681596
682597
1. permission `UPDATE_TAXONOMY` is required to perform this operation
683598
684-
2. permission `ADD_TAXONOMY_METADATA` is required if any metadata field is provided.
599+
2. permission `ADD_TAXONOMY_METADATA` is required if any new metadata field is provided.
685600
686-
3. permission `DELETE_TAXONOMY_METADATA` is required if there is any existing metadata field in the taxonomy.
601+
3. permission `UPDATE_TAXONOMY_METADATA` is required if any metadata field provided already exist in the taxonomy.
687602
operationId: "skillstaxonomiestaxonomyIdPATCH"
688603
parameters:
689604
- name: "taxonomyId"
@@ -717,105 +632,21 @@ paths:
717632
security:
718633
- Bearer: []
719634
x-swagger-router-controller: "Taxonomy"
720-
/taxonomies/{taxonomyId}/metadata:
721635
put:
722636
tags:
723-
- "Taxonomy Metadata"
724-
description: >
725-
Fully update the metadata of an existing taxonomy with given id.
726-
727-
Used to entirely overwrite existing metadata.
728-
729-
730-
**PERMISSION**
731-
732-
1. permission `ADD_TAXONOMY_METADATA` is required if any metadata field is provided.
733-
734-
2. permission `DELETE_TAXONOMY_METADATA` is required if there is any existing metadata field in the taxonomy.
735-
parameters:
736-
- name: "taxonomyId"
737-
in: "path"
738-
description: "The taxonomy id"
739-
required: true
740-
type: "string"
741-
format: "UUID"
742-
- in: "body"
743-
name: "body"
744-
required: true
745-
schema:
746-
$ref: "#/definitions/TaxonomyMetadataUpdateRequestBody"
747-
responses:
748-
"200":
749-
description: "OK - the request was successful"
750-
schema:
751-
$ref: "#/definitions/Taxonomy"
752-
"400":
753-
$ref: "#/definitions/BadRequest"
754-
"401":
755-
$ref: "#/definitions/Unauthorized"
756-
"403":
757-
$ref: "#/definitions/Forbidden"
758-
"404":
759-
$ref: "#/definitions/NotFound"
760-
"500":
761-
$ref: "#/definitions/ServerError"
762-
security:
763-
- Bearer: []
764-
patch:
765-
tags:
766-
- "Taxonomy Metadata"
637+
- "Taxonomies"
767638
description: >
768-
Partically update the metadata of an existing taxonomy with given id.
769-
770-
Used to update existing fields in metadata and to add new fields to metadata.
639+
Fully update an existing taxonomy with given id.
771640
772641
773642
**PERMISSION**
774643
775-
1. permission `ADD_TAXONOMY_METADATA` is required if any new metadata field is provided.
776-
777-
2. permission `UPDATE_TAXONOMY_METADATA` is required if any metadata field provided already exist in the taxonomy.
778-
parameters:
779-
- name: "taxonomyId"
780-
in: "path"
781-
description: "The taxonomy id"
782-
required: true
783-
type: "string"
784-
format: "UUID"
785-
- in: "body"
786-
name: "body"
787-
required: true
788-
schema:
789-
$ref: "#/definitions/TaxonomyMetadataUpdateRequestBody"
790-
responses:
791-
"200":
792-
description: "OK - the request was successful"
793-
schema:
794-
$ref: "#/definitions/Taxonomy"
795-
"400":
796-
$ref: "#/definitions/BadRequest"
797-
"401":
798-
$ref: "#/definitions/Unauthorized"
799-
"403":
800-
$ref: "#/definitions/Forbidden"
801-
"404":
802-
$ref: "#/definitions/NotFound"
803-
"500":
804-
$ref: "#/definitions/ServerError"
805-
security:
806-
- Bearer: []
807-
delete:
808-
tags:
809-
- "Taxonomy Metadata"
810-
description: >
811-
Remove fields from the metadata of an existing taxonomy with given id.
812-
813-
404 error will be raised if one or more of the provided fields is missing in the existing metadata.
814-
644+
1. permission `UPDATE_TAXONOMY` is required to perform this operation
815645
816-
**PERMISSION**
646+
2. permission `ADD_TAXONOMY_METADATA` is required if any metadata field is provided.
817647
818-
Permission `DELETE_TAXONOMY_METADATA` is required to perform this operation.
648+
3. permission `DELETE_TAXONOMY_METADATA` is required if there is any existing metadata field in the taxonomy.
649+
operationId: "skillstaxonomiestaxonomyIdPUT"
819650
parameters:
820651
- name: "taxonomyId"
821652
in: "path"
@@ -827,7 +658,7 @@ paths:
827658
name: "body"
828659
required: true
829660
schema:
830-
$ref: "#/definitions/TaxonomyMetadataDeleteRequestBody"
661+
$ref: "#/definitions/TaxonomyRequestBody"
831662
responses:
832663
"200":
833664
description: "OK - the request was successful"
@@ -841,10 +672,13 @@ paths:
841672
$ref: "#/definitions/Forbidden"
842673
"404":
843674
$ref: "#/definitions/NotFound"
675+
"409":
676+
$ref: "#/definitions/Conflict"
844677
"500":
845678
$ref: "#/definitions/ServerError"
846679
security:
847680
- Bearer: []
681+
x-swagger-router-controller: "Taxonomy"
848682

849683
securityDefinitions:
850684
Bearer:
@@ -954,26 +788,6 @@ definitions:
954788
metadata:
955789
challengeProminence: "challengeProminence"
956790
memberProminence: "memberProminence"
957-
SkillMetadataUpdateRequestBody:
958-
type: "object"
959-
description: "The metadata for a skill. Can contain arbitrary fields"
960-
properties:
961-
challengeProminence:
962-
type: "string"
963-
description: "The challenge prominence ranging from [0, 1]"
964-
memberProminence:
965-
type: "string"
966-
description: "The member prominence ranging from [0, 1]"
967-
example:
968-
verions: "1.0.0"
969-
random_field: "random_value"
970-
SkillMetadataDeleteRequestBody:
971-
type: "array"
972-
description: "The list of fields to be removed from the metadata for a skill."
973-
items:
974-
type: string
975-
description: "field name"
976-
example: ["version", "random_field"]
977791
Taxonomy:
978792
allOf:
979793
- type: "object"
@@ -1015,19 +829,8 @@ definitions:
1015829
description: "The metadata of the taxonomy."
1016830
example:
1017831
name: "name"
1018-
TaxonomyMetadataUpdateRequestBody:
1019-
type: "object"
1020-
description: "The metadata for a taxonomy. Can contain arbitrary fields"
1021-
example:
1022-
verions: "1.0.0"
1023-
random_field: "random_value"
1024-
TaxonomyMetadataDeleteRequestBody:
1025-
type: "array"
1026-
description: "The list of fields to be removed from the metadata for a taxonomy."
1027-
items:
1028-
type: string
1029-
description: "field name"
1030-
example: ["version", "random_field"]
832+
metadata:
833+
random_field_name_01: random_value_01
1031834
Unauthorized:
1032835
type: "object"
1033836
properties:

‎src/common/controller-helper.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ function getControllerMethods (service) {
2424
res.json(await service.patch(req.params.id, req.body, req.authUser))
2525
}
2626

27+
/**
28+
* fully update entity by id
29+
* @param req the http request
30+
* @param res the http response
31+
*/
32+
async function fullyUpdate (req, res) {
33+
res.json(await service.fullyUpdate(req.params.id, req.body, req.authUser))
34+
}
35+
2736
/**
2837
* get entity by id
2938
* @param req the http request
@@ -59,7 +68,8 @@ function getControllerMethods (service) {
5968
search,
6069
remove,
6170
get,
62-
patch
71+
patch,
72+
fullyUpdate
6373
}
6474
}
6575

‎src/constants.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,9 @@ const API_ACTION = {
4949
SkillCreate: 'skill.create',
5050
SkillUpdate: 'skill.update',
5151
SkillDelete: 'skill.delete',
52-
SkillPutMetadata: 'skill.putMetadata',
53-
SkillPatchMetadata: 'skill.patchMetadata',
54-
SkillDeleteMetadata: 'skill.deleteMetadata',
5552
TaxonomyCreate: 'taxonomy.create',
5653
TaxonomyUpdate: 'taxonomy.update',
57-
TaxonomyDelete: 'taxonomy.delete',
58-
TaxonomyPutMetadata: 'taxonomy.putMetadata',
59-
TaxonomyPatchMetadata: 'taxonomy.patchMetadata',
60-
TaxonomyDeleteMetadata: 'taxonomy.deleteMetadata'
54+
TaxonomyDelete: 'taxonomy.delete'
6155
}
6256

6357
module.exports = {

‎src/modules/SkillMetadata/controller.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎src/modules/SkillMetadata/route.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎src/modules/SkillMetadata/service.js

Lines changed: 0 additions & 148 deletions
This file was deleted.

‎src/modules/skill/route.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ module.exports = {
3030
auth: 'jwt',
3131
permission: 'skill.edit'
3232
},
33+
put: {
34+
method: Controller.fullyUpdate,
35+
auth: 'jwt',
36+
permission: 'skill.edit'
37+
},
3338
delete: {
3439
method: Controller.remove,
3540
auth: 'jwt',

‎src/modules/skill/service.js

Lines changed: 99 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,46 +69,25 @@ create.schema = {
6969
}
7070

7171
/**
72-
* patch skill by id
73-
* @param id the skill id
74-
* @param entity the request skill entity
72+
* Update skill by id. Used in functions patch and fullyUpdate.
73+
*
74+
* @param instance the skill instance
75+
* @param updateData the data to be updated
7576
* @param auth the auth object
7677
* @return the updated skill
7778
*/
78-
async function patch (id, entity, auth) {
79-
let taxonomy
80-
if (entity.taxonomyId) {
81-
taxonomy = await dbHelper.get(Taxonomy, entity.taxonomyId)
82-
}
83-
84-
await dbHelper.makeSureUnique(Skill, entity, uniqueFields)
85-
86-
const instance = await dbHelper.get(Skill, id)
87-
88-
if (entity.metadata) {
89-
if (Object.keys(entity.metadata).length) {
90-
// check permission for adding new metadata fields
91-
serviceHelper.hasPermission(PERMISSION.ADD_SKILL_METADATA, auth)
92-
}
93-
if (Object.keys(instance.metadata).length) {
94-
// check permission for removing existing metadata fields
95-
serviceHelper.hasPermission(PERMISSION.DELETE_SKILL_METADATA, auth)
96-
}
97-
}
98-
79+
async function update (instance, updateData, auth) {
9980
let payload
10081
try {
10182
return await sequelize.transaction(async () => {
10283
const newEntity = await instance.update({
103-
...entity,
84+
...updateData,
10485
updatedBy: helper.getAuthUser(auth)
10586
})
10687

10788
payload = newEntity.dataValues
10889

109-
if (!taxonomy) {
110-
taxonomy = await dbHelper.get(Taxonomy, newEntity.taxonomyId)
111-
}
90+
const taxonomy = await dbHelper.get(Taxonomy, newEntity.taxonomyId)
11291
const updated = { ...newEntity.dataValues, taxonomyName: taxonomy.name }
11392

11493
await serviceHelper.patchRecordInEs(resource, updated)
@@ -123,6 +102,47 @@ async function patch (id, entity, auth) {
123102
}
124103
}
125104

105+
/**
106+
* Patch skill by id.
107+
* If the metadata field is provided, existing metadata fields would be updated and new metadata fields would be added.
108+
*
109+
* @param id the skill id
110+
* @param entity the request skill entity
111+
* @param auth the auth object
112+
* @return the updated skill
113+
*/
114+
async function patch (id, entity, auth) {
115+
// check if the skill exists or not
116+
const instance = await dbHelper.get(Skill, id)
117+
118+
if (entity.taxonomyId && entity.taxonomyId !== instance.taxonomyId) {
119+
// check if the taxonomy exists or not
120+
await dbHelper.get(Taxonomy, entity.taxonomyId)
121+
}
122+
123+
// check if the skill has conflict or not
124+
await dbHelper.makeSureUnique(Skill, entity, uniqueFields)
125+
126+
if (entity.metadata) {
127+
const inputFields = Object.keys(entity.metadata)
128+
const existingFields = Object.keys(instance.metadata)
129+
const sharedFields = _.intersection(inputFields, existingFields)
130+
131+
if (inputFields.length > sharedFields.length) {
132+
// check permission for adding new fields
133+
serviceHelper.hasPermission(PERMISSION.ADD_SKILL_METADATA, auth)
134+
}
135+
if (sharedFields.length) {
136+
// check permission for updating fields
137+
serviceHelper.hasPermission(PERMISSION.UPDATE_SKILL_METADATA, auth)
138+
}
139+
}
140+
141+
const updateData = { ...instance, ...entity, metadata: { ...instance.metadata, ...entity.metadata } }
142+
143+
return update(instance, updateData, auth)
144+
}
145+
126146
patch.schema = {
127147
id: joi.string().uuid().required(),
128148
entity: joi.object().keys({
@@ -138,6 +158,56 @@ patch.schema = {
138158
auth: joi.object()
139159
}
140160

161+
/**
162+
* Fully update skill by id.
163+
* Existing metadata fields would be entirely replace with the new ones.
164+
*
165+
* @param id the skill id
166+
* @param entity the request skill entity
167+
* @param auth the auth object
168+
* @return the updated skill
169+
*/
170+
async function fullyUpdate (id, entity, auth) {
171+
// check if the skill exists or not
172+
const instance = await dbHelper.get(Skill, id)
173+
174+
if (entity.taxonomyId !== instance.taxonomyId) {
175+
// check if the taxonomy exists or not
176+
await dbHelper.get(Taxonomy, entity.taxonomyId)
177+
}
178+
179+
// check if the skill has conflict or not
180+
await dbHelper.makeSureUnique(Skill, entity, uniqueFields)
181+
182+
if (Object.keys(entity.metadata).length) {
183+
// check permission for adding new metadata fields
184+
serviceHelper.hasPermission(PERMISSION.ADD_SKILL_METADATA, auth)
185+
}
186+
if (Object.keys(instance.metadata).length) {
187+
// check permission for removing existing metadata fields
188+
serviceHelper.hasPermission(PERMISSION.DELETE_SKILL_METADATA, auth)
189+
}
190+
191+
const updateData = entity
192+
193+
return update(instance, updateData, auth)
194+
}
195+
196+
fullyUpdate.schema = {
197+
id: joi.string().uuid().required(),
198+
entity: joi.object().keys({
199+
taxonomyId: joi.string().uuid().required(),
200+
name: joi.string().required(),
201+
uri: joi.string().default(null),
202+
externalId: joi.string().default(null),
203+
metadata: joi.object().keys({
204+
challengeProminence: joi.prominence('challengeProminence'),
205+
memberProminence: joi.prominence('memberProminence')
206+
}).unknown(true).required()
207+
}).required(),
208+
auth: joi.object()
209+
}
210+
141211
/**
142212
* get skill by id
143213
* @param id the skill id
@@ -263,6 +333,7 @@ module.exports = {
263333
create,
264334
search,
265335
patch,
336+
fullyUpdate,
266337
get,
267338
remove
268339
}

‎src/modules/taxonomy/route.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ module.exports = {
3030
auth: 'jwt',
3131
permission: 'taxonomy.edit'
3232
},
33+
put: {
34+
method: Controller.fullyUpdate,
35+
auth: 'jwt',
36+
permission: 'taxonomy.edit'
37+
},
3338
delete: {
3439
method: Controller.remove,
3540
auth: 'jwt',

‎src/modules/taxonomy/service.js

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,37 +57,26 @@ create.schema = {
5757
}
5858

5959
/**
60-
* patch taxonomy by id
61-
* @param id the taxonomy id
62-
* @param entity the request taxonomy entity
60+
* Update taxonomy by id. Used in functions patch and fullyUpdate.
61+
*
62+
* @param instance the taxonomy instance
63+
* @param updateData the data to be updated
6364
* @param auth the auth object
64-
* @param params the query params
6565
* @return the updated taxonomy
6666
*/
67-
async function patch (id, entity, auth) {
68-
const instance = await dbHelper.get(Taxonomy, id)
69-
70-
if (entity.metadata) {
71-
if (Object.keys(entity.metadata).length) {
72-
// check permission for adding new metadata fields
73-
serviceHelper.hasPermission(PERMISSION.ADD_TAXONOMY_METADATA, auth)
74-
}
75-
if (Object.keys(instance.metadata).length) {
76-
// check permission for removing existing metadata fields
77-
serviceHelper.hasPermission(PERMISSION.DELETE_TAXONOMY_METADATA, auth)
78-
}
79-
}
80-
67+
async function update (instance, updateData, auth) {
8168
let payload
8269
try {
8370
return await sequelize.transaction(async () => {
8471
const newEntity = await instance.update({
85-
...entity,
72+
...updateData,
8673
updatedBy: helper.getAuthUser(auth)
8774
})
75+
8876
payload = newEntity.dataValues
8977

9078
await serviceHelper.patchRecordInEs(resource, newEntity.dataValues)
79+
9180
return helper.omitAuditFields(newEntity.dataValues)
9281
})
9382
} catch (e) {
@@ -98,6 +87,39 @@ async function patch (id, entity, auth) {
9887
}
9988
}
10089

90+
/**
91+
* Patch taxonomy by id.
92+
* If the metadata field is provided, existing metadata fields would be updated and new metadata fields would be added.
93+
*
94+
* @param id the taxonomy id
95+
* @param entity the request taxonomy entity
96+
* @param auth the auth object
97+
* @param params the query params
98+
* @return the updated taxonomy
99+
*/
100+
async function patch (id, entity, auth) {
101+
const instance = await dbHelper.get(Taxonomy, id)
102+
103+
if (entity.metadata) {
104+
const inputFields = Object.keys(entity.metadata)
105+
const existingFields = Object.keys(instance.metadata)
106+
const sharedFields = _.intersection(inputFields, existingFields)
107+
108+
if (inputFields.length > sharedFields.length) {
109+
// check permission for adding new fields
110+
serviceHelper.hasPermission(PERMISSION.ADD_TAXONOMY_METADATA, auth)
111+
}
112+
if (sharedFields.length) {
113+
// check permission for updating fields
114+
serviceHelper.hasPermission(PERMISSION.UPDATE_TAXONOMY_METADATA, auth)
115+
}
116+
}
117+
118+
const updateData = { ...instance, ...entity, metadata: { ...instance.metadata, ...entity.metadata } }
119+
120+
return update(instance, updateData, auth)
121+
}
122+
101123
patch.schema = {
102124
id: joi.string().uuid().required(),
103125
entity: joi.object().keys({
@@ -107,6 +129,44 @@ patch.schema = {
107129
auth: joi.object()
108130
}
109131

132+
/**
133+
* Fully update taxonomy by id.
134+
* Existing metadata fields would be entirely replace with the new ones.
135+
*
136+
* @param id the taxonomy id
137+
* @param entity the request taxonomy entity
138+
* @param auth the auth object
139+
* @param params the query params
140+
* @return the updated taxonomy
141+
*/
142+
async function fullyUpdate (id, entity, auth) {
143+
const instance = await dbHelper.get(Taxonomy, id)
144+
145+
if (entity.metadata) {
146+
if (Object.keys(entity.metadata).length) {
147+
// check permission for adding new metadata fields
148+
serviceHelper.hasPermission(PERMISSION.ADD_TAXONOMY_METADATA, auth)
149+
}
150+
if (Object.keys(instance.metadata).length) {
151+
// check permission for removing existing metadata fields
152+
serviceHelper.hasPermission(PERMISSION.DELETE_TAXONOMY_METADATA, auth)
153+
}
154+
}
155+
156+
const updateData = entity
157+
158+
return update(instance, updateData, auth)
159+
}
160+
161+
fullyUpdate.schema = {
162+
id: joi.string().uuid().required(),
163+
entity: joi.object().keys({
164+
name: joi.string().required(),
165+
metadata: joi.object().default({})
166+
}).required(),
167+
auth: joi.object()
168+
}
169+
110170
/**
111171
* get taxonomy by id
112172
* @param id the taxonomy id
@@ -201,6 +261,7 @@ module.exports = {
201261
create,
202262
search,
203263
patch,
264+
fullyUpdate,
204265
get,
205266
remove
206267
}

‎src/modules/taxonomyMetadata/controller.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎src/modules/taxonomyMetadata/route.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎src/modules/taxonomyMetadata/service.js

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)
This repository has been archived.