Populate paymentParams with actual data from database

- Updated ViewLocationCard to accept userSettings prop
- Replaced all hardcoded payment values with dynamic data:
  * Amount calculated from monthly expenses
  * Payer info from tenant fields (name, street, town)
  * Recipient info from userSettings (name, street, town, IBAN)
  * Reference number and description generated from location data
- Created getUserSettingsByUserId function for fetching owner settings on public pages
- Updated LocationViewPage to fetch and pass userSettings to ViewLocationCard

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Knee Cola
2025-11-22 15:47:07 +01:00
parent ec39eda51f
commit 280e2ec029
3 changed files with 39 additions and 17 deletions

View File

@@ -1,5 +1,6 @@
import { ViewLocationCard } from '@/app/ui/ViewLocationCard'; import { ViewLocationCard } from '@/app/ui/ViewLocationCard';
import { fetchLocationById, setSeenByTenant } from '@/app/lib/actions/locationActions'; import { fetchLocationById, setSeenByTenant } from '@/app/lib/actions/locationActions';
import { getUserSettingsByUserId } from '@/app/lib/actions/userSettingsActions';
import { notFound } from 'next/navigation'; import { notFound } from 'next/navigation';
import { myAuth } from '@/app/lib/auth'; import { myAuth } from '@/app/lib/auth';
@@ -10,14 +11,17 @@ export default async function LocationViewPage({ locationId }: { locationId:stri
return(notFound()); return(notFound());
} }
// Fetch user settings for the location owner
const userSettings = await getUserSettingsByUserId(location.userId);
// Check if the page was accessed by an authenticated user who is the owner // Check if the page was accessed by an authenticated user who is the owner
const session = await myAuth(); const session = await myAuth();
const isOwner = session?.user?.id === location.userId; const isOwner = session?.user?.id === location.userId;
// If the page is not visited by the owner, mark it as seen by tenant // If the page is not visited by the owner, mark it as seen by tenant
if (!isOwner) { if (!isOwner) {
await setSeenByTenant(locationId); await setSeenByTenant(locationId);
} }
return (<ViewLocationCard location={location} />); return (<ViewLocationCard location={location} userSettings={userSettings} />);
} }

View File

@@ -123,6 +123,21 @@ export const getUserSettings = withUser(async (user: AuthenticatedUser) => {
return userSettings; 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 * Update user settings
*/ */

View File

@@ -1,7 +1,7 @@
'use client'; 'use client';
import { FC } from "react"; import { FC } from "react";
import { BilledTo, BillingLocation } from "../lib/db-types"; import { BilledTo, BillingLocation, UserSettings } from "../lib/db-types";
import { formatYearMonth } from "../lib/format"; import { formatYearMonth } from "../lib/format";
import { formatCurrency } from "../lib/formatStrings"; import { formatCurrency } from "../lib/formatStrings";
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
@@ -10,10 +10,13 @@ import { Pdf417Barcode } from "./Pdf417Barcode";
import { PaymentParams } from "hub-3a-payment-encoder"; import { PaymentParams } from "hub-3a-payment-encoder";
export interface ViewLocationCardProps { export interface ViewLocationCardProps {
location: BillingLocation location: BillingLocation;
userSettings: UserSettings | null;
} }
export const ViewLocationCard:FC<ViewLocationCardProps> = ({location: { _id, name, yearMonth, bills }}) => { export const ViewLocationCard:FC<ViewLocationCardProps> = ({location, userSettings}) => {
const { _id, name, yearMonth, bills, tenantFirstName, tenantLastName, tenantStreet, tenantTown } = location;
const t = useTranslations("home-page.location-card"); const t = useTranslations("home-page.location-card");
@@ -21,18 +24,18 @@ export const ViewLocationCard:FC<ViewLocationCardProps> = ({location: { _id, nam
const monthlyExpense = bills.reduce((acc, bill) => (bill.paid && (bill.billedTo ?? BilledTo.Tenant) === BilledTo.Tenant) ? acc + (bill.payedAmount ?? 0) : acc, 0); const monthlyExpense = bills.reduce((acc, bill) => (bill.paid && (bill.billedTo ?? BilledTo.Tenant) === BilledTo.Tenant) ? acc + (bill.payedAmount ?? 0) : acc, 0);
const paymentParams:PaymentParams = { const paymentParams:PaymentParams = {
Iznos:"123,66", // NOTE: use comma, not period! Iznos: (monthlyExpense/100).toFixed(2).replace(".",","),
ImePlatitelja:"Ivan Horvat", ImePlatitelja: (tenantFirstName && tenantLastName) ? `${tenantFirstName} ${tenantLastName}` : "",
AdresaPlatitelja:"Ilica 23", AdresaPlatitelja: tenantStreet ?? "",
SjedistePlatitelja:"10000 Zagreb", SjedistePlatitelja: tenantTown ?? "",
Primatelj:"Nikola Derežić", Primatelj: (userSettings?.firstName && userSettings?.lastName) ? `${userSettings.firstName} ${userSettings.lastName}` : "",
AdresaPrimatelja:"Divka Budaka 17", AdresaPrimatelja: userSettings?.street ?? "",
SjedistePrimatelja:"Zagreb", SjedistePrimatelja: userSettings?.town ?? "",
IBAN:"HR8924020061100679445", IBAN: userSettings?.iban ?? "",
ModelPlacanja: "HR00", // MUST contain "HR" prefix! ModelPlacanja: "HR00",
PozivNaBroj:"2025-05", PozivNaBroj: `${yearMonth.year}-${yearMonth.month.toString().padStart(2,"0")}`,
SifraNamjene:"", SifraNamjene: "",
OpisPlacanja:"Režije-Budakova-05", OpisPlacanja: `Režije-${name}-${yearMonth.month.toString().padStart(2,"0")}`,
}; };
return( return(