151 lines
5.8 KiB
TypeScript
151 lines
5.8 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { http } from "@/serverCom";
|
|
import type { AxiosResponse } from "axios";
|
|
import type {
|
|
CreateInspectionViewModel,
|
|
CreateOrUpdateInspectionPointResultCommand,
|
|
InspectionNextViewModel,
|
|
InspectionViewModel,
|
|
MinifiedInspectionViewModel,
|
|
UpdateInspectionViewModel,
|
|
} from "@/viewmodels/admin/unit/inspection/inspection.models";
|
|
|
|
export const useInspectionStore = defineStore("inspection", {
|
|
state: () => {
|
|
return {
|
|
inspections: [] as Array<MinifiedInspectionViewModel & { tab_pos: number }>,
|
|
totalCount: 0 as number,
|
|
loading: "loading" as "loading" | "fetched" | "failed",
|
|
nextInspections: [] as Array<InspectionNextViewModel & { tab_pos: number }>,
|
|
nextTotalCount: 0 as number,
|
|
nextLoading: "loading" as "loading" | "fetched" | "failed",
|
|
activeInspection: null as string | null,
|
|
activeInspectionObj: null as InspectionViewModel | null,
|
|
loadingActive: "loading" as "loading" | "fetched" | "failed",
|
|
};
|
|
},
|
|
actions: {
|
|
fetchRunningInspections(offset = 0, count = 25, search = "", clear = false) {
|
|
if (clear) this.inspections = [];
|
|
this.loading = "loading";
|
|
http
|
|
.get(`/admin/inspection/running?offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`)
|
|
.then((result) => {
|
|
this.totalCount = result.data.total;
|
|
result.data.inspections
|
|
.filter((elem: MinifiedInspectionViewModel) => this.inspections.findIndex((m) => m.id == elem.id) == -1)
|
|
.map(
|
|
(elem: MinifiedInspectionViewModel, index: number): MinifiedInspectionViewModel & { tab_pos: number } => {
|
|
return {
|
|
...elem,
|
|
tab_pos: index + offset,
|
|
};
|
|
}
|
|
)
|
|
.forEach((elem: MinifiedInspectionViewModel & { tab_pos: number }) => {
|
|
this.inspections.push(elem);
|
|
});
|
|
this.loading = "fetched";
|
|
})
|
|
.catch((err) => {
|
|
this.loading = "failed";
|
|
});
|
|
},
|
|
fetchNextInspections(offset = 0, count = 25, search = "", clear = false) {
|
|
if (clear) this.nextInspections = [];
|
|
this.nextLoading = "loading";
|
|
http
|
|
.get(`/admin/inspection/next?offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`)
|
|
.then((result) => {
|
|
this.nextTotalCount = result.data.total;
|
|
result.data.inspections
|
|
.filter((elem: InspectionNextViewModel) => this.nextInspections.findIndex((m) => m.id == elem.id) == -1)
|
|
.map((elem: InspectionNextViewModel, index: number): InspectionNextViewModel & { tab_pos: number } => {
|
|
return {
|
|
...elem,
|
|
tab_pos: index + offset,
|
|
};
|
|
})
|
|
.forEach((elem: InspectionNextViewModel & { tab_pos: number }) => {
|
|
this.nextInspections.push(elem);
|
|
});
|
|
this.nextLoading = "fetched";
|
|
})
|
|
.catch((err) => {
|
|
this.nextLoading = "failed";
|
|
});
|
|
},
|
|
fetchInspectionByActiveId() {
|
|
this.loadingActive = "loading";
|
|
http
|
|
.get(`/admin/inspection/${this.activeInspection}`)
|
|
.then((res) => {
|
|
this.activeInspectionObj = res.data;
|
|
this.loadingActive = "fetched";
|
|
})
|
|
.catch((err) => {
|
|
this.loadingActive = "failed";
|
|
});
|
|
},
|
|
fetchInspectionById(id: string) {
|
|
return http.get(`/admin/inspection/${id}`);
|
|
},
|
|
fetchInspectionPrintoutById() {
|
|
return http.get(`/admin/inspection/${this.activeInspectionObj?.id}/printout`, {
|
|
responseType: "blob",
|
|
});
|
|
},
|
|
fetchUploadedFileByPointId(id: string) {
|
|
return http.get(`/admin/inspection/${this.activeInspectionObj?.id}/${id}/upload`, {
|
|
responseType: "blob",
|
|
});
|
|
},
|
|
async createInspection(inspection: CreateInspectionViewModel): Promise<AxiosResponse<any, any>> {
|
|
const result = await http.post(`/admin/inspection`, {
|
|
assigned: inspection.assigned,
|
|
relatedId: inspection.relatedId,
|
|
inspectionPlanId: inspection.inspectionPlanId,
|
|
nextInspection: inspection.nextInspection,
|
|
context: inspection.context,
|
|
});
|
|
return result;
|
|
},
|
|
async updateActiveInspection(inspection: UpdateInspectionViewModel): Promise<AxiosResponse<any, any>> {
|
|
const result = await http.patch(`/admin/inspection/${this.activeInspection}`, {
|
|
nextInspection: inspection.nextInspection,
|
|
context: inspection.context,
|
|
});
|
|
this.fetchInspectionByActiveId();
|
|
return result;
|
|
},
|
|
async updateActiveInspectionResults(
|
|
results: Array<CreateOrUpdateInspectionPointResultCommand>,
|
|
files: { [key: string]: File | null }
|
|
): Promise<AxiosResponse<any, any>> {
|
|
const formData = new FormData();
|
|
formData.append("results", JSON.stringify(results));
|
|
Object.entries(files).forEach(([key, file]) => {
|
|
if (file) {
|
|
const extension = file.name.split(".").pop() || "";
|
|
formData.append(`files`, new File([file], `${key}.${extension}`, { type: file.type }));
|
|
}
|
|
});
|
|
const result = await http.patch(`/admin/inspection/${this.activeInspection}/results`, formData, {
|
|
headers: {
|
|
"Content-Type": "multipart/form-data",
|
|
},
|
|
});
|
|
this.fetchInspectionByActiveId();
|
|
return result;
|
|
},
|
|
async finishActiveInspection(): Promise<AxiosResponse<any, any>> {
|
|
const result = await http.patch(`/admin/inspection/${this.activeInspection}/finish`);
|
|
this.fetchInspectionByActiveId();
|
|
return result;
|
|
},
|
|
async deleteInspection(inspection: string): Promise<AxiosResponse<any, any>> {
|
|
const result = await http.delete(`/admin/inspection/${inspection}`);
|
|
return result;
|
|
},
|
|
},
|
|
});
|