Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 310adc9

Browse files
1. Set the enrich policy name as a config
2. Fix issue where migrate data from db to es would not save boolean fields 3. Fix issue where updating skill or attribute would result in referenced fields to also be passed in bus api
1 parent 2bf09e4 commit 310adc9

File tree

6 files changed

+119
-52
lines changed

6 files changed

+119
-52
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Configuration for the application is at config/default.js and config/production.
4040
- ATTRIBUTE_GROUP_PIPELINE_ID: The pipeline id for enrichment with attribute group. Default is `attributegroup-pipeline`
4141
- SKILL_PROVIDER_PIPELINE_ID: The pipeline id for enrichment with skill provider. Default is `skillprovider-pipeline`
4242
- USER_PIPELINE_ID: The pipeline id for enrichment of user details. Default is `user-pipeline`
43+
- ATTRIBUTE_GROUP_ENRICH_POLICYNAME: The enrich policy for attribute group. Default is `attributegroup-policy`
44+
- SKILL_PROVIDER_ENRICH_POLICYNAME: The enrich policy for skill provider. Default is `skillprovider-policy`
45+
- ROLE_ENRICH_POLICYNAME: The enrich policy for role. Default is `role-policy`
46+
- ACHIEVEMENT_PROVIDER_ENRICH_POLICYNAME: The enrich policy for achievement provider. Default is `achievementprovider-policy`
47+
- SKILL_ENRICH_POLICYNAME: The enrich policy for skill. Default is `skill-policy`
48+
- ATTRIBUTE_ENRICH_POLICYNAME: The enrich policy for skill. Default is `attribute-policy`
4349

4450
For `ES.DOCUMENTS` configuration, you will find multiple other configurations below it. Each has default values that you can override using the environment variables
4551

