From 00fc7578116b34c59c42215ced59d090c29d7985 Mon Sep 17 00:00:00 2001 From: Borja Garibotti Date: Mon, 17 Jun 2024 22:49:12 -0300 Subject: [PATCH 1/2] Add distribution strategies --- my-app/src/lib/formatter.ts | 14 ++- my-app/src/lib/index.ts | 6 + .../details/[[id=integer]]/+page.server.ts | 11 +- .../details/[[id=integer]]/+page.svelte | 10 +- .../details/[[id=integer]]/+page.server.ts | 2 +- .../details/[[id=integer]]/+page.server.ts | 45 +++++-- .../details/[[id=integer]]/+page.svelte | 119 ++++++++++++++++-- my-app/src/types.d.ts | 10 +- my-app/static/styles.css | 8 ++ 9 files changed, 194 insertions(+), 31 deletions(-) diff --git a/my-app/src/lib/formatter.ts b/my-app/src/lib/formatter.ts index 59bb2cd..42dd936 100644 --- a/my-app/src/lib/formatter.ts +++ b/my-app/src/lib/formatter.ts @@ -32,9 +32,13 @@ export function fixDateString(dateString: string, timezoneOffset: number) { } export function formatMoney(amount: number) { - const fixed = amount - .toFixed(2) - .replace('.', ',') - .replace(/\d(?=(\d{3})+,)/g, '$&.'); - return `$ ${fixed}`; + try { + const fixed = amount + .toFixed(2) + .replace('.', ',') + .replace(/\d(?=(\d{3})+,)/g, '$&.'); + return `$ ${fixed}`; + } catch (error) { + return '-'; + } } diff --git a/my-app/src/lib/index.ts b/my-app/src/lib/index.ts index 85815cc..4f4c3f2 100644 --- a/my-app/src/lib/index.ts +++ b/my-app/src/lib/index.ts @@ -78,3 +78,9 @@ export function getUserEmailById(users: User[], id: number): User['email'] { /// When a budget is greater or equal to this threshold, it is considered to be near its limit. export const BUDGET_NEAR_LIMIT_THRESHOLD = 0.9; + +export const strategies: Record = { + equalparts: 'En partes iguales', + percentage: 'En porcentajes', + custom: 'Manual' +}; diff --git a/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts b/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts index 438be75..321f9de 100644 --- a/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts +++ b/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts @@ -2,14 +2,14 @@ import { error, redirect } from '@sveltejs/kit'; import type { Actions } from './$types'; import { categoryService, groupService } from '$lib/server/api'; import type { PageServerLoad } from './$types'; -import { routes } from '$lib'; +import { routes, strategies } from '$lib'; export const load: PageServerLoad = async ({ params, url, cookies }) => { const group_id = Number(url.searchParams.get('groupId')) || 0; const id = Number(params.id) || 0; const category: Category = id ? await categoryService.get(id, cookies) - : { id: 0, group_id, name: '', description: '', strategy: '' }; + : { id: 0, group_id, name: '', description: '', strategy: 'equalparts' }; const groups: Group[] = await groupService.list(cookies); return { category, groups }; }; @@ -21,7 +21,7 @@ export const actions: Actions = { const name = data.get('name')?.toString(); const description = data.get('description')?.toString(); const group_id = Number(data.get('groupId')); - const strategy = ''; + const strategyData = data.get('stategy')?.toString(); if (!name) { throw error(400, 'Name is required'); @@ -32,6 +32,11 @@ export const actions: Actions = { if (!group_id) { throw error(400, 'Group is required'); } + if (!strategyData || !Object.keys(strategies).includes(strategyData)) { + throw error(400, 'Strategy is required'); + } + + const strategy = strategyData as Strategy; const category: Category = { id, group_id, name, description, strategy }; await categoryService.save(category, cookies); diff --git a/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte b/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte index 8b37db3..3c8bd28 100644 --- a/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte +++ b/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte @@ -1,5 +1,5 @@