Make get_session() readonly and throw an exception if session cannot be found rather...
[ndcode_site.git] / api / nodemailers / set.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 nodemailers => {
13       // do not bother trying to coerce and/or validate
14       // too complex and nested (do it when we have an automated routine)
15
16       let transaction = await env.site.database.Transaction()
17       try {
18         let root = await transaction.get()
19         let session = await get_session(env, root)
20
21         let account = await get_account(root, session)
22         if (!await account.get_json('administrator'))
23           throw new jst_server.Problem(
24             'Unauthorized',
25             'Not administrator.',
26             401
27           )
28
29         root.set_json('nodemailers', nodemailers)
30         await transaction.commit()
31       }
32       catch (error) {
33         transaction.rollback()
34         throw error
35       }
36     }
37   )
38 }