refactor dashboard actions and send email actions

This commit is contained in:
Jay_Makwana
2019-11-18 19:41:31 +05:30
parent 4a8ac36b3a
commit 94d48c76b8
12 changed files with 93 additions and 130 deletions

View File

@ -260,6 +260,8 @@ export default {
created_message: 'Estimate created successfully', created_message: 'Estimate created successfully',
updated_message: 'Estimate updated successfully', updated_message: 'Estimate updated successfully',
deleted_message: 'Estimate deleted successfully | Estimates deleted successfully', deleted_message: 'Estimate deleted successfully | Estimates deleted successfully',
user_email_does_not_exist: 'User email does not exist',
something_went_wrong: 'something went wrong',
item: { item: {
title: 'Item Title', title: 'Item Title',
description: 'Description', description: 'Description',
@ -349,6 +351,8 @@ export default {
updated_message: 'Invoice updated successfully', updated_message: 'Invoice updated successfully',
deleted_message: 'Invoice deleted successfully | Invoices deleted successfully', deleted_message: 'Invoice deleted successfully | Invoices deleted successfully',
marked_as_sent_message: 'Invoice marked as sent successfully', marked_as_sent_message: 'Invoice marked as sent successfully',
user_email_does_not_exist: 'User email does not exist',
something_went_wrong: 'something went wrong',
invalid_due_amount_message: 'Total Invoice amount cannot be less than total paid amount for this Invoice. Please update the invoice or delete the associated payments to continue.' invalid_due_amount_message: 'Total Invoice amount cannot be less than total paid amount for this Invoice. Please update the invoice or delete the associated payments to continue.'
}, },
credit_notes: { credit_notes: {

View File

@ -23,108 +23,3 @@ export const getChart = ({ commit, dispatch, state }) => {
}) })
}) })
} }
export const sendEmail = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => {
window.axios.post(`/api/invoices/send`, data).then((response) => {
commit(types.UPDATE_INVOICE_STATUS, { id: data.id, status: 'SENT' })
resolve(response)
}).catch((err) => {
reject(err)
})
})
}
export const markAsSent = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => {
window.axios.post(`/api/invoices/mark-as-sent`, data).then((response) => {
commit(types.UPDATE_INVOICE_STATUS, { id: data.id, status: 'SENT' })
resolve(response)
}).catch((err) => {
reject(err)
})
})
}
export const deleteInvoice = ({ commit, dispatch, state }, id) => {
return new Promise((resolve, reject) => {
window.axios.delete(`/api/invoices/${id}`).then((response) => {
if (response.data.error) {
resolve(response)
} else {
commit(types.DELETE_INVOICE, id)
resolve(response)
}
}).catch((err) => {
reject(err)
})
})
}
export const sendEstimateEmail = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/send`, data).then((response) => {
commit(types.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'SENT' })
resolve(response)
}).catch((err) => {
reject(err)
})
})
}
export const markAsAccepted = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/accept`, data).then((response) => {
commit(types.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'ACCEPTED' })
resolve(response)
}).catch((err) => {
reject(err)
})
})
}
export const markAsRejected = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/reject`, data).then((response) => {
commit(types.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'REJECTED' })
resolve(response)
}).catch((err) => {
reject(err)
})
})
}
export const markEstimateAsSent = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/mark-as-sent`, data).then((response) => {
commit(types.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'SENT' })
resolve(response)
}).catch((err) => {
reject(err)
})
})
}
export const convertToInvoice = ({ commit, dispatch, state }, id) => {
return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/${id}/convert-to-invoice`).then((response) => {
// commit(types.UPDATE_INVOICE, response.data)
resolve(response)
}).catch((err) => {
reject(err)
})
})
}
export const deleteEstimate = ({ commit, dispatch, state }, id) => {
return new Promise((resolve, reject) => {
window.axios.delete(`/api/estimates/${id}`).then((response) => {
commit(types.DELETE_ESTIMATE, id)
resolve(response)
}).catch((err) => {
reject(err)
})
})
}

View File

@ -1,7 +1,7 @@
import * as types from './mutation-types' import * as types from './mutation-types'
export default { export default {
[types.SET_INITIAL_DATA](state, data) { [types.SET_INITIAL_DATA] (state, data) {
state.contacts = data.customersCount state.contacts = data.customersCount
state.invoices = data.invoicesCount state.invoices = data.invoicesCount
state.estimates = data.estimatesCount state.estimates = data.estimatesCount
@ -30,29 +30,33 @@ export default {
state.netProfit = data.netProfit state.netProfit = data.netProfit
}, },
[types.GET_INITIAL_DATA](state, data) { [types.GET_INITIAL_DATA] (state, data) {
state.isDataLoaded = data state.isDataLoaded = data
}, },
[types.UPDATE_INVOICE_STATUS](state, data) { [types.UPDATE_INVOICE_STATUS] (state, data) {
let pos = state.dueInvoices.findIndex(invoice => invoice.id === data.id) let pos = state.dueInvoices.findIndex(invoice => invoice.id === data.id)
state.dueInvoices[pos].status = data.status if (state.dueInvoices[pos]) {
state.dueInvoices[pos].status = data.status
}
}, },
[types.DELETE_INVOICE](state, id) { [types.DELETE_INVOICE] (state, id) {
let index = state.dueInvoices.findIndex(invoice => invoice.id === id) let index = state.dueInvoices.findIndex(invoice => invoice.id === id)
state.dueInvoices.splice(index, 1) state.dueInvoices.splice(index, 1)
}, },
[types.DELETE_ESTIMATE](state, id) { [types.DELETE_ESTIMATE] (state, id) {
let index = state.recentEstimates.findIndex(estimate => estimate.id === id) let index = state.recentEstimates.findIndex(estimate => estimate.id === id)
state.recentEstimates.splice(index, 1) state.recentEstimates.splice(index, 1)
}, },
[types.UPDATE_ESTIMATE_STATUS](state, data) { [types.UPDATE_ESTIMATE_STATUS] (state, data) {
let pos = state.recentEstimates.findIndex(estimate => estimate.id === data.id) let pos = state.recentEstimates.findIndex(estimate => estimate.id === data.id)
state.recentEstimates[pos].status = data.status if (state.recentEstimates[pos]) {
}, state.recentEstimates[pos].status = data.status
}
}
} }

View File

@ -1,4 +1,5 @@
import * as types from './mutation-types' import * as types from './mutation-types'
import * as dashboardTypes from '../dashboard/mutation-types'
export const fetchEstimates = ({ commit, dispatch, state }, params) => { export const fetchEstimates = ({ commit, dispatch, state }, params) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -56,7 +57,10 @@ export const fetchViewEstimate = ({ commit, dispatch, state }, id) => {
export const sendEmail = ({ commit, dispatch, state }, data) => { export const sendEmail = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/send`, data).then((response) => { window.axios.post(`/api/estimates/send`, data).then((response) => {
commit(types.UPDATE_ESTIMATE_STATUS, {id: data.id, status: 'SENT'}) if (response.data.success) {
commit(types.UPDATE_ESTIMATE_STATUS, {id: data.id, status: 'SENT'})
commit('dashboard/' + dashboardTypes.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'SENT' }, { root: true })
}
resolve(response) resolve(response)
}).catch((err) => { }).catch((err) => {
reject(err) reject(err)
@ -80,6 +84,7 @@ export const deleteEstimate = ({ commit, dispatch, state }, id) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.axios.delete(`/api/estimates/${id}`).then((response) => { window.axios.delete(`/api/estimates/${id}`).then((response) => {
commit(types.DELETE_ESTIMATE, id) commit(types.DELETE_ESTIMATE, id)
commit('dashboard/' + dashboardTypes.DELETE_ESTIMATE, id, { root: true })
resolve(response) resolve(response)
}).catch((err) => { }).catch((err) => {
reject(err) reject(err)
@ -112,6 +117,7 @@ export const updateEstimate = ({ commit, dispatch, state }, data) => {
export const markAsAccepted = ({ commit, dispatch, state }, data) => { export const markAsAccepted = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/accept`, data).then((response) => { window.axios.post(`/api/estimates/accept`, data).then((response) => {
commit('dashboard/' + dashboardTypes.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'ACCEPTED' }, { root: true })
resolve(response) resolve(response)
}).catch((err) => { }).catch((err) => {
reject(err) reject(err)
@ -122,6 +128,7 @@ export const markAsAccepted = ({ commit, dispatch, state }, data) => {
export const markAsRejected = ({ commit, dispatch, state }, data) => { export const markAsRejected = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/reject`, data).then((response) => { window.axios.post(`/api/estimates/reject`, data).then((response) => {
commit('dashboard/' + dashboardTypes.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'REJECTED' }, { root: true })
resolve(response) resolve(response)
}).catch((err) => { }).catch((err) => {
reject(err) reject(err)
@ -133,6 +140,7 @@ export const markAsSent = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.axios.post(`/api/estimates/mark-as-sent`, data).then((response) => { window.axios.post(`/api/estimates/mark-as-sent`, data).then((response) => {
commit(types.UPDATE_ESTIMATE_STATUS, {id: data.id, status: 'SENT'}) commit(types.UPDATE_ESTIMATE_STATUS, {id: data.id, status: 'SENT'})
commit('dashboard/' + dashboardTypes.UPDATE_ESTIMATE_STATUS, { id: data.id, status: 'SENT' }, { root: true })
resolve(response) resolve(response)
}).catch((err) => { }).catch((err) => {
reject(err) reject(err)

View File

@ -40,7 +40,9 @@ export default {
[types.UPDATE_ESTIMATE_STATUS] (state, data) { [types.UPDATE_ESTIMATE_STATUS] (state, data) {
let pos = state.estimates.findIndex(estimate => estimate.id === data.id) let pos = state.estimates.findIndex(estimate => estimate.id === data.id)
state.estimates[pos].status = data.status if (state.estimates[pos]) {
state.estimates[pos].status = data.status
}
}, },
[types.RESET_SELECTED_ESTIMATES] (state, data) { [types.RESET_SELECTED_ESTIMATES] (state, data) {

View File

@ -1,5 +1,5 @@
import * as types from './mutation-types' import * as types from './mutation-types'
// import moment from 'moment' import * as dashboardTypes from '../dashboard/mutation-types'
export const fetchInvoices = ({ commit, dispatch, state }, params) => { export const fetchInvoices = ({ commit, dispatch, state }, params) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -47,7 +47,10 @@ export const fetchViewInvoice = ({ commit, dispatch, state }, id) => {
export const sendEmail = ({ commit, dispatch, state }, data) => { export const sendEmail = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.axios.post(`/api/invoices/send`, data).then((response) => { window.axios.post(`/api/invoices/send`, data).then((response) => {
commit(types.UPDATE_INVOICE_STATUS, {id: data.id, status: 'SENT'}) if (response.data.success) {
commit(types.UPDATE_INVOICE_STATUS, {id: data.id, status: 'SENT'})
commit('dashboard/' + dashboardTypes.UPDATE_INVOICE_STATUS, {id: data.id, status: 'SENT'}, { root: true })
}
resolve(response) resolve(response)
}).catch((err) => { }).catch((err) => {
reject(err) reject(err)
@ -83,6 +86,7 @@ export const deleteInvoice = ({ commit, dispatch, state }, id) => {
resolve(response) resolve(response)
} else { } else {
commit(types.DELETE_INVOICE, id) commit(types.DELETE_INVOICE, id)
commit('dashboard/' + dashboardTypes.DELETE_INVOICE, id, { root: true })
resolve(response) resolve(response)
} }
}).catch((err) => { }).catch((err) => {
@ -123,6 +127,7 @@ export const markAsSent = ({ commit, dispatch, state }, data) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.axios.post(`/api/invoices/mark-as-sent`, data).then((response) => { window.axios.post(`/api/invoices/mark-as-sent`, data).then((response) => {
commit(types.UPDATE_INVOICE_STATUS, {id: data.id, status: 'SENT'}) commit(types.UPDATE_INVOICE_STATUS, {id: data.id, status: 'SENT'})
commit('dashboard/' + dashboardTypes.UPDATE_INVOICE_STATUS, {id: data.id, status: 'SENT'}, { root: true })
resolve(response) resolve(response)
}).catch((err) => { }).catch((err) => {
reject(err) reject(err)

View File

@ -31,7 +31,9 @@ export default {
[types.UPDATE_INVOICE_STATUS] (state, data) { [types.UPDATE_INVOICE_STATUS] (state, data) {
let pos = state.invoices.findIndex(invoice => invoice.id === data.id) let pos = state.invoices.findIndex(invoice => invoice.id === data.id)
state.invoices[pos].status = data.status if (state.invoices[pos]) {
state.invoices[pos].status = data.status
}
}, },
[types.RESET_SELECTED_INVOICES] (state, data) { [types.RESET_SELECTED_INVOICES] (state, data) {

View File

@ -404,17 +404,23 @@ export default {
methods: { methods: {
...mapActions('dashboard', [ ...mapActions('dashboard', [
'getChart', 'getChart',
'loadData', 'loadData'
]),
...mapActions('invoice', [
'deleteInvoice', 'deleteInvoice',
'sendEmail', 'sendEmail',
'markAsSent', 'markAsSent'
'sendEstimateEmail', ]),
...mapActions('estimate', [
'deleteEstimate', 'deleteEstimate',
'markAsAccepted', 'markAsAccepted',
'markAsRejected', 'markAsRejected',
'markEstimateAsSent',
'convertToInvoice' 'convertToInvoice'
]), ]),
...mapActions('estimate', {
'sendEstimateEmail': 'sendEmail',
'markEstimateAsSent': 'markAsSent'
}),
async loadChart () { async loadChart () {
await this.$store.dispatch('dashboard/getChart') await this.$store.dispatch('dashboard/getChart')
@ -532,9 +538,15 @@ export default {
} }
let response = await this.sendEmail(data) let response = await this.sendEmail(data)
this.refreshInvTable() this.refreshInvTable()
if (response.data) { if (response.data.success) {
window.toastr['success'](this.$tc('invoices.send_invoice_successfully')) window.toastr['success'](this.$tc('invoices.send_invoice_successfully'))
return true
} }
if (response.data.error === 'user_email_does_not_exist') {
window.toastr['error'](this.$tc('invoices.user_email_does_not_exist'))
return false
}
window.toastr['error'](this.$tc('invoices.something_went_wrong'))
} }
}) })
}, },
@ -618,9 +630,15 @@ export default {
} }
let response = await this.sendEstimateEmail(data) let response = await this.sendEstimateEmail(data)
this.refreshEstTable() this.refreshEstTable()
if (response.data) { if (response.data.success) {
window.toastr['success'](this.$tc('estimates.send_estimate_successfully')) window.toastr['success'](this.$tc('estimates.send_estimate_successfully'))
return true
} }
if (response.data.error === 'user_email_does_not_exist') {
window.toastr['success'](this.$tc('estimates.user_email_does_not_exist'))
return true
}
window.toastr['error'](this.$tc('estimates.something_went_wrong'))
} }
}) })
} }

View File

@ -590,9 +590,15 @@ export default {
} }
let response = await this.sendEmail(data) let response = await this.sendEmail(data)
this.refreshTable() this.refreshTable()
if (response.data) { if (response.data.success) {
window.toastr['success'](this.$tc('estimates.send_estimate_successfully')) window.toastr['success'](this.$tc('estimates.send_estimate_successfully'))
return true
} }
if (response.data.error === 'user_email_does_not_exist') {
window.toastr['success'](this.$tc('estimates.user_email_does_not_exist'))
return true
}
window.toastr['error'](this.$tc('estimates.something_went_wrong'))
} }
}) })
} }

View File

@ -260,9 +260,15 @@ export default {
this.isSendingEmail = true this.isSendingEmail = true
let response = await this.sendEmail({id: this.estimate.id}) let response = await this.sendEmail({id: this.estimate.id})
this.isSendingEmail = false this.isSendingEmail = false
if (response.data) { if (response.data.success) {
window.toastr['success'](this.$tc('estimates.send_estimate_successfully')) window.toastr['success'](this.$tc('estimates.send_estimate_successfully'))
return true
} }
if (response.data.error === 'user_email_does_not_exist') {
window.toastr['success'](this.$tc('estimates.user_email_does_not_exist'))
return true
}
window.toastr['error'](this.$tc('estimates.something_went_wrong'))
} }
}) })
}, },

View File

@ -397,9 +397,15 @@ export default {
} }
let response = await this.sendEmail(data) let response = await this.sendEmail(data)
this.refreshTable() this.refreshTable()
if (response.data) { if (response.data.success) {
window.toastr['success'](this.$tc('invoices.send_invoice_successfully')) window.toastr['success'](this.$tc('invoices.send_invoice_successfully'))
return true
} }
if (response.data.error === 'user_email_does_not_exist') {
window.toastr['error'](this.$tc('invoices.user_email_does_not_exist'))
return false
}
window.toastr['error'](this.$tc('invoices.something_went_wrong'))
} }
}) })
}, },
@ -505,6 +511,7 @@ export default {
if (res.data.success) { if (res.data.success) {
window.toastr['success'](this.$tc('invoices.deleted_message')) window.toastr['success'](this.$tc('invoices.deleted_message'))
this.$refs.table.refresh()
return true return true
} }

View File

@ -260,9 +260,15 @@ export default {
this.isSendingEmail = true this.isSendingEmail = true
let response = await this.sendEmail({id: this.invoice.id}) let response = await this.sendEmail({id: this.invoice.id})
this.isSendingEmail = false this.isSendingEmail = false
if (response.data) { if (response.data.success) {
window.toastr['success'](this.$tc('invoices.confirm_send_invoice')) window.toastr['success'](this.$tc('invoices.send_invoice_successfully'))
return true
} }
if (response.data.error === 'user_email_does_not_exist') {
window.toastr['error'](this.$tc('invoices.user_email_does_not_exist'))
return false
}
window.toastr['error'](this.$tc('invoices.something_went_wrong'))
} }
}) })
}, },