From 5ba74d198a5f7a03482f95a8623ecd607818aeb0 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 26 Jul 2022 23:38:22 +0200 Subject: [PATCH] feat(webapp): add backend for data view --- .../molecules/MoleculeDataTable.vue | 62 ++++-- src/components/services/account.service.ts | 42 ++++ src/components/services/bank.service.ts | 25 +-- src/components/services/data.service.ts | 33 +++ src/components/services/date.service.ts | 21 +- src/components/views/BankView.vue | 14 +- src/components/views/DataView.vue | 196 ++++-------------- src/interfaces/account-data.interface.ts | 7 + src/interfaces/account.interface.ts | 1 + .../personal-information.interface.ts | 5 + 10 files changed, 208 insertions(+), 198 deletions(-) create mode 100644 src/components/services/account.service.ts create mode 100644 src/components/services/data.service.ts create mode 100644 src/interfaces/account-data.interface.ts create mode 100644 src/interfaces/personal-information.interface.ts diff --git a/src/components/molecules/MoleculeDataTable.vue b/src/components/molecules/MoleculeDataTable.vue index 6ae9cc4..aa3b9b5 100644 --- a/src/components/molecules/MoleculeDataTable.vue +++ b/src/components/molecules/MoleculeDataTable.vue @@ -3,31 +3,38 @@ - + - + @@ -36,17 +43,40 @@ - - \ No newline at end of file diff --git a/src/components/services/account.service.ts b/src/components/services/account.service.ts new file mode 100644 index 0000000..576d03e --- /dev/null +++ b/src/components/services/account.service.ts @@ -0,0 +1,42 @@ +import { Databases, Models, Query } from 'appwrite'; +import { AppwriteService } from './appwrite.service'; +import { IAccount } from '../../interfaces/account.interface'; + +const DATABASE_ID = '62dfd5787755e7ed9fcd'; +const ACCOUNTS_COLLECTION_ID = '62dfd6ca06197f9baa86'; +const sdk = AppwriteService.getSDK(); +const database = new Databases(sdk, DATABASE_ID); + +export const AccountService = { + async getAccount(accountNumber: string): Promise { + const accountDocument = await database.listDocuments( + ACCOUNTS_COLLECTION_ID, [Query.equal('accountNumber', accountNumber)], 1); + return accountDocument.documents[0]; + }, + async updateBalance(accountNumber: string, balance: number): Promise { + const account = await this.getAccount(accountNumber); + await database.updateDocument(ACCOUNTS_COLLECTION_ID, account.$id, { + balance: account.balance + balance, + }); + return account; + }, + async getAllAccounts(): Promise { + let accountDocuments: Models.DocumentList, + offset = 0; + const accounts: IAccount[] = []; + do { + accountDocuments = await database.listDocuments( + ACCOUNTS_COLLECTION_ID, [], 100, offset); + accounts.push(...accountDocuments.documents.map(account => ({ + accountNumber: account.accountNumber, + firstName: account.firstName, + lastName: account.lastName, + balance: account.balance, + transactions: [], + lastCheckIn: account.lastCheckIn, + }))); + offset += 100; + } while(accountDocuments.total > offset); + return accounts; + }, +}; \ No newline at end of file diff --git a/src/components/services/bank.service.ts b/src/components/services/bank.service.ts index 1e0b115..84c59af 100644 --- a/src/components/services/bank.service.ts +++ b/src/components/services/bank.service.ts @@ -1,10 +1,10 @@ +import { Databases, Models, Query } from 'appwrite'; +import { AccountService } from './account.service'; import { AppwriteService } from './appwrite.service'; import { IAccount } from '../../interfaces/account.interface'; -import { Databases, Models, Query } from 'appwrite'; import { ITransaction } from '../../interfaces/transaction.interface'; const DATABASE_ID = '62dfd5787755e7ed9fcd'; -const ACCOUNTS_COLLECTION_ID = '62dfd6ca06197f9baa86'; const TRANSACTIONS_COLLECTION_ID = '62dfd81b7671727b27cd'; const DEPOSIT_LABEL = 'Bargeldeinzahlung'; const SALARY_LABEL = 'Gehalt'; @@ -14,35 +14,30 @@ const sdk = AppwriteService.getSDK(); export const BankService = { async getAccountDetails(accountNumber: string): Promise { const database = new Databases(sdk, DATABASE_ID); - const accountDocument = await database.listDocuments( - ACCOUNTS_COLLECTION_ID, [Query.equal('accountNumber', accountNumber)], 1); + const account = await AccountService.getAccount(accountNumber); const transactionDocuments = await database.listDocuments( TRANSACTIONS_COLLECTION_ID, [Query.equal('accountNumber', accountNumber)], 100, 0, undefined, undefined, ['$createdAt'], ['DESC']); - const account: IAccount = { - accountNumber: accountDocument.documents[0].accountNumber, - firstName: accountDocument.documents[0].firstName, - lastName: accountDocument.documents[0].lastName, - balance: accountDocument.documents[0].balance, + return { + accountNumber: account.accountNumber, + firstName: account.firstName, + lastName: account.lastName, + balance: account.balance, transactions: transactionDocuments.documents.map(transaction => ({ label: transaction.label, amount: transaction.amount, date: new Date(transaction.$createdAt * 1000), })), + lastCheckIn: account.lastCheckIn, }; - return account; }, async addTransaction(accountNumber: string, amount: number, type: TransactionType): Promise { const database = new Databases(sdk, DATABASE_ID); - const accountDocument = await database.listDocuments( - ACCOUNTS_COLLECTION_ID, [Query.equal('accountNumber', accountNumber)], 1); const transactionDocument = await database.createDocument(TRANSACTIONS_COLLECTION_ID, 'unique()', { accountNumber: accountNumber, label: type === TransactionType.DEPOSIT ? DEPOSIT_LABEL : (type === TransactionType.SALARY ? SALARY_LABEL : WITHDRAW_LABEL), amount: (type === TransactionType.WITHDRAW ? -amount : amount), }); - await database.updateDocument(ACCOUNTS_COLLECTION_ID, accountDocument.documents[0].$id, { - balance: accountDocument.documents[0].balance + (type === TransactionType.WITHDRAW ? -amount : amount), - }); + await AccountService.updateBalance(accountNumber, type === TransactionType.WITHDRAW ? -amount : amount); return { label: transactionDocument.label, amount: transactionDocument.amount, date: new Date(transactionDocument.$createdAt * 1000) }; }, }; diff --git a/src/components/services/data.service.ts b/src/components/services/data.service.ts new file mode 100644 index 0000000..ee9252d --- /dev/null +++ b/src/components/services/data.service.ts @@ -0,0 +1,33 @@ +import { Databases, Models } from 'appwrite'; +import { AccountService } from './account.service'; +import { AppwriteService } from './appwrite.service'; +import { IAccountData } from '../../interfaces/account-data.interface'; +import { IPersonalInformation } from '../../interfaces/personal-information.interface'; + +const DATABASE_ID = '62dfd5787755e7ed9fcd'; +const PERSONAL_INFORMATION_COLLECTION_ID = '62e004abb0cdae53b483'; +const sdk = AppwriteService.getSDK(); +const database = new Databases(sdk, DATABASE_ID); + +export const DataService = { + async getAllData(): Promise { + const accounts = (await AccountService.getAllAccounts()) as IAccountData[]; + let offset = 0, + personalInformationDocuments: Models.DocumentList; + do { + personalInformationDocuments = await database.listDocuments( + PERSONAL_INFORMATION_COLLECTION_ID, [], 100, offset); + personalInformationDocuments.documents.forEach(personalInformation => { + const account = accounts.find(account => account.accountNumber === personalInformation.accountNumber); + if(account) { + account.name = `${account.firstName} ${account.lastName}`; + account.birthday = personalInformation.birthday; + account.address = personalInformation.address; + } + }, + ); + offset += 100; + } while(personalInformationDocuments.total > offset); + return accounts; + }, +}; \ No newline at end of file diff --git a/src/components/services/date.service.ts b/src/components/services/date.service.ts index d263e41..15fbca5 100644 --- a/src/components/services/date.service.ts +++ b/src/components/services/date.service.ts @@ -1,5 +1,11 @@ export class DateService { - public static toString(date: Date): string { + public static toString(date: Date | number): string { + if(typeof date === 'number') { + date = new Date(date * 1000); + } + if(!date) { + return 'n/a'; + } return date.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', @@ -9,4 +15,17 @@ export class DateService { second: '2-digit', }); } + public static toShortString(date: Date | number): string { + if(typeof date === 'number') { + date = new Date(date * 1000); + } + if(!date) { + return 'n/a'; + } + return date.toLocaleDateString('de-DE', { + day: '2-digit', + month: '2-digit', + year: 'numeric', + }); + } } \ No newline at end of file diff --git a/src/components/views/BankView.vue b/src/components/views/BankView.vue index e43f725..a9a4932 100644 --- a/src/components/views/BankView.vue +++ b/src/components/views/BankView.vue @@ -12,7 +12,7 @@ :transactions="accountDetails.transactions" /> -
+
{{ key }}{{ header.title }}
- {{ entry }} + + {{ entry[header.key] }} + + + {{ DateService.toShortString(entry[header.key]) }} + + + {{ DateService.toString(entry[header.key]) }} + + + {{ CurrencyService.toString(entry[header.key]) }} +