config/default.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,40 @@ module.exports = {
5555
DOCUMENTS: {
5656
achievementprovider: {
5757
index: process.env.ACHIEVEMENT_PROVIDER_INDEX || 'achievement_provider',
58-
type: '_doc'
58+
type: '_doc',
59+
enrichPolicyName: process.env.ACHIEVEMENT_PROVIDER_ENRICH_POLICYNAME || 'achievementprovider-policy'
5960
},
6061
attribute: {
6162
index: process.env.ATTRIBUTE_INDEX || 'attribute',
62-
type: '_doc'
63+
type: '_doc',
64+
enrichPolicyName: process.env.ATTRIBUTE_ENRICH_POLICYNAME || 'attribute-policy'
6365
},
6466
attributegroup: {
6567
index: process.env.ATTRIBUTE_GROUP_INDEX || 'attribute_group',
6668
type: '_doc',
67-
pipelineId: process.env.ATTRIBUTE_GROUP_PIPELINE_ID || 'attributegroup-pipeline'
69+
pipelineId: process.env.ATTRIBUTE_GROUP_PIPELINE_ID || 'attributegroup-pipeline',
70+
enrichPolicyName: process.env.ATTRIBUTE_GROUP_ENRICH_POLICYNAME || 'attributegroup-policy'
6871
},
6972
organization: {
7073
index: process.env.ORGANIZATION_INDEX || 'organization',
71-
type: '_doc'
74+
type: '_doc',
75+
enrichPolicyName: process.env.ORGANIZATION_ENRICH_POLICYNAME || 'organization-policy'
7276
},
7377
role: {
7478
index: process.env.ROLE_INDEX || 'role',
75-
type: '_doc'
79+
type: '_doc',
80+
enrichPolicyName: process.env.ROLE_ENRICH_POLICYNAME || 'role-policy'
7681
},
7782
skill: {
7883
index: process.env.SKILL_INDEX || 'skill',
79-
type: '_doc'
84+
type: '_doc',
85+
enrichPolicyName: process.env.SKILL_ENRICH_POLICYNAME || 'skill-policy'
8086
},
8187
skillprovider: {
8288
index: process.env.SKILL_PROVIDER_INDEX || 'skill_provider',
8389
type: '_doc',
84-
pipelineId: process.env.SKILL_PROVIDER_PIPELINE_ID || 'skillprovider-pipeline'
90+
pipelineId: process.env.SKILL_PROVIDER_PIPELINE_ID || 'skillprovider-pipeline',
91+
enrichPolicyName: process.env.SKILL_PROVIDER_ENRICH_POLICYNAME || 'skillprovider-policy'
8592
},
8693
user: {
8794
index: process.env.USER_INDEX || 'user',

scripts/constants.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const topResources = {
1010
index: config.get('ES.DOCUMENTS.skillprovider.index'),
1111
type: config.get('ES.DOCUMENTS.skillprovider.type'),
1212
enrich: {
13-
policyName: 'skillprovider-policy',
13+
policyName: config.get('ES.DOCUMENTS.skillprovider.enrichPolicyName'),
1414
matchField: 'id',
1515
enrichFields: ['id', 'name', 'created', 'updated', 'createdBy', 'updatedBy']
1616
},
@@ -26,7 +26,7 @@ const topResources = {
2626
index: config.get('ES.DOCUMENTS.role.index'),
2727
type: config.get('ES.DOCUMENTS.role.type'),
2828
enrich: {
29-
policyName: 'role-policy',
29+
policyName: config.get('ES.DOCUMENTS.role.enrichPolicyName'),
3030
matchField: 'id',
3131
enrichFields: ['id', 'name', 'created', 'updated', 'createdBy', 'updatedBy']
3232
}
@@ -36,7 +36,7 @@ const topResources = {
3636
index: config.get('ES.DOCUMENTS.achievementprovider.index'),
3737
type: config.get('ES.DOCUMENTS.achievementprovider.type'),
3838
enrich: {
39-
policyName: 'achievementprovider-policy',
39+
policyName: config.get('ES.DOCUMENTS.achievementprovider.enrichPolicyName'),
4040
matchField: 'id',
4141
enrichFields: ['id', 'name', 'created', 'updated', 'createdBy', 'updatedBy']
4242
}
@@ -46,7 +46,7 @@ const topResources = {
4646
index: config.get('ES.DOCUMENTS.attributegroup.index'),
4747
type: config.get('ES.DOCUMENTS.attributegroup.type'),
4848
enrich: {
49-
policyName: 'attributegroup-policy',
49+
policyName: config.get('ES.DOCUMENTS.attributegroup.enrichPolicyName'),
5050
matchField: 'id',
5151
enrichFields: ['id', 'name', 'organizationId', 'created', 'updated', 'createdBy', 'updatedBy']
5252
},
@@ -62,7 +62,7 @@ const topResources = {
6262
index: config.get('ES.DOCUMENTS.skill.index'),
6363
type: config.get('ES.DOCUMENTS.skill.type'),
6464
enrich: {
65-
policyName: 'skill-policy',
65+
policyName: config.get('ES.DOCUMENTS.skill.enrichPolicyName'),
6666
matchField: 'id',
6767
enrichFields: ['id', 'skillProviderId', 'name', 'externalId', 'uri', 'created', 'updated', 'createdBy', 'updatedBy', 'skillprovider']
6868
},
@@ -77,7 +77,7 @@ const topResources = {
7777
index: config.get('ES.DOCUMENTS.attribute.index'),
7878
type: config.get('ES.DOCUMENTS.attribute.type'),
7979
enrich: {
80-
policyName: 'attribute-policy',
80+
policyName: config.get('ES.DOCUMENTS.attribute.enrichPolicyName'),
8181
matchField: 'id',
8282
enrichFields: ['id', 'name', 'attributeGroupId', 'created', 'updated', 'createdBy', 'updatedBy', 'attributegroup']
8383
},
@@ -168,7 +168,7 @@ const organizationResources = {
168168
propertyName: config.get('ES.DOCUMENTS.organizationskillprovider.orgField'),
169169
relateKey: 'skillProviderId',
170170
enrich: {
171-
policyName: 'organization-policy',
171+
policyName: config.get('ES.DOCUMENTS.organization.enrichPolicyName'),
172172
matchField: 'id',
173173
enrichFields: ['id', 'name', 'created', 'updated', 'createdBy', 'updatedBy', 'skillProviders']
174174
}

scripts/db/dumpDbToEs.js

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,88 @@ const RESOURCES_IN_ORDER = [
2929

3030
const client = getESClient()
3131

32+
const RESOURCE_NOT_FOUND = 'resource_not_found_exception'
33+
const INDEX_NOT_FOUND = 'index_not_found_exception'
34+
3235
/**
3336
* Cleans up the data in elasticsearch
3437
* @param {Array} keys Array of models
3538
*/
3639
async function cleanupES (keys) {
3740
const client = getESClient()
38-
await client.ingest.deletePipeline({
39-
id: topResources.user.pipeline.id
40-
})
41-
await client.ingest.deletePipeline({
42-
id: topResources.skillprovider.pipeline.id
43-
})
44-
await client.ingest.deletePipeline({
45-
id: topResources.attributegroup.pipeline.id
46-
})
47-
for (let i = 0; i < keys.length; i++) {
48-
const key = keys[i]
49-
if (models[key].tableName) {
50-
const esResourceName = modelToESIndexMapping[key]
51-
if (_.includes(_.keys(topResources), esResourceName)) {
52-
if (topResources[esResourceName].enrich) {
53-
await client.enrich.deletePolicy({
54-
name: topResources[esResourceName].enrich.policyName
55-
})
41+
try {
42+
await client.ingest.deletePipeline({
43+
id: topResources.user.pipeline.id
44+
})
45+
} catch (e) {
46+
if (e.meta && e.meta.body.error.type !== RESOURCE_NOT_FOUND) {
47+
throw e
48+
}
49+
}
50+
51+
try {
52+
await client.ingest.deletePipeline({
53+
id: topResources.skillprovider.pipeline.id
54+
})
55+
} catch (e) {
56+
if (e.meta && e.meta.body.error.type !== RESOURCE_NOT_FOUND) {
57+
throw e
58+
}
59+
}
60+
61+
try {
62+
await client.ingest.deletePipeline({
63+
id: topResources.attributegroup.pipeline.id
64+
})
65+
} catch (e) {
66+
if (e.meta && e.meta.body.error.type !== RESOURCE_NOT_FOUND) {
67+
throw e
68+
}
69+
}
70+
71+
try {
72+
for (let i = 0; i < keys.length; i++) {
73+
const key = keys[i]
74+
if (models[key].tableName) {
75+
const esResourceName = modelToESIndexMapping[key]
76+
if (_.includes(_.keys(topResources), esResourceName)) {
77+
if (topResources[esResourceName].enrich) {
78+
try {
79+
await client.enrich.deletePolicy({
80+
name: topResources[esResourceName].enrich.policyName
81+
})
82+
} catch (e) {
83+
if (e.meta && e.meta.body.error.type !== RESOURCE_NOT_FOUND) {
84+
throw e
85+
}
86+
}
87+
}
88+
89+
try {
90+
await client.indices.delete({
91+
index: topResources[esResourceName].index
92+
})
93+
} catch (e) {
94+
if (e.meta && e.meta.body.error.type !== INDEX_NOT_FOUND) {
95+
throw e
96+
}
97+
}
98+
} else if (_.includes(_.keys(organizationResources), esResourceName)) {
99+
try {
100+
await client.enrich.deletePolicy({
101+
name: organizationResources[esResourceName].enrich.policyName
102+
})
103+
} catch (e) {
104+
if (e.meta && e.meta.body.error.type !== RESOURCE_NOT_FOUND) {
105+
throw e
106+
}
107+
}
56108
}
57-
await client.indices.delete({
58-
index: topResources[esResourceName].index
59-
})
60-
} else if (_.includes(_.keys(organizationResources), esResourceName)) {
61-
await client.enrich.deletePolicy({
62-
name: organizationResources[esResourceName].enrich.policyName
63-
})
64109
}
65110
}
111+
} catch (e) {
112+
console.log(JSON.stringify(e))
113+
throw e
66114
}
67115
console.log('Existing data in elasticsearch has been deleted!')
68116
}

src/common/helper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ function readerToJson (reader) {
109109
toRealValue(r, setValue(name, []))
110110
r.stepOut()
111111
break
112+
case IonTypes.BOOL:
113+
setValue(name, r.booleanValue())
114+
break
112115
}
113116
nextT = reader.next()
114117
}

src/common/service-helper.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function getServiceMethods (Model, createSchema, patchSchema, searchSchema, buil
190190
async function patch (id, entity, auth, params) {
191191
await makeSureRefExist(entity)
192192

193-
const dbEntity = await get(id, auth, params)
193+
const dbEntity = await get(id, auth, params, {}, true)
194194
const newEntity = new Model()
195195
_.extend(newEntity, dbEntity, entity)
196196
newEntity.updated = new Date()
@@ -218,23 +218,26 @@ function getServiceMethods (Model, createSchema, patchSchema, searchSchema, buil
218218
* @param auth the auth obj
219219
* @param params the path parameters
220220
* @param query the query parameters
221+
* @param fromDb Should we bypass Elasticsearch for the record and fetch from db instead?
221222
* @return {Promise} the db device
222223
*/
223-
async function get (id, auth, params, query = {}) {
224+
async function get (id, auth, params, query = {}, fromDb = false) {
224225
let recordObj
225226
// Merge path and query params
226227
const trueParams = _.assign(params, query)
227-
try {
228-
const result = await esHelper.getFromElasticSearch(resource, id, auth, trueParams)
229-
// check permission
230-
permissionCheck(auth, result)
231-
return result
232-
} catch (err) {
233-
// return error if enrich fails or permission fails
234-
if ((resource === 'user' && trueParams.enrich) || (err.status && err.status === 403)) {
235-
throw errors.elasticSearchEnrichError(err.message)
228+
if (!fromDb) {
229+
try {
230+
const result = await esHelper.getFromElasticSearch(resource, id, auth, trueParams)
231+
// check permission
232+
permissionCheck(auth, result)
233+
return result
234+
} catch (err) {
235+
// return error if enrich fails or permission fails
236+
if ((resource === 'user' && trueParams.enrich) || (err.status && err.status === 403)) {
237+
throw errors.elasticSearchEnrichError(err.message)
238+
}
239+
logger.logFullError(err)
236240
}
237-
logger.logFullError(err)
238241
}
239242
if (_.isNil(trueParams) || _.isEmpty(trueParams)) {
240243
recordObj = await models.DBHelper.get(Model, id)
@@ -302,7 +305,7 @@ function getServiceMethods (Model, createSchema, patchSchema, searchSchema, buil
302305
*/
303306
async function remove (id, auth, params) {
304307
let payload
305-
await get(id, auth, params) // check exist
308+
await get(id, auth, params, {}, true) // check exist
306309
await models.DBHelper.delete(Model, id, buildQueryByParams(params))
307310
if (SUB_USER_DOCUMENTS[resource] || SUB_ORG_DOCUMENTS[resource]) {
308311
payload = _.assign({}, params)

0 commit comments

Comments
 (0)