Refactor: renamed AccountForm to UserSettingsForm and update related actions and translations

This commit is contained in:
Knee Cola
2025-11-18 12:52:43 +01:00
parent b03f0574cd
commit be5d5074fe
8 changed files with 282 additions and 52 deletions

View File

@@ -2,7 +2,7 @@
import { z } from 'zod';
import { getDbClient } from '../dbClient';
import { UserProfile } from '../db-types';
import { UserSettings } from '../db-types';
import { withUser } from '@/app/lib/auth';
import { AuthenticatedUser } from '../types/next-auth';
import { unstable_noStore as noStore } from 'next/cache';
@@ -25,7 +25,7 @@ export type State = {
};
/**
* Schema for validating user profile form fields
* Schema for validating user settings form fields
*/
const FormSchema = (t: IntlTemplateFn) => z.object({
firstName: z.string().optional(),
@@ -87,27 +87,27 @@ const FormSchema = (t: IntlTemplateFn) => z.object({
});
/**
* Get user profile
* Get user settings
*/
export const getUserProfile = withUser(async (user: AuthenticatedUser) => {
export const getUserSettings = withUser(async (user: AuthenticatedUser) => {
noStore();
const dbClient = await getDbClient();
const { id: userId } = user;
const profile = await dbClient.collection<UserProfile>("users")
const userSettings = await dbClient.collection<UserSettings>("userSettings")
.findOne({ userId });
return profile;
return userSettings;
});
/**
* Update user profile
* Update user settings
*/
export const updateUserProfile = withUser(async (user: AuthenticatedUser, prevState: State, formData: FormData) => {
export const updateUserSettings = withUser(async (user: AuthenticatedUser, prevState: State, formData: FormData) => {
noStore();
const t = await getTranslations("settings-form.validation");
const t = await getTranslations("user-settings-form.validation");
const validatedFields = FormSchema(t).safeParse({
firstName: formData.get('firstName') || undefined,
@@ -131,11 +131,11 @@ export const updateUserProfile = withUser(async (user: AuthenticatedUser, prevSt
// Normalize IBAN: remove spaces and convert to uppercase
const normalizedIban = iban ? iban.replace(/\s/g, '').toUpperCase() : null;
// Update the user profile in MongoDB
// Update the user settings in MongoDB
const dbClient = await getDbClient();
const { id: userId } = user;
const userProfile: UserProfile = {
const userSettings: UserSettings = {
userId,
firstName: firstName || null,
lastName: lastName || null,
@@ -144,18 +144,18 @@ export const updateUserProfile = withUser(async (user: AuthenticatedUser, prevSt
show2dCodeInMonthlyStatement: show2dCodeInMonthlyStatement ?? false,
};
await dbClient.collection<UserProfile>("users")
await dbClient.collection<UserSettings>("userSettings")
.updateOne(
{ userId },
{ $set: userProfile },
{ $set: userSettings },
{ upsert: true }
);
revalidatePath('/account');
revalidatePath('/settings');
// Get current locale and redirect to home with success message
const locale = await getLocale();
await gotoHomeWithMessage(locale, 'profileSaved');
await gotoHomeWithMessage(locale, 'user-settings-saved-message');
// This return is needed for TypeScript, but won't be reached due to redirect
return {

View File

@@ -14,8 +14,8 @@ export interface YearMonth {
month: number;
};
/** User profile data */
export interface UserProfile {
/** User settings data */
export interface UserSettings {
/** user's ID */
userId: string;
/** first name */