@@ -108,34 +108,37 @@ export class ClientSession implements Session {
108
108
async getAccount ( ctx : ClientSessionCtx ) : Promise < void > {
109
109
const account = this . _pipeline . context . modelDb . getAccountByEmail ( this . token . email )
110
110
if ( account === undefined && this . token . extra ?. admin === 'true' ) {
111
- const systemAccount = this . _pipeline . context . modelDb . findObject ( this . token . email as Ref < Account > )
112
- if ( systemAccount === undefined ) {
113
- // Generate account for admin user
114
- const factory = new TxFactory ( core . account . System )
115
- const email = `system:${ this . token . email } `
116
- const createTx = factory . createTxCreateDoc (
117
- core . class . Account ,
118
- core . space . Model ,
119
- {
120
- role : AccountRole . Owner ,
121
- email
122
- } ,
123
- this . token . email as Ref < Account >
124
- )
125
- this . includeSessionContext ( ctx . ctx )
126
- await this . _pipeline . tx ( ctx . ctx , [ createTx ] )
127
- const acc = TxProcessor . createDoc2Doc ( createTx )
128
- await ctx . sendResponse ( acc )
129
- return
130
- } else {
131
- await ctx . sendResponse ( systemAccount )
132
- return
133
- }
111
+ await ctx . sendResponse ( this . getSystemAccount ( ) )
112
+ return
134
113
}
135
114
await ctx . sendResponse ( account )
136
115
}
137
116
117
+ private getSystemAccount ( ) : Account {
118
+ // Generate account for admin user
119
+ const factory = new TxFactory ( core . account . System )
120
+ const email = `system:${ this . token . email } `
121
+ const createTx = factory . createTxCreateDoc (
122
+ core . class . Account ,
123
+ core . space . Model ,
124
+ {
125
+ role : AccountRole . Owner ,
126
+ email
127
+ } ,
128
+ email as Ref < Account >
129
+ )
130
+ return TxProcessor . createDoc2Doc ( createTx )
131
+ }
132
+
138
133
includeSessionContext ( ctx : MeasureContext ) : void {
134
+ let account : Account | undefined
135
+ if ( this . token . extra ?. admin === 'true' ) {
136
+ account = this . _pipeline . context . modelDb . getAccountByEmail ( this . token . email )
137
+ if ( account === undefined ) {
138
+ account = this . getSystemAccount ( )
139
+ }
140
+ }
141
+
139
142
const contextData = new SessionDataImpl (
140
143
this . token . email ,
141
144
this . sessionId ,
@@ -149,7 +152,8 @@ export class ClientSession implements Session {
149
152
false ,
150
153
new Map ( ) ,
151
154
new Map ( ) ,
152
- this . _pipeline . context . modelDb
155
+ this . _pipeline . context . modelDb ,
156
+ account
153
157
)
154
158
ctx . contextData = contextData
155
159
}
0 commit comments