ff-admin/src/stores/admin/unit/inspection/inspection.ts

147 lines
5.6 KiB
TypeScript
Raw Normal View History

2025-05-16 10:27:08 +02:00
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type {
CreateInspectionViewModel,
2025-07-11 14:02:39 +02:00
CreateOrUpdateInspectionPointResultCommand,
2025-07-10 13:22:38 +02:00
InspectionNextViewModel,
InspectionViewModel,
2025-07-10 13:22:38 +02:00
MinifiedInspectionViewModel,
UpdateInspectionViewModel,
} from "@/viewmodels/admin/unit/inspection/inspection.models";
2025-05-16 10:27:08 +02:00
export const useInspectionStore = defineStore("inspection", {
state: () => {
return {
2025-07-10 13:22:38 +02:00
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",
2025-05-16 10:27:08 +02:00
activeInspection: null as string | null,
activeInspectionObj: null as InspectionViewModel | null,
loadingActive: "loading" as "loading" | "fetched" | "failed",
};
},
actions: {
2025-07-10 13:22:38 +02:00
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";
});
},
2025-05-16 10:27:08 +02:00
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}`);
},
2025-07-11 14:02:39 +02:00
fetchInspectionPrintoutById() {
return http.get(`/admin/inspection/${this.activeInspectionObj?.id}/printout`, {
responseType: "blob",
});
},
async createInspection(inspection: CreateInspectionViewModel): Promise<AxiosResponse<any, any>> {
2025-05-16 10:27:08 +02:00
const result = await http.post(`/admin/inspection`, {
assigned: inspection.assigned,
relatedId: inspection.relatedId,
inspectionPlanId: inspection.inspectionPlanId,
nextInspection: inspection.nextInspection,
context: inspection.context,
2025-05-16 10:27:08 +02:00
});
return result;
},
async updateActiveInspection(inspection: UpdateInspectionViewModel): Promise<AxiosResponse<any, any>> {
2025-07-11 14:02:39 +02:00
const result = await http.patch(`/admin/inspection/${this.activeInspection}`, {
nextInspection: inspection.nextInspection,
context: inspection.context,
2025-05-16 10:27:08 +02:00
});
this.fetchInspectionByActiveId();
return result;
},
2025-07-11 14:02:39 +02:00
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",
},
});
2025-05-16 10:27:08 +02:00
this.fetchInspectionByActiveId();
return result;
},
2025-07-11 14:02:39 +02:00
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;
},
2025-05-16 10:27:08 +02:00
},
});