felicity-lims/webapp/composables/samples.ts

532 lines
16 KiB
TypeScript
Raw Normal View History

2021-11-09 14:48:44 +08:00
import Swal from 'sweetalert2';
2021-11-12 05:30:10 +08:00
import { toRefs, computed, reactive } from 'vue';
2021-11-09 14:48:44 +08:00
import {
REINSTATE_SAMPLES,
RECEIVE_SAMPLES,
CANCEL_SAMPLES,
CLONE_SAMPLES,
2023-02-24 08:44:14 +08:00
PRINT_SAMPLES,
2021-11-29 06:42:34 +08:00
PUBLISH_SAMPLES,
INVALIDATE_SAMPLES,
VERIFY_SAMPLES,
REJECT_SAMPLES,
2021-11-12 05:30:10 +08:00
} from '../graphql/analyses.mutations';
2023-02-26 22:14:43 +08:00
import {
DOWNLOAD_IMPRESS, DOWNLOAD_IMPRESS_SAMPLES
} from '../graphql/analyses.queries';
2023-01-15 14:04:29 +08:00
import { RECOVER_SAMPLES, STORE_SAMPLES } from '../graphql/storage.mutations'
import { useSampleStore } from '../stores';
2021-11-29 06:42:34 +08:00
import { ISample } from '../models/analysis';
2022-04-04 02:54:31 +08:00
import useApiUtil from "./api_util";
2023-02-26 22:14:43 +08:00
import useNotifyToast from './alert_toast';
2021-11-09 14:48:44 +08:00
2021-11-12 05:30:10 +08:00
export default function useSampleComposable(){
2021-11-09 14:48:44 +08:00
2022-04-04 02:54:31 +08:00
const sampleStore = useSampleStore();
2023-02-26 22:14:43 +08:00
const { withClientMutation, withClientQuery } = useApiUtil();
const { toastInfo } = useNotifyToast()
2021-11-29 18:51:20 +08:00
2021-11-09 14:48:44 +08:00
const state = reactive({
2022-04-04 02:54:31 +08:00
samples: computed(() => sampleStore.getSamples ),
2021-11-09 14:48:44 +08:00
})
2022-04-04 02:54:31 +08:00
const _updateSamplesStatus = async (samples: ISample[]) => sampleStore.updateSamplesStatus(samples);
const _updateSampleStatus = async (sample: ISample) => sampleStore.updateSampleStatus(sample);
2023-01-15 14:04:29 +08:00
const _updateSamples = async (samples: ISample[]) => sampleStore.updateSamples(samples)
const _addSampleClones = async (samples: ISample[]) => sampleStore.addSampleClones(samples)
2021-11-29 06:42:34 +08:00
const _fetchAnalysesResultsFor = async (uid: number) => {
if(!uid) return;
2022-04-04 02:54:31 +08:00
sampleStore.fetchAnalysisResultsForSample(uid);
2021-11-29 06:42:34 +08:00
}
2021-11-09 14:48:44 +08:00
// CANCEL_SAMPLES
2023-03-19 23:21:32 +08:00
const cancelSamples = async (uids: string[]) => {
2021-11-09 14:48:44 +08:00
try {
2023-01-15 14:04:29 +08:00
await Swal.fire({
2021-11-09 14:48:44 +08:00
title: 'Are you sure?',
text: "You want to cancel these samples",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, cancel now!',
cancelButtonText: 'No, do not cancel!',
2023-01-15 14:04:29 +08:00
}).then(async (result) => {
2021-11-09 14:48:44 +08:00
if (result.isConfirmed) {
2021-11-12 05:30:10 +08:00
2022-04-04 02:54:31 +08:00
withClientMutation(CANCEL_SAMPLES, { samples: uids }, "cancelSamples")
.then(resp => {
2023-01-15 14:04:29 +08:00
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
_updateSampleStatus(resp.samples[0])
if(resp.samples.length !== 1) return;
_fetchAnalysesResultsFor(resp.samples[0].uid)
2021-11-29 06:42:34 +08:00
});
2021-11-09 14:48:44 +08:00
2023-01-15 14:04:29 +08:00
await Swal.fire(
2021-11-09 14:48:44 +08:00
'Its Happening!',
'Your samples have been cancelled.',
'success'
2021-11-29 06:42:34 +08:00
).then(_ => {})
2021-11-09 14:48:44 +08:00
}
})
} catch (error) {
2021-11-09 14:48:44 +08:00
}
}
// CLONE_SAMPLES
2023-03-19 23:21:32 +08:00
const cloneSamples = async (uids: string[]) => {
try {
await Swal.fire({
title: 'Are you sure?',
text: "You want to clone these samples",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, cancel now!',
cancelButtonText: 'No, do not clone!',
}).then(async (result) => {
if (result.isConfirmed) {
withClientMutation(CLONE_SAMPLES, { samples: uids }, "cloneSamples")
.then(resp => {
if(resp.samples.length <= 0) return;
_addSampleClones(resp.samples)
});
await Swal.fire(
'Its Happening!',
'Your samples have been cloned.',
'success'
).then(_ => {})
}
})
} catch (error) {
}
}
2021-11-09 14:48:44 +08:00
// REINSTATE_SAMPLES
2023-03-19 23:21:32 +08:00
const reInstateSamples = async (uids: string[]) => {
2021-11-09 14:48:44 +08:00
try {
2023-01-15 14:04:29 +08:00
await Swal.fire({
2021-11-09 14:48:44 +08:00
title: 'Are you sure?',
text: "You want to reinstate samples",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, reinstate now!',
cancelButtonText: 'No, do not reinstate!',
2023-01-15 14:04:29 +08:00
}).then(async (result) => {
2021-11-09 14:48:44 +08:00
if (result.isConfirmed) {
2021-11-12 05:30:10 +08:00
2022-04-04 02:54:31 +08:00
withClientMutation(REINSTATE_SAMPLES, { samples: uids }, "reInstateSamples")
.then(resp => {
2023-01-15 14:04:29 +08:00
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
_updateSampleStatus(resp.samples[0])
if(resp.samples.length !== 1) return;
_fetchAnalysesResultsFor(resp.samples[0].uid)
2022-04-04 02:54:31 +08:00
})
2021-11-09 14:48:44 +08:00
2023-01-15 14:04:29 +08:00
await Swal.fire(
2021-11-09 14:48:44 +08:00
'Its Happening!',
'Your samples have been reinstated.',
'success'
2021-11-29 06:42:34 +08:00
).then(_ => {})
2021-11-09 14:48:44 +08:00
}
})
} catch (error) {
2021-11-09 14:48:44 +08:00
}
}
// RECEIVE_SAMPLES
2023-03-19 23:21:32 +08:00
const receiveSamples = async (uids: string[]) => {
2021-11-09 14:48:44 +08:00
try {
2023-01-15 14:04:29 +08:00
await Swal.fire({
2021-11-09 14:48:44 +08:00
title: 'Are you sure?',
text: "You want to receive samples",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, receice now!',
cancelButtonText: 'No, do not receive!',
2023-01-15 14:04:29 +08:00
}).then(async (result) => {
2021-11-09 14:48:44 +08:00
if (result.isConfirmed) {
2021-11-12 05:30:10 +08:00
2022-04-04 02:54:31 +08:00
withClientMutation(RECEIVE_SAMPLES, { samples: uids }, "receiveSamples")
.then(resp => {
2023-01-15 14:04:29 +08:00
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
_updateSampleStatus(resp.samples[0])
if(resp.samples.length !== 1) return;
_fetchAnalysesResultsFor(resp.samples[0].uid)
2021-11-29 06:42:34 +08:00
});
2021-11-09 14:48:44 +08:00
2023-01-15 14:04:29 +08:00
await Swal.fire(
2021-11-09 14:48:44 +08:00
'Its Happening!',
2021-11-12 05:30:10 +08:00
'Your samples have been received.',
2021-11-09 14:48:44 +08:00
'success'
2021-11-29 06:42:34 +08:00
).then(_ => {})
}
})
} catch (error) {
2021-11-29 06:42:34 +08:00
}
}
2023-01-15 14:04:29 +08:00
// RECOVER
2023-03-19 23:21:32 +08:00
const recoverSamples = async (sampleUids: string[]) => {
2023-01-15 14:04:29 +08:00
try {
await Swal.fire({
title: 'Are you sure?',
text: "You want to recover these samples from storage",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, recover now!',
cancelButtonText: 'No, do not recover!',
}).then(async (result) => {
if (result.isConfirmed) {
withClientMutation(RECOVER_SAMPLES, { sampleUids }, "recoverSamples")
.then(resp => {
console.log(resp)
2023-01-15 14:04:29 +08:00
if(resp.length <= 0) return;
_updateSamples(resp.samples)
if(resp.samples.length !== 1) return;
_fetchAnalysesResultsFor(resp.samples[0].uid)
});
await Swal.fire(
'Its Happening!',
'Your samples have been recovered.',
'success'
).then(_ => {})
}
})
} catch (error) {
}
}
2021-11-29 06:42:34 +08:00
// PUBLISH_SAMPLES
2023-02-26 22:14:43 +08:00
const publishSamples = async (samples: any[]) => {
2021-11-29 06:42:34 +08:00
try {
2023-01-15 14:04:29 +08:00
await Swal.fire({
2021-11-29 06:42:34 +08:00
title: 'Are you sure?',
2023-02-24 08:44:14 +08:00
text: "You want to publish samples",
2021-11-29 06:42:34 +08:00
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
2023-02-26 22:14:43 +08:00
confirmButtonText: 'Yes, publish now!',
cancelButtonText: 'No, do not publish!',
2023-01-15 14:04:29 +08:00
}).then(async (result) => {
2021-11-29 06:42:34 +08:00
if (result.isConfirmed) {
2023-02-26 22:14:43 +08:00
withClientMutation(PUBLISH_SAMPLES, { samples }, "publishSamples")
2022-04-04 02:54:31 +08:00
.then(resp => {
2023-02-26 22:14:43 +08:00
toastInfo(resp.message)
2021-11-29 06:42:34 +08:00
});
2023-01-15 14:04:29 +08:00
await Swal.fire(
2021-11-29 06:42:34 +08:00
'Its Happening!',
2023-02-24 08:44:14 +08:00
'Your sample were submitted for impress',
'success'
).then(_ => {})
}
})
} catch (error) {
}
}
2023-02-26 22:14:43 +08:00
// DOWNLOAD_IMPRESS by SAMPLES
2023-03-19 23:21:32 +08:00
const downloadSamplesImpress = async (uids: string[]) => {
2023-02-26 22:14:43 +08:00
try {
await Swal.fire({
title: 'Are you sure?',
text: "You want to download pdfs",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, download now!',
cancelButtonText: 'No, do not download!',
}).then(async (result) => {
if (result.isConfirmed) {
withClientQuery(DOWNLOAD_IMPRESS_SAMPLES, { uids }, "impressReportsDownload")
.then(resp => {
const tempLink = document.createElement('a');
tempLink.href = `data:application/pdf;base64,${resp}`;
tempLink.setAttribute('download', 'impress-report.pdf');
tempLink.click();
});
await Swal.fire(
'Its Happening!',
'Downloading .....',
'success'
).then(_ => {})
}
})
} catch (error) {
}
}
// DOWNLOAD_IMPRESS
const downloadImpress = async (uid) => {
try {
await Swal.fire({
title: 'Are you sure?',
text: "You want to download this report",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, download now!',
cancelButtonText: 'No, do not download!',
}).then(async (result) => {
if (result.isConfirmed) {
withClientQuery(DOWNLOAD_IMPRESS, { uid }, "impressReportDownload")
.then(resp => {
const tempLink = document.createElement('a');
tempLink.href = `data:application/pdf;base64,${resp}`;
tempLink.setAttribute('download', 'impress-report.pdf');
tempLink.click();
});
await Swal.fire(
'Its Happening!',
'Downloading .....',
'success'
).then(_ => {})
}
})
} catch (error) {
}
}
2023-02-24 08:44:14 +08:00
// PRINT_SAMPLES
2023-03-19 23:21:32 +08:00
const printSamples = async (uids: string[]) => {
2023-02-24 08:44:14 +08:00
try {
await Swal.fire({
title: 'Are you sure?',
text: "You want to flag as printed",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, flag now!',
cancelButtonText: 'No, do not flag!',
}).then(async (result) => {
if (result.isConfirmed) {
withClientMutation(PRINT_SAMPLES, { samples: uids }, "printSamples")
.then(resp => {
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
_updateSampleStatus(resp.samples[0])
if(resp.samples.length !== 1) return;
_fetchAnalysesResultsFor(resp.samples[0].uid)
});
await Swal.fire(
'Its Happening!',
'Your sample have been marked as printed.',
2021-11-29 06:42:34 +08:00
'success'
).then(_ => {})
}
})
} catch (error) {
2021-11-29 06:42:34 +08:00
}
}
// verify sample(s) incase it did not auto transition
2023-03-19 23:21:32 +08:00
const verifySamples = async (uids: string[]) => {
2021-11-29 06:42:34 +08:00
try {
2023-01-15 14:04:29 +08:00
await Swal.fire({
2021-11-29 06:42:34 +08:00
title: 'Are you sure?',
text: "You want to verify sample",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, verify now!',
cancelButtonText: 'No, do not verify!',
2023-01-15 14:04:29 +08:00
}).then(async (result) => {
2021-11-29 06:42:34 +08:00
if (result.isConfirmed) {
2022-04-04 02:54:31 +08:00
withClientMutation(VERIFY_SAMPLES, { samples: uids }, "verifySamples")
.then(resp => {
2023-01-15 14:04:29 +08:00
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
_updateSampleStatus(resp.samples[0])
2021-11-29 06:42:34 +08:00
});
2023-01-15 14:04:29 +08:00
await Swal.fire(
2021-11-29 06:42:34 +08:00
'Its Happening!',
'Your sample have been verified.',
'success'
).then(_ => {})
}
})
} catch (error) {
2021-11-29 06:42:34 +08:00
}
}
// reject sample(s)
const rejectSamples = async (samples: any[]) => {
2023-02-18 20:59:45 +08:00
let rejected = false;
2021-11-29 06:42:34 +08:00
try {
2023-01-15 14:04:29 +08:00
await Swal.fire({
2021-11-29 06:42:34 +08:00
title: 'Are you sure?',
text: "You want to invalidate sample",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, Reject now!',
cancelButtonText: 'No, do not reject!',
2023-01-15 14:04:29 +08:00
}).then(async (result) => {
2021-11-29 06:42:34 +08:00
if (result.isConfirmed) {
2022-04-04 02:54:31 +08:00
withClientMutation(REJECT_SAMPLES, { samples }, "rejectSamples")
.then(resp => {
2023-02-18 20:59:45 +08:00
rejected = true;
2023-01-15 14:04:29 +08:00
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
2021-11-29 06:42:34 +08:00
});
2023-01-15 14:04:29 +08:00
await Swal.fire(
2021-11-29 06:42:34 +08:00
'Its Happening!',
'Your sample(s) have been rejected.',
'success'
2022-02-06 03:36:45 +08:00
).then(_ => {}) // router.push({ name: "samples-listing" })
2021-11-29 06:42:34 +08:00
}
})
} catch (error) {
2021-11-29 06:42:34 +08:00
}
2023-02-18 20:59:45 +08:00
return rejected;
2021-11-29 06:42:34 +08:00
}
// invalidate sample
2023-03-19 23:21:32 +08:00
const invalidateSamples = async (uids: string[]): Promise<ISample[]> => {
2021-12-27 02:41:54 +08:00
let invalidated: ISample[] = [];
2021-11-29 06:42:34 +08:00
try {
2021-12-27 02:41:54 +08:00
await Swal.fire({
2021-11-29 06:42:34 +08:00
title: 'Are you sure?',
text: "You want to invalidate sample",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, invalidate now!',
cancelButtonText: 'No, do not invalidate!',
2021-12-27 02:41:54 +08:00
}).then(async (result) => {
2021-11-29 06:42:34 +08:00
if (result.isConfirmed) {
2022-04-04 02:54:31 +08:00
withClientMutation(INVALIDATE_SAMPLES, { samples: uids }, "invalidateSamples")
.then(resp => {
2023-01-15 14:04:29 +08:00
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
_updateSampleStatus(resp.samples[0])
2021-11-29 06:42:34 +08:00
});
2021-12-27 02:41:54 +08:00
await Swal.fire(
2021-11-29 06:42:34 +08:00
'Its Happening!',
'Your sample(s) have been invalidated.',
'success'
).then(_ => {})
2021-11-09 14:48:44 +08:00
}
})
} catch (error) {
2021-11-09 14:48:44 +08:00
}
2021-12-27 02:41:54 +08:00
return invalidated;
2021-11-09 14:48:44 +08:00
}
2023-01-04 04:12:12 +08:00
// store samples
const storeSamples = async (storageParams): Promise<ISample[]> => {
let stored: ISample[] = [];
try {
await Swal.fire({
title: 'Are you sure?',
text: "You want to store these samples",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, store now!',
cancelButtonText: 'No, do not store!',
}).then(async (result) => {
if (result.isConfirmed) {
withClientMutation(STORE_SAMPLES, { payload: storageParams }, "storeSamples")
.then(resp => {
2023-01-15 14:04:29 +08:00
if(resp.samples.length <= 0) return;
_updateSamplesStatus(resp.samples)
2023-01-04 04:12:12 +08:00
});
await Swal.fire(
'Its Happening!',
'Your sample(s) have been added to storage.',
'success'
).then(_ => {})
}
})
} catch (error) {
}
return stored;
}
2021-11-09 14:48:44 +08:00
return {
...toRefs(state),
2021-11-12 05:30:10 +08:00
cancelSamples,
reInstateSamples,
receiveSamples,
2023-01-15 14:04:29 +08:00
recoverSamples,
2021-11-29 06:42:34 +08:00
verifySamples,
2023-02-24 08:44:14 +08:00
printSamples,
2021-11-29 06:42:34 +08:00
publishSamples,
2023-02-26 22:14:43 +08:00
downloadSamplesImpress,
downloadImpress,
2021-11-29 06:42:34 +08:00
invalidateSamples,
rejectSamples,
2023-01-04 04:12:12 +08:00
storeSamples,
cloneSamples
2021-11-09 14:48:44 +08:00
}
}