Abstract saving to DB.

This commit is contained in:
HenriT
2021-02-16 22:34:50 +02:00
parent 5cf166ca12
commit 320bc5a3ad
4 changed files with 52 additions and 39 deletions

View File

@ -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);
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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()