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 pathgetRenderDone.js
More file actions
60 lines (55 loc) · 1.45 KB
/
getRenderDone.js
File metadata and controls
60 lines (55 loc) · 1.45 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
const Nano = require('nano')
const debug = require('debug')('choirless')
let nano = null
let db = null
// get render done
// choirId - the id of the choir
// songId - the id of the song
const getRenderDone = async (opts) => {
// connect to db - reuse connection if present
if (!db) {
nano = Nano(process.env.COUCH_URL)
db = nano.db.use(process.env.COUCH_RENDER_DATABASE)
}
// check mandatory parameters
if (!opts.choirId || !opts.songId) {
return {
body: { ok: false, message: 'missing mandatory parameters' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
// get render status doc
let statusCode = 200
let body = {}
try {
debug('getRenderDone', opts.choirId, opts.songId)
const query = {
selector: {
status: 'done',
choirId: opts.choirId,
songId: opts.songId
},
sort: [
{ choirId: 'desc' },
{ songId: 'desc' },
{ date: 'desc' }
],
limit: 50,
use_index: 'find/completedRenders',
execution_stats: true
}
const result = await db.find(query)
body.renders = result.docs.map((d) => { delete d._id; delete d._rev; return d })
} catch (e) {
body = { ok: false, err: 'Failed to fetch completed renders' }
statusCode = 404
}
// return API response
return {
body: body,
statusCode: statusCode,
headers: { 'Content-Type': 'application/json' }
}
}
module.exports = getRenderDone