add medreport schema

This commit is contained in:
Danel Kungla
2025-07-09 13:31:37 +03:00
parent 9371ff7710
commit d9198a8a12
73 changed files with 420 additions and 270 deletions

View File

@@ -22,6 +22,7 @@ export const updateTeamAccountName = enhanceAction(
logger.info(ctx, `Updating team name...`);
const { error, data } = await client
.schema('medreport')
.from('accounts')
.update({
name,

View File

@@ -41,6 +41,7 @@ export const createInvitationsAction = enhanceAction(
);
const { data: company, error: companyError } = await client
.schema('medreport')
.from('accounts')
.select('id')
.eq('slug', accountSlug);

View File

@@ -79,9 +79,11 @@ export const transferOwnershipAction = enhanceAction(
logger.info(ctx, 'Processing team ownership transfer request...');
// assert that the user is the owner of the account
const { data: isOwner, error } = await client.rpc('is_account_owner', {
account_id: data.accountId,
});
const { data: isOwner, error } = await client
.schema('medreport')
.rpc('is_account_owner', {
account_id: data.accountId,
});
if (error || !isOwner) {
logger.error(ctx, 'User is not the owner of this account');

View File

@@ -17,6 +17,7 @@ export class TeamAccountsApi {
*/
async getTeamAccount(slug: string) {
const { data, error } = await this.client
.schema('medreport')
.from('accounts')
.select('*')
.eq('slug', slug)
@@ -36,6 +37,7 @@ export class TeamAccountsApi {
*/
async getTeamAccountById(accountId: string) {
const { data, error } = await this.client
.schema('medreport')
.from('accounts')
.select('*')
.eq('id', accountId)
@@ -55,6 +57,7 @@ export class TeamAccountsApi {
*/
async getSubscription(accountId: string) {
const { data, error } = await this.client
.schema('medreport')
.from('subscriptions')
.select('*, items: subscription_items !inner (*)')
.eq('account_id', accountId)
@@ -73,6 +76,7 @@ export class TeamAccountsApi {
*/
async getOrder(accountId: string) {
const response = await this.client
.schema('medreport')
.from('orders')
.select('*, items: order_items !inner (*)')
.eq('account_id', accountId)
@@ -91,13 +95,17 @@ export class TeamAccountsApi {
* @param slug
*/
async getAccountWorkspace(slug: string, userId: string) {
const accountPromise = this.client.rpc('team_account_workspace', {
account_slug: slug,
});
const accountPromise = this.client
.schema('medreport')
.rpc('team_account_workspace', {
account_slug: slug,
});
const accountsPromise = this.client
.schema('medreport')
.from('accounts_memberships')
.select(`
.select(
`
account_id,
user_accounts (
id,
@@ -106,7 +114,8 @@ export class TeamAccountsApi {
slug,
picture_url
)
`)
`,
)
.eq('user_id', userId)
.eq('account_role', 'owner');
@@ -114,7 +123,7 @@ export class TeamAccountsApi {
accountPromise,
accountsPromise,
]);
if (accountResult.error) {
return {
error: accountResult.error,
@@ -154,7 +163,7 @@ export class TeamAccountsApi {
async hasPermission(params: {
accountId: string;
userId: string;
permission: Database['public']['Enums']['app_permissions'];
permission: Database['medreport']['Enums']['app_permissions'];
}) {
const { data, error } = await this.client.rpc('has_permission', {
account_id: params.accountId,
@@ -176,6 +185,7 @@ export class TeamAccountsApi {
*/
async getMembersCount(accountId: string) {
const { count, error } = await this.client
.schema('medreport')
.from('accounts_memberships')
.select('*', {
head: true,
@@ -197,6 +207,7 @@ export class TeamAccountsApi {
*/
async getCustomerId(accountId: string) {
const { data, error } = await this.client
.schema('medreport')
.from('billing_customers')
.select('customer_id')
.eq('account_id', accountId)
@@ -217,6 +228,7 @@ export class TeamAccountsApi {
*/
async getInvitation(adminClient: SupabaseClient<Database>, token: string) {
const { data: invitation, error } = await adminClient
.schema('medreport')
.from('invitations')
.select<
string,

View File

@@ -43,6 +43,7 @@ class AccountInvitationsService {
logger.info(ctx, 'Removing invitation...');
const { data, error } = await this.client
.schema('medreport')
.from('invitations')
.delete()
.match({
@@ -76,6 +77,7 @@ class AccountInvitationsService {
logger.info(ctx, 'Updating invitation...');
const { data, error } = await this.client
.schema('medreport')
.from('invitations')
.update({
role: params.role,
@@ -105,12 +107,11 @@ class AccountInvitationsService {
invitation: z.infer<typeof InviteMembersSchema>['invitations'][number],
accountSlug: string,
) {
const { data: members, error } = await this.client.rpc(
'get_account_members',
{
const { data: members, error } = await this.client
.schema('medreport')
.rpc('get_account_members', {
account_slug: accountSlug,
},
);
});
if (error) {
throw error;
@@ -169,6 +170,7 @@ class AccountInvitationsService {
}
const accountResponse = await this.client
.schema('medreport')
.from('accounts')
.select('name')
.eq('slug', accountSlug)
@@ -183,10 +185,12 @@ class AccountInvitationsService {
throw new Error('Account not found');
}
const response = await this.client.rpc('add_invitations_to_account', {
invitations,
account_slug: accountSlug,
});
const response = await this.client
.schema('medreport')
.rpc('add_invitations_to_account', {
invitations,
account_slug: accountSlug,
});
if (response.error) {
logger.error(
@@ -232,10 +236,12 @@ class AccountInvitationsService {
logger.info(ctx, 'Accepting invitation to team');
const { error, data } = await adminClient.rpc('accept_invitation', {
token: params.inviteToken,
user_id: params.userId,
});
const { error, data } = await adminClient
.schema('medreport')
.rpc('accept_invitation', {
token: params.inviteToken,
user_id: params.userId,
});
if (error) {
logger.error(
@@ -272,6 +278,7 @@ class AccountInvitationsService {
const sevenDaysFromNow = formatISO(addDays(new Date(), 7));
const { data, error } = await this.client
.schema('medreport')
.from('invitations')
.update({
expires_at: sevenDaysFromNow,

View File

@@ -37,6 +37,7 @@ class AccountMembersService {
logger.info(ctx, `Removing member from account...`);
const { data, error } = await this.client
.schema('medreport')
.from('accounts_memberships')
.delete()
.match({
@@ -88,7 +89,7 @@ class AccountMembersService {
logger.info(ctx, `Validating permissions to update member role...`);
const { data: canActionAccountMember, error: accountError } =
await this.client.rpc('can_action_account_member', {
await this.client.schema('medreport').rpc('can_action_account_member', {
target_user_id: params.userId,
target_team_account_id: params.accountId,
});
@@ -112,6 +113,7 @@ class AccountMembersService {
// for updating accounts_memberships. Instead, we use the can_action_account_member
// RPC to validate permissions to update the role
const { data, error } = await adminClient
.schema('medreport')
.from('accounts_memberships')
.update({
account_role: params.role,
@@ -157,13 +159,12 @@ class AccountMembersService {
logger.info(ctx, `Transferring ownership of account...`);
const { data, error } = await adminClient.rpc(
'transfer_team_account_ownership',
{
const { data, error } = await adminClient
.schema('medreport')
.rpc('transfer_team_account_ownership', {
target_account_id: params.accountId,
new_owner_id: params.userId,
},
);
});
if (error) {
logger.error(

View File

@@ -36,6 +36,7 @@ class AccountPerSeatBillingService {
);
const { data, error } = await this.client
.schema('medreport')
.from('subscriptions')
.select(
`

View File

@@ -22,9 +22,11 @@ class CreateTeamAccountService {
logger.info(ctx, `Creating new team account...`);
const { error, data } = await this.client.rpc('create_team_account', {
account_name: params.name,
});
const { error, data } = await this.client
.schema('medreport')
.rpc('create_team_account', {
account_name: params.name,
});
if (error) {
logger.error(

View File

@@ -40,6 +40,7 @@ class DeleteTeamAccountService {
// we can use the admin client to delete the account.
const { error } = await adminClient
.schema('medreport')
.from('accounts')
.delete()
.eq('id', params.accountId);

View File

@@ -45,6 +45,7 @@ class LeaveTeamAccountService {
const { accountId, userId } = Schema.parse(params);
const { error } = await this.adminClient
.schema('medreport')
.from('accounts_memberships')
.delete()
.match({

View File

@@ -5,7 +5,7 @@ import { z } from 'zod';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
type Invitation = Database['public']['Tables']['invitations']['Row'];
type Invitation = Database['medreport']['Tables']['invitations']['Row'];
const invitePath = '/join';
@@ -72,6 +72,7 @@ class AccountInvitationsWebhookService {
);
const inviter = await this.adminClient
.schema('medreport')
.from('accounts')
.select('email, name')
.eq('id', invitation.invited_by)
@@ -90,6 +91,7 @@ class AccountInvitationsWebhookService {
}
const team = await this.adminClient
.schema('medreport')
.from('accounts')
.select('name')
.eq('id', invitation.account_id)

View File

@@ -3,7 +3,7 @@ import { z } from 'zod';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
type Account = Database['public']['Tables']['accounts']['Row'];
type Account = Database['medreport']['Tables']['accounts']['Row'];
export function createAccountWebhooksService() {
return new AccountWebhooksService();