Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: server/client: getAccount refactored to use early returns #6836

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions server/server/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,35 @@ export class ClientSession implements Session {

async getAccount (ctx: ClientSessionCtx): Promise<void> {
const account = this._pipeline.context.modelDb.getAccountByEmail(this.token.email)
if (account === undefined && this.token.extra?.admin === 'true') {
const systemAccount = this._pipeline.context.modelDb.findObject(this.token.email as Ref<Account>)
if (systemAccount === undefined) {
// Generate account for admin user
const factory = new TxFactory(core.account.System)
const email = `system:${this.token.email}`
const createTx = factory.createTxCreateDoc(
core.class.Account,
core.space.Model,
{
role: AccountRole.Owner,
email
},
this.token.email as Ref<Account>
)
this.includeSessionContext(ctx.ctx)
await this._pipeline.tx(ctx.ctx, [createTx])
const acc = TxProcessor.createDoc2Doc(createTx)
await ctx.sendResponse(acc)
return
} else {
await ctx.sendResponse(systemAccount)
return
}

if (account !== undefined || this.token.extra?.admin !== 'true') {
await ctx.sendResponse(account);
return;
}

const systemAccount = this._pipeline.context.modelDb.findObject(this.token.email as Ref<Account>);
if (systemAccount === undefined) {
await ctx.sendResponse(systemAccount);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just responds with undefined. Is this correct? I haven't changed the logic.

return;
}
await ctx.sendResponse(account)

// Generate account for admin user
const factory = new TxFactory(core.account.System);
const email = `system:${this.token.email}`;
const createTx = factory.createTxCreateDoc(
core.class.Account,
core.space.Model,
{
role: AccountRole.Owner,
email
},
this.token.email as Ref<Account>
);

this.includeSessionContext(ctx.ctx);
await this._pipeline.tx(ctx.ctx, [createTx]);
const acc = TxProcessor.createDoc2Doc(createTx);
await ctx.sendResponse(acc);
}

includeSessionContext (ctx: MeasureContext): void {
Expand Down