1
1
/*
2
2
* @Author : Maslow<[email protected] >
3
3
* @Date : 2021-08-31 15:00:04
4
- * @LastEditTime : 2021-09-03 19:07:51
4
+ * @LastEditTime : 2021-09-06 22:22:21
5
5
* @Description :
6
6
*/
7
7
8
+ import * as assert from 'assert'
8
9
import { Request , Response } from 'express'
9
- import { ApplicationStruct , createApplicationDb , generateAppid } from '../../api/application'
10
+ import { getAccountByAppid } from '../../api/account'
11
+ import { ApplicationStruct , createApplicationDb , generateAppid , getMyApplications } from '../../api/application'
10
12
import { Constants } from '../../constants'
13
+ import { ErrorCodes } from '../../constants/error-code'
11
14
import { DatabaseAgent } from '../../lib/db-agent'
12
15
import { logger } from '../../lib/logger'
13
16
import { generatePassword } from '../../utils/rand'
@@ -20,11 +23,20 @@ export async function handleCreateApplication(req: Request, res: Response) {
20
23
if ( ! uid )
21
24
return res . status ( 401 ) . send ( )
22
25
23
- const app_name = req . body ?. name ?? 'default'
24
26
const db = DatabaseAgent . sys_db
25
27
26
- const appid = generateAppid ( )
28
+ // check the application quota in account
29
+ const account = await getAccountByAppid ( uid )
30
+ assert . ok ( account , 'empty account got' )
31
+ const app_quota = account . quota ?. app_count ?? 0
32
+ const my_apps = await getMyApplications ( uid )
33
+ if ( my_apps . length >= app_quota ) {
34
+ return res . send ( ErrorCodes . MEET_APPLICATION_QUOTA_LIMIT )
35
+ }
27
36
37
+ // build the application config
38
+ const app_name = req . body ?. name ?? 'default'
39
+ const appid = generateAppid ( )
28
40
const _salt = generatePassword ( 6 , true , false )
29
41
const db_name = `app_${ appid } _${ _salt } `
30
42
const db_user = db_name
@@ -47,13 +59,15 @@ export async function handleCreateApplication(req: Request, res: Response) {
47
59
updated_at : now
48
60
}
49
61
62
+ // save it
50
63
const ret = await db . collection ( Constants . cn . applications )
51
64
. add ( data )
52
65
53
66
if ( ! ret . id ) {
54
67
return res . status ( 400 ) . send ( 'failed to create application' )
55
68
}
56
69
70
+ // create app db
57
71
const result = await createApplicationDb ( data )
58
72
logger . debug ( `create application db ${ db_name } ` , result )
59
73
0 commit comments