Implement a command-line interface to the running webserver, and a way to get/set...
[ndcode_site.git] / api / account / change_details / get.json.jst
1 let logjson = (await import('@ndcode/logjson')).default
2 let XDate = require('xdate')
3
4 return async env => {
5   let post_request = await _require('/_lib/post_request.jst')
6   let session_cookie = await _require('/_lib/session_cookie.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         // initialize env.session_key, set cookie in env.response
17         await session_cookie(env, transaction)
18         if (env.signed_in_as === null)
19           throw new Problem('Unauthorized', 'Please sign in first.', 401)
20
21         let account = await (
22           await (
23             await transaction.get({})
24           ).get('accounts', {})
25         ).get(env.signed_in_as)
26         let details = {
27           given_names: await logjson.logjson_to_json(
28             await account.get('given_names')
29           ),
30           family_name: await logjson.logjson_to_json(
31             await account.get('family_name')
32           ),
33           contact_me: await logjson.logjson_to_json(
34             await account.get('contact_me')
35           )
36         }
37
38         await transaction.commit()
39         return details
40       }
41       catch (error) {
42         transaction.rollback()
43         throw error
44       }
45     }
46   )
47 }