Add complete settlement system with visual distinction
- Add settlement category with handshake emoji (🤝) - Create settlement page for recording debt payments with user → user flow - Implement settlement detection and visual styling across all views - Add conditional "Settle Debts" button (hidden when balance is 0) - Style settlement payments distinctly in recent activity with large profile pictures - Add settlement flow styling in payments overview with green theme - Update backend validation and Mongoose schema for settlement category - Fix settlement receiver detection with proper user flow logic 🤝 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -43,16 +43,6 @@
|
||||
// Recalculate when debtData changes
|
||||
singleDebtUser = getSingleDebtUser();
|
||||
shouldShowIntegratedView = singleDebtUser !== null;
|
||||
|
||||
// Temporary debug logging
|
||||
if (!loading) {
|
||||
console.log('🔍 Debug Info:');
|
||||
console.log('- debtData:', debtData);
|
||||
console.log('- whoOwesMe length:', debtData.whoOwesMe.length);
|
||||
console.log('- whoIOwe length:', debtData.whoIOwe.length);
|
||||
console.log('- singleDebtUser:', singleDebtUser);
|
||||
console.log('- shouldShowIntegratedView:', shouldShowIntegratedView);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -22,6 +22,10 @@ export const PAYMENT_CATEGORIES = {
|
||||
fun: {
|
||||
name: 'Fun',
|
||||
emoji: '🎉'
|
||||
},
|
||||
settlement: {
|
||||
name: 'Settlement',
|
||||
emoji: '🤝'
|
||||
}
|
||||
} as const;
|
||||
|
||||
|
63
src/lib/utils/settlements.ts
Normal file
63
src/lib/utils/settlements.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
// Utility functions for identifying and handling settlement payments
|
||||
|
||||
/**
|
||||
* Identifies if a payment is a settlement payment based on category
|
||||
*/
|
||||
export function isSettlementPayment(payment: any): boolean {
|
||||
if (!payment) return false;
|
||||
|
||||
// Check if category is settlement
|
||||
return payment.category === 'settlement';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the settlement icon for settlement payments
|
||||
*/
|
||||
export function getSettlementIcon(): string {
|
||||
return '🤝'; // Handshake emoji for settlements
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets appropriate styling classes for settlement payments
|
||||
*/
|
||||
export function getSettlementClasses(payment: any): string[] {
|
||||
if (!isSettlementPayment(payment)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return ['settlement-payment'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets settlement-specific display text
|
||||
*/
|
||||
export function getSettlementDisplayText(payment: any): string {
|
||||
if (!isSettlementPayment(payment)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return 'Settlement';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the other user in a settlement (the one who didn't pay)
|
||||
*/
|
||||
export function getSettlementReceiver(payment: any): string {
|
||||
if (!isSettlementPayment(payment) || !payment.splits) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Find the user who has a positive amount (the receiver)
|
||||
const receiver = payment.splits.find(split => split.amount > 0);
|
||||
if (receiver && receiver.username) {
|
||||
return receiver.username;
|
||||
}
|
||||
|
||||
// Fallback: find the user who is not the payer
|
||||
const otherUser = payment.splits.find(split => split.username !== payment.paidBy);
|
||||
if (otherUser && otherUser.username) {
|
||||
return otherUser.username;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
@@ -9,7 +9,7 @@ export interface IPayment {
|
||||
paidBy: string; // username/nickname of the person who paid
|
||||
date: Date;
|
||||
image?: string; // path to uploaded image
|
||||
category: 'groceries' | 'shopping' | 'travel' | 'restaurant' | 'utilities' | 'fun';
|
||||
category: 'groceries' | 'shopping' | 'travel' | 'restaurant' | 'utilities' | 'fun' | 'settlement';
|
||||
splitMethod: 'equal' | 'full' | 'proportional' | 'personal_equal';
|
||||
createdBy: string; // username/nickname of the person who created the payment
|
||||
createdAt?: Date;
|
||||
@@ -55,7 +55,7 @@ const PaymentSchema = new mongoose.Schema(
|
||||
category: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: ['groceries', 'shopping', 'travel', 'restaurant', 'utilities', 'fun'],
|
||||
enum: ['groceries', 'shopping', 'travel', 'restaurant', 'utilities', 'fun', 'settlement'],
|
||||
default: 'groceries'
|
||||
},
|
||||
splitMethod: {
|
||||
|
@@ -61,6 +61,21 @@ export const GET: RequestHandler = async ({ locals, url }) => {
|
||||
.limit(10)
|
||||
.lean();
|
||||
|
||||
// For settlements, fetch the other user's split info
|
||||
for (const split of recentSplits) {
|
||||
if (split.paymentId && split.paymentId.category === 'settlement') {
|
||||
// This is a settlement, find the other user
|
||||
const otherSplit = await PaymentSplit.findOne({
|
||||
paymentId: split.paymentId._id,
|
||||
username: { $ne: username }
|
||||
}).lean();
|
||||
|
||||
if (otherSplit) {
|
||||
split.otherUser = otherSplit.username;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return json({
|
||||
netBalance,
|
||||
recentSplits
|
||||
|
@@ -52,7 +52,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
throw error(400, 'Invalid split method');
|
||||
}
|
||||
|
||||
if (category && !['groceries', 'shopping', 'travel', 'restaurant', 'utilities', 'fun'].includes(category)) {
|
||||
if (category && !['groceries', 'shopping', 'travel', 'restaurant', 'utilities', 'fun', 'settlement'].includes(category)) {
|
||||
throw error(400, 'Invalid category');
|
||||
}
|
||||
|
||||
|
@@ -6,6 +6,7 @@
|
||||
import EnhancedBalance from '$lib/components/EnhancedBalance.svelte';
|
||||
import DebtBreakdown from '$lib/components/DebtBreakdown.svelte';
|
||||
import { getCategoryEmoji, getCategoryName } from '$lib/utils/categories';
|
||||
import { isSettlementPayment, getSettlementIcon, getSettlementClasses, getSettlementReceiver } from '$lib/utils/settlements';
|
||||
|
||||
export let data; // Used by the layout for session data
|
||||
|
||||
@@ -57,6 +58,27 @@
|
||||
// Use pushState for true shallow routing - only updates URL without navigation
|
||||
pushState(`/cospend/payments/view/${paymentId}`, { paymentId });
|
||||
}
|
||||
|
||||
function getSettlementReceiverFromSplit(split) {
|
||||
if (!isSettlementPayment(split.paymentId)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// In a settlement, the receiver is the person who is NOT the payer
|
||||
// Since we're viewing the current user's activity, the receiver is the current user
|
||||
// when someone else paid, or the other user when current user paid
|
||||
|
||||
const paidBy = split.paymentId?.paidBy;
|
||||
const currentUser = data.session?.user?.nickname;
|
||||
|
||||
if (paidBy === currentUser) {
|
||||
// Current user paid, so receiver is the other user
|
||||
return split.otherUser || '';
|
||||
} else {
|
||||
// Someone else paid, so current user is the receiver
|
||||
return currentUser;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -74,6 +96,9 @@
|
||||
<div class="actions">
|
||||
<a href="/cospend/payments/add" class="btn btn-primary">Add Payment</a>
|
||||
<a href="/cospend/payments" class="btn btn-secondary">View All Payments</a>
|
||||
{#if balance.netBalance !== 0}
|
||||
<a href="/cospend/settle" class="btn btn-settlement">Settle Debts</a>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<DebtBreakdown />
|
||||
@@ -87,46 +112,79 @@
|
||||
<h2>Recent Activity</h2>
|
||||
<div class="activity-dialog">
|
||||
{#each balance.recentSplits as split}
|
||||
<div class="activity-message" class:is-me={split.paymentId?.paidBy === data.session?.user?.nickname}>
|
||||
<div class="message-content">
|
||||
<ProfilePicture username={split.paymentId?.paidBy || 'Unknown'} size={36} />
|
||||
<a
|
||||
href="/cospend/payments/view/{split.paymentId?._id}"
|
||||
class="activity-bubble"
|
||||
on:click={(e) => handlePaymentClick(split.paymentId?._id, e)}
|
||||
>
|
||||
<div class="activity-header">
|
||||
<div class="user-info">
|
||||
<div class="payment-title-row">
|
||||
<span class="category-emoji">{getCategoryEmoji(split.paymentId?.category || 'groceries')}</span>
|
||||
<strong class="payment-title">{split.paymentId?.title || 'Payment'}</strong>
|
||||
</div>
|
||||
<span class="username">Paid by {split.paymentId?.paidBy || 'Unknown'}</span>
|
||||
<span class="category-name">{getCategoryName(split.paymentId?.category || 'groceries')}</span>
|
||||
{#if isSettlementPayment(split.paymentId)}
|
||||
<!-- Settlement Payment Display - User -> User Flow -->
|
||||
<a
|
||||
href="/cospend/payments/view/{split.paymentId?._id}"
|
||||
class="settlement-flow-activity"
|
||||
on:click={(e) => handlePaymentClick(split.paymentId?._id, e)}
|
||||
>
|
||||
<div class="settlement-activity-content">
|
||||
<div class="settlement-user-flow">
|
||||
<div class="settlement-payer">
|
||||
<ProfilePicture username={split.paymentId?.paidBy || 'Unknown'} size={64} />
|
||||
<span class="settlement-username">{split.paymentId?.paidBy || 'Unknown'}</span>
|
||||
</div>
|
||||
<div class="activity-amount" class:positive={split.amount < 0} class:negative={split.amount > 0}>
|
||||
{#if split.amount > 0}
|
||||
-{formatCurrency(split.amount)}
|
||||
{:else if split.amount < 0}
|
||||
+{formatCurrency(split.amount)}
|
||||
{:else}
|
||||
even
|
||||
<div class="settlement-arrow-section">
|
||||
<div class="settlement-amount-large">
|
||||
{formatCurrency(Math.abs(split.amount))}
|
||||
</div>
|
||||
<div class="settlement-flow-arrow">→</div>
|
||||
<div class="settlement-date">{formatDate(split.createdAt)}</div>
|
||||
</div>
|
||||
<div class="settlement-receiver">
|
||||
<ProfilePicture username={getSettlementReceiverFromSplit(split) || 'Unknown'} size={64} />
|
||||
<span class="settlement-username">{getSettlementReceiverFromSplit(split) || 'Unknown'}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
{:else}
|
||||
<!-- Regular Payment Display - Speech Bubble Style -->
|
||||
<div class="activity-message"
|
||||
class:is-me={split.paymentId?.paidBy === data.session?.user?.nickname}>
|
||||
<div class="message-content">
|
||||
<ProfilePicture username={split.paymentId?.paidBy || 'Unknown'} size={36} />
|
||||
<a
|
||||
href="/cospend/payments/view/{split.paymentId?._id}"
|
||||
class="activity-bubble"
|
||||
on:click={(e) => handlePaymentClick(split.paymentId?._id, e)}
|
||||
>
|
||||
<div class="activity-header">
|
||||
<div class="user-info">
|
||||
<div class="payment-title-row">
|
||||
<span class="category-emoji">{getCategoryEmoji(split.paymentId?.category || 'groceries')}</span>
|
||||
<strong class="payment-title">{split.paymentId?.title || 'Payment'}</strong>
|
||||
</div>
|
||||
<span class="username">Paid by {split.paymentId?.paidBy || 'Unknown'}</span>
|
||||
<span class="category-name">{getCategoryName(split.paymentId?.category || 'groceries')}</span>
|
||||
</div>
|
||||
<div class="activity-amount"
|
||||
class:positive={split.amount < 0}
|
||||
class:negative={split.amount > 0}>
|
||||
{#if split.amount > 0}
|
||||
-{formatCurrency(split.amount)}
|
||||
{:else if split.amount < 0}
|
||||
+{formatCurrency(split.amount)}
|
||||
{:else}
|
||||
even
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<div class="payment-details">
|
||||
<div class="payment-meta">
|
||||
<span class="payment-date">{formatDate(split.createdAt)}</span>
|
||||
</div>
|
||||
{#if split.paymentId?.description}
|
||||
<div class="payment-description">
|
||||
{truncateDescription(split.paymentId.description)}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<div class="payment-details">
|
||||
<div class="payment-meta">
|
||||
<span class="payment-date">{formatDate(split.createdAt)}</span>
|
||||
</div>
|
||||
{#if split.paymentId?.description}
|
||||
<div class="payment-description">
|
||||
{truncateDescription(split.paymentId.description)}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</a>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
@@ -216,6 +274,16 @@
|
||||
background-color: #e8e8e8;
|
||||
}
|
||||
|
||||
.btn-settlement {
|
||||
background: linear-gradient(135deg, #28a745, #20c997);
|
||||
color: white;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.btn-settlement:hover {
|
||||
background: linear-gradient(135deg, #20c997, #1e7e34);
|
||||
}
|
||||
|
||||
.recent-activity {
|
||||
background: white;
|
||||
padding: 1.5rem;
|
||||
@@ -300,6 +368,79 @@
|
||||
border-right-color: transparent;
|
||||
}
|
||||
|
||||
|
||||
/* New Settlement Flow Activity Styles */
|
||||
.settlement-flow-activity {
|
||||
display: block;
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
background: linear-gradient(135deg, #f8fff9, #e8f5e8);
|
||||
border: 2px solid #28a745;
|
||||
border-radius: 1rem;
|
||||
padding: 1.5rem;
|
||||
margin: 0 auto 1rem auto;
|
||||
max-width: 400px;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.settlement-flow-activity:hover {
|
||||
box-shadow: 0 6px 20px rgba(40, 167, 69, 0.2);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.settlement-activity-content {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.settlement-user-flow {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.settlement-payer, .settlement-receiver {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
|
||||
.settlement-username {
|
||||
font-weight: 600;
|
||||
color: #28a745;
|
||||
font-size: 1rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.settlement-arrow-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.settlement-amount-large {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
color: #28a745;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.settlement-flow-arrow {
|
||||
font-size: 1.8rem;
|
||||
color: #28a745;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.settlement-date {
|
||||
font-size: 0.9rem;
|
||||
color: #666;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.activity-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
@@ -399,5 +540,28 @@
|
||||
max-width: 300px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
/* Mobile Settlement Flow */
|
||||
.settlement-user-flow {
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.settlement-payer, .settlement-receiver {
|
||||
order: 1;
|
||||
}
|
||||
|
||||
.settlement-arrow-section {
|
||||
order: 2;
|
||||
}
|
||||
|
||||
.settlement-flow-arrow {
|
||||
transform: rotate(90deg);
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.settlement-amount-large {
|
||||
font-size: 1.3rem;
|
||||
}
|
||||
}
|
||||
</style>
|
@@ -3,6 +3,7 @@
|
||||
import { goto } from '$app/navigation';
|
||||
import ProfilePicture from '$lib/components/ProfilePicture.svelte';
|
||||
import { getCategoryEmoji, getCategoryName } from '$lib/utils/categories';
|
||||
import { isSettlementPayment, getSettlementIcon, getSettlementReceiver } from '$lib/utils/settlements';
|
||||
|
||||
export let data;
|
||||
|
||||
@@ -135,24 +136,45 @@
|
||||
{:else}
|
||||
<div class="payments-grid">
|
||||
{#each payments as payment}
|
||||
<div class="payment-card">
|
||||
<div class="payment-card" class:settlement-card={isSettlementPayment(payment)}>
|
||||
<div class="payment-header">
|
||||
<div class="payment-title-section">
|
||||
<ProfilePicture username={payment.paidBy} size={40} />
|
||||
<div class="payment-title">
|
||||
<div class="title-with-category">
|
||||
<span class="category-emoji">{getCategoryEmoji(payment.category || 'groceries')}</span>
|
||||
<h3>{payment.title}</h3>
|
||||
{#if isSettlementPayment(payment)}
|
||||
<div class="settlement-flow">
|
||||
<div class="settlement-user-from">
|
||||
<ProfilePicture username={payment.paidBy} size={32} />
|
||||
<span class="username">{payment.paidBy}</span>
|
||||
</div>
|
||||
<div class="payment-meta">
|
||||
<span class="category-name">{getCategoryName(payment.category || 'groceries')}</span>
|
||||
<span class="date">{formatDate(payment.date)}</span>
|
||||
<span class="amount">{formatCurrency(payment.amount)}</span>
|
||||
<div class="settlement-arrow">
|
||||
<span class="arrow">→</span>
|
||||
<span class="settlement-badge-small">Settlement</span>
|
||||
</div>
|
||||
<div class="settlement-user-to">
|
||||
<ProfilePicture username={getSettlementReceiver(payment)} size={32} />
|
||||
<span class="username">{getSettlementReceiver(payment)}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{#if payment.image}
|
||||
<img src={payment.image} alt="Receipt" class="receipt-thumb" />
|
||||
<div class="settlement-amount">
|
||||
<span class="amount settlement-amount-text">{formatCurrency(payment.amount)}</span>
|
||||
<span class="date">{formatDate(payment.date)}</span>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="payment-title-section">
|
||||
<ProfilePicture username={payment.paidBy} size={40} />
|
||||
<div class="payment-title">
|
||||
<div class="title-with-category">
|
||||
<span class="category-emoji">{getCategoryEmoji(payment.category || 'groceries')}</span>
|
||||
<h3>{payment.title}</h3>
|
||||
</div>
|
||||
<div class="payment-meta">
|
||||
<span class="category-name">{getCategoryName(payment.category || 'groceries')}</span>
|
||||
<span class="date">{formatDate(payment.date)}</span>
|
||||
<span class="amount">{formatCurrency(payment.amount)}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{#if payment.image}
|
||||
<img src={payment.image} alt="Receipt" class="receipt-thumb" />
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
@@ -339,6 +361,71 @@
|
||||
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
.settlement-card {
|
||||
background: linear-gradient(135deg, #f8fff9, #f0f8f0);
|
||||
border: 2px solid #28a745;
|
||||
}
|
||||
|
||||
.settlement-card:hover {
|
||||
box-shadow: 0 4px 16px rgba(40, 167, 69, 0.2);
|
||||
}
|
||||
|
||||
.settlement-flow {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.settlement-user-from, .settlement-user-to {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.settlement-user-from .username,
|
||||
.settlement-user-to .username {
|
||||
font-weight: 500;
|
||||
color: #28a745;
|
||||
}
|
||||
|
||||
.settlement-arrow {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.settlement-arrow .arrow {
|
||||
color: #28a745;
|
||||
font-size: 1.2rem;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.settlement-badge-small {
|
||||
background: linear-gradient(135deg, #28a745, #20c997);
|
||||
color: white;
|
||||
padding: 0.125rem 0.375rem;
|
||||
border-radius: 0.75rem;
|
||||
font-size: 0.65rem;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.025em;
|
||||
}
|
||||
|
||||
.settlement-amount {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.settlement-amount-text {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
color: #28a745;
|
||||
}
|
||||
|
||||
.payment-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
|
13
src/routes/cospend/settle/+page.server.ts
Normal file
13
src/routes/cospend/settle/+page.server.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const auth = await locals.auth();
|
||||
if (!auth || !auth.user) {
|
||||
throw redirect(302, '/login');
|
||||
}
|
||||
|
||||
return {
|
||||
session: auth
|
||||
};
|
||||
};
|
615
src/routes/cospend/settle/+page.svelte
Normal file
615
src/routes/cospend/settle/+page.svelte
Normal file
@@ -0,0 +1,615 @@
|
||||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import ProfilePicture from '$lib/components/ProfilePicture.svelte';
|
||||
import { PREDEFINED_USERS, isPredefinedUsersMode } from '$lib/config/users';
|
||||
|
||||
export let data;
|
||||
|
||||
let debtData = {
|
||||
whoOwesMe: [],
|
||||
whoIOwe: [],
|
||||
totalOwedToMe: 0,
|
||||
totalIOwe: 0
|
||||
};
|
||||
let loading = true;
|
||||
let error = null;
|
||||
let selectedSettlement = null;
|
||||
let settlementAmount = '';
|
||||
let submitting = false;
|
||||
let predefinedMode = isPredefinedUsersMode();
|
||||
|
||||
onMount(async () => {
|
||||
await fetchDebtData();
|
||||
});
|
||||
|
||||
async function fetchDebtData() {
|
||||
try {
|
||||
loading = true;
|
||||
const response = await fetch('/api/cospend/debts');
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch debt data');
|
||||
}
|
||||
debtData = await response.json();
|
||||
|
||||
// For predefined mode with 2 users, auto-select the debt if there's only one
|
||||
if (predefinedMode && PREDEFINED_USERS.length === 2) {
|
||||
const totalDebts = debtData.whoOwesMe.length + debtData.whoIOwe.length;
|
||||
if (totalDebts === 1) {
|
||||
if (debtData.whoOwesMe.length === 1) {
|
||||
selectedSettlement = {
|
||||
type: 'receive',
|
||||
from: debtData.whoOwesMe[0].username,
|
||||
to: data.session?.user?.nickname,
|
||||
amount: debtData.whoOwesMe[0].netAmount,
|
||||
description: `Settlement: ${debtData.whoOwesMe[0].username} pays ${data.session?.user?.nickname}`
|
||||
};
|
||||
settlementAmount = debtData.whoOwesMe[0].netAmount.toString();
|
||||
} else if (debtData.whoIOwe.length === 1) {
|
||||
selectedSettlement = {
|
||||
type: 'pay',
|
||||
from: data.session?.user?.nickname,
|
||||
to: debtData.whoIOwe[0].username,
|
||||
amount: debtData.whoIOwe[0].netAmount,
|
||||
description: `Settlement: ${data.session?.user?.nickname} pays ${debtData.whoIOwe[0].username}`
|
||||
};
|
||||
settlementAmount = debtData.whoIOwe[0].netAmount.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
error = err.message;
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function selectSettlement(type, user, amount) {
|
||||
const currentUser = data.session?.user?.nickname;
|
||||
if (type === 'receive') {
|
||||
selectedSettlement = {
|
||||
type: 'receive',
|
||||
from: user,
|
||||
to: currentUser,
|
||||
amount: amount,
|
||||
description: `Settlement: ${user} pays ${currentUser}`
|
||||
};
|
||||
} else {
|
||||
selectedSettlement = {
|
||||
type: 'pay',
|
||||
from: currentUser,
|
||||
to: user,
|
||||
amount: amount,
|
||||
description: `Settlement: ${currentUser} pays ${user}`
|
||||
};
|
||||
}
|
||||
settlementAmount = amount.toString();
|
||||
}
|
||||
|
||||
async function processSettlement() {
|
||||
if (!selectedSettlement || !settlementAmount) {
|
||||
error = 'Please select a settlement and enter an amount';
|
||||
return;
|
||||
}
|
||||
|
||||
const amount = parseFloat(settlementAmount);
|
||||
if (isNaN(amount) || amount <= 0) {
|
||||
error = 'Please enter a valid positive amount';
|
||||
return;
|
||||
}
|
||||
|
||||
if (amount > selectedSettlement.amount) {
|
||||
error = 'Settlement amount cannot exceed the debt amount';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
submitting = true;
|
||||
error = null;
|
||||
|
||||
// Create a settlement payment
|
||||
const payload = {
|
||||
title: `Settlement Payment`,
|
||||
description: selectedSettlement.description,
|
||||
amount: amount,
|
||||
paidBy: selectedSettlement.from,
|
||||
date: new Date().toISOString().split('T')[0],
|
||||
category: 'settlement', // Using settlement category
|
||||
splitMethod: 'full',
|
||||
splits: [
|
||||
{
|
||||
username: selectedSettlement.from,
|
||||
amount: -amount // Payer gets negative (receives money back)
|
||||
},
|
||||
{
|
||||
username: selectedSettlement.to,
|
||||
amount: amount // Receiver owes money
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const response = await fetch('/api/cospend/payments', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.message || 'Failed to record settlement');
|
||||
}
|
||||
|
||||
// Redirect back to dashboard
|
||||
goto('/cospend');
|
||||
} catch (err) {
|
||||
error = err.message;
|
||||
} finally {
|
||||
submitting = false;
|
||||
}
|
||||
}
|
||||
|
||||
function formatCurrency(amount) {
|
||||
return new Intl.NumberFormat('de-CH', {
|
||||
style: 'currency',
|
||||
currency: 'CHF'
|
||||
}).format(amount);
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Settle Debts - Cospend</title>
|
||||
</svelte:head>
|
||||
|
||||
<main class="settle-main">
|
||||
<div class="header-section">
|
||||
<h1>Settle Debts</h1>
|
||||
<p>Record payments to settle outstanding debts between users</p>
|
||||
</div>
|
||||
|
||||
{#if loading}
|
||||
<div class="loading">Loading debt information...</div>
|
||||
{:else if error}
|
||||
<div class="error">Error: {error}</div>
|
||||
{:else if debtData.whoOwesMe.length === 0 && debtData.whoIOwe.length === 0}
|
||||
<div class="no-debts">
|
||||
<h2>🎉 All Settled!</h2>
|
||||
<p>No outstanding debts to settle. Everyone is even!</p>
|
||||
<div class="actions">
|
||||
<a href="/cospend" class="btn btn-primary">Back to Dashboard</a>
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="settlement-container">
|
||||
<!-- Available Settlements -->
|
||||
<div class="available-settlements">
|
||||
<h2>Available Settlements</h2>
|
||||
|
||||
{#if debtData.whoOwesMe.length > 0}
|
||||
<div class="settlement-section">
|
||||
<h3>Money You're Owed</h3>
|
||||
{#each debtData.whoOwesMe as debt}
|
||||
<div class="settlement-option"
|
||||
class:selected={selectedSettlement?.type === 'receive' && selectedSettlement?.from === debt.username}
|
||||
on:click={() => selectSettlement('receive', debt.username, debt.netAmount)}>
|
||||
<div class="settlement-user">
|
||||
<ProfilePicture username={debt.username} size={40} />
|
||||
<div class="user-details">
|
||||
<span class="username">{debt.username}</span>
|
||||
<span class="debt-amount">owes you {formatCurrency(debt.netAmount)}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settlement-action">
|
||||
<span class="action-text">Receive Payment</span>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if debtData.whoIOwe.length > 0}
|
||||
<div class="settlement-section">
|
||||
<h3>Money You Owe</h3>
|
||||
{#each debtData.whoIOwe as debt}
|
||||
<div class="settlement-option"
|
||||
class:selected={selectedSettlement?.type === 'pay' && selectedSettlement?.to === debt.username}
|
||||
on:click={() => selectSettlement('pay', debt.username, debt.netAmount)}>
|
||||
<div class="settlement-user">
|
||||
<ProfilePicture username={debt.username} size={40} />
|
||||
<div class="user-details">
|
||||
<span class="username">{debt.username}</span>
|
||||
<span class="debt-amount">you owe {formatCurrency(debt.netAmount)}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settlement-action">
|
||||
<span class="action-text">Make Payment</span>
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<!-- Settlement Details -->
|
||||
{#if selectedSettlement}
|
||||
<div class="settlement-details">
|
||||
<h2>Settlement Details</h2>
|
||||
|
||||
<div class="settlement-summary">
|
||||
<div class="settlement-flow">
|
||||
<div class="user-from">
|
||||
<ProfilePicture username={selectedSettlement.from} size={48} />
|
||||
<span class="username">{selectedSettlement.from}</span>
|
||||
{#if selectedSettlement.from === data.session?.user?.nickname}
|
||||
<span class="you-badge">You</span>
|
||||
{/if}
|
||||
</div>
|
||||
<div class="flow-arrow">→</div>
|
||||
<div class="user-to">
|
||||
<ProfilePicture username={selectedSettlement.to} size={48} />
|
||||
<span class="username">{selectedSettlement.to}</span>
|
||||
{#if selectedSettlement.to === data.session?.user?.nickname}
|
||||
<span class="you-badge">You</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="settlement-amount-section">
|
||||
<label for="amount">Settlement Amount</label>
|
||||
<div class="amount-input">
|
||||
<span class="currency">CHF</span>
|
||||
<input
|
||||
id="amount"
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0.01"
|
||||
max={selectedSettlement.amount}
|
||||
bind:value={settlementAmount}
|
||||
placeholder="0.00"
|
||||
/>
|
||||
</div>
|
||||
<small class="max-amount">
|
||||
Maximum: {formatCurrency(selectedSettlement.amount)}
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<div class="settlement-description">
|
||||
<strong>Description:</strong> {selectedSettlement.description}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="settlement-actions">
|
||||
<button
|
||||
class="btn btn-settlement"
|
||||
on:click={processSettlement}
|
||||
disabled={submitting || !settlementAmount}>
|
||||
{#if submitting}
|
||||
Recording Settlement...
|
||||
{:else}
|
||||
Record Settlement
|
||||
{/if}
|
||||
</button>
|
||||
<button class="btn btn-secondary" on:click={() => selectedSettlement = null}>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="back-actions">
|
||||
<a href="/cospend" class="btn btn-secondary">← Back to Dashboard</a>
|
||||
</div>
|
||||
{/if}
|
||||
</main>
|
||||
|
||||
<style>
|
||||
.settle-main {
|
||||
max-width: 1000px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.header-section {
|
||||
text-align: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.header-section h1 {
|
||||
color: #333;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.header-section p {
|
||||
color: #666;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.loading, .error {
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #d32f2f;
|
||||
background-color: #ffebee;
|
||||
border-radius: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.no-debts {
|
||||
text-align: center;
|
||||
padding: 3rem 2rem;
|
||||
background: white;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.no-debts h2 {
|
||||
color: #28a745;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.settlement-container {
|
||||
display: grid;
|
||||
gap: 2rem;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.settlement-container {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.available-settlements {
|
||||
background: white;
|
||||
padding: 1.5rem;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.settlement-section {
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.settlement-section h3 {
|
||||
color: #333;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
.settlement-option {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 1rem;
|
||||
border: 2px solid #e9ecef;
|
||||
border-radius: 0.75rem;
|
||||
margin-bottom: 0.75rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.settlement-option:hover {
|
||||
border-color: #28a745;
|
||||
box-shadow: 0 2px 8px rgba(40, 167, 69, 0.1);
|
||||
}
|
||||
|
||||
.settlement-option.selected {
|
||||
border-color: #28a745;
|
||||
background-color: #f8fff9;
|
||||
}
|
||||
|
||||
.settlement-user {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.user-details {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.username {
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.debt-amount {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.settlement-action {
|
||||
color: #28a745;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.settlement-details {
|
||||
background: white;
|
||||
padding: 1.5rem;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
height: fit-content;
|
||||
}
|
||||
|
||||
.settlement-summary {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.settlement-flow {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 1.5rem;
|
||||
padding: 1rem;
|
||||
background-color: #f8f9fa;
|
||||
border-radius: 0.5rem;
|
||||
}
|
||||
|
||||
.user-from, .user-to {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.flow-arrow {
|
||||
font-size: 1.5rem;
|
||||
color: #28a745;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.you-badge {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
padding: 0.125rem 0.5rem;
|
||||
border-radius: 1rem;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.settlement-amount-section {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.settlement-amount-section label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.amount-input {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
background: #f8f9fa;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 0.375rem;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.currency {
|
||||
color: #666;
|
||||
font-weight: 500;
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
|
||||
.amount-input input {
|
||||
border: none;
|
||||
background: none;
|
||||
flex: 1;
|
||||
padding: 0.25rem;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.amount-input input:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.max-amount {
|
||||
color: #666;
|
||||
font-size: 0.85rem;
|
||||
margin-top: 0.25rem;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.settlement-description {
|
||||
color: #333;
|
||||
font-size: 0.9rem;
|
||||
padding: 1rem;
|
||||
background-color: #f8f9fa;
|
||||
border-radius: 0.375rem;
|
||||
}
|
||||
|
||||
.settlement-actions {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.75rem 1.5rem;
|
||||
border: none;
|
||||
border-radius: 0.375rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
display: inline-block;
|
||||
text-align: center;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #1976d2;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background-color: #1565c0;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background-color: #f5f5f5;
|
||||
color: #333;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background-color: #e8e8e8;
|
||||
}
|
||||
|
||||
.btn-settlement {
|
||||
background: linear-gradient(135deg, #28a745, #20c997);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-settlement:hover:not(:disabled) {
|
||||
background: linear-gradient(135deg, #20c997, #1e7e34);
|
||||
}
|
||||
|
||||
.btn-settlement:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.back-actions {
|
||||
text-align: center;
|
||||
margin-top: 2rem;
|
||||
}
|
||||
|
||||
.actions {
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.settle-main {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.settlement-flow {
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.flow-arrow {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
.settlement-actions {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.btn {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
Reference in New Issue
Block a user