0ca4b664c5ee0c1cdda98e235e77b375e5c5527c
[ndcode_site.git] / api / nodemailer / set.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 nodemailer => {
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         // initialize env.session_key, set cookie in env.response
19         await get_session(env, transaction)
20         if (env.signed_in_as === null)
21           throw new Problem('Unauthorized', 'Please sign in first.', 401)
22
23         let root = await transaction.get({})
24         let account = await (
25           await root.get('accounts', {})
26         ).get(env.signed_in_as)
27         if (!await account.get_json('administrator'))
28           throw new Problem('Unauthorized', 'Not administrator.', 401)
29
30         root.set_json('nodemailer', nodemailer)
31         await transaction.commit()
32       }
33       catch (error) {
34         transaction.rollback()
35         throw error
36       }
37     }
38   )
39 }