From 9bf2c972d853da9abe01a4ef96d83b6cc68461fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?=
<47506558+MegaRedHand@users.noreply.github.com>
Date: Sun, 9 Jun 2024 20:07:32 -0300
Subject: [PATCH 1/6] feat: add new route
---
my-app/src/lib/server/api.ts | 7 +++
my-app/src/routes/api/members/+server.ts | 8 +++
.../movements/[id=integer]/+page.svelte | 1 +
.../details/[[id=integer]]/+page.server.ts | 33 ++++++++++
.../details/[[id=integer]]/+page.svelte | 62 +++++++++++++++++++
my-app/src/types.d.ts | 10 ++-
6 files changed, 120 insertions(+), 1 deletion(-)
create mode 100644 my-app/src/routes/api/members/+server.ts
create mode 100644 my-app/src/routes/payments/details/[[id=integer]]/+page.server.ts
create mode 100644 my-app/src/routes/payments/details/[[id=integer]]/+page.svelte
diff --git a/my-app/src/lib/server/api.ts b/my-app/src/lib/server/api.ts
index 027d1df..e330030 100644
--- a/my-app/src/lib/server/api.ts
+++ b/my-app/src/lib/server/api.ts
@@ -84,6 +84,13 @@ export const spendingService = {
: post('spending', data, getAuthHeader(cookies)),
list: (groupId: Id, cookies: Cookies) => get(`group/${groupId}/spending`, getAuthHeader(cookies))
};
+export const paymentService = {
+ save: (data: Payment, cookies: Cookies) =>
+ data.id > 0
+ ? put(`payment/${data.id}`, data, getAuthHeader(cookies))
+ : post('payment', data, getAuthHeader(cookies)),
+ list: (groupId: Id, cookies: Cookies) => get(`group/${groupId}/payment`, getAuthHeader(cookies))
+};
export const budgetService = {
save: (data: Budget, cookies: Cookies) =>
data.id > 0
diff --git a/my-app/src/routes/api/members/+server.ts b/my-app/src/routes/api/members/+server.ts
new file mode 100644
index 0000000..8759a8d
--- /dev/null
+++ b/my-app/src/routes/api/members/+server.ts
@@ -0,0 +1,8 @@
+import { groupService } from '$lib/server/api';
+import type { RequestHandler } from './$types';
+
+export const GET: RequestHandler = async ({ url, cookies }) => {
+ const groupId = url.searchParams.get('groupId') || 0;
+ const body = await groupService.listAllMembers(+groupId, cookies);
+ return new Response(JSON.stringify(body));
+};
diff --git a/my-app/src/routes/groups/movements/[id=integer]/+page.svelte b/my-app/src/routes/groups/movements/[id=integer]/+page.svelte
index 4d78e76..18d1ecd 100644
--- a/my-app/src/routes/groups/movements/[id=integer]/+page.svelte
+++ b/my-app/src/routes/groups/movements/[id=integer]/+page.svelte
@@ -65,6 +65,7 @@
Añadir gasto
Añadir presupuesto
Añadir categoría
+ Añadir pago
diff --git a/my-app/src/routes/payments/details/[[id=integer]]/+page.server.ts b/my-app/src/routes/payments/details/[[id=integer]]/+page.server.ts
new file mode 100644
index 0000000..9cd7331
--- /dev/null
+++ b/my-app/src/routes/payments/details/[[id=integer]]/+page.server.ts
@@ -0,0 +1,33 @@
+import { error, redirect } from '@sveltejs/kit';
+import type { Actions, PageServerLoad } from './$types';
+import { groupService, paymentService } from '$lib/server/api';
+import { getUserId } from '$lib/auth';
+
+export const load: PageServerLoad = async ({ params, url, cookies }) => {
+ const groupId = url.searchParams.get('groupId') || '';
+ // const id = Number(params.id) || 0;
+ const groups: Group[] = await groupService.list(cookies);
+ const userId = getUserId(cookies);
+ return { userId, groupId, groups };
+};
+
+export const actions: Actions = {
+ default: async ({ cookies, request, params }) => {
+ const id = Number(params.id) || 0;
+ const data = await request.formData();
+
+ const group_id = Number(data.get('groupId'));
+ const to_id = Number(data.get('payeeId'));
+ const from_id = getUserId(cookies);
+ const amount = Number(data.get('amount'));
+
+ if (!group_id) {
+ throw error(400, 'Group is required');
+ }
+
+ const payment: Payment = { id, group_id, from_id, to_id, amount };
+ await paymentService.save(payment, cookies);
+
+ redirect(302, `/groups/movements/${group_id}`);
+ }
+};
diff --git a/my-app/src/routes/payments/details/[[id=integer]]/+page.svelte b/my-app/src/routes/payments/details/[[id=integer]]/+page.svelte
new file mode 100644
index 0000000..7509d25
--- /dev/null
+++ b/my-app/src/routes/payments/details/[[id=integer]]/+page.svelte
@@ -0,0 +1,62 @@
+
+
+
+ {title} - Nuevo Pago
+
+
+
+
+Registrando Pago
+
diff --git a/my-app/src/types.d.ts b/my-app/src/types.d.ts
index 72e2ddc..6dc7e20 100644
--- a/my-app/src/types.d.ts
+++ b/my-app/src/types.d.ts
@@ -36,6 +36,14 @@ declare global {
amount: number;
date: string;
};
+ type Payment = {
+ id: Id;
+ group_id: Id;
+ from_id: Id;
+ to_id: Id;
+ amount: number;
+ date: string;
+ };
type Invite = {
id: Id;
sender_id: Id;
@@ -68,4 +76,4 @@ declare global {
};
}
-export {};
+export { };
From e60a194d9a20102bb181747aa80ef36248c8594e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?=
<47506558+MegaRedHand@users.noreply.github.com>
Date: Sun, 9 Jun 2024 23:07:35 -0300
Subject: [PATCH 2/6] feat: list payments in moovements page
---
.../movements/[id=integer]/+page.server.ts | 5 ++--
.../movements/[id=integer]/+page.svelte | 30 ++++++++++++++-----
2 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts b/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts
index 548ca68..d251415 100644
--- a/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts
+++ b/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts
@@ -1,4 +1,4 @@
-import { budgetService, categoryService, groupService, spendingService } from '$lib/server/api';
+import { budgetService, categoryService, groupService, paymentService, spendingService } from '$lib/server/api';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ cookies, params }) => {
@@ -7,13 +7,14 @@ export const load: PageServerLoad = async ({ cookies, params }) => {
const spendings: Spending[] = await spendingService.list(id, cookies);
const budgets: Budget[] = await budgetService.list(id, cookies);
const categories: Category[] = await categoryService.list(id, cookies);
+ const payments: Payment[] = await paymentService.list(id, cookies);
const categoryBalances: CategoryBalance[] = computeBalancesPerCategory(
spendings,
budgets,
categories
);
- return { group, spendings, budgets, categories, categoryBalances };
+ return { group, spendings, payments, budgets, categories, categoryBalances };
};
function computeBalancesPerCategory(
diff --git a/my-app/src/routes/groups/movements/[id=integer]/+page.svelte b/my-app/src/routes/groups/movements/[id=integer]/+page.svelte
index 18d1ecd..22144ac 100644
--- a/my-app/src/routes/groups/movements/[id=integer]/+page.svelte
+++ b/my-app/src/routes/groups/movements/[id=integer]/+page.svelte
@@ -6,6 +6,9 @@
export let data: PageServerData;
+ const movements = [...data.spendings, ...data.payments];
+ movements.sort((a, b) => Date.parse(b.date) - Date.parse(a.date));
+
const totalBudgets = data?.categoryBalances.reduce((acc, { budgets }) => acc + budgets, 0);
const totalSpendings = data?.categoryBalances.reduce((acc, { spendings }) => acc + spendings, 0);
const totalBalance = totalBudgets - totalSpendings;
@@ -114,14 +117,25 @@
-{#each data.spendings as spending}
-
- {formatDateTimeString(spending.date)}
-
- {spending.category_id}
- {spending.description}
- {formatMoney(spending.amount)}
-
+{#each movements as movement}
+ {#if 'category_id' in movement}
+ {@const spending = movement}
+
+ {formatDateTimeString(spending.date)}
+
+ {spending.category_id}
+ {spending.description}
+ {formatMoney(spending.amount)}
+
+ {:else}
+ {@const payment = movement}
+
+ {formatDateTimeString(payment.date)}
+
+ {payment.from_id} → {payment.to_id}
+ {formatMoney(payment.amount)}
+
+ {/if}
{/each}