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
/
postRender.js
69 lines (63 loc) · 1.81 KB
/
postRender.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
const Nano = require('nano')
const debug = require('debug')('choirless')
const kuuid = require('kuuid')
let nano = null
let db = null
// create a render status object
// choirId - the id of the choir
// songId - the id of the song
// partId - the id of the part the triggered the render
// status - the status of the render new/converted/aligned/rendered/composited/done
const postRender = 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 for mandatory fields
if (!opts.choirId || !opts.songId) {
return {
body: { ok: false, message: 'missing mandatory fields' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
// check the status field
if (!opts.status) {
opts.status = 'new'
}
if (!['new', 'converted', 'aligned', 'rendered', 'composited', 'done'].includes(opts.status)) {
return {
body: { ok: false, message: 'invalid status' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
// write render doc
let statusCode = 200
let body = {}
try {
const doc = {
_id: [opts.choirId, opts.songId, kuuid.id()].join(':'),
choirId: opts.choirId,
songId: opts.songId,
partId: opts.partId || null,
status: opts.status,
date: new Date().toISOString()
}
debug('postRender write render status', doc)
await db.insert(doc)
statusCode = 200
body = { ok: true, id: doc._id }
} catch (e) {
body = { ok: false, err: 'Failed to create render status' }
statusCode = 404
}
// return API response
return {
body: body,
statusCode: statusCode,
headers: { 'Content-Type': 'application/json' }
}
}
module.exports = postRender