This repository has been 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
/
postChoir.js
108 lines (101 loc) · 3.09 KB
/
postChoir.js
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
const Nano = require('nano')
const debug = require('debug')('choirless')
const kuuid = require('kuuid')
let nano = null
let db = null
const DB_NAME = process.env.COUCH_CHOIRLESS_DATABASE
// create/edit a choir
// Parameters:
// - `choirId` - if omitted a new choir is generated.
// - `name` - name of choir.
// - `description` - description of choir.
// - `createdByUserId` - id of user creating the choir. (required for new choirs)
// - `createdByName` - name of user creating the choir. (required for new choirs)
// - `choirType` - one of `private`/`public`. (required for new choirs)
const postChoir = 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
let choirId = opts.choirId
const now = new Date()
let doc = {}
// check choirType is valid
if (opts.choirType && !['private', 'public'].includes(opts.choirType)) {
return {
body: { ok: false, message: 'invalid choirType' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
// is this a request to edit an existing choir
if (choirId) {
try {
debug('postChoir fetch choir', choirId)
doc = await db.get(choirId + ':0')
doc.name = opts.name ? opts.name : doc.name
doc.description = opts.description ? opts.description : doc.description
doc.choirType = opts.choirType ? opts.choirType : doc.choirType
} catch (e) {
return {
body: { ok: false, message: 'choir not found' },
statusCode: 404,
headers: { 'Content-Type': 'application/json' }
}
}
} else {
if (!opts.name || !opts.createdByUserId || !opts.createdByName || !opts.choirType) {
return {
body: { ok: false, message: 'missing mandatory parameters name/createdByUserId/createdByName/choirType' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
choirId = kuuid.id()
doc = {
_id: choirId + ':0',
type: 'choir',
choirId: choirId,
name: opts.name,
description: opts.description,
choirType: opts.choirType,
createdOn: now.toISOString(),
createdByUserId: opts.createdByUserId,
createdByName: opts.createdByName
}
}
// write user to database
let statusCode = 200
let body = null
try {
debug('postChoir write choir', doc)
await db.insert(doc)
// if this is the creation of a new choir
if (!doc._rev) {
// add the choir creator as a member
const member = {
_id: choirId + ':member:' + opts.createdByUserId,
type: 'choirmember',
choirId: choirId,
userId: opts.createdByUserId,
joined: now.toISOString(),
name: opts.createdByName,
memberType: 'leader'
}
await db.insert(member)
}
body = { ok: true, choirId: choirId }
} catch (e) {
body = { ok: false }
statusCode = 404
}
// return API response
return {
body: body,
statusCode: statusCode,
headers: { 'Content-Type': 'application/json' }
}
}
module.exports = postChoir