Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/fastify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ declare module "fastify" {
}

interface FastifyRequest {
authEmailPrefix?: string;
account?: Account;
authEmailPrefix: string | undefined;
account: Account | undefined;
}

interface FastifyContextConfig {
Expand Down
36 changes: 34 additions & 2 deletions packages/fastify/src/model/accounts/handlers/myAccount.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
import AccountUserService from "../../accountUsers/service";

import type { FastifyReply } from "fastify";
import type { SessionRequest } from "supertokens-node/framework/fastify";

const myAccount = async (request: SessionRequest, reply: FastifyReply) => {
if (!request.account) {
const { account, config, dbSchema, slonik, user } = request;

if (!user) {
return reply.status(401).send({
error: "Unauthorized",
message: "unauthorized",
statusCode: 401,
});
}

if (!account) {
return reply.status(400).send({
error: "Bad Request",
message: "Bad Request",
statusCode: 400,
});
}

reply.send(request.account);
const accountUserService = new AccountUserService(config, slonik, dbSchema);

const accountUser = await accountUserService.findOne({
AND: [
{
key: "user_id",
operator: "eq",
value: user.id,
},
{
key: "account_id",
operator: "eq",
value: account.id,
},
],
});

reply.send({
...account,
role: accountUser?.roleId,
});
};

export default myAccount;
9 changes: 8 additions & 1 deletion packages/fastify/src/model/accounts/handlers/myAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ const myAccounts = async (request: SessionRequest, reply: FastifyReply) => {
.join(","),
});

reply.send(accounts);
const accountsWithRole = accounts.map((account) => ({
...account,
role:
accountUsers.find((user) => user.accountId === account.id)?.roleId ||
undefined,
}));

reply.send(accountsWithRole);
};

export default myAccounts;
40 changes: 11 additions & 29 deletions packages/fastify/src/model/accounts/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import type { Service } from "@dzangolab/fastify-slonik";
import type { QueryResultRow } from "slonik";

class AccountService<
Account extends QueryResultRow,
AccountCreateInput extends QueryResultRow,
AccountUpdateInput extends QueryResultRow,
T extends QueryResultRow,
C extends QueryResultRow,
U extends QueryResultRow,
>
extends BaseService<Account, AccountCreateInput, AccountUpdateInput>
implements Service<Account, AccountCreateInput, AccountUpdateInput>
extends BaseService<T, C, U>
implements Service<T, C, U>
{
create = async (data: AccountCreateInput): Promise<Account | undefined> => {
create = async (data: C): Promise<T | undefined> => {
if (this.saasConfig.subdomains === "disabled" || data.slug === "") {
delete data.slug;
delete data.domain;
Expand Down Expand Up @@ -61,12 +61,12 @@ class AccountService<
return connection.query(query).then((data) => {
return data.rows[0];
});
})) as Account;
})) as T;

return result ? this.postCreate(result) : undefined;
};

findByHostname = async (hostname: string): Promise<Account | null> => {
findByHostname = async (hostname: string): Promise<T | null> => {
const saasConfig = getSaasConfig(this.config);

const query = this.factory.getFindByHostnameSql(
Expand All @@ -81,16 +81,6 @@ class AccountService<
return account;
};

findByUserId = async (userId: string): Promise<Account | null> => {
const query = this.factory.getFindByUserIdSql(userId);

const account = await this.database.connect(async (connection) => {
return connection.maybeOne(query);
});

return account;
};

validateSlugOrDomain = async (slug: string, domain?: string) => {
const query = this.factory.getFindBySlugOrDomainSql(slug, domain);

Expand Down Expand Up @@ -121,18 +111,10 @@ class AccountService<
}

if (!this._factory) {
this._factory = new AccountSqlFactory<
Account,
AccountCreateInput,
AccountUpdateInput
>(this);
this._factory = new AccountSqlFactory<T, C, U>(this);
}

return this._factory as AccountSqlFactory<
Account,
AccountCreateInput,
AccountUpdateInput
>;
return this._factory as AccountSqlFactory<T, C, U>;
}

get saasConfig() {
Expand All @@ -143,7 +125,7 @@ class AccountService<
return this.saasConfig.tables.accounts.name;
}

protected postCreate = async (account: Account): Promise<Account> => {
protected postCreate = async (account: T): Promise<T> => {
if (
this.saasConfig.subdomains === "disabled" ||
!this.saasConfig.multiDatabase?.enabled
Expand Down
29 changes: 6 additions & 23 deletions packages/fastify/src/model/accounts/sqlFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
createTableFragment,
DefaultSqlFactory,
} from "@dzangolab/fastify-slonik";
import { DefaultSqlFactory } from "@dzangolab/fastify-slonik";
import { sql } from "slonik";

import getSaasConfig from "../../config";
Expand All @@ -11,12 +8,12 @@ import type { QueryResultRow, QuerySqlToken } from "slonik";

/* eslint-disable brace-style */
class AccountSqlFactory<
Account extends QueryResultRow,
AccountCreateInput extends QueryResultRow,
AccountUpdateInput extends QueryResultRow,
T extends QueryResultRow,
C extends QueryResultRow,
U extends QueryResultRow,
>
extends DefaultSqlFactory<Account, AccountCreateInput, AccountUpdateInput>
implements SqlFactory<Account, AccountCreateInput, AccountUpdateInput>
extends DefaultSqlFactory<T, C, U>
implements SqlFactory<T, C, U>
{
getFindByHostnameSql = (
hostname: string,
Expand Down Expand Up @@ -55,20 +52,6 @@ class AccountSqlFactory<
`;
};

getFindByUserIdSql = (userId: string): QuerySqlToken => {
const accountUsersTable = createTableFragment(
this.saasConfig.tables.accountUsers.name,
this.schema,
);

return sql.type(this.validationSchema)`
SELECT c.*
FROM ${this.getTableFragment()} AS c
JOIN ${accountUsersTable} AS cu on c.id = cu.account_id
WHERE cu.user_id = ${userId};
`;
};

get saasConfig() {
return getSaasConfig(this.config);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/fastify/src/plugins/accountDiscoveryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import getHost from "../lib/getHost";
import type { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";

const plugin = async (fastify: FastifyInstance) => {
fastify.decorateRequest("account");
fastify.decorateRequest("authEmailPrefix");

fastify.addHook(
"onRequest",
async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down