-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
executable file
·67 lines (55 loc) · 1.83 KB
/
utils.ts
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
import { Request, Response, NextFunction } from "express"
import { authUseridentifiers, dbUserIdentifiers } from "./config"
import createHttpError from "http-errors"
export const get_current_user_id = (req: Request, res: Response) => {
const current_user = res.locals?.user ?? res.locals?.user?.properties
if (!current_user) return
const aui = authUseridentifiers.find((aui) => current_user[aui])
if (!aui) return
return current_user[aui]
}
export const batch_items = (batch_size: number) => `
// Aggregation
WITH
COLLECT(PROPERTIES(item)) as items,
COUNT(item) as count,
toInteger($start_index) as start_index,
toInteger($batch_size) as batch_size,
(toInteger($start_index) + toInteger($batch_size)) as end_index
// Batching
// Note: if end_index is -1, returns all except last
RETURN
count,
${
batch_size > 0 ? "items[start_index..end_index] AS batch" : "items AS batch"
},
start_index,
batch_size
`
export const format_batched_response = (records: any) => {
const record = records[0]
if (!record) throw createHttpError(400, "Query did not yield any match")
const items = record.get("batch")
items.forEach((item: any) => {
if (item.password_hashed) delete item.password_hashed
})
return {
batch_size: record.get("batch_size"),
start_index: record.get("start_index"),
count: record.get("count"),
items,
}
}
export const errorHandler = (
error: any,
req: Request,
res: Response,
next: NextFunction
) => {
console.error(error)
let { statusCode = 500, message = error } = error
if (isNaN(statusCode) || statusCode > 600) statusCode = 500
res.status(statusCode).send(message)
}
export const getCypherUserIdentifiers = (name: string = "user") =>
`[${dbUserIdentifiers.map((i) => `${name}.${i}`).join(",")}]`