diff --git a/src/lib/schemas/clients.schema.ts b/src/lib/schemas/clients.schema.ts index f82c641..1b95d2d 100644 --- a/src/lib/schemas/clients.schema.ts +++ b/src/lib/schemas/clients.schema.ts @@ -14,11 +14,8 @@ export const clientEditSchema = clientCreateSchema.extend(idSchema.shape); export const clientOnboardingCreateSchema = clientCreateSchema.extend({ primaryContactName: requiredText('Primary contact name'), primaryContactRole: optionalText(120), - primaryContactEmail: optionalEmail, + primaryContactEmail: z.string().trim().email('Enter a valid primary contact email address.'), primaryContactPhone: optionalText(80), - primaryContactReceivesInvoices: z.enum(['true', 'false']).default('false'), - primaryContactReceivesContracts: z.enum(['true', 'false']).default('false'), - primaryAddressType: z.enum(['primary', 'invoicing', 'contract']).default('primary'), primaryAddressLine1: requiredText('Address line 1'), primaryAddressLine2: optionalText(160), primaryAddressLine3: optionalText(160), diff --git a/src/routes/dashboard/clients/+page.server.ts b/src/routes/dashboard/clients/+page.server.ts index 20d7f01..24124b4 100644 --- a/src/routes/dashboard/clients/+page.server.ts +++ b/src/routes/dashboard/clients/+page.server.ts @@ -75,11 +75,11 @@ export const actions: Actions = { if (!form.valid) return message(form, 'Check the highlighted fields.', { status: 400 }); try { - await db.transaction(async (tx) => { - const clientId = crypto.randomUUID(); - const now = new Date(); + const clientId = crypto.randomUUID(); + const now = new Date(); - await tx.insert(clients).values({ + await db.batch([ + db.insert(clients).values({ id: clientId, organizationId: activeOrganizationId, name: form.data.name, @@ -88,9 +88,9 @@ export const actions: Actions = { notes: form.data.notes || null, updatedAt: now, createdAt: now - }); + }), - await tx.insert(contacts).values({ + db.insert(contacts).values({ id: crypto.randomUUID(), organizationId: activeOrganizationId, clientId, @@ -99,17 +99,17 @@ export const actions: Actions = { email: form.data.primaryContactEmail || null, phone: form.data.primaryContactPhone || null, isPrimary: true, - receivesInvoices: form.data.primaryContactReceivesInvoices === 'true', - receivesContracts: form.data.primaryContactReceivesContracts === 'true', + receivesInvoices: true, + receivesContracts: true, createdAt: now, updatedAt: now - }); + }), - await tx.insert(addresses).values({ + db.insert(addresses).values({ id: crypto.randomUUID(), organizationId: activeOrganizationId, clientId, - type: form.data.primaryAddressType, + type: 'primary', line1: form.data.primaryAddressLine1, line2: form.data.primaryAddressLine2 || null, line3: form.data.primaryAddressLine3 || null, @@ -119,9 +119,10 @@ export const actions: Actions = { country: form.data.primaryAddressCountry, createdAt: now, updatedAt: now - }); - }); - } catch { + }) + ]); + } catch (cause) { + console.error('Unable to create client', cause); return message(form, 'Unable to create client.', { status: 400 }); } diff --git a/src/routes/dashboard/clients/create-client-dialog.svelte b/src/routes/dashboard/clients/create-client-dialog.svelte index f95d923..7633322 100644 --- a/src/routes/dashboard/clients/create-client-dialog.svelte +++ b/src/routes/dashboard/clients/create-client-dialog.svelte @@ -3,7 +3,6 @@ import PlusIcon from '@lucide/svelte/icons/plus'; import { Field as FormField, Control, FieldErrors } from 'formsnap'; import FormSelect from '$lib/components/form-select.svelte'; - import FormCheckbox from '$lib/components/form-checkbox.svelte'; import { industryOptions } from '$lib/constants/industries'; import * as Field from '$lib/components/ui/field/index.js'; import { Button } from '$lib/components/ui/button/index.js'; @@ -66,7 +65,7 @@ {#snippet children({ props })} Name - + {/snippet} @@ -144,7 +143,12 @@ {#snippet children({ props })} Email - + {/snippet} @@ -161,50 +165,19 @@ -
- - -
- - - - {#snippet children({ props })} - Type - - {/snippet} - - - - {#snippet children({ props })} Address line 1 - + {/snippet} @@ -238,7 +211,12 @@ {#snippet children({ props })} City - + {/snippet} @@ -262,7 +240,12 @@ {#snippet children({ props })} Postcode - + {/snippet}