This repository was archived by the owner on Mar 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (71 loc) · 2.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Lambda function to sync user skills
*/
const _ = require('lodash')
const config = require('config')
const helper = require('./src/common/helper')
/**
* Function to sync user skill
* @param {String} userId the userId
* @param {String} tagId the tag id
* @param {Number} score the skill score
* @param {String} taxonomyId the taxonomy id
* @param {boolean} isDelete if delete the skill
* @returns {Promise}
*/
async function syncUserSkill(userId, tagId, score, taxonomyId, isDelete) {
const name = await helper.getTagName(tagId)
const skill = await helper.getV5SkillResource('skills', { taxonomyId, name })
const skillExist = await helper.checkUserSkillExist(userId, skill.id)
if (isDelete && skillExist) {
helper.deleteUserSkill(userId, skill.id)
} else if (skillExist) {
await helper.updateUserSkill(userId, score, skill.id)
} else {
await helper.createUserSkill(userId, score, skill.id)
}
}
/**
* Sync member skills
* @param {Object} event the event object
*/
module.exports.handle = async (event) => {
try {
console.log(`Received event: `, JSON.stringify(event))
const taxonomy = await helper.getV5SkillResource('taxonomies', { name: config.TAXONOMY_NAME })
for (const record of event.Records) {
try {
const handle = _.get(record, 'dynamodb.NewImage.userHandle.S')
const newSkills = JSON.parse(_.get(record, 'dynamodb.NewImage.skills.S', '{}'))
const oldSkills = JSON.parse(_.get(record, 'dynamodb.OldImage.skills.S', '{}'))
const createSkills = _.omit(newSkills, _.keys(oldSkills))
const deleteSkills = _.omit(oldSkills, _.keys(newSkills))
const updateSkills = _.filter(_.pick(newSkills, _.keys(oldSkills)), (v, k) => oldSkills[k].score !== v.score)
if (_.isEmpty(createSkills) && _.isEmpty(deleteSkills) && _.isEmpty(updateSkills)) {
console.log(`there are no skills to create, update or delete for user ${handle}`)
continue
}
console.log('Skills to create:', JSON.stringify(createSkills))
console.log('Skills to update:', JSON.stringify(updateSkills))
console.log('Skills to delete:', JSON.stringify(deleteSkills))
const user = await helper.getUser(handle)
for (const key of _.keys(createSkills)) {
await syncUserSkill(user.id, key, _.get(createSkills,`${key}.score`, 0), taxonomy.id)
await helper.sleep()
}
for (const key of _.keys(updateSkills)) {
await syncUserSkill(user.id, key, _.get(createSkills,`${key}.score`, 0), taxonomy.id)
await helper.sleep()
}
for (const key of _.keys(deleteSkills)) {
await syncUserSkill(user.id, key, 0, taxonomy.id, true)
await helper.sleep()
}
} catch (e) {
console.log(`sync user's skills failed. Error: ${e.message}`)
}
}
} catch (e) {
console.log(`some error occured: ${e.message}`)
}
}