From d6fbe06cab5dbc329a2ee61672221e840acc8d7a Mon Sep 17 00:00:00 2001 From: William Brawner Date: Thu, 25 Nov 2021 07:03:20 -0700 Subject: [PATCH] Add recurring transaction methods to api service --- src/app/shared/twigs.http.service.ts | 80 +++++++++++++++++++++++++++ src/app/shared/twigs.local.service.ts | 39 +++++++++++++ src/app/shared/twigs.service.ts | 25 +++++++++ 3 files changed, 144 insertions(+) diff --git a/src/app/shared/twigs.http.service.ts b/src/app/shared/twigs.http.service.ts index fc74d06..67a6b73 100644 --- a/src/app/shared/twigs.http.service.ts +++ b/src/app/shared/twigs.http.service.ts @@ -5,6 +5,7 @@ import { Budget } from '../budgets/budget'; import { Category } from '../categories/category'; import { Transaction } from '../transactions/transaction'; import { environment } from '../../environments/environment'; +import { Frequency, RecurringTransaction } from '../recurringtransactions/recurringtransaction'; @Injectable({ providedIn: 'root' @@ -245,6 +246,85 @@ export class TwigsHttpService implements TwigsService { return this.request(url, HttpMethod.DELETE) } + // Recurring Transactions + async getRecurringTransactions( + budgetId?: string, + categoryId?: string, + count?: number, + from?: Date, + to?: Date + ): Promise { + const url = new URL(`/api/recurringtransactions`, this.apiUrl) + if (budgetId) { + url.searchParams.set('budgetIds', budgetId); + } + if (categoryId) { + url.searchParams.set('categoryIds', categoryId); + } + if (from) { + url.searchParams.set('from', from.toISOString()); + } + if (to) { + url.searchParams.set('to', to.toISOString()); + } + const transactions: RecurringTransaction[] = await this.request(url, HttpMethod.GET) + transactions.forEach(transaction => { + transaction.frequency = Frequency.parse(transaction.frequency as any) + }) + return transactions + } + + async getRecurringTransaction(id: string): Promise { + const url = new URL(`/api/recurringtransactions/${id}`, this.apiUrl) + const transaction: RecurringTransaction = await this.request(url, HttpMethod.GET) + transaction.frequency = Frequency.parse(transaction.frequency as any) + return transaction + } + + async createRecurringTransaction( + id: string, + budgetId: string, + name: string, + description: string, + amount: number, + frequency: Frequency, + start: Date, + expense: boolean, + category: string, + end?: Date, + ): Promise { + const url = new URL(`/api/transactions`, this.apiUrl) + const body = { + 'id': id, + 'title': name, + 'description': description, + 'frequency': frequency.toString(), + 'start': start.toISOString(), + 'finish': end?.toISOString(), + 'amount': amount, + 'expense': expense, + 'categoryId': category, + 'budgetId': budgetId + }; + const transaction: RecurringTransaction = await this.request(url, HttpMethod.POST, body) + transaction.frequency = Frequency.parse(transaction.frequency as any) + return transaction + } + + async updateRecurringTransaction(id: string, transaction: RecurringTransaction): Promise { + const body: any = transaction; + body.frequency = transaction.frequency.toString() + const url = new URL(`/api/transactions/${id}`, this.apiUrl) + const updatedTransaction: RecurringTransaction = await this.request(url, HttpMethod.PUT, body) + updatedTransaction.frequency = Frequency.parse(updatedTransaction.frequency as any) + return updatedTransaction + } + + deleteRecurringTransaction(id: string): Promise { + const url = new URL(`/api/recurringtransactions/${id}`, this.apiUrl) + return this.request(url, HttpMethod.DELETE) + } + // Users getProfile(id: string): Promise { const url = new URL(`/api/users/${id}`, this.apiUrl) diff --git a/src/app/shared/twigs.local.service.ts b/src/app/shared/twigs.local.service.ts index 89db219..15d21bd 100644 --- a/src/app/shared/twigs.local.service.ts +++ b/src/app/shared/twigs.local.service.ts @@ -5,6 +5,7 @@ import { Budget } from '../budgets/budget'; import { Category } from '../categories/category'; import { Transaction } from '../transactions/transaction'; import { randomId } from '../shared/utils'; +import { Frequency, RecurringTransaction } from '../recurringtransactions/recurringtransaction'; /** * This is intended to be a very simple implementation of the TwigsService used for testing out the UI and quickly iterating on it. @@ -284,6 +285,44 @@ export class TwigsLocalService implements TwigsService { }); } + // Recurring Transactions + getRecurringTransactions( + budgetId?: string, + categoryId?: string, + count?: number, + from?: Date, + to?: Date + ): Promise { + return Promise.reject("Not yet implemented") + } + + getRecurringTransaction(id: string): Promise { + return Promise.reject("Not yet implemented") + } + + createRecurringTransaction( + id: string, + budgetId: string, + name: string, + description: string, + amount: number, + frequency: Frequency, + start: Date, + expense: boolean, + category: string, + end?: Date, + ): Promise { + return Promise.reject("Not yet implemented") + } + + updateRecurringTransaction(id: string, transaction: RecurringTransaction): Promise { + return Promise.reject("Not yet implemented") + } + + deleteRecurringTransaction(id: string): Promise { + return Promise.reject("Not yet implemented") + } + // Users getProfile(id: string): Promise { return Promise.reject("Not yet implemented"); diff --git a/src/app/shared/twigs.service.ts b/src/app/shared/twigs.service.ts index a88e830..8d0a98a 100644 --- a/src/app/shared/twigs.service.ts +++ b/src/app/shared/twigs.service.ts @@ -2,6 +2,7 @@ import { InjectionToken } from '@angular/core'; import { User, UserPermission } from '../users/user'; import { Budget } from '../budgets/budget'; import { Category } from '../categories/category'; +import { RecurringTransaction, Frequency } from '../recurringtransactions/recurringtransaction'; import { Transaction } from '../transactions/transaction'; export interface TwigsService { @@ -53,6 +54,30 @@ export interface TwigsService { updateTransaction(id: string, transaction: Transaction): Promise; deleteTransaction(id: string): Promise; + // Recurring Transactions + getRecurringTransactions( + budgetId?: string, + categoryId?: string, + count?: number, + from?: Date, + to?: Date + ): Promise; + getRecurringTransaction(id: string): Promise; + createRecurringTransaction( + id: string, + budgetId: string, + name: string, + description: string, + amount: number, + frequency: Frequency, + start: Date, + expense: boolean, + category: string, + end?: Date, + ): Promise; + updateRecurringTransaction(id: string, transaction: RecurringTransaction): Promise; + deleteRecurringTransaction(id: string): Promise; + getProfile(id: string): Promise; getUsersByUsername(username: string): Promise; }