This repository was archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgetChoirMembers.js
More file actions
61 lines (56 loc) · 1.39 KB
/
Copy pathgetChoirMembers.js
File metadata and controls
61 lines (56 loc) · 1.39 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
const Nano = require('nano')
const debug = require('debug')('choirless')
let nano = null
let db = null
const DB_NAME = process.env.COUCH_CHOIRLESS_DATABASE
// fetch choir members
// Parameters:
// - `choirId` - the choir to fetch
const getChoirMembers = async (opts) => {
// connect to db - reuse connection if present
if (!db) {
nano = Nano(process.env.COUCH_URL)
db = nano.db.use(DB_NAME)
}
// extract parameters
const choirId = opts.choirId
if (!choirId) {
return {
body: { ok: false, message: 'missing mandatory parameter choirId' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
// fetch user from database
let statusCode = 200
let body = null
try {
debug('getChoirMembers', choirId)
const query = {
selector: {
type: 'choirmember'
}
}
const response = await db.partitionedFind(choirId, query)
body = {
ok: true,
members: response.docs.map((m) => {
delete m._id
delete m._rev
// infer userType if missing
m.userType = m.userType ? m.userType : 'regular'
return m
})
}
} catch (e) {
body = { ok: false, message: 'choir not found' }
statusCode = 404
}
// return API response
return {
body: body,
statusCode: statusCode,
headers: { 'Content-Type': 'application/json' }
}
}
module.exports = getChoirMembers