From 320bc5a3ad658aa1ca907c5859adebd4564e16e8 Mon Sep 17 00:00:00 2001 From: HenriT Date: Tue, 16 Feb 2021 22:34:50 +0200 Subject: [PATCH] Abstract saving to DB. --- src/services/bank-account.service.js | 22 +++++++++-------- src/services/client.service.js | 35 ++++++++++++++-------------- src/services/invoice.service.js | 29 ++++++++++++++--------- src/utils/helpers.js | 5 ++++ 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/services/bank-account.service.js b/src/services/bank-account.service.js index 4b48773..a0dc1b7 100644 --- a/src/services/bank-account.service.js +++ b/src/services/bank-account.service.js @@ -1,4 +1,5 @@ import storage from 'localforage'; +import { removeVuexORMFlags } from '@/utils/helpers'; class BankAccountService { async getBankAccounts() { @@ -13,21 +14,22 @@ class BankAccountService { } async createBankAccount(bankAccount) { - const bankAccounts = await this.getBankAccounts(); - - delete bankAccount.$id; - delete bankAccount.$isNew; - delete bankAccount.$isDirty; - - bankAccounts.push(bankAccount); - await storage.setItem('bank_accounts', bankAccounts); - return bankAccount; + return this.saveBankAccount(bankAccount); } async updateBankAccount(bankAccount) { + return this.saveBankAccount(bankAccount); + } + + async saveBankAccount(bankAccount) { const bankAccounts = await this.getBankAccounts(); const index = bankAccounts.findIndex(item => item.id === bankAccount.id); - bankAccounts[index] = bankAccount; + removeVuexORMFlags(bankAccount); + if (index === -1) { + bankAccounts.push(bankAccount); + } else { + bankAccounts[index] = bankAccount; + } return storage.setItem('bank_accounts', bankAccounts); } } diff --git a/src/services/client.service.js b/src/services/client.service.js index 613d360..f6bdddc 100644 --- a/src/services/client.service.js +++ b/src/services/client.service.js @@ -1,4 +1,5 @@ import storage from 'localforage'; +import { removeVuexORMFlags } from '@/utils/helpers'; class ClientService { async getClients() { @@ -13,27 +14,11 @@ class ClientService { } async createClient(client) { - const clients = await this.getClients(); - - delete client.$id; - delete client.$isNew; - delete client.$isDirty; - - clients.push(client); - await storage.setItem('clients', clients); - return client; + return this.saveClient(client); } async updateClient(client) { - const clients = await this.getClients(); - const index = clients.findIndex(item => item.id === client.id); - - // TODO: Fix this mess - if (index === -1) { - return false; - } - clients[index] = client; - return storage.setItem('clients', clients); + return this.saveClient(client); } async deleteClient(clientId) { @@ -42,6 +27,20 @@ class ClientService { clients.splice(index, 1); return storage.setItem('clients', clients); } + + async saveClient(client) { + const clients = await this.getClients(); + const index = clients.findIndex(item => item.id === client.id); + removeVuexORMFlags(client); + + if (index === -1) { + clients.push(client); + } else { + clients[index] = client; + } + await storage.setItem('clients', clients); + return client; + } } export default new ClientService(); diff --git a/src/services/invoice.service.js b/src/services/invoice.service.js index f0a1d27..c0e4723 100644 --- a/src/services/invoice.service.js +++ b/src/services/invoice.service.js @@ -1,6 +1,6 @@ import storage from 'localforage'; import TeamService from '@/services/team.service'; -import { validate, generateInvoiceNumber } from '@/utils/helpers'; +import { validate, generateInvoiceNumber, removeVuexORMFlags } from '@/utils/helpers'; import dayjs from 'dayjs'; class InvoiceService { @@ -40,13 +40,9 @@ class InvoiceService { invoice.vat_rate = 0; invoice.currency = 'USD'; - delete invoice.$id; - delete invoice.$isNew; - delete invoice.$isDirty; delete invoice.client; - invoices.push(invoice); - await storage.setItem('invoices', invoices); + return this.saveInvoice(invoice); } async updateInvoice(invoice) { @@ -63,12 +59,8 @@ class InvoiceService { if (Object.keys(res.errors).length > 0) { return Promise.reject(res); } - delete invoice.client; - const invoices = await this.getInvoices(); - const index = invoices.findIndex(item => item.id === invoice.id); - invoices[index] = invoice; - return storage.setItem('invoices', invoices); + return this.saveInvoice(invoice); } async deleteInvoice(invoiceId) { @@ -119,6 +111,21 @@ class InvoiceService { invoice.status = 'booked'; return this.updateInvoice(invoice); } + + async saveInvoice(invoice) { + const invoices = await this.getInvoices(); + const index = invoices.findIndex(item => item.id === invoice.id); + + delete invoice.client; + removeVuexORMFlags(invoice); + + if (index === -1) { + invoices.push(invoice); + } else { + invoices[index] = invoice; + } + return storage.setItem('invoices', invoices); + } } export default new InvoiceService(); diff --git a/src/utils/helpers.js b/src/utils/helpers.js index 503ba45..994b4b7 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -65,6 +65,11 @@ export function validateField(input, field, label) { return null; } +export function removeVuexORMFlags(obj) { + delete obj.$id; + delete obj.$isNew; + delete obj.$isDirty; +} export function generateInvoiceNumber(invoices) { const date = dayjs()