Add /_lib/get_account.jst, remove env.signed_in_as
[ndcode_site.git] / api / nodemailer / get.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 () => {
14       let transaction = await env.site.database.Transaction()
15       try {
16         let account = await get_account(
17           env,
18           transaction,
19           await get_session(env, transaction)
20         )
21         if (account === undefined)
22           throw new Problem('Unauthorized', 'Please sign in first.', 401)
23
24         if (!await account.get_json('administrator'))
25           throw new Problem('Unauthorized', 'Not administrator.', 401)
26
27         nodemailer = await (
28           await transaction.get({})
29         ).get_json('nodemailer', {})
30
31         await transaction.commit()
32         return nodemailer
33       }
34       catch (error) {
35         transaction.rollback()
36         throw error
37       }
38     }
39   )
40 }