This repository was archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (129 loc) · 3.12 KB
/
index.js
File metadata and controls
150 lines (129 loc) · 3.12 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const jsonErrorHandler = require('express-json-error-handler').default;
require('dotenv').config()
const zammadClient = require('./zammad');
const zammad = new zammadClient();
const assert = require('assert')
const Evaluation = require('./evaluation');
const eval = new Evaluation();
// Prio definition
const prio_medium = 16;
const prio_high = 21;
const app = express();
app.use(express.json());
app.use(jsonErrorHandler());
app.use(morgan('combined'));
zammad.listUsers()
.then(() => console.log('zammad works'));
app.use(cors());
app.get('/', (req, res) => {
res.json({});
});
if (process.env.ZAMMAD_DB_ENABLED) {
const zamamdDB = require('./zammad.db')
const db = new zamamdDB()
app.get('/ticket/:id', async (req, res) => {
const ticketId = req.params.id
const position = await db.getPosition(ticketId)
res.json({ ticketId, position })
})
} else {
app.get('/ticket/:id', async (req, res) => {
res.status(500).send('Position query not enabled')
})
}
/**
* Create new ticket
*
* POST with body:
*
* {
* q1: "q1_option2",
* q2: "q2_option0",
* q3: "q3_option1",
* ...
* }
*/
app.options('/evaluate', cors()); // Pre-Flight
app.post('/evaluate', async (req, res) => {
let answers = req.body
try {
assert.ok(answers, 'answers not present')
const evaluation = await eval.evaluate(answers)
res.json(evaluation)
} catch (err) {
res.status(500).send(err.message)
}
})
/**
* Create new ticket
*
* POST with body:
*
* {
* meta: {
* firstname: string,
* lastname: string,
* phone: string
* },
* answers: {
* q1: 2,
* q2: 3,
* q3: 2,
* ...
* }
* }
*/
app.options('/ticket', cors()); // Pre-Flight
app.post('/ticket', async (req, res) => {
try {
let user = req.body.meta
//TODO: validate answers
let answers = req.body.answers
assert.ok(answers, 'answers not present')
assert.ok(user, 'meta not present')
assert.ok(user.phone, 'phone not present')
user = await zammad.createUser(user)
assert.ok(user.id, 'user creation failed');
const { medical_priority, note, tags } = await eval.evaluate(answers)
let priority = "1 low";
if (medical_priority > prio_medium) {
priority = "2 medium";
}
if (medical_priority > prio_high) {
priority = "3 high";
}
const response = await zammad.createTicket({
"title": "Rückrufwunsch Corona-Hotline Prio " + medical_priority,
"group": "Users",
"customer_id": user.id,
"priority_id": priority,
"article": {
"subject": "Rückrufwunsch Corona-Hotline Prio " + medical_priority,
"body": note,
"type": "note",
"internal": false
},
med_prio: medical_priority,
})
assert.ok(response.id, 'ticket creation failed');
// Create Tags
for (t of tags) {
await zammad.addTag(response.id, t)
}
res.json({
id: response.id,
priority: response.priority_id,
med_prio: response.med_prio,
tags: tags
})
} catch (error) {
console.error(error)
res.status(500).json({ error: error.message });
}
})
app.listen(process.env.PORT || 3000, () => {
console.debug('App listening on :3000');
});