2021-11-14 23:33:28 +00:00
|
|
|
import { api, isAxiosError } from '@flaschengeist/api';
|
2021-05-25 15:11:44 +00:00
|
|
|
import { AxiosError } from 'axios';
|
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
|
|
|
interface UserService {
|
|
|
|
user: FG.Service;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixJob(job: FG.Job) {
|
|
|
|
job.start = new Date(job.start);
|
|
|
|
if (job.end) job.end = new Date(job.end);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixEvent(event: FG.Event) {
|
|
|
|
event.start = new Date(event.start);
|
|
|
|
if (event.end) event.end = new Date(event.end);
|
|
|
|
|
|
|
|
event.jobs.forEach((job) => fixJob(job));
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useScheduleStore = defineStore({
|
2021-11-14 23:33:28 +00:00
|
|
|
id: 'events',
|
2021-05-25 15:11:44 +00:00
|
|
|
|
|
|
|
state: () => ({
|
|
|
|
jobTypes: [] as FG.JobType[],
|
|
|
|
eventTypes: [] as FG.EventType[],
|
|
|
|
templates: [] as FG.Event[],
|
|
|
|
}),
|
|
|
|
|
|
|
|
getters: {},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
async getJobTypes(force = false) {
|
|
|
|
if (force || this.jobTypes.length == 0)
|
|
|
|
try {
|
|
|
|
const { data } = await api.get<FG.JobType[]>('/events/job-types');
|
|
|
|
this.jobTypes = data;
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return this.jobTypes;
|
|
|
|
},
|
|
|
|
|
2021-11-14 23:33:28 +00:00
|
|
|
addJobType(name: string) {
|
|
|
|
return api
|
|
|
|
.post<FG.JobType>('/events/job-types', { name: name })
|
|
|
|
.then(({ data }) => this.jobTypes.push(data));
|
2021-05-25 15:11:44 +00:00
|
|
|
},
|
|
|
|
|
2021-11-14 23:33:28 +00:00
|
|
|
removeJobType(id: number) {
|
|
|
|
return api
|
|
|
|
.delete(`/events/job-types/${id}`)
|
|
|
|
.then(() => this.jobTypes = this.jobTypes.filter(v => v.id !== id));
|
2021-05-25 15:11:44 +00:00
|
|
|
},
|
|
|
|
|
2021-11-14 23:33:28 +00:00
|
|
|
renameJobType(id: number, newName: string) {
|
|
|
|
return api
|
|
|
|
.put(`/events/job-types/${id}`, { name: newName })
|
|
|
|
.then(() => {
|
|
|
|
const idx = this.jobTypes.findIndex(v=>v.id===id);
|
|
|
|
if (idx >= 0) this.jobTypes[idx].name = newName;
|
|
|
|
})
|
2021-05-25 15:11:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async getEventTypes(force = false) {
|
|
|
|
if (force || this.eventTypes.length == 0)
|
|
|
|
try {
|
|
|
|
const { data } = await api.get<FG.EventType[]>('/events/event-types');
|
|
|
|
this.eventTypes = data;
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return this.eventTypes;
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Add new EventType
|
|
|
|
*
|
|
|
|
* @param name Name of new EventType
|
|
|
|
*/
|
2021-11-14 23:33:28 +00:00
|
|
|
addEventType(name: string) {
|
|
|
|
return api
|
|
|
|
.post<FG.EventType>('/events/event-types', { name: name })
|
|
|
|
.then(({data}) => this.eventTypes.push(data))
|
2021-05-25 15:11:44 +00:00
|
|
|
},
|
2021-11-14 23:33:28 +00:00
|
|
|
|
2021-05-25 15:11:44 +00:00
|
|
|
async removeEvent(id: number) {
|
|
|
|
try {
|
|
|
|
await api.delete(`/events/${id}`);
|
|
|
|
const idx = this.templates.findIndex((v) => v.id === id);
|
|
|
|
if (idx !== -1) this.templates.splice(idx, 1);
|
|
|
|
} catch (e) {
|
|
|
|
const error = <AxiosError>e;
|
|
|
|
if (error.response && error.response.status === 404) return false;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2021-11-14 23:33:28 +00:00
|
|
|
removeEventType(id: number) {
|
|
|
|
return api
|
|
|
|
.delete(`/events/event-types/${id}`)
|
|
|
|
.then(() => this.eventTypes = this.eventTypes.filter(v => v.id !== id));
|
2021-05-25 15:11:44 +00:00
|
|
|
},
|
|
|
|
|
2021-11-14 23:33:28 +00:00
|
|
|
renameEventType(id: number, newName: string) {
|
|
|
|
return api
|
|
|
|
.put(`/events/event-types/${id}`, { name: newName })
|
|
|
|
.then(() => {
|
|
|
|
const idx = this.eventTypes.findIndex(v=>v.id===id);
|
|
|
|
if (idx >= 0) this.eventTypes[idx].name = newName;
|
|
|
|
})
|
2021-05-25 15:11:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async getTemplates(force = false) {
|
|
|
|
if (force || this.templates.length == 0) {
|
|
|
|
const { data } = await api.get<FG.Event[]>('/events/templates');
|
|
|
|
data.forEach((element) => fixEvent(element));
|
|
|
|
this.templates = data;
|
|
|
|
}
|
|
|
|
return this.templates;
|
|
|
|
},
|
|
|
|
|
|
|
|
async getEvents(filter: { from?: Date; to?: Date } | undefined = undefined) {
|
|
|
|
try {
|
|
|
|
const { data } = await api.get<FG.Event[]>('/events', { params: filter });
|
|
|
|
data.forEach((element) => fixEvent(element));
|
|
|
|
return data;
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async getEvent(id: number) {
|
|
|
|
try {
|
|
|
|
const { data } = await api.get<FG.Event>(`/events/${id}`);
|
|
|
|
fixEvent(data);
|
|
|
|
return data;
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async updateJob(eventId: number, jobId: number, service: FG.Service | UserService) {
|
|
|
|
try {
|
|
|
|
const { data } = await api.put<FG.Job>(`/events/${eventId}/jobs/${jobId}`, service);
|
|
|
|
fixJob(data);
|
|
|
|
return data;
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async addEvent(event: FG.Event) {
|
|
|
|
const { data } = await api.post<FG.Event>('/events', event);
|
|
|
|
if (data.is_template) this.templates.push(data);
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|