forked from OHDSI/WebAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconceptset.js
More file actions
348 lines (304 loc) · 12.3 KB
/
Copy pathconceptset.js
File metadata and controls
348 lines (304 loc) · 12.3 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { Router } from 'express'
import db from '../db.js'
import { getPool, getSource } from '../sources.js'
import config from '../config.js'
const router = Router()
// --- helpers ---
function formatDate (ms) {
if (!ms) return null
return new Date(ms).toISOString()
}
function toUserRef (login) {
if (!login) return null
return { id: 0, login, name: login }
}
function rowToDto (row) {
return {
id: row.id,
name: row.name,
description: row.description || null,
createdBy: toUserRef(row.created_by),
createdDate: formatDate(row.created_date),
modifiedBy: toUserRef(row.modified_by),
modifiedDate: formatDate(row.modified_date),
tags: [],
hasReadAccess: true,
hasWriteAccess: true
}
}
function defaultVocabSource () {
const s = config.sources[0]
if (!s) throw Object.assign(new Error('No vocabulary source configured'), { status: 503 })
return s
}
// Fetch concept details from the vocabulary source for a list of integer concept IDs.
async function fetchConceptDetails (source, conceptIds) {
if (conceptIds.length === 0) return {}
const pool = getPool(source.sourceKey)
const idList = conceptIds.join(',')
const result = await pool.request().query(
`SELECT CONCEPT_ID, CONCEPT_NAME, ISNULL(STANDARD_CONCEPT,'N') STANDARD_CONCEPT,
ISNULL(INVALID_REASON,'V') INVALID_REASON, CONCEPT_CODE, CONCEPT_CLASS_ID,
DOMAIN_ID, VOCABULARY_ID, VALID_START_DATE, VALID_END_DATE
FROM ${source.vocabSchema}.concept
WHERE CONCEPT_ID IN (${idList})`
)
const SC_CAPTION = { S: 'Standard', C: 'Classification', N: 'Non-Standard' }
const IR_CAPTION = { V: 'Valid', D: 'Deleted', U: 'Updated' }
const map = {}
for (const row of result.recordset) {
const d = row.VALID_START_DATE
const e = row.VALID_END_DATE
const sc = row.STANDARD_CONCEPT || 'N'
const ir = row.INVALID_REASON || 'V'
map[row.CONCEPT_ID] = {
CONCEPT_ID: row.CONCEPT_ID,
CONCEPT_NAME: row.CONCEPT_NAME,
STANDARD_CONCEPT: sc,
STANDARD_CONCEPT_CAPTION: SC_CAPTION[sc] || 'Non-Standard',
INVALID_REASON: ir,
INVALID_REASON_CAPTION: IR_CAPTION[ir] || ir,
CONCEPT_CODE: row.CONCEPT_CODE,
CONCEPT_CLASS_ID: row.CONCEPT_CLASS_ID,
DOMAIN_ID: row.DOMAIN_ID,
VOCABULARY_ID: row.VOCABULARY_ID,
VALID_START_DATE: d instanceof Date ? d.toISOString().slice(0, 10) : String(d || '').slice(0, 10),
VALID_END_DATE: e instanceof Date ? e.toISOString().slice(0, 10) : String(e || '').slice(0, 10)
}
}
return map
}
// Build ConceptSetExpression from stored items + looked-up concept details.
async function buildExpression (conceptSetId, source) {
const items = db.prepare('SELECT * FROM concept_set_item WHERE concept_set_id = ?').all(conceptSetId)
const conceptIds = items.map(i => i.concept_id)
const details = await fetchConceptDetails(source, conceptIds)
return {
items: items.map(i => ({
concept: details[i.concept_id] || { CONCEPT_ID: i.concept_id },
isExcluded: Boolean(i.is_excluded),
includeDescendants: Boolean(i.include_descendants),
includeMapped: Boolean(i.include_mapped)
}))
}
}
// --- static routes (must precede /:id) ---
// POST /check — tag/integrity diagnostics; we enforce no tag rules so always clean
router.post('/check', (_req, res) => {
res.json({ warnings: [] })
})
// GET /exportlist
router.get('/exportlist', (_req, res) => {
res.json([])
})
// GET /
router.get('/', (_req, res) => {
const rows = db.prepare('SELECT * FROM concept_set ORDER BY id DESC').all()
res.json(rows.map(rowToDto))
})
// POST /
router.post('/', (req, res) => {
const { name, description } = req.body || {}
if (!name) return res.status(400).json({ message: 'name is required' })
const login = req.user ? req.user.login : 'anonymous'
const result = db.prepare(
'INSERT INTO concept_set (name, description, created_by) VALUES (?, ?, ?)'
).run(name, description || null, login)
const row = db.prepare('SELECT * FROM concept_set WHERE id = ?').get(result.lastInsertRowid)
res.status(201).json(rowToDto(row))
})
// --- single concept set ---
// GET /:id
router.get('/:id', (req, res) => {
const row = db.prepare('SELECT * FROM concept_set WHERE id = ?').get(req.params.id)
if (!row) return res.status(404).json({ message: 'Concept set not found' })
res.json(rowToDto(row))
})
// PUT /:id
router.put('/:id', (req, res) => {
const row = db.prepare('SELECT * FROM concept_set WHERE id = ?').get(req.params.id)
if (!row) return res.status(404).json({ message: 'Concept set not found' })
const { name, description } = req.body || {}
const login = req.user ? req.user.login : 'anonymous'
db.prepare(
`UPDATE concept_set SET name = ?, description = ?, modified_by = ?,
modified_date = (unixepoch() * 1000) WHERE id = ?`
).run(name || row.name, description !== undefined ? description : row.description, login, row.id)
const updated = db.prepare('SELECT * FROM concept_set WHERE id = ?').get(row.id)
res.json(rowToDto(updated))
})
// DELETE /:id
router.delete('/:id', (req, res) => {
const row = db.prepare('SELECT id FROM concept_set WHERE id = ?').get(req.params.id)
if (!row) return res.status(404).json({ message: 'Concept set not found' })
db.prepare('DELETE FROM concept_set WHERE id = ?').run(row.id)
res.sendStatus(204)
})
// --- copy name ---
// GET /:id/copy-name
router.get('/:id/copy-name', (req, res) => {
const row = db.prepare('SELECT name FROM concept_set WHERE id = ?').get(req.params.id)
if (!row) return res.status(404).json({ message: 'Concept set not found' })
res.json({ copyName: `Copy of ${row.name}` })
})
// --- exists check ---
// GET /:id/exists?name=...
router.get('/:id/exists', (req, res) => {
const name = req.query.name
const id = parseInt(req.params.id, 10)
const row = db.prepare('SELECT COUNT(*) AS cnt FROM concept_set WHERE name = ? AND id != ?').get(name, id)
res.json(row.cnt)
})
// --- items ---
// GET /:id/items
router.get('/:id/items', (req, res) => {
const cs = db.prepare('SELECT id FROM concept_set WHERE id = ?').get(req.params.id)
if (!cs) return res.status(404).json({ message: 'Concept set not found' })
const items = db.prepare('SELECT * FROM concept_set_item WHERE concept_set_id = ?').all(req.params.id)
res.json(items.map(i => ({
id: i.id,
conceptSetId: i.concept_set_id,
conceptId: i.concept_id,
isExcluded: Boolean(i.is_excluded),
includeDescendants: Boolean(i.include_descendants),
includeMapped: Boolean(i.include_mapped)
})))
})
// PUT /:id/items
router.put('/:id/items', (req, res) => {
const cs = db.prepare('SELECT id FROM concept_set WHERE id = ?').get(req.params.id)
if (!cs) return res.status(404).json({ message: 'Concept set not found' })
const incomingItems = req.body || []
const saveItems = db.transaction((csId, items) => {
db.prepare('DELETE FROM concept_set_item WHERE concept_set_id = ?').run(csId)
const insert = db.prepare(
`INSERT INTO concept_set_item (concept_set_id, concept_id, is_excluded, include_descendants, include_mapped)
VALUES (?, ?, ?, ?, ?)`
)
for (const item of items) {
insert.run(
csId,
Number(item.conceptId || item.concept_id),
item.isExcluded || item.is_excluded ? 1 : 0,
item.includeDescendants || item.include_descendants ? 1 : 0,
item.includeMapped || item.include_mapped ? 1 : 0
)
}
db.prepare(
`UPDATE concept_set SET modified_date = (unixepoch() * 1000) WHERE id = ?`
).run(csId)
return db.prepare('SELECT * FROM concept_set_item WHERE concept_set_id = ?').all(csId)
})
const saved = saveItems(cs.id, incomingItems)
res.json(saved.map(i => ({
id: i.id,
conceptSetId: i.concept_set_id,
conceptId: i.concept_id,
isExcluded: Boolean(i.is_excluded),
includeDescendants: Boolean(i.include_descendants),
includeMapped: Boolean(i.include_mapped)
})))
})
// --- expression ---
// GET /:id/expression
router.get('/:id/expression', async (req, res, next) => {
try {
const cs = db.prepare('SELECT id FROM concept_set WHERE id = ?').get(req.params.id)
if (!cs) return res.status(404).json({ message: 'Concept set not found' })
const source = defaultVocabSource()
res.json(await buildExpression(cs.id, source))
} catch (err) { next(err) }
})
// GET /:id/expression/:sourceKey
router.get('/:id/expression/:sourceKey', async (req, res, next) => {
try {
const cs = db.prepare('SELECT id FROM concept_set WHERE id = ?').get(req.params.id)
if (!cs) return res.status(404).json({ message: 'Concept set not found' })
const source = getSource(req.params.sourceKey)
res.json(await buildExpression(cs.id, source))
} catch (err) { next(err) }
})
// GET /:id/version/:version/expression
router.get('/:id/version/:version/expression', async (req, res, next) => {
try {
const ver = db.prepare(
`SELECT expression FROM version WHERE entity_type = 'conceptset' AND entity_id = ? AND version = ?`
).get(req.params.id, req.params.version)
if (!ver || !ver.expression) return res.status(404).json({ message: 'Version not found' })
res.json(JSON.parse(ver.expression))
} catch (err) { next(err) }
})
// --- annotation stubs ---
// Annotations are user notes attached to concept sets; we don't implement the feature
// but Atlas calls these endpoints and treats a 404 as an error.
router.get('/:id/annotation', (_req, res) => res.json([]))
router.put('/:id/annotation', (_req, res) => res.json([]))
router.delete('/:id/annotation/:annotationId', (_req, res) => res.sendStatus(204))
// --- generation info (stub) ---
// GET /:id/generationinfo
router.get('/:id/generationinfo', (req, res) => {
res.json([])
})
// --- version endpoints ---
const ENTITY_TYPE = 'concept_set'
function versionToDto (row) {
return {
id: row.id,
entityId: row.entity_id,
version: row.version,
description: row.description || null,
archived: !!row.is_archived,
createdBy: { id: 0, login: row.created_by, name: row.created_by },
createdDate: row.created_date ? new Date(row.created_date).toISOString() : null,
hasWriteAccess: true
}
}
function nextVersion (entityId) {
return ((db.prepare(
'SELECT MAX(version) AS v FROM version WHERE entity_type = ? AND entity_id = ?'
).get(ENTITY_TYPE, entityId))?.v ?? 0) + 1
}
router.get('/:id/version', (req, res) => {
const rows = db.prepare(
'SELECT * FROM version WHERE entity_type = ? AND entity_id = ? ORDER BY version DESC'
).all(ENTITY_TYPE, req.params.id)
res.json(rows.map(versionToDto))
})
router.post('/:id/version', (req, res) => {
const items = db.prepare('SELECT * FROM concept_set_item WHERE concept_set_id = ?').all(req.params.id)
const user = req.user?.login || 'anonymous'
const ver = nextVersion(req.params.id)
db.prepare(
'INSERT INTO version (entity_type, entity_id, version, expression, description, created_by) VALUES (?, ?, ?, ?, ?, ?)'
).run(ENTITY_TYPE, req.params.id, ver, JSON.stringify(items), req.body?.description || null, user)
const saved = db.prepare(
'SELECT * FROM version WHERE entity_type = ? AND entity_id = ? AND version = ?'
).get(ENTITY_TYPE, req.params.id, ver)
res.status(201).json(versionToDto(saved))
})
router.get('/:id/version/:ver', (req, res) => {
const row = db.prepare(
'SELECT * FROM version WHERE entity_type = ? AND entity_id = ? AND version = ?'
).get(ENTITY_TYPE, req.params.id, req.params.ver)
if (!row) return res.status(404).json({ message: 'Version not found' })
res.json(versionToDto(row))
})
router.put('/:id/version/:ver', (req, res) => {
const { description, archived } = req.body
const result = db.prepare(
'UPDATE version SET description = ?, is_archived = ? WHERE entity_type = ? AND entity_id = ? AND version = ?'
).run(description || null, archived ? 1 : 0, ENTITY_TYPE, req.params.id, req.params.ver)
if (!result.changes) return res.status(404).json({ message: 'Version not found' })
const row = db.prepare(
'SELECT * FROM version WHERE entity_type = ? AND entity_id = ? AND version = ?'
).get(ENTITY_TYPE, req.params.id, req.params.ver)
res.json(versionToDto(row))
})
router.delete('/:id/version/:ver', (req, res) => {
const result = db.prepare(
'DELETE FROM version WHERE entity_type = ? AND entity_id = ? AND version = ?'
).run(ENTITY_TYPE, req.params.id, req.params.ver)
if (!result.changes) return res.status(404).json({ message: 'Version not found' })
res.sendStatus(200)
})
export default router