Compare commits

...

4 Commits

10 changed files with 181 additions and 115 deletions
@@ -31,29 +31,26 @@
<div class="grid gap-5">
<div class="grid gap-3">
<div class="grid gap-2 sm:grid-cols-2">
<Field.Field>
<Field.Label for={`${prefix}-name`}>Name</Field.Label>
<Input
id={`${prefix}-name`}
name="name"
type="text"
value={organization?.name ?? ''}
required
/>
</Field.Field>
<Field.Field>
<Field.Label for={`${prefix}-address-line-1`}>Address line 1</Field.Label>
<Input
id={`${prefix}-address-line-1`}
name="addressLine1"
type="text"
value={organization?.addressLine1 ?? ''}
required
/>
</Field.Field>
</div>
<Field.Field>
<Field.Label for={`${prefix}-name`}>Name</Field.Label>
<Input
id={`${prefix}-name`}
name="name"
type="text"
value={organization?.name ?? ''}
required
/>
</Field.Field>
<Field.Field>
<Field.Label for={`${prefix}-address-line-1`}>Address line 1</Field.Label>
<Input
id={`${prefix}-address-line-1`}
name="addressLine1"
type="text"
value={organization?.addressLine1 ?? ''}
required
/>
</Field.Field>
<Field.Field>
<Field.Label for={`${prefix}-address-line-2`}>Address line 2</Field.Label>
<Input
+1 -1
View File
@@ -13,4 +13,4 @@ export const addressCreateSchema = z.object({
country: requiredText('Country', 80).default('United Kingdom')
});
export const addressEditSchema = addressCreateSchema.extend(idSchema.shape);
export const addressEditSchema = addressCreateSchema.omit({ type: true }).extend(idSchema.shape);
+12 -6
View File
@@ -6,12 +6,18 @@ const optionalPassword = z.string().refine((value) => value.length === 0 || valu
});
const roleSchema = z.enum(['admin', 'user'], 'Select a role.');
export const createAdminSchema = z.object({
name: z.string().trim().min(1, 'Enter a name.'),
email: z.email('Enter a valid email address.').transform((value) => value.toLowerCase()),
password: optionalPassword,
role: roleSchema.default('admin')
});
export const createAdminSchema = z
.object({
name: z.string().trim().min(1, 'Enter a name.'),
email: z.email('Enter a valid email address.').transform((value) => value.toLowerCase()),
password: optionalPassword,
confirmPassword: optionalPassword,
role: roleSchema.default('admin')
})
.refine((data) => data.password === data.confirmPassword, {
message: 'Confirm password must match password.',
path: ['confirmPassword']
});
export const editAdminSchema = z.object({
id: z.string().min(1, 'Admin id is required.'),
+1 -4
View File
@@ -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),
+15 -14
View File
@@ -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 });
}
@@ -78,7 +78,8 @@ export const actions: Actions = {
if (!form.valid) return message(form, 'Check the highlighted fields.', { status: 400 });
try {
await db.insert(addresses).values({
const now = new Date();
const insertAddress = db.insert(addresses).values({
id: crypto.randomUUID(),
organizationId: activeOrganizationId,
clientId: form.data.clientId,
@@ -90,9 +91,28 @@ export const actions: Actions = {
region: form.data.region || null,
postcode: form.data.postcode,
country: form.data.country,
updatedAt: new Date(),
createdAt: new Date()
updatedAt: now,
createdAt: now
});
if (form.data.type === 'primary') {
await db.batch([
db
.update(addresses)
.set({ type: 'invoicing', updatedAt: now })
.where(
and(
eq(addresses.clientId, params.id),
eq(addresses.organizationId, activeOrganizationId),
eq(addresses.type, 'primary'),
isNull(addresses.archivedAt)
)
),
insertAddress
]);
} else {
await insertAddress;
}
} catch {
return message(form, 'Unable to create address.', { status: 400 });
}
@@ -113,7 +133,6 @@ export const actions: Actions = {
.update(addresses)
.set({
clientId: form.data.clientId,
type: form.data.type,
line1: form.data.line1,
line2: form.data.line2 || null,
line3: form.data.line3 || null,
@@ -137,6 +156,59 @@ export const actions: Actions = {
return message(form, 'Address updated.');
},
makePrimary: async ({ locals, params, request }) => {
const { activeOrganizationId } = await loadOrganizationContext(locals);
const form = await superValidate(await request.formData(), zod4(archiveSchema), {
id: 'addresses-primary'
});
if (!form.valid) return message(form, 'Address id is required.', { status: 400 });
const [address] = await db
.select({ id: addresses.id, type: addresses.type })
.from(addresses)
.where(
and(
eq(addresses.id, form.data.id),
eq(addresses.clientId, params.id),
eq(addresses.organizationId, activeOrganizationId),
isNull(addresses.archivedAt)
)
)
.limit(1);
if (!address) return message(form, 'Choose a valid address.', { status: 400 });
if (address.type === 'primary') return message(form, 'Address is already primary.');
const now = new Date();
await db.batch([
db
.update(addresses)
.set({ type: address.type, updatedAt: now })
.where(
and(
eq(addresses.clientId, params.id),
eq(addresses.organizationId, activeOrganizationId),
eq(addresses.type, 'primary'),
isNull(addresses.archivedAt)
)
),
db
.update(addresses)
.set({ type: 'primary', updatedAt: now })
.where(
and(
eq(addresses.id, form.data.id),
eq(addresses.clientId, params.id),
eq(addresses.organizationId, activeOrganizationId),
isNull(addresses.archivedAt)
)
)
]);
return message(form, 'Primary address updated.');
},
archive: async ({ locals, params, request }) => {
const { activeOrganizationId } = await loadOrganizationContext(locals);
const form = await superValidate(await request.formData(), zod4(archiveSchema), {
@@ -4,6 +4,7 @@
import { Button } from '$lib/components/ui/button/index.js';
import * as DropdownMenu from '$lib/components/ui/dropdown-menu/index.js';
import * as Table from '$lib/components/ui/table/index.js';
import { cn } from '$lib/utils.js';
let {
data,
openEdit,
@@ -50,6 +51,19 @@
>
<DropdownMenu.Content align="end" class="w-36">
<DropdownMenu.Item onclick={() => openEdit(record)}>Edit</DropdownMenu.Item>
{#if record.type !== 'primary'}
<form method="POST" action="?/makePrimary">
<button
type="submit"
name="id"
value={record.id}
class={cn(
'flex w-full cursor-default items-center rounded-md px-1.5 py-1 text-left text-sm outline-hidden select-none',
'hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground'
)}>Make Primary</button
>
</form>
{/if}
<DropdownMenu.Item variant="destructive" onclick={() => openArchive(record)}
>Archive</DropdownMenu.Item
>
@@ -1,7 +1,6 @@
<script lang="ts">
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Field as FormField, Control, FieldErrors } from 'formsnap';
import FormSelect from '$lib/components/form-select.svelte';
import * as Field from '$lib/components/ui/field/index.js';
import { Button } from '$lib/components/ui/button/index.js';
import * as Dialog from '$lib/components/ui/dialog/index.js';
@@ -30,26 +29,6 @@
{#if editingRecord}
<form method="POST" action="?/edit" class="grid gap-2" use:enhanceEdit>
<input type="hidden" name="id" value={$editData.id} />
<FormField form={editForm} name="type">
<Field.Field>
<Control id="edit-type">
{#snippet children({ props })}
<Field.Label>Type</Field.Label>
<FormSelect
name="type"
bind:value={$editData.type}
options={[
{ value: 'primary', label: 'Primary' },
{ value: 'invoicing', label: 'Invoicing' },
{ value: 'contract', label: 'Contract' }
]}
triggerProps={props}
/>
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
</Field.Field>
</FormField>
<FormField form={editForm} name="line1">
<Field.Field>
<Control id="edit-line1">
@@ -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 @@
<Control id="create-name">
{#snippet children({ props })}
<Field.Label>Name</Field.Label>
<Input {...props} type="text" bind:value={$createData.name} />
<Input {...props} type="text" required bind:value={$createData.name} />
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
@@ -144,7 +143,12 @@
<Control id="create-primary-contact-email">
{#snippet children({ props })}
<Field.Label>Email</Field.Label>
<Input {...props} type="email" bind:value={$createData.primaryContactEmail} />
<Input
{...props}
type="email"
required
bind:value={$createData.primaryContactEmail}
/>
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
@@ -161,50 +165,19 @@
<Field.Error><FieldErrors /></Field.Error>
</Field.Field>
</FormField>
<div class="grid gap-3 py-1">
<FormCheckbox
id="create-primary-contact-receives-invoices"
name="primaryContactReceivesInvoices"
label="Receives invoices"
description="Send invoices raised for this client to this contact."
bind:value={$createData.primaryContactReceivesInvoices}
/>
<FormCheckbox
id="create-primary-contact-receives-contracts"
name="primaryContactReceivesContracts"
label="Receives contracts"
description="Send contract documents for this client to this contact."
bind:value={$createData.primaryContactReceivesContracts}
/>
</div>
</div>
<div class={['grid gap-2', createStep !== 1 && 'hidden']}>
<FormField form={createForm} name="primaryAddressType">
<Field.Field>
<Control id="create-primary-address-type">
{#snippet children({ props })}
<Field.Label>Type</Field.Label>
<FormSelect
name="primaryAddressType"
bind:value={$createData.primaryAddressType}
options={[
{ value: 'primary', label: 'Primary' },
{ value: 'invoicing', label: 'Invoicing' },
{ value: 'contract', label: 'Contract' }
]}
triggerProps={props}
/>
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
</Field.Field>
</FormField>
<FormField form={createForm} name="primaryAddressLine1">
<Field.Field>
<Control id="create-primary-address-line-1">
{#snippet children({ props })}
<Field.Label>Address line 1</Field.Label>
<Input {...props} type="text" bind:value={$createData.primaryAddressLine1} />
<Input
{...props}
type="text"
required
bind:value={$createData.primaryAddressLine1}
/>
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
@@ -238,7 +211,12 @@
<Control id="create-primary-address-city">
{#snippet children({ props })}
<Field.Label>City</Field.Label>
<Input {...props} type="text" bind:value={$createData.primaryAddressCity} />
<Input
{...props}
type="text"
required
bind:value={$createData.primaryAddressCity}
/>
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
@@ -262,7 +240,12 @@
<Control id="create-primary-address-postcode">
{#snippet children({ props })}
<Field.Label>Postcode</Field.Label>
<Input {...props} type="text" bind:value={$createData.primaryAddressPostcode} />
<Input
{...props}
type="text"
required
bind:value={$createData.primaryAddressPostcode}
/>
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
@@ -76,6 +76,23 @@
<Field.Error><FieldErrors /></Field.Error>
</Field.Field>
</FormField>
<FormField form={createForm} name="confirmPassword">
<Field.Field>
<Control id="create-confirm-password">
{#snippet children({ props })}
<Field.Label>Confirm password</Field.Label>
<Input
{...props}
type="password"
autocomplete="new-password"
placeholder="Repeat password"
bind:value={$createData.confirmPassword}
/>
{/snippet}
</Control>
<Field.Error><FieldErrors /></Field.Error>
</Field.Field>
</FormField>
<FormField form={createForm} name="role">
<Field.Field>
<Control id="create-role">