validatePersonFields now returns a PersonValidationKey instead of a hardcoded German string. resolveValidationMessage() translates the key through Paraglide so English and Spanish locale users no longer see German error text. Adds validation_last_name_required and validation_first_name_required to all three message files. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
151 lines
5.0 KiB
TypeScript
151 lines
5.0 KiB
TypeScript
import { error, fail, redirect } from '@sveltejs/kit';
|
|
import { createApiClient } from '$lib/api.server';
|
|
import { getErrorMessage } from '$lib/errors';
|
|
import {
|
|
normalizePersonType,
|
|
validatePersonFields,
|
|
resolveValidationMessage
|
|
} from '$lib/person-validation';
|
|
|
|
export async function load({ params, fetch, locals }) {
|
|
const canWrite =
|
|
(locals.user as { groups?: { permissions: string[] }[] } | undefined)?.groups?.some((g) =>
|
|
g.permissions.includes('WRITE_ALL')
|
|
) ?? false;
|
|
|
|
if (!canWrite) throw error(403, 'Forbidden');
|
|
|
|
const { id } = params;
|
|
const api = createApiClient(fetch);
|
|
const [result, aliasesResult] = await Promise.all([
|
|
api.GET('/api/persons/{id}', { params: { path: { id } } }),
|
|
api.GET('/api/persons/{id}/aliases', { params: { path: { id } } })
|
|
]);
|
|
|
|
if (!result.response.ok) {
|
|
const code = (result.error as unknown as { code?: string })?.code;
|
|
throw error(result.response.status, getErrorMessage(code));
|
|
}
|
|
|
|
const person = result.data!;
|
|
const personType = normalizePersonType(person.personType);
|
|
return { person: { ...person, personType }, aliases: aliasesResult.data ?? [] };
|
|
}
|
|
|
|
export const actions = {
|
|
update: async ({ request, params, fetch }) => {
|
|
const formData = await request.formData();
|
|
const personType = normalizePersonType(formData.get('personType')?.toString());
|
|
const title = formData.get('title')?.toString().trim() || undefined;
|
|
const firstName = formData.get('firstName')?.toString().trim();
|
|
const lastName = formData.get('lastName')?.toString().trim();
|
|
const alias = formData.get('alias')?.toString().trim() || undefined;
|
|
const notes = formData.get('notes')?.toString().trim() || undefined;
|
|
const birthYearStr = formData.get('birthYear')?.toString().trim();
|
|
const deathYearStr = formData.get('deathYear')?.toString().trim();
|
|
const birthYear = birthYearStr ? parseInt(birthYearStr, 10) : undefined;
|
|
const deathYear = deathYearStr ? parseInt(deathYearStr, 10) : undefined;
|
|
|
|
const validationKey = validatePersonFields(personType, firstName, lastName);
|
|
if (validationKey) {
|
|
return fail(400, { updateError: resolveValidationMessage(validationKey) });
|
|
}
|
|
|
|
const api = createApiClient(fetch);
|
|
const result = await api.PUT('/api/persons/{id}', {
|
|
params: { path: { id: params.id } },
|
|
body: {
|
|
personType,
|
|
...(title ? { title } : {}),
|
|
...(firstName ? { firstName } : {}),
|
|
lastName,
|
|
...(alias ? { alias } : {}),
|
|
...(notes ? { notes } : {}),
|
|
...(birthYear ? { birthYear } : {}),
|
|
...(deathYear ? { deathYear } : {})
|
|
}
|
|
});
|
|
|
|
if (!result.response.ok) {
|
|
const code = (result.error as unknown as { code?: string })?.code;
|
|
return fail(result.response.status, { updateError: getErrorMessage(code) });
|
|
}
|
|
|
|
throw redirect(303, `/persons/${params.id}`);
|
|
},
|
|
|
|
discard: async ({ params }) => {
|
|
throw redirect(303, `/persons/${params.id}`);
|
|
},
|
|
|
|
merge: async ({ request, params, fetch }) => {
|
|
const formData = await request.formData();
|
|
const targetPersonId = formData.get('targetPersonId')?.toString();
|
|
|
|
if (!targetPersonId) {
|
|
return fail(400, { mergeError: 'Bitte eine Zielperson auswählen.' });
|
|
}
|
|
|
|
const api = createApiClient(fetch);
|
|
const result = await api.POST('/api/persons/{id}/merge', {
|
|
params: { path: { id: params.id } },
|
|
body: { targetPersonId }
|
|
});
|
|
|
|
if (!result.response.ok) {
|
|
const code = (result.error as unknown as { code?: string })?.code;
|
|
return fail(result.response.status, { mergeError: getErrorMessage(code) });
|
|
}
|
|
|
|
throw redirect(303, `/persons/${targetPersonId}`);
|
|
},
|
|
|
|
addAlias: async ({ request, params, fetch }) => {
|
|
const formData = await request.formData();
|
|
const lastName = formData.get('lastName')?.toString().trim();
|
|
const firstName = formData.get('firstName')?.toString().trim() || undefined;
|
|
const type = formData.get('type')?.toString();
|
|
|
|
if (!lastName) {
|
|
return fail(400, { aliasError: 'Nachname ist ein Pflichtfeld.' });
|
|
}
|
|
if (!type) {
|
|
return fail(400, { aliasError: 'Art ist ein Pflichtfeld.' });
|
|
}
|
|
|
|
const api = createApiClient(fetch);
|
|
const result = await api.POST('/api/persons/{id}/aliases', {
|
|
params: { path: { id: params.id } },
|
|
body: { lastName, firstName, type: type as 'BIRTH' | 'WIDOWED' | 'DIVORCED' | 'OTHER' }
|
|
});
|
|
|
|
if (!result.response.ok) {
|
|
const code = (result.error as unknown as { code?: string })?.code;
|
|
return fail(result.response.status, { aliasError: getErrorMessage(code) });
|
|
}
|
|
|
|
return { aliasSuccess: true };
|
|
},
|
|
|
|
removeAlias: async ({ request, params, fetch }) => {
|
|
const formData = await request.formData();
|
|
const aliasId = formData.get('aliasId')?.toString();
|
|
|
|
if (!aliasId) {
|
|
return fail(400, { aliasError: 'Alias ID fehlt.' });
|
|
}
|
|
|
|
const api = createApiClient(fetch);
|
|
const result = await api.DELETE('/api/persons/{id}/aliases/{aliasId}', {
|
|
params: { path: { id: params.id, aliasId } }
|
|
});
|
|
|
|
if (!result.response.ok) {
|
|
const code = (result.error as unknown as { code?: string })?.code;
|
|
return fail(result.response.status, { aliasError: getErrorMessage(code) });
|
|
}
|
|
|
|
return { aliasSuccess: true };
|
|
}
|
|
};
|