forked from picsoung/tf-lambda-limit-one-entry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
76 lines (67 loc) · 1.76 KB
/
handler.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
75
76
'use strict';
const tf = require('@typeform/api-client')
const typeform = tf.createClient({
token: process.env.TF_TOKEN
})
module.exports.addLimitLogic = async (event, context, callback) => {
console.log(JSON.parse(event.body))
// extract answer to be limited
const evt = JSON.parse(event.body)
const answer = evt.form_response.answers.find(a => a.field.id === process.env.FIELD_ID)
const type = answer.type
const new_value = answer[ type ]
const form_definition = await typeform.forms.get({uid: process.env.FORM_ID})
let { logic = {} } = form_definition
let new_action = {
"action": "jump",
"details": {
"to": {
"type": "field",
"value": process.env.ALREADY_FILLED_FIELD_REF
}
},
"condition": {
"op": "equal",
"vars": [
{
"type": "field",
"value": process.env.FIELD_REF
},
{
"type": "constant",
"value": new_value
}
]
}
}
//extract logic
if(logic.length){ //existing logic definition
// extract logic for this field
let jump = logic.find(l => l.type==="field" && l.ref===process.env.FIELD_REF)
// add condition to this jump
jump.actions.unshift(new_action)
}else{
form_definition.logic = {
type: "field",
ref: process.env.FIELD_ID,
actions:[new_action]
}
}
typeform.forms.update({
uid: process.env.FORM_ID,
override: true,
data: form_definition
}).then(response => {
console.log("RESPONSE", response)
}).catch(err => {
console.log('err', err)
})
const resp = {
statusCode: 200,
body: JSON.stringify({
message: 'Form updated',
input: event.body,
}),
};
callback(null, resp);
};