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

122 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-07-19 11:02:03 +02:00
import { defineStore } from "pinia";
2025-07-21 12:58:32 +02:00
import type {
CreateRepairViewModel,
RepairViewModel,
UpdateRepairViewModel,
} from "@/viewmodels/admin/unit/repair.models";
2025-07-19 11:02:03 +02:00
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
export const useRepairStore = defineStore("repair", {
state: () => {
return {
repairs: [] as Array<RepairViewModel & { tab_pos: number }>,
totalCount: 0 as number,
loading: "loading" as "loading" | "fetched" | "failed",
activeRepair: null as string | null,
activeRepairObj: null as RepairViewModel | null,
loadingActive: "loading" as "loading" | "fetched" | "failed",
};
},
actions: {
formatQueryReturnToPagination(result: AxiosResponse<any, any>, offset: number) {
this.totalCount = result.data.total;
result.data.repairs
.filter((elem: RepairViewModel) => this.repairs.findIndex((m) => m.id == elem.id) == -1)
.map((elem: RepairViewModel, index: number): RepairViewModel & { tab_pos: number } => {
return {
...elem,
tab_pos: index + offset,
};
})
.forEach((elem: RepairViewModel & { tab_pos: number }) => {
this.repairs.push(elem);
});
},
fetchOpenRepairs(offset = 0, count = 25, search = "", clear = false) {
if (clear) this.repairs = [];
this.loading = "loading";
http
.get(`/admin/repair?done=false&offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`)
.then((result) => {
this.formatQueryReturnToPagination(result, offset);
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
fetchDoneRepairs(offset = 0, count = 25, search = "", clear = false) {
if (clear) this.repairs = [];
this.loading = "loading";
http
.get(`/admin/repair?done=true&offset=${offset}&count=${count}${search != "" ? "&search=" + search : ""}`)
.then((result) => {
this.formatQueryReturnToPagination(result, offset);
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
async getAllRepairs(): Promise<AxiosResponse<any, any>> {
return await http.get(`/admin/repair?noLimit=true`).then((res) => {
return { ...res, data: res.data.repairs };
});
},
async getRepairsByIds(ids: Array<string>): Promise<AxiosResponse<any, any>> {
return await http
.post(`/admin/repair/ids`, {
ids,
})
.then((res) => {
return { ...res, data: res.data.repairs };
});
},
async searchRepairs(search: string): Promise<AxiosResponse<any, any>> {
return await http.get(`/admin/repair?search=${search}&noLimit=true`).then((res) => {
return { ...res, data: res.data.repairs };
});
},
fetchRepairByActiveId() {
this.loadingActive = "loading";
http
.get(`/admin/repair/${this.activeRepair}`)
.then((res) => {
this.activeRepairObj = res.data;
this.loadingActive = "fetched";
})
.catch((err) => {
this.loadingActive = "failed";
});
},
fetchRepairById(id: string) {
return http.get(`/admin/repair/${id}`);
},
loadRepairImage(url: string) {
return http.get(`/admin/repair/${this.activeRepairObj?.id}/${url}`, {
responseType: "blob",
});
},
2025-07-21 12:58:32 +02:00
async createRepair(repair: CreateRepairViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.post(`/admin/repair`, {
affected: repair.affected,
affectedId: repair.affectedId,
title: repair.title,
description: repair.description,
responsible: repair.responsible,
reports: repair.reports,
});
return result;
},
2025-07-19 11:02:03 +02:00
async updateRepair(repair: UpdateRepairViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/repair/${repair.id}`, {
status: repair.status,
noteByWorker: repair.noteByWorker,
done: repair.done,
});
return result;
},
},
});