fix company creation for admin and inviting of new employees

This commit is contained in:
Danel Kungla
2025-07-31 12:27:30 +03:00
parent 87363051cd
commit a39c21e4e7
18 changed files with 496 additions and 57 deletions

View File

@@ -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>

View File

@@ -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,

View File

@@ -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>;

View File

@@ -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`,