refactor: clean up imports and enhance error logging in user workspace and team invitations actions
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import { use } from 'react';
|
||||
|
||||
import { PageBody } from '@kit/ui/page';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
@@ -6,9 +9,7 @@ import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import Dashboard from '../_components/dashboard';
|
||||
// local imports
|
||||
import { HomeLayoutPageHeader } from '../_components/home-page-header';
|
||||
import { use } from 'react';
|
||||
import { loadUserWorkspace } from '../_lib/server/load-user-workspace';
|
||||
import { PageBody } from '@kit/ui/page';
|
||||
|
||||
export const generateMetadata = async () => {
|
||||
const i18n = await createI18nServerInstance();
|
||||
|
||||
@@ -35,13 +35,13 @@ async function workspaceLoader() {
|
||||
accountsPromise(),
|
||||
workspacePromise,
|
||||
requireUserInServerComponent(),
|
||||
tempAccountsPromise()
|
||||
tempAccountsPromise(),
|
||||
]);
|
||||
|
||||
return {
|
||||
accounts,
|
||||
workspace,
|
||||
user,
|
||||
tempVisibleAccounts
|
||||
tempVisibleAccounts,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ class AccountsApi {
|
||||
const { data, error: userError } = authUser;
|
||||
|
||||
if (userError) {
|
||||
console.error('Failed to get user', userError);
|
||||
throw userError;
|
||||
}
|
||||
|
||||
@@ -65,25 +66,26 @@ class AccountsApi {
|
||||
.from('accounts_memberships')
|
||||
.select(
|
||||
`
|
||||
account_id,
|
||||
user_accounts (
|
||||
name,
|
||||
slug,
|
||||
picture_url,
|
||||
)
|
||||
`,
|
||||
account_id,
|
||||
accounts (
|
||||
name,
|
||||
slug,
|
||||
picture_url
|
||||
)
|
||||
`,
|
||||
)
|
||||
.eq('user_id', user.id)
|
||||
.eq('account_role', 'owner');
|
||||
|
||||
if (error) {
|
||||
console.error('error', error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return accounts.map(({ user_accounts }) => ({
|
||||
label: user_accounts.name,
|
||||
value: user_accounts.slug,
|
||||
image: user_accounts.picture_url,
|
||||
return accounts.map(({ accounts }) => ({
|
||||
label: accounts.name,
|
||||
value: accounts.slug,
|
||||
image: accounts.picture_url,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ export function MultiFactorChallengeContainer({
|
||||
|
||||
const verifyMFAChallenge = useVerifyMFAChallenge({
|
||||
onSuccess: () => {
|
||||
router.replace(paths.redirectPath);
|
||||
router.replace('/');
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import { redirect } from 'next/navigation';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { enhanceAction } from '@kit/next/actions';
|
||||
import { createNotificationsApi } from '@kit/notifications/api';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
@@ -16,8 +18,6 @@ import { RenewInvitationSchema } from '../../schema/renew-invitation.schema';
|
||||
import { UpdateInvitationSchema } from '../../schema/update-invitation.schema';
|
||||
import { createAccountInvitationsService } from '../services/account-invitations.service';
|
||||
import { createAccountPerSeatBillingService } from '../services/account-per-seat-billing.service';
|
||||
import { createNotificationsApi } from '@kit/notifications/api';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
|
||||
/**
|
||||
* @name createInvitationsAction
|
||||
@@ -32,48 +32,53 @@ export const createInvitationsAction = enhanceAction(
|
||||
const service = createAccountInvitationsService(client);
|
||||
const api = createNotificationsApi(serviceClient);
|
||||
|
||||
//TODO: This does not send email. It only creates invitations in the database.
|
||||
await service.sendInvitations(params);
|
||||
|
||||
const { invitations: invitationParams, accountSlug } = params;
|
||||
const personalCodes = invitationParams.map(({ personal_code }) => personal_code);
|
||||
const personalCodes = invitationParams.map(
|
||||
({ personal_code }) => personal_code,
|
||||
);
|
||||
|
||||
const { data: company, error: companyError } = await client
|
||||
.from('accounts')
|
||||
.select('id')
|
||||
.eq('slug', accountSlug);
|
||||
|
||||
logger.debug({ company, companyError, personalCodes })
|
||||
logger.debug({ company, companyError, personalCodes });
|
||||
|
||||
if (companyError || !company?.length || !company[0]) {
|
||||
throw new Error(`Failed to fetch company id: ${companyError?.message || 'not found'}`);
|
||||
throw new Error(
|
||||
`Failed to fetch company id: ${companyError?.message || 'not found'}`,
|
||||
);
|
||||
}
|
||||
|
||||
const { data: invitations, error: invitationError } = await serviceClient.rpc(
|
||||
'get_invitations_with_account_ids',
|
||||
{
|
||||
const { data: invitations, error: invitationError } =
|
||||
await serviceClient.rpc('get_invitations_with_account_ids', {
|
||||
company_id: company[0].id,
|
||||
personal_codes: personalCodes,
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
logger.debug({ invitations, invitationError })
|
||||
logger.debug({ invitations, invitationError });
|
||||
|
||||
if (invitationError) {
|
||||
throw new Error(`Failed to fetch invitations with accounts: ${invitationError.message}`);
|
||||
throw new Error(
|
||||
`Failed to fetch invitations with accounts: ${invitationError.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
const notificationPromises = invitations
|
||||
.map(({ invite_token, account_id }) =>
|
||||
const notificationPromises = invitations.map(
|
||||
({ invite_token, account_id }) =>
|
||||
api.createNotification({
|
||||
account_id: account_id!,
|
||||
body: `You are invited to join the company: ${accountSlug}`,
|
||||
link: `/join?invite_token=${invite_token}`,
|
||||
})
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
await Promise.all(notificationPromises);
|
||||
|
||||
logger.info('All invitation notifications are sent')
|
||||
logger.info('All invitation notifications are sent');
|
||||
revalidateMemberPage();
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user