refactor: convert repository to monorepo with npm workspaces
Restructured the repository into a monorepo to better organize application code and maintenance scripts. ## Workspace Structure - web-app: Next.js application (all app code moved from root) - housekeeping: Database backup and maintenance scripts ## Key Changes - Moved all application code to web-app/ using git mv - Moved database scripts to housekeeping/ workspace - Updated Dockerfile for monorepo build process - Updated docker-compose files (volume paths: ./web-app/etc/hosts/) - Updated .gitignore for workspace-level node_modules - Updated documentation (README.md, CLAUDE.md, CHANGELOG.md) ## Migration Impact - Root package.json now manages workspaces - Build commands delegate to web-app workspace - All file history preserved via git mv - Docker build process updated for workspace structure 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
220
web-app/app/lib/actions/userSettingsActions.ts
Normal file
220
web-app/app/lib/actions/userSettingsActions.ts
Normal file
@@ -0,0 +1,220 @@
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
import { getDbClient } from '../dbClient';
|
||||
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';
|
||||
import { IntlTemplateFn } from '@/app/i18n';
|
||||
import { getTranslations, getLocale } from "next-intl/server";
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { gotoHomeWithMessage } from './navigationActions';
|
||||
import * as IBAN from 'iban';
|
||||
|
||||
export type State = {
|
||||
errors?: {
|
||||
ownerName?: string[];
|
||||
ownerStreet?: string[];
|
||||
ownerTown?: string[];
|
||||
ownerIBAN?: string[];
|
||||
currency?: string[];
|
||||
ownerRevolutProfileName?: string[];
|
||||
};
|
||||
message?: string | null;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Schema for validating user settings form fields
|
||||
*/
|
||||
const FormSchema = (t: IntlTemplateFn) => z.object({
|
||||
currency: z.string().optional(),
|
||||
enableIbanPayment: z.boolean().optional(),
|
||||
ownerName: z.string().max(25).optional(),
|
||||
ownerStreet: z.string().max(25).optional(),
|
||||
ownerTown: z.string().max(27).optional(),
|
||||
ownerIBAN: z.string()
|
||||
.optional()
|
||||
.refine(
|
||||
(val) => {
|
||||
if (!val || val.trim() === '') return true;
|
||||
// Remove spaces and validate using iban.js library
|
||||
const cleaned = val.replace(/\s/g, '').toUpperCase();
|
||||
return IBAN.isValid(cleaned);
|
||||
},
|
||||
{ message: t("owner-iban-invalid") }
|
||||
),
|
||||
enableRevolutPayment: z.boolean().optional(),
|
||||
ownerRevolutProfileName: z.string().max(25).optional(),
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.enableIbanPayment) {
|
||||
return !!data.ownerName && data.ownerName.trim().length > 0;
|
||||
}
|
||||
return true;
|
||||
}, {
|
||||
message: t("owner-name-required"),
|
||||
path: ["ownerName"],
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.enableIbanPayment) {
|
||||
return !!data.ownerStreet && data.ownerStreet.trim().length > 0;
|
||||
}
|
||||
return true;
|
||||
}, {
|
||||
message: t("owner-street-required"),
|
||||
path: ["ownerStreet"],
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.enableIbanPayment) {
|
||||
return !!data.ownerTown && data.ownerTown.trim().length > 0;
|
||||
}
|
||||
return true;
|
||||
}, {
|
||||
message: t("owner-town-required"),
|
||||
path: ["ownerTown"],
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.enableIbanPayment) {
|
||||
if (!data.ownerIBAN || data.ownerIBAN.trim().length === 0) {
|
||||
return false;
|
||||
}
|
||||
// Validate IBAN format when required
|
||||
const cleaned = data.ownerIBAN.replace(/\s/g, '').toUpperCase();
|
||||
return IBAN.isValid(cleaned);
|
||||
}
|
||||
return true;
|
||||
}, {
|
||||
message: t("owner-iban-required"),
|
||||
path: ["ownerIBAN"],
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.enableIbanPayment) {
|
||||
return !!data.currency && data.currency.trim().length > 0;
|
||||
}
|
||||
return true;
|
||||
}, {
|
||||
message: t("currency-required"),
|
||||
path: ["currency"],
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.enableRevolutPayment) {
|
||||
return !!data.ownerRevolutProfileName && data.ownerRevolutProfileName.trim().length > 0;
|
||||
}
|
||||
return true;
|
||||
}, {
|
||||
message: t("owner-revolut-profile-required"),
|
||||
path: ["ownerRevolutProfileName"],
|
||||
})
|
||||
.refine((data) => {
|
||||
if (data.enableRevolutPayment && data.ownerRevolutProfileName) {
|
||||
const profileName = data.ownerRevolutProfileName.trim();
|
||||
// Must start with @ and contain only English letters and numbers
|
||||
return /^@[a-zA-Z0-9]+$/.test(profileName);
|
||||
}
|
||||
return true;
|
||||
}, {
|
||||
message: t("owner-revolut-profile-invalid"),
|
||||
path: ["ownerRevolutProfileName"],
|
||||
});
|
||||
|
||||
/**
|
||||
* Get user settings
|
||||
*/
|
||||
export const getUserSettings = withUser(async (user: AuthenticatedUser) => {
|
||||
noStore();
|
||||
|
||||
const dbClient = await getDbClient();
|
||||
const { id: userId } = user;
|
||||
|
||||
const userSettings = await dbClient.collection<UserSettings>("userSettings")
|
||||
.findOne({ userId });
|
||||
|
||||
return userSettings;
|
||||
});
|
||||
|
||||
/**
|
||||
* Get user settings by userId (without authentication)
|
||||
* Used for public/shared pages where we need to display owner's payment information
|
||||
*/
|
||||
export const getUserSettingsByUserId = async (userId: string): Promise<UserSettings | null> => {
|
||||
noStore();
|
||||
|
||||
const dbClient = await getDbClient();
|
||||
|
||||
const userSettings = await dbClient.collection<UserSettings>("userSettings")
|
||||
.findOne({ userId });
|
||||
|
||||
return userSettings;
|
||||
};
|
||||
|
||||
/**
|
||||
* Update user settings
|
||||
*/
|
||||
export const updateUserSettings = withUser(async (user: AuthenticatedUser, prevState: State, formData: FormData) => {
|
||||
noStore();
|
||||
|
||||
const t = await getTranslations("user-settings-form.validation");
|
||||
|
||||
const validatedFields = FormSchema(t).safeParse({
|
||||
ownerName: formData.get('ownerName') || undefined,
|
||||
ownerStreet: formData.get('ownerStreet') || undefined,
|
||||
ownerTown: formData.get('ownerTown') || undefined,
|
||||
ownerIBAN: formData.get('ownerIBAN') || undefined,
|
||||
currency: formData.get('currency') || undefined,
|
||||
enableIbanPayment: formData.get('enableIbanPayment') === 'on' ? true : false,
|
||||
enableRevolutPayment: formData.get('enableRevolutPayment') === 'on' ? true : false,
|
||||
ownerRevolutProfileName: formData.get('ownerRevolutProfileName') || undefined,
|
||||
});
|
||||
|
||||
// If form validation fails, return errors early. Otherwise, continue...
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
errors: validatedFields.error.flatten().fieldErrors,
|
||||
message: t("validation-failed"),
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const { enableIbanPayment, ownerName, ownerStreet, ownerTown, ownerIBAN, currency, enableRevolutPayment, ownerRevolutProfileName } = validatedFields.data;
|
||||
|
||||
// Normalize IBAN: remove spaces and convert to uppercase
|
||||
const normalizedOwnerIBAN = ownerIBAN ? ownerIBAN.replace(/\s/g, '').toUpperCase() : null;
|
||||
|
||||
// Update the user settings in MongoDB
|
||||
const dbClient = await getDbClient();
|
||||
const { id: userId } = user;
|
||||
|
||||
const userSettings: UserSettings = {
|
||||
userId,
|
||||
enableIbanPayment: enableIbanPayment ?? false,
|
||||
ownerName: ownerName ?? undefined,
|
||||
ownerStreet: ownerStreet ?? undefined,
|
||||
ownerTown: ownerTown ?? undefined,
|
||||
ownerIBAN: normalizedOwnerIBAN,
|
||||
currency: currency ?? undefined,
|
||||
enableRevolutPayment: enableRevolutPayment ?? false,
|
||||
ownerRevolutProfileName: ownerRevolutProfileName ?? undefined,
|
||||
};
|
||||
|
||||
await dbClient.collection<UserSettings>("userSettings")
|
||||
.updateOne(
|
||||
{ userId },
|
||||
{ $set: userSettings },
|
||||
{ upsert: true }
|
||||
);
|
||||
|
||||
revalidatePath('/settings');
|
||||
|
||||
// Get current locale and redirect to home with success message
|
||||
const locale = await getLocale();
|
||||
await gotoHomeWithMessage(locale, 'userSettingsSaved');
|
||||
|
||||
// This return is needed for TypeScript, but won't be reached due to redirect
|
||||
return {
|
||||
message: null,
|
||||
errors: {},
|
||||
success: true,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user