623c002329f7bae3cd687536766d22566d77f0d0
[ndcode_site.git] / api / nodemailer / set.json.jst
1 let XDate = require('xdate')
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   let Problem = await _require('/_lib/Problem.jst')
8
9   await post_request(
10     // env
11     env,
12     // handler
13     async nodemailer => {
14       // do not bother trying to coerce and/or validate
15       // too complex and nested (do it when we have an automated routine)
16
17       let transaction = await env.site.database.Transaction()
18       try {
19         let root = await transaction.get({})
20         let session = await get_session(env, root)
21
22         let account = await get_account(root, session)
23         if (account === undefined)
24           throw new Problem('Unauthorized', 'Please sign in first.', 401)
25         if (!await account.get_json('administrator'))
26           throw new Problem('Unauthorized', 'Not administrator.', 401)
27
28         root.set_json('nodemailer', nodemailer)
29         await transaction.commit()
30       }
31       catch (error) {
32         transaction.rollback()
33         throw error
34       }
35     }
36   )
37 }