When creating new client add distinct custom fields from all previous clients.

This commit is contained in:
HenriT
2021-04-12 14:35:04 +03:00
parent 292fafe3a5
commit dce73b5603
2 changed files with 26 additions and 8 deletions

View File

@ -23,6 +23,20 @@ export default {
client_id: clientId, client_id: clientId,
}); });
}, },
async addAllFields(store, clientId) {
// Get all distinct custom fields
const uniqueLabels = ClientField.all()
.map(field => field.label)
.filter((value, index, self) => self.indexOf(value) === index);
await Promise.all(uniqueLabels.map(async (label) => {
const field = await ClientField.createNew();
await field.$update({
label,
client_id: clientId,
});
}));
},
async deleteClientField({ dispatch }, fieldId) { async deleteClientField({ dispatch }, fieldId) {
await ClientField.delete(fieldId); await ClientField.delete(fieldId);
return dispatch('clients/updateClient', null, { root: true }); // TODO: pass clientId to make generic return dispatch('clients/updateClient', null, { root: true }); // TODO: pass clientId to make generic

View File

@ -1,6 +1,12 @@
import ClientService from '@/services/client.service'; import ClientService from '@/services/client.service';
import Client from '@/store/models/client'; import Client from '@/store/models/client';
function getClientById(clientId) {
return Client.query()
.with(['bank_account', 'fields'])
.find(clientId);
}
export default { export default {
namespaced: true, namespaced: true,
state: { state: {
@ -32,13 +38,15 @@ export default {
commit('clientId', client.id); commit('clientId', client.id);
Client.insert({ data: client }); Client.insert({ data: client });
}, },
async createNewClient(store, client) { async createNewClient({ dispatch }, client) {
if (!client.hasOwnProperty('id')) { if (!client.hasOwnProperty('id')) {
client = new Client(client); client = new Client(client);
} }
await dispatch('clientFields/addAllFields', client.id, { root: true });
const res = await ClientService.createClient(client); const res = await ClientService.createClient(client);
await Client.insert({ data: res }); await Client.insert({ data: res });
return Client.find(res.id); return getClientById(res.id);
}, },
clientProps({ state }, props) { clientProps({ state }, props) {
return Client.update({ return Client.update({
@ -53,9 +61,7 @@ export default {
return ClientService.updateClient(getters.client); return ClientService.updateClient(getters.client);
}, },
async updateClientById(store, payload) { async updateClientById(store, payload) {
const client = Client.query() const client = getClientById(payload.clientId);
.with('fields')
.find(payload.clientId);
client.$update(payload.props); client.$update(payload.props);
return ClientService.updateClient(client); return ClientService.updateClient(client);
}, },
@ -72,9 +78,7 @@ export default {
}, },
getters: { getters: {
client(state) { client(state) {
return Client.query() return getClientById(state.clientId);
.with(['bank_account', 'fields'])
.find(state.clientId);
}, },
all() { all() {
return Client.query() return Client.query()