Use jst_server.Problem everywhere, make /_lib/post_request.jst use jst_server's new...
[ndcode_site.git] / api / account / sign_up / set_draft.json.jst
1 let jst_server = (await import('@ndcode/jst_server')).default
2 let XDate = require('xdate')
3
4 return async env => {
5   let post_request = await _require('/_lib/post_request.jst')
6   let get_session = await _require('/_lib/get_session.jst')
7
8   await post_request(
9     // env
10     env,
11     // handler
12     async details => {
13       // coerce and/or validate
14       if (details !== null)
15         details = {
16           email: details.email.slice(0, 256).toLowerCase(),
17           given_names: details.given_names.slice(0, 256),
18           family_name: details.family_name.slice(0, 256),
19           contact_me: details.contact_me ? true : false
20         }
21
22       let transaction = await env.site.database.Transaction()
23       try {
24         let root = await transaction.get({})
25         let session = await get_session(env, root)
26
27         if (details) {
28           let expires = new XDate()
29           expires.addDays(1)
30           session.set_json(
31             'sign_up_draft',
32             {
33               email: details.email,
34               given_names: details.given_names,
35               family_name: details.family_name,
36               contact_me: details.contact_me,
37               expires: expires.getTime()
38             }
39           )
40         }
41         else
42           session.delete('sign_up_draft')
43
44         await transaction.commit()
45       }
46       catch (error) {
47         transaction.rollback()
48         throw error
49       }
50     }
51   )
52 }