2019-05-29 05:21:06 -04:00
|
|
|
const { User } = require('../../models/User')
|
2021-09-20 08:20:14 -04:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-08-16 09:08:03 -04:00
|
|
|
function _featuresChanged(newFeatures, featuresBefore) {
|
|
|
|
for (const feature in newFeatures) {
|
|
|
|
if (featuresBefore[feature] !== newFeatures[feature]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
module.exports = {
|
2020-01-07 06:03:14 -05:00
|
|
|
updateFeatures(userId, features, callback) {
|
2021-05-18 09:41:10 -04:00
|
|
|
const update = {
|
|
|
|
featuresUpdatedAt: new Date(),
|
|
|
|
}
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const key in features) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const value = features[key]
|
|
|
|
update[`features.${key}`] = value
|
|
|
|
}
|
2021-08-16 09:08:03 -04:00
|
|
|
User.findByIdAndUpdate(userId, update, (err, docBeforeUpdate) => {
|
|
|
|
let featuresChanged = false
|
|
|
|
if (docBeforeUpdate) {
|
|
|
|
featuresChanged = _featuresChanged(features, docBeforeUpdate.features)
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(err, features, featuresChanged)
|
|
|
|
})
|
2019-12-16 05:55:10 -05:00
|
|
|
},
|
|
|
|
|
2020-01-07 06:03:14 -05:00
|
|
|
overrideFeatures(userId, features, callback) {
|
2021-05-18 09:41:10 -04:00
|
|
|
const update = { features, featuresUpdatedAt: new Date() }
|
2021-08-16 09:08:03 -04:00
|
|
|
User.findByIdAndUpdate(userId, update, (err, docBeforeUpdate) => {
|
|
|
|
let featuresChanged = false
|
|
|
|
if (docBeforeUpdate) {
|
|
|
|
featuresChanged = _featuresChanged(features, docBeforeUpdate.features)
|
|
|
|
}
|
|
|
|
return callback(err, featuresChanged)
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-09-20 08:20:14 -04:00
|
|
|
|
|
|
|
module.exports.promises = promisifyAll(module.exports, {
|
|
|
|
multiResult: {
|
|
|
|
updateFeatures: ['features', 'featuresChanged'],
|
|
|
|
},
|
|
|
|
})
|