feat: manage primary address action

This commit is contained in:
2026-06-26 13:04:54 +01:00
parent b2cf967b78
commit d892621e9b
4 changed files with 91 additions and 26 deletions
+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);
@@ -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">