diff --git a/src/components/molecules/MoleculeMigrateAccountModal.vue b/src/components/molecules/MoleculeMigrateAccountModal.vue new file mode 100644 index 0000000..a6cbfc8 --- /dev/null +++ b/src/components/molecules/MoleculeMigrateAccountModal.vue @@ -0,0 +1,75 @@ + + + + + \ No newline at end of file diff --git a/src/components/services/account.service.ts b/src/components/services/account.service.ts index 07f4b42..9f3a37a 100644 --- a/src/components/services/account.service.ts +++ b/src/components/services/account.service.ts @@ -53,4 +53,14 @@ export const AccountService = { const accountDocument = await database.createDocument(ACCOUNTS_COLLECTION_ID, 'unique()', account); return accountDocument; }, + async migrateAccount(oldAccountNumber: string, newAccountNumber: string): Promise { + const account = await this.getAccount(oldAccountNumber); + if(!account) { + throw new Error('Account not found'); + } + await database.updateDocument(ACCOUNTS_COLLECTION_ID, account.$id, { + accountNumber: newAccountNumber, + }); + return account; + }, }; \ No newline at end of file diff --git a/src/components/services/bank.service.ts b/src/components/services/bank.service.ts index 03d9400..f79a8b0 100644 --- a/src/components/services/bank.service.ts +++ b/src/components/services/bank.service.ts @@ -11,10 +11,10 @@ const SALARY_LABEL = 'Gehalt'; const WITHDRAW_LABEL = 'Bargeldauszahlung'; const OPEN_ACCOUNT_LABEL = 'Kontoeröffnung'; const sdk = AppwriteService.getSDK(); +const database = new Databases(sdk, DATABASE_ID); export const BankService = { async getAccountDetails(accountNumber: string): Promise { - const database = new Databases(sdk, DATABASE_ID); const account = await AccountService.getAccount(accountNumber); if(!account) { throw new Error('Account not found'); @@ -35,7 +35,6 @@ export const BankService = { }; }, async addTransaction(accountNumber: string, amount: number, type: TransactionType): Promise { - const database = new Databases(sdk, DATABASE_ID); let label = ''; switch (type) { case TransactionType.DEPOSIT: @@ -59,6 +58,16 @@ export const BankService = { await AccountService.updateBalance(accountNumber, type === TransactionType.WITHDRAW ? -amount : amount); return { label: transactionDocument.label, amount: transactionDocument.amount, date: new Date(transactionDocument.$createdAt * 1000) }; }, + async migrateTransactions(oldAccountNumber: string, newAccountNumber: string): Promise { + const transactionDocuments = await database.listDocuments( + TRANSACTIONS_COLLECTION_ID, [Query.equal('accountNumber', oldAccountNumber)], 100, 0, undefined, undefined, ['$createdAt'], ['DESC']); + transactionDocuments.documents.forEach(async transaction => { + await database.updateDocument(TRANSACTIONS_COLLECTION_ID, transaction.$id, { + accountNumber: newAccountNumber, + }); + }); + return true; + }, }; export enum TransactionType { diff --git a/src/components/services/data.service.ts b/src/components/services/data.service.ts index 8a1e52c..02fc06b 100644 --- a/src/components/services/data.service.ts +++ b/src/components/services/data.service.ts @@ -65,4 +65,9 @@ export const DataService = { accountDocument.balance = OPEN_ACCOUNT_BALANCE; return accountDocument; }, + async migrateAccount(oldAccountNumber: string, newAccountNumber: string): Promise { + await AccountService.migrateAccount(oldAccountNumber, newAccountNumber); + await BankService.migrateTransactions(oldAccountNumber, newAccountNumber); + return true; + }, }; \ No newline at end of file diff --git a/src/components/views/DataView.vue b/src/components/views/DataView.vue index bbfa1a4..58a2f74 100644 --- a/src/components/views/DataView.vue +++ b/src/components/views/DataView.vue @@ -27,10 +27,19 @@ :name="contactName" :contact="contact" /> + +