fix company creation for admin and inviting of new employees
This commit is contained in:
@@ -163,9 +163,9 @@ export function PersonalAccountDropdown({
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
|
||||
<DropdownMenuSeparator />
|
||||
|
||||
<If condition={accounts.length > 0}>
|
||||
<DropdownMenuSeparator />
|
||||
|
||||
<span className="text-muted-foreground px-2 text-xs">
|
||||
<Trans
|
||||
i18nKey={'teams:yourTeams'}
|
||||
|
||||
@@ -20,7 +20,6 @@ import { Button } from '@kit/ui/button';
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormDescription,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
@@ -29,10 +28,13 @@ import {
|
||||
import { If } from '@kit/ui/if';
|
||||
import { Input } from '@kit/ui/input';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { createCompanyAccountAction } from '../lib/server/admin-server-actions';
|
||||
import { CreateCompanySchema, CreateCompanySchemaType } from '../lib/server/schema/create-company.schema';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
import {
|
||||
CreateCompanySchema,
|
||||
CreateCompanySchemaType,
|
||||
} from '../lib/server/schema/create-company.schema';
|
||||
|
||||
export function AdminCreateCompanyDialog(props: React.PropsWithChildren) {
|
||||
const [pending, startTransition] = useTransition();
|
||||
@@ -58,14 +60,9 @@ export function AdminCreateCompanyDialog(props: React.PropsWithChildren) {
|
||||
|
||||
setOpen(false);
|
||||
setError(null);
|
||||
|
||||
|
||||
} else {
|
||||
setError('Something went wrong with company creation');
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : 'Error');
|
||||
}
|
||||
@@ -100,17 +97,17 @@ export function AdminCreateCompanyDialog(props: React.PropsWithChildren) {
|
||||
</If>
|
||||
|
||||
<FormField
|
||||
name={'name'}
|
||||
name="name"
|
||||
render={({ field }) => {
|
||||
return (
|
||||
<FormItem>
|
||||
<FormLabel>
|
||||
<Trans i18nKey={'teams:teamNameLabel'} />
|
||||
<Trans i18nKey="teams:teamNameLabel" />
|
||||
</FormLabel>
|
||||
|
||||
<FormControl>
|
||||
<Input
|
||||
data-test={'create-team-name-input'}
|
||||
data-test="create-team-name-input"
|
||||
required
|
||||
minLength={2}
|
||||
maxLength={50}
|
||||
@@ -119,9 +116,31 @@ export function AdminCreateCompanyDialog(props: React.PropsWithChildren) {
|
||||
/>
|
||||
</FormControl>
|
||||
|
||||
<FormDescription>
|
||||
<Trans i18nKey={'teams:teamNameDescription'} />
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
name="ownerPersonalCode"
|
||||
render={({ field }) => {
|
||||
return (
|
||||
<FormItem>
|
||||
<FormLabel>
|
||||
<Trans i18nKey="teams:teamOwnerPersonalCodeLabel" />
|
||||
</FormLabel>
|
||||
|
||||
<FormControl>
|
||||
<Input
|
||||
data-test="create-team-owner-personal-code-input"
|
||||
required
|
||||
minLength={2}
|
||||
maxLength={50}
|
||||
placeholder={''}
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
|
||||
@@ -239,7 +239,7 @@ export const resetPasswordAction = adminAction(
|
||||
);
|
||||
|
||||
export const createCompanyAccountAction = enhanceAction(
|
||||
async ({ name }, user) => {
|
||||
async ({ name, ownerPersonalCode }, user) => {
|
||||
const logger = await getLogger();
|
||||
const client = getSupabaseServerClient();
|
||||
const service = createCreateCompanyAccountService(client);
|
||||
@@ -254,7 +254,7 @@ export const createCompanyAccountAction = enhanceAction(
|
||||
|
||||
const { data, error } = await service.createNewOrganizationAccount({
|
||||
name,
|
||||
userId: user.id,
|
||||
ownerPersonalCode,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
@@ -266,8 +266,7 @@ export const createCompanyAccountAction = enhanceAction(
|
||||
}
|
||||
|
||||
logger.info(ctx, `Company account created`);
|
||||
|
||||
redirect(`/home/${data.slug}/settings`);
|
||||
redirect(`/admin/accounts/${data.id}`);
|
||||
},
|
||||
{
|
||||
schema: CreateCompanySchema,
|
||||
|
||||
@@ -46,7 +46,11 @@ export const CompanyNameSchema = z
|
||||
*/
|
||||
export const CreateCompanySchema = z.object({
|
||||
name: CompanyNameSchema,
|
||||
ownerPersonalCode: z
|
||||
.string()
|
||||
.regex(/^[1-6]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}\d$/, {
|
||||
message: 'Invalid Estonian personal code format',
|
||||
}),
|
||||
});
|
||||
|
||||
export type CreateCompanySchemaType = z.infer<typeof CreateCompanySchema>;
|
||||
|
||||
|
||||
@@ -16,7 +16,10 @@ class CreateTeamAccountService {
|
||||
|
||||
constructor(private readonly client: SupabaseClient<Database>) {}
|
||||
|
||||
async createNewOrganizationAccount(params: { name: string; userId: string }) {
|
||||
async createNewOrganizationAccount(params: {
|
||||
name: string;
|
||||
ownerPersonalCode: string;
|
||||
}) {
|
||||
const logger = await getLogger();
|
||||
const ctx = { ...params, namespace: this.namespace };
|
||||
|
||||
@@ -26,12 +29,13 @@ class CreateTeamAccountService {
|
||||
.schema('medreport')
|
||||
.rpc('create_team_account', {
|
||||
account_name: params.name,
|
||||
new_personal_code: params.ownerPersonalCode,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
logger.error(
|
||||
{
|
||||
error,
|
||||
error: error.message,
|
||||
...ctx,
|
||||
},
|
||||
`Error creating company account`,
|
||||
|
||||
@@ -34,20 +34,18 @@ import {
|
||||
import { Spinner } from '@kit/ui/spinner';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
|
||||
export function MultiFactorChallengeContainer({
|
||||
paths,
|
||||
userId,
|
||||
}: React.PropsWithChildren<{
|
||||
userId: string;
|
||||
paths: {
|
||||
redirectPath: string;
|
||||
};
|
||||
}>) {
|
||||
const router = useRouter();
|
||||
|
||||
const verifyMFAChallenge = useVerifyMFAChallenge({
|
||||
onSuccess: () => {
|
||||
router.replace('/');
|
||||
router.replace(pathsConfig.app.home);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -206,6 +204,10 @@ function useVerifyMFAChallenge({ onSuccess }: { onSuccess: () => void }) {
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
console.warn(
|
||||
{ error: response.error.message },
|
||||
'Failed to verify MFA challenge',
|
||||
);
|
||||
throw response.error;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'server-only';
|
||||
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
type Notification = Database['medreport']['Tables']['notifications'];
|
||||
@@ -14,12 +15,17 @@ class NotificationsService {
|
||||
constructor(private readonly client: SupabaseClient<Database>) {}
|
||||
|
||||
async createNotification(params: Notification['Insert']) {
|
||||
const logger = await getLogger();
|
||||
const { error } = await this.client
|
||||
.schema('medreport')
|
||||
.from('notifications')
|
||||
.insert(params);
|
||||
|
||||
if (error) {
|
||||
logger.error(
|
||||
{ ...params },
|
||||
`Could not create notification: ${error.message}`,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +54,9 @@ export const createInvitationsAction = enhanceAction(
|
||||
);
|
||||
}
|
||||
|
||||
const { data: invitations, error: invitationError } =
|
||||
await serviceClient.rpc('get_invitations_with_account_ids', {
|
||||
const { data: invitations, error: invitationError } = await serviceClient
|
||||
.schema('medreport')
|
||||
.rpc('get_invitations_with_account_ids', {
|
||||
company_id: company[0].id,
|
||||
personal_codes: personalCodes,
|
||||
});
|
||||
|
||||
@@ -51,7 +51,10 @@ class AccountInvitationsService {
|
||||
});
|
||||
|
||||
if (error) {
|
||||
logger.error(ctx, `Failed to remove invitation`);
|
||||
logger.error(
|
||||
{ ...ctx, error: error.message },
|
||||
`Failed to remove invitation`,
|
||||
);
|
||||
|
||||
throw error;
|
||||
}
|
||||
@@ -184,7 +187,7 @@ class AccountInvitationsService {
|
||||
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
console.log('property', invitations, accountSlug);
|
||||
const response = await this.client
|
||||
.schema('medreport')
|
||||
.rpc('add_invitations_to_account', {
|
||||
|
||||
Reference in New Issue
Block a user