Skip to content

Commit

Permalink
Tobi develop (#1182)
Browse files Browse the repository at this point in the history
* fix(Work.vue):

- Change string comparison to include check for '400' in response

* fix(Work.vue):

- Change string comparison to include check for '400' in response

* Refactor error handling in Work.vue and main.ts

- refactor: Replace string comparison with response status check for better error identification in Work.vue
- fix: Correct error handling in main.ts to rethrow errors after logging warning messages
- chore: Import AxiosError in WorksiteForm.vue
- feat: Add saveWorksite retry mechanism and storage of unsaved worksite details
- feat: Implement call status update function in WorksiteForm.vue
  • Loading branch information
tabiodun authored Oct 19, 2024
1 parent 44c1ca1 commit 0097dd2
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 9 deletions.
139 changes: 132 additions & 7 deletions src/components/work/WorksiteForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ import * as turf from '@turf/turf';
import * as L from 'leaflet';
import { useI18n } from 'vue-i18n';
import { useToast } from 'vue-toastification';
import axios from 'axios';
import axios, { AxiosError } from 'axios';
import GeocoderService from '../../services/geocoder.service';
import Worksite from '../../models/Worksite';
import { StorageService } from '../../services/storage.service';
Expand All @@ -468,6 +468,9 @@ import SectionHeading from './SectionHeading.vue';
import WorksiteNotes from './WorksiteNotes.vue';
import Language from '@/models/Language';
import { useRecentWorksites } from '@/hooks/useRecentWorksites';
import useConnectFirst from '@/hooks/useConnectFirst';
import { Store } from 'vuex';
import PhoneOutbound from '@/models/PhoneOutbound';
const AUTO_CONTACT_FREQUENCY_OPTIONS = [
'formOptions.often',
Expand Down Expand Up @@ -526,6 +529,10 @@ export default defineComponent({
updatedFiles.value.push(formData.id);
};
const { call, isInboundCall } = useConnectFirst({
emit,
});
const contactFrequencyOptions = AUTO_CONTACT_FREQUENCY_OPTIONS.map(
(key) => {
return {
Expand Down Expand Up @@ -977,7 +984,7 @@ export default defineComponent({
updatedFiles.value = imageList;
}
async function saveWorksite(reload = true) {
async function saveWorksite(reload = true, isRetry = false) {
let firstErrorField;
let firstErrorMessage = '';
Expand Down Expand Up @@ -1115,14 +1122,35 @@ export default defineComponent({
if (isFavorite.value) {
await Worksite.api().favorite(worksite.value.id);
}
return worksite.value.id;
} else {
const savedWorksite = await Worksite.api().post('/worksites', {
const response = await Worksite.api().post('/worksites', {
...worksite.value,
incident: props.incidentId,
skip_duplicate_check: true,
send_sms: true,
});
const worksiteId = savedWorksite.entities.worksites[0].id;
if (response.response instanceof AxiosError) {
// if error is a 403, save the current details to local storage and reload the page with a query param
if ([500, 403].includes(response.response.status) && !isRetry) {
StorageService.setItem('currentWorksiteToSave', worksite.value);
if (call.value) {
StorageService.setItem('callToComplete', {
call: call.value,
type: isInboundCall.value ? 'inbound' : 'outbound',
});
}
window.location.href = `${window.location.origin}${window.location.pathname}?continueSaving=true`;
return;
}
$toasted.error(getErrorMessage(response.response));
return;
}
const worksiteId = response.entities.worksites[0].id;
StorageService.removeItem('currentWorksite');
await Promise.all(
notesToSave.map((n) => Worksite.api().addNote(worksiteId, n)),
Expand Down Expand Up @@ -1158,17 +1186,17 @@ export default defineComponent({
addRecentWorksite(worksite.value as Worksite);
}
await $toasted.success(t('caseForm.new_case_success'));
$toasted.success(t('caseForm.new_case_success'));
dirtyFields.value = new Set();
if (reload) {
emit('reloadTable');
emit('reloadMap', worksite.value.id);
emit('savedWorksite', worksite.value);
}
return true;
return worksite.value.id;
} catch (error) {
await $toasted.error(getErrorMessage(error));
return false;
return;
}
}
Expand Down Expand Up @@ -1400,6 +1428,103 @@ export default defineComponent({
}
}
async function updateCallStatus(worksiteId = null) {
const callToComplete = StorageService.getItem('callToComplete');
if (callToComplete) {
StorageService.removeItem('callToComplete');
try {
if (callToComplete.type === 'outbound') {
await PhoneOutbound.api().updateStatus(callToComplete.call.id, {
statusId: worksiteId ? 1 : 23,
worksiteId: worksiteId,
notes: worksiteId
? 'Automatically saved case and completed call'
: `Automatically completed call case not saved for outbound ${callToComplete.call.id}`,
});
}
if (callToComplete.type === 'inbound') {
let data = {
status: worksiteId ? 1 : 23,
notes: worksiteId
? 'Automatically saved case and completed call'
: `Automatically completed call case not saved for inbound ${callToComplete.call.id}`,
cases: [],
};
if (worksiteId) {
data = { ...data, cases: [worksiteId] };
}
await axios.post(
`${import.meta.env.VITE_APP_API_BASE_URL}/phone_inbound/${
callToComplete.call.id
}/update_status`,
data,
);
}
} catch (error) {
$toasted.error(getErrorMessage(error));
} finally {
StorageService.removeItem('callToComplete');
}
}
}
watch(
() => form.value,
(newValue) => {
if (newValue && route.query.continueSaving) {
delete route.query.continueSaving;
worksite.value = StorageService.getItem('currentWorksiteToSave');
StorageService.removeItem('currentWorksiteToSave');
const toastId = $toasted.info(
t('~~Attempting to save case please wait...'),
);
saveWorksite(true, true)
.then(async (worksiteId) => {
$toasted.dismiss(toastId);
await updateCallStatus(worksiteId);
if (worksiteId) {
StorageService.removeItem('currentWorksite');
}
await (worksiteId
? confirm({
title: t('~~Case saved'),
content: t(
'~~Case saved successfully and call status updated if needed',
),
actions: {
ok: {
text: t('actions.ok'),
type: 'solid',
},
},
})
: confirm({
title: t('~~Case not saved'),
content: t(
'~~Case not saved successfully, please try saving again ',
),
actions: {
ok: {
text: t('actions.ok'),
type: 'solid',
},
},
}));
})
.catch(() => {
$toasted.dismiss(toastId);
});
}
},
);
watch(
() => props.dataPrefill,
(newValue) => {
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ axios.interceptors.response.use(
error instanceof AxiosError &&
[400, 408, 409, 422, 502].includes(error.response?.status as number)
) {
return getAndToastWarningMessage(error);
getAndToastWarningMessage(error);
}
throw error;
},
);
const buildApp = (app: VueApp) =>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Work.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ export default defineComponent({
title: t('info.processing_download'),
content: t('info.processing_download_d'),
});
} else if (typeof response === 'string' && response.includes('400')) {
} else if (response.status === 400) {
const result = await confirm({
title: t('~~Large Data Download'),
content: t(
Expand Down

0 comments on commit 0097dd2

Please sign in to comment.