forked from shihjay2/nosh3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.mjs
174 lines (170 loc) · 5.19 KB
/
api.mjs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import dotenv from 'dotenv'
dotenv.config()
import express from 'express'
import moment from 'moment'
import objectPath from 'object-path'
import PouchDB from 'pouchdb'
import settings from './settings.mjs'
import { v4 as uuidv4 } from 'uuid'
import { eventAdd, eventUser, isMarkdown, pollSet, sync, urlFix, verifyJWT } from './core.mjs'
import { Worker } from 'node:worker_threads'
const router = express.Router()
import PouchDBFind from 'pouchdb-find'
PouchDB.plugin(PouchDBFind)
export default router
router.get('/:pid/Timeline', verifyJWT, getTimeline)
router.put('/:pid/md', verifyJWT, putMarkdown)
async function getTimeline(req, res) {
let prefix = ''
if (process.env.INSTANCE === 'digitalocean' && process.env.NOSH_ROLE === 'patient') {
prefix = req.params.pid + '_'
}
const process_db_remote = new PouchDB(urlFix(settings.couchdb_uri) + 'timeline_process', settings.couchdb_auth)
if (Object.keys(req.query).length === 0) {
await sync('timeline', req.params.pid)
const db = new PouchDB(prefix + 'timeline')
const timeline_result = await db.allDocs({
include_docs: true,
attachments: true,
startkey: 'nosh_'
})
if (timeline_result.rows.length > 0) {
const timeline = objectPath.get(timeline_result, 'rows.0.doc.timeline')
await process_db_remote.info()
const id = 'nosh_' + uuidv4()
await process_db_remote.put({
_id: id,
status: 'pending',
pid: req.params.pid,
timestamp: moment().unix(),
data: '',
})
const opts = {
pid: req.params.pid,
process_id: id,
prefix: prefix,
timeline: timeline
}
res.status(200).json({process: id})
const worker = new Worker('./worker.mjs', { workerData: opts })
worker.on('message', (result) => {
console.log(result)
})
worker.on('error', (error) => {
console.log('worker error')
console.log(error)
})
} else {
res.sendStatus(404)
}
} else {
if (objectPath.has(req, 'query.process')) {
try {
const process_doc = await process_db_remote.get(req.query.process)
if (process_doc.status === 'pending') {
res.sendStatus(404)
} else {
res.status(200)
res.setHeader('Content-type', "text/markdown")
res.setHeader('Content-disposition', 'attachment; filename=nosh_timeline_' + Date.now() + '.md')
res.send(atob(objectPath.get(process_doc, 'data')))
}
} catch (e) {
console.log(e)
res.status(401).send('Unauthorized')
}
} else {
console.log('no query item')
res.status(401).send('Unauthorized')
}
}
}
async function putMarkdown(req, res) {
let prefix = ''
if (process.env.INSTANCE === 'digitalocean' && process.env.NOSH_ROLE === 'patient') {
prefix = req.params.pid + '_'
}
await sync('document_references', req.params.pid)
const db = new PouchDB(prefix + 'document_references')
const db_binary = new PouchDB(urlFix(settings.couchdb_uri) + prefix + 'binaries', settings.couchdb_auth)
const id = 'nosh_' + uuidv4()
const binary_id = 'nosh_' + uuidv4()
const md = req.body.content
if (isMarkdown(md)) {
const doc = {
"_id": id,
"resourceType": "DocumentReference",
"id": id,
"status": "current",
"subject": {
"reference": 'Patient/' + req.params.pid
},
"date": moment().format('YYYY-MM-DD HH:mm'),
"type": {
"coding": [
{
"system": "http://loinc.org",
"code": "103140-0",
"display": "Personal Health Attachment Document"
}
]
},
"category": [{
"coding": [
{
"system": "http://loinc.org",
"code": "103140-0",
"display": "Personal Health Monitoring Report Document"
}
]
}],
"description": "AI Chat",
"content": [
{
"attachment": {
"contentType": "text/plain; charset=utf-8",
"url": "Binary/" + binary_id
}
}
]
}
const binary_doc = {
"resourceType": "Binary",
"id": binary_id,
"_id": binary_id,
"contentType": "text/plain; charset=utf-8",
"data": btoa(md)
}
try {
const body = await db.put(doc)
await db_binary.put(binary_doc)
await sync('document_references', req.params.pid)
let opts = {
doc_db: 'document_references',
doc_id: id,
diff: null
}
opts = await eventUser(res, opts, prefix)
await eventAdd('Updated document reference', opts, req.params.pid)
let binary_opts = {
doc_db: 'binaries',
doc_id: binary_id,
diff: null
}
binary_opts = await eventUser(res, binary_opts, prefix)
await eventAdd('Updated binary', binary_opts, req.params.pid)
await pollSet(req.params.pid, 'document_references')
await pollSet(req.params.pid, 'binaries')
res.set('ETag', 'W/"' + body.rev + '"')
res.status(200).json(body)
} catch(err) {
console.log(err)
res.status(200).json(err)
}
} else {
res.status(200).json({
"error": true,
"message": "not markdown document"
})
}
}