Refactor data handlers + misc fixes

This refactors the data handlers into a more standard/understood model-like architecture, to prepare for a new, more robust config system.

It also fixes a problem with creating new Notes and uploading new Photos via the web interface (related to #58).

Finally, it speeds up docker builds by sending in less files, which aren't necessary or will be built anyway.

This is all in preparation to allow building #13 more robustly.
This commit is contained in:
Bruno Bernardino
2025-05-24 08:24:10 +01:00
parent e1193a2770
commit 6cfb62d1a2
61 changed files with 1822 additions and 1774 deletions

View File

@@ -2,13 +2,7 @@ import { Handlers, PageProps } from 'fresh/server.ts';
import { currencyMap, FreshContextState, SupportedCurrencySymbol } from '/lib/types.ts';
import { PASSWORD_SALT } from '/lib/auth.ts';
import {
createVerificationCode,
deleteUser,
getUserByEmail,
updateUser,
validateVerificationCode,
} from '/lib/data/user.ts';
import { UserModel, VerificationCodeModel } from '/lib/models/user.ts';
import { convertFormDataToObject, generateHash, validateEmail } from '/lib/utils/misc.ts';
import { getFormDataField } from '/lib/form-utils.tsx';
import { sendVerifyEmailEmail } from '/lib/providers/brevo.ts';
@@ -72,14 +66,14 @@ export const handler: Handlers<Data, FreshContextState> = {
throw new Error(`New email is the same as the current email.`);
}
const matchingUser = await getUserByEmail(email);
const matchingUser = await UserModel.getByEmail(email);
if (matchingUser) {
throw new Error('Email is already in use.');
}
if (action === 'change-email' && isEmailEnabled()) {
const verificationCode = await createVerificationCode(user, email, 'email');
const verificationCode = await VerificationCodeModel.create(user, email, 'email');
await sendVerifyEmailEmail(email, verificationCode);
@@ -89,12 +83,12 @@ export const handler: Handlers<Data, FreshContextState> = {
if (isEmailEnabled()) {
const code = getFormDataField(formData, 'verification-code');
await validateVerificationCode(user, email, code, 'email');
await VerificationCodeModel.validate(user, email, code, 'email');
}
user.email = email;
await updateUser(user);
await UserModel.update(user);
successTitle = 'Email updated!';
successMessage = 'Email updated successfully.';
@@ -120,7 +114,7 @@ export const handler: Handlers<Data, FreshContextState> = {
user.hashed_password = hashedNewPassword;
await updateUser(user);
await UserModel.update(user);
successTitle = 'Password changed!';
successMessage = 'Password changed successfully.';
@@ -139,7 +133,7 @@ export const handler: Handlers<Data, FreshContextState> = {
user.extra.dav_hashed_password = hashedNewDavPassword;
await updateUser(user);
await UserModel.update(user);
successTitle = 'DAV Password changed!';
successMessage = 'DAV Password changed successfully.';
@@ -152,7 +146,7 @@ export const handler: Handlers<Data, FreshContextState> = {
throw new Error('Invalid current password.');
}
await deleteUser(user.id);
await UserModel.delete(user.id);
return new Response('Account deleted successfully', {
status: 303,
@@ -167,7 +161,7 @@ export const handler: Handlers<Data, FreshContextState> = {
user.extra.expenses_currency = newCurrencySymbol;
await updateUser(user);
await UserModel.update(user);
successTitle = 'Currency changed!';
successMessage = 'Currency changed successfully.';