Make get_session() readonly and throw an exception if session cannot be found rather...
[ndcode_site.git] / api / navigation / get.json.jst
1 let jst_server = (await import('@ndcode/jst_server')).default
2
3 return async env => {
4   let get_account = await _require('/_lib/get_account.jst')
5   let get_session = await _require('/_lib/get_session.jst')
6   let post_request = await _require('/_lib/post_request.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         let root = await transaction.get()
16         let session = await get_session(env, root)
17
18         let account = await get_account(root, session)
19         if (!await account.get_json('administrator'))
20           throw new jst_server.Problem(
21             'Unauthorized',
22             'Not administrator.',
23             401
24           )
25
26         return /*await*/ root.get_json('navigation', {})
27       }
28       finally {
29         transaction.rollback()
30       }
31     }
32   )
33 }