import { defineStore } from "pinia"; import type { CreateRepairViewModel, RepairViewModel, UpdateRepairViewModel, } from "@/viewmodels/admin/unit/repair.models"; import { http } from "@/serverCom"; import type { AxiosResponse } from "axios"; export const useRepairStore = defineStore("repair", { state: () => { return { repairs: [] as Array, 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, 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> { return await http.get(`/admin/repair?noLimit=true`).then((res) => { return { ...res, data: res.data.repairs }; }); }, async getRepairsByIds(ids: Array): Promise> { return await http .post(`/admin/repair/ids`, { ids, }) .then((res) => { return { ...res, data: res.data.repairs }; }); }, async searchRepairs(search: string): Promise> { 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", }); }, async createRepair(repair: CreateRepairViewModel): Promise> { 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; }, async updateRepair(repair: UpdateRepairViewModel): Promise> { const result = await http.patch(`/admin/repair/${repair.id}`, { status: repair.status, noteByWorker: repair.noteByWorker, done: repair.done, }); return result; }, }, });