Last active
February 25, 2020 04:42
-
-
Save michaellarrubis/e4d9dd8e0bbda8378c3605d3d724d921 to your computer and use it in GitHub Desktop.
bookings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
import { getField, updateField } from 'vuex-map-fields' | |
import moment from 'moment' | |
import _ from 'lodash' | |
function formatDate(date) { | |
// 15 Feb 2018 | |
if (date){ | |
let year = moment(date).format('YYYY') | |
let day = moment(date).format('D') | |
let month = moment(date).format('MMM') | |
return `${day} ${month} ${year}` | |
} | |
return null | |
} | |
export const state = () => ({ | |
bookingCreated: false, | |
bookingNotCreated: false, | |
costRemoved: false, | |
costNotRemoved: false, | |
bookingCostsSaved: false, | |
bookingCostsNotSaved: false, | |
bookingExpenses: { | |
total_extra_costs: '', | |
extra_costs: [], | |
guide_fee: '', | |
insurance_fee: '', | |
total_fee: '', | |
}, | |
reservationDetail: { | |
id: null, | |
schedule: null, | |
schedule_end: null, | |
camp_site_duration: null, | |
meeting_time: null, | |
meeting_time_hour: null, | |
meeting_time_minute: null, | |
place: null, | |
place_latitude: null, | |
duration_day: null, | |
duration_night: null, | |
insurance_fee: null, | |
num_of_people: null, | |
place_longitude: null, | |
guide_fee: null, | |
total_extra_costs: null, | |
total_fee: null, | |
memo: null, | |
booking: { | |
ready_for_request: null, | |
progress: null, | |
message_thread_id: null | |
} | |
}, | |
reservationItineraries: [], | |
reservationMemoGuestList: null, | |
reservationMemoGuestDetail: null, | |
topBooking: { | |
booking_id: null, | |
reservation_id: null | |
}, | |
isTrips: false | |
}); | |
export const getters = { | |
getField | |
}; | |
export const mutations = { | |
SET_BOOKING_EXPENSES: (state, expenses) => { | |
state.bookingExpenses = expenses; | |
}, | |
REMOVE_BOOKING_COST: (state, cost) => { | |
for (let i = 0; i < state.bookingExpenses.costs.length; i++) { | |
if (cost.expenseId == state.bookingExpenses.costs[i].id) { | |
state.bookingExpenses.costs.splice(i, 1); | |
state.bookingExpenses = { | |
total_costs: state.bookingExpenses.total_costs, | |
costs: state.bookingExpenses.costs, | |
}; | |
break; | |
} | |
} | |
}, | |
updateField, | |
SET_BOOKING_CREATED: (state, created) => { | |
state.bookingCreated = created; | |
}, | |
SET_BOOKING_NOT_CREATED: (state, notCreated) => { | |
state.bookingNotCreated = notCreated; | |
}, | |
SET_COST_REMOVED: (state, removed) => { | |
state.costRemoved = removed; | |
}, | |
SET_COST_NOT_REMOVED: (state, notRemoved) => { | |
state.costNotRemoved = notRemoved; | |
}, | |
SET_BOOKING_COST_SAVED: (state, saved) => { | |
state.bookingCostsSaved = saved; | |
}, | |
SET_BOOKING_COST_NOT_SAVED: (state, notSaved) => { | |
state.bookingCostsNotSaved = notSaved; | |
}, | |
SET_RESERVATION_DETAIL: (state, reservationDetail) => { | |
state.reservationDetail = reservationDetail | |
}, | |
SET_RESERVATION_ITINERARIES: (state, reservationItineraries) => { | |
state.reservationItineraries = reservationItineraries | |
}, | |
REORDER_RESERVATION_ITINERARY: (state, { orderNum }) => { | |
const first = _.find(state.reservationItineraries, i => i.order_num == orderNum) | |
const second = _.find(state.reservationItineraries, i => i.order_num == orderNum + 1) | |
first.order_num++ | |
second.order_num-- | |
}, | |
SET_RESERVATION_MEMO_GUEST_LIST: (state, reservationMemoGuestList) => { | |
state.reservationMemoGuestList = reservationMemoGuestList | |
}, | |
SET_RESERVATION_MEMO_GUEST_DETAIL: (state, reservationMemoGuestDetail) => { | |
state.reservationMemoGuestDetail = reservationMemoGuestDetail | |
}, | |
UPDATE_RESERVATION_ITINERARY: (state, itinerary) => { | |
state.reservationItineraries = [ | |
...(_.reject(state.reservationItineraries, { 'id': itinerary.id })), | |
itinerary | |
] | |
}, | |
DELETE_BOOKING_ITINERARY: (state, itineraryId) => { | |
state.reservationItineraries = [ | |
...(_.reject(state.reservationItineraries, { 'id': itineraryId })), | |
] | |
}, | |
SET_TOP_BOOKING: (state, topBooking) => { | |
state.topBooking = { | |
booking_id: topBooking ? topBooking.id : null, | |
reservation_id: topBooking ? topBooking.reservation.id : null, | |
} | |
} | |
}; | |
export const actions = { | |
async saveBooking({ commit, dispatch }, booking) { | |
commit('SET_BOOKING_CREATED', false); | |
commit('SET_BOOKING_NOT_CREATED', false); | |
try { | |
const { data } = await axios.post('/api/bookings/createBooking', { booking }); | |
commit('SET_BOOKING_CREATED', true); | |
commit('SET_BOOKING_NOT_CREATED', false); | |
dispatch('getBookingItineraries', { params: { booking_id: data.booking.id, reservation_id: data.booking.reservation.id } }) | |
return data | |
} catch (error) { | |
commit('SET_BOOKING_CREATED', false); | |
commit('SET_BOOKING_NOT_CREATED', true); | |
console.log('Error creating booking: ', error); | |
throw error; | |
} | |
}, | |
async getBookingCosts({ commit, state }, booking) { | |
try { | |
let params = { | |
booking_id: booking.bookingId, | |
reservation_id: booking.reservationId | |
} | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
const response = await axios.get('/api/bookings/getBookingCosts', { params }); | |
commit('SET_BOOKING_EXPENSES', response.data); | |
} catch (error) { | |
console.log('Error fetching booking costs: ', error); | |
throw error; | |
} | |
}, | |
async saveBookingCosts({ commit, dispatch }, bookingCosts) { | |
commit('SET_BOOKING_COST_SAVED', false); | |
commit('SET_BOOKING_COST_NOT_SAVED', false); | |
try { | |
const response = await axios.post('/api/bookings/saveBookingCosts', { bookingCosts }); | |
commit('SET_BOOKING_COST_SAVED', true); | |
commit('SET_BOOKING_COST_NOT_SAVED', false); | |
} catch (error) { | |
commit('SET_BOOKING_COST_SAVED', false); | |
commit('SET_BOOKING_COST_NOT_SAVED', true); | |
console.log('Error saving booking costs: ', error); | |
throw error; | |
} finally { | |
dispatch('getReservationDetail', { booking_id: bookingCosts.bookingId, reservation_id: bookingCosts.reservationId }) | |
dispatch('getBookingCosts', { bookingId: bookingCosts.bookingId, reservationId: bookingCosts.reservationId }) | |
} | |
}, | |
async deleteCost({ commit, dispatch }, cost) { | |
commit('SET_COST_REMOVED', false); | |
commit('SET_COST_NOT_REMOVED', false); | |
try { | |
const response = await axios.post('/api/bookings/deleteBookingCost', { cost }); | |
commit('SET_COST_REMOVED', true); | |
commit('SET_COST_NOT_REMOVED', false); | |
} catch (error) { | |
commit('SET_COST_REMOVED', false); | |
commit('SET_COST_NOT_REMOVED', true); | |
console.log('Error deleting booking cost: ', error); | |
throw error; | |
} finally { | |
dispatch('getReservationDetail', { booking_id: cost.bookingId, reservation_id: cost.reservationId }) | |
dispatch('getBookingCosts', { bookingId: cost.bookingId, reservationId: cost.reservationId }) | |
} | |
}, | |
setTopBooking({commit}, data){ | |
commit('SET_TOP_BOOKING', data) | |
}, | |
clearFlags({ commit }) { | |
commit('SET_BOOKING_CREATED', false); | |
commit('SET_BOOKING_NOT_CREATED', false); | |
commit('SET_COST_REMOVED', false); | |
commit('SET_COST_NOT_REMOVED', false); | |
commit('SET_BOOKING_COST_SAVED', false); | |
commit('SET_BOOKING_COST_NOT_SAVED', false); | |
}, | |
clearReservationDetail({ commit }) { | |
const reservationDetail = { | |
id: null, | |
schedule: null, | |
schedule_end: null, | |
camp_site_duration: null, | |
meeting_time: null, | |
meeting_time_hour: null, | |
meeting_time_minute: null, | |
place: null, | |
place_latitude: null, | |
duration_day: null, | |
duration_night: null, | |
insurance_fee: null, | |
num_of_people: null, | |
place_longitude: null, | |
guide_fee: null, | |
total_extra_costs: null, | |
total_fee: null, | |
memo: null, | |
booking: { | |
ready_for_request: null, | |
progress: null, | |
message_thread_id: null | |
} | |
}; | |
commit('SET_RESERVATION_DETAIL', reservationDetail); | |
}, | |
async getReservationDetail({ commit, state, dispatch }, { booking_id, reservation_id }) { | |
try { | |
let params = { | |
booking_id, | |
reservation_id | |
} | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
const { data } = await axios.post('/api/bookings/reservations/getReservationDetail', { params }) | |
data.reservation.schedule = formatDate(data.reservation.schedule) | |
data.reservation.schedule_end = formatDate(data.reservation.schedule_end) | |
data.reservation.meeting_time_hour = data.reservation.meeting_time ? moment(data.reservation.meeting_time, 'HH:mm').format('H') : null | |
data.reservation.meeting_time_minute = data.reservation.meeting_time ? moment(data.reservation.meeting_time, 'HH:mm').format('mm') : null | |
commit('SET_RESERVATION_DETAIL', data.reservation) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
// Called when selecting a Booking in sidebar: | |
// - get itinerary list | |
// - get reservation detail (for details and memo) | |
// - get reservation extra costs | |
// - get reservation guests | |
async getAllBookingDetails({ commit, state, dispatch }, { booking_id, reservation_id, is_trips }) { | |
state.isTrips = is_trips | |
return Promise.all([ | |
await dispatch('getReservationDetail', { booking_id, reservation_id }), | |
await dispatch('getBookingCosts', { bookingId: booking_id, reservationId: reservation_id }), | |
await dispatch('getBookingItineraries', { params: { booking_id, reservation_id } }), | |
await dispatch('getReservationMemoGuestList', { booking_id, reservation_id }), | |
]) | |
}, | |
async updateReservationDetail({ commit, state, dispatch }, { booking_id, reservation_id }) { | |
let reservation = state.reservationDetail | |
let hour = reservation.meeting_time_hour | |
let minute = reservation.meeting_time_minute | |
reservation.meeting_time = hour && minute ? `${moment(hour, 'H').format('HH')}:${minute}` : null | |
reservation.description = reservation.memo | |
reservation.price = reservation.guide_fee | |
reservation.schedule = formatDate(reservation.schedule) | |
reservation.schedule_end = formatDate(reservation.schedule_end) | |
try { | |
const { data } = await axios.post('/api/bookings/reservations/updateReservationDetail', { booking_id, reservation_id, reservation }) | |
dispatch('getReservationDetail', { booking_id, reservation_id }) | |
dispatch('getBookingCosts', { bookingId: booking_id, reservationId: reservation_id }) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async getReservationMemoGuestList({ commit, state }, { booking_id, reservation_id }) { | |
try { | |
let params = { | |
booking_id, | |
reservation_id | |
} | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
const { data } = await axios.post('/api/bookings/reservations/getReservationMemoGuestList', { params }) | |
commit('SET_RESERVATION_MEMO_GUEST_LIST', data.guests) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async createReservationMemoGuestDetail({ commit, state, dispatch }, { params }) { | |
let booking_id = params.booking_id | |
let reservation_id = params.reservation_id | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
try { | |
const { data } = await axios.post('/api/bookings/reservations/createReservationMemoGuestDetail', { params }) | |
dispatch('getReservationMemoGuestList', { booking_id, reservation_id }) | |
dispatch('getReservationDetail', { booking_id, reservation_id }) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async updateReservationMemoGuestDetail({ commit, state, dispatch }, { params }) { | |
let booking_id = params.booking_id | |
let reservation_id = params.reservation_id | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
try { | |
const { data } = await axios.post('/api/bookings/reservations/updateReservationMemoGuestDetail', { params }) | |
dispatch('getReservationMemoGuestList', { booking_id, reservation_id }) | |
dispatch('getReservationDetail', { booking_id, reservation_id }) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async deleteReservationMemoGuestDetail({ commit, state, dispatch }, { params }) { | |
let booking_id = params.booking_id | |
let reservation_id = params.reservation_id | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
try { | |
const { data } = await axios.post('/api/bookings/reservations/deleteReservationMemoGuestDetail', { params }) | |
dispatch('getReservationMemoGuestList', { booking_id, reservation_id }) | |
dispatch('getReservationDetail', { booking_id, reservation_id }) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async getReservationMemoGuestDetail({ commit, state, dispatch }, { params }) { | |
try { | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
return await axios.post('/api/bookings/reservations/getReservationMemoGuestDetail', { params }) | |
} catch (error) { | |
throw error | |
} | |
}, | |
async getBookingList({commit}, { params }) { | |
try { | |
const { data } = await axios.get('/api/bookings/getBookList', { params }) | |
commit('SET_TOP_BOOKING', data.data && data.data.bookings.length > 0 ? data.data.bookings[0] : null) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async acceptOfferByGuide({commit, dispatch}, { booking_id, reservation_id}) { | |
try { | |
let data = await axios.post('/api/bookings/acceptOfferByGuide', { booking_id }) | |
dispatch('getReservationDetail', { booking_id, reservation_id }) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async acceptOfferByGuest({commit, dispatch}, { booking_id, reservation_id }) { | |
try { | |
await axios.post('/api/bookings/acceptOfferByGuest', { booking_id }) | |
return dispatch('getReservationDetail', { booking_id, reservation_id }) | |
} catch (error) { | |
throw error | |
} | |
}, | |
async cancelOffer({commit, dispatch}, { params }) { | |
try { | |
await axios.post('/api/bookings/cancelOffer', { params }) | |
dispatch('getReservationDetail', { booking_id: params.booking_id, reservation_id: params.reservation_id }) | |
return | |
} catch (error) { | |
throw error | |
} | |
}, | |
async getBookingItineraries({commit, state}, { params }) { | |
try { | |
if (state.isTrips) { | |
params['trips'] = true | |
} | |
const { data } = await axios.get('/api/bookings/itinerary', { params }) | |
commit('SET_RESERVATION_ITINERARIES', data && data.itinerary.length > 0 ? data.itinerary : []) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async reorderBookingItinerary ({ commit, state }, { params, orderNum }) { | |
try { | |
commit('REORDER_RESERVATION_ITINERARY', { orderNum }) | |
const itinerary = { | |
sequence: _.map(state.reservationItineraries, (i) => { return { id: i.id, order_num: i.order_num } }) | |
} | |
params['itinerary'] = itinerary | |
return await axios.post(`/api/bookings/itinerary/updateSequence`, { params }) | |
} catch (error) { | |
throw error | |
} | |
}, | |
// Update/Create itinerary from modal | |
async updateBookingItinerary ({ commit, dispatch }, { params }) { | |
try { | |
let itineraryToSave = Object.assign({}, params.itinerary) | |
if(!itineraryToSave.new_image_uploaded) { | |
// prevent uploading of image if theres no change | |
delete itineraryToSave.image | |
delete itineraryToSave.image_thumb | |
} | |
let itineraryToUpdate = params.itinerary | |
params['itinerary'] = itineraryToSave | |
if(params.itinerary.id) { | |
await axios.post(`/api/bookings/itinerary/update`, { params }) | |
} else { | |
params.itinerary = _.pick(params.itinerary, ['image', 'title', 'summary', 'location']) | |
const { data: { itinerary } } = await axios.post(`/api/bookings/itinerary/create`, { params }) | |
itineraryToUpdate = itinerary | |
} | |
dispatch('getBookingItineraries', { params: {booking_id: params.booking_id, reservation_id: params.reservation_id} }) | |
return commit('UPDATE_RESERVATION_ITINERARY', itineraryToUpdate) | |
} catch (error) { | |
throw error | |
} | |
}, | |
async deleteBookingItineraryImage ({ commit, dispatch }, { params }) { | |
try { | |
let data = await axios.post(`/api/bookings/itinerary/delete_image`, { params }) | |
dispatch('getBookingItineraries', { params }) | |
return data | |
} catch (error) { | |
throw error | |
} | |
}, | |
async deleteBookingItinerary ({ commit, dispatch }, { params }) { | |
try { | |
const { data } = await axios.post(`/api/bookings/itinerary/delete`, { params }) | |
dispatch('getBookingItineraries', { params }) | |
return commit('DELETE_BOOKING_ITINERARY', params.itineraryId) | |
} catch (error) { | |
throw error | |
} | |
}, | |
}; | |
export default { | |
namespaced: true, | |
state, | |
getters, | |
mutations, | |
actions | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment