-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
59 lines (51 loc) · 1.67 KB
/
handler.js
File metadata and controls
59 lines (51 loc) · 1.67 KB
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
const mongoose = require('mongoose')
const { keys, difference, set, size } = require('lodash')
let connection = null
module.exports.translate = async (event, context) => {
const { mongoURI, _id, md, category, params, args } = event
if (!mongoURI) {
throw new Error('Mongo URI not defined')
}
context.callBackWaitsForEmptyEventLoop = false
if (connection == null) {
connection = await mongoose.createConnection(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
bufferCommands: false,
bufferMaxEntries: 0
})
const schema = require('./schema')
connection.model('Translation', schema)
}
const model = connection.model('Translation')
if (!args.language) {
return await model.findById(_id)
}
const fields = { params: 1 }
fields[args.language] = 1
const translation = await model.findById(_id).select(fields)
if (!args.skipSettings) {
if (!translation) {
model.create({ _id, md, category, params: keys(params) })
return { _id }
} else {
const modifier = {}
if (md !== translation.md) {
md ? set(modifier, '$set.md', md) : set(modifier, '$unset.md', '')
}
const newParams = difference(keys(params), translation.params)
if (newParams.length) {
newParams.forEach(param => set(modifier, '$addToSet.params', param))
}
// maybe update category here too (for rare cases?)
if (size(modifier) && _id) {
// console.log(
// `Translation ${_id} updated with ${JSON.stringify(modifier)}`
// )
model.update({ _id }, modifier)
}
}
}
model.update({ _id }, { $inc: { views: 1 } })
return translation
}