04d64eda2289bae2fd2e314e40faa07c02412c4f
[ndcode_site.git] / api / globals / get.json.jst
1 let XDate = require('xdate')
2
3 return async env => {
4   let post_request = await _require('/_lib/post_request.jst')
5   let get_session = await _require('/_lib/get_session.jst')
6   let Problem = await _require('/_lib/Problem.jst')
7
8   await post_request(
9     // env
10     env,
11     // handler
12     async () => {
13       let transaction = await env.site.database.Transaction()
14       try {
15         // initialize env.session_key, set cookie in env.response
16         await get_session(env, transaction)
17         if (env.signed_in_as === null)
18           throw new Problem('Unauthorized', 'Please sign in first.', 401)
19
20         let root = await transaction.get({})
21         let account = await (
22           await root.get('accounts', {})
23         ).get(env.signed_in_as)
24         if (!await account.get_json('administrator'))
25           throw new Problem('Unauthorized', 'Not administrator.', 401)
26
27         globals = await root.get_json('globals', {})
28
29         await transaction.commit()
30         return globals
31       }
32       catch (error) {
33         transaction.rollback()
34         throw error
35       }
36     }
37   )
38 }