Update schedules API

This commit is contained in:
Dane Everitt
2022-02-27 12:35:27 -05:00
parent bfd92c314d
commit feffdacf57
21 changed files with 172 additions and 167 deletions
+32
View File
@@ -115,3 +115,35 @@ interface Subuser extends Model {
can (permission: SubuserPermission): boolean;
}
interface Schedule extends Model {
id: number;
name: string;
cron: {
dayOfWeek: string;
month: string;
dayOfMonth: string;
hour: string;
minute: string;
};
isActive: boolean;
isProcessing: boolean;
onlyWhenOnline: boolean;
lastRunAt: Date | null;
nextRunAt: Date | null;
createdAt: Date;
updatedAt: Date;
tasks: Task[];
}
interface Task extends Model {
id: number;
sequenceId: number;
action: string;
payload: string;
timeOffset: number;
isQueued: boolean;
continueOnFailure: boolean;
createdAt: Date;
updatedAt: Date;
}
@@ -109,4 +109,41 @@ export default class Transformers {
can: permission => (attributes.permissions || []).indexOf(permission) >= 0,
};
}
static toServerTask ({ attributes }: FractalResponseData): Models.Task {
return {
id: attributes.id,
sequenceId: attributes.sequence_id,
action: attributes.action,
payload: attributes.payload,
timeOffset: attributes.time_offset,
isQueued: attributes.is_queued,
continueOnFailure: attributes.continue_on_failure,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
};
}
static toServerSchedule ({ attributes }: FractalResponseData): Models.Schedule {
return {
id: attributes.id,
name: attributes.name,
cron: {
dayOfWeek: attributes.cron.day_of_week,
month: attributes.cron.month,
dayOfMonth: attributes.cron.day_of_month,
hour: attributes.cron.hour,
minute: attributes.cron.minute,
},
isActive: attributes.is_active,
isProcessing: attributes.is_processing,
onlyWhenOnline: attributes.only_when_online,
lastRunAt: attributes.last_run_at ? new Date(attributes.last_run_at) : null,
nextRunAt: attributes.next_run_at ? new Date(attributes.next_run_at) : null,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
// @ts-expect-error
tasks: (attributes.relationships?.tasks?.data || []).map((row: any) => this.toServerTask(row.attributes)),
};
}
}
+88
View File
@@ -0,0 +1,88 @@
import http from '@/api/http';
import { Schedule, Task, Transformers } from '@definitions/user';
type CreateScheduleData = Pick<Schedule, 'cron' | 'name' | 'onlyWhenOnline' | 'isActive'> & { id?: number }
interface CreateTaskData {
action: string;
payload: string;
timeOffset: string | number;
continueOnFailure: boolean;
}
const createOrUpdateSchedule = async (uuid: string, schedule: CreateScheduleData): Promise<Schedule> => {
const { data } = await http.post(`/api/client/servers/${uuid}/schedules${schedule.id ? `/${schedule.id}` : ''}`, {
is_active: schedule.isActive,
only_when_online: schedule.onlyWhenOnline,
name: schedule.name,
minute: schedule.cron.minute,
hour: schedule.cron.hour,
day_of_month: schedule.cron.dayOfMonth,
month: schedule.cron.month,
day_of_week: schedule.cron.dayOfWeek,
});
return Transformers.toServerSchedule(data);
};
const createOrUpdateScheduleTask = async (uuid: string, schedule: number, task: number | undefined, data: CreateTaskData): Promise<Task> => {
const { data: response } = await http.post(`/api/client/servers/${uuid}/schedules/${schedule}/tasks${task ? `/${task}` : ''}`, {
action: data.action,
payload: data.payload,
continue_on_failure: data.continueOnFailure,
time_offset: data.timeOffset,
});
return Transformers.toServerTask(response);
};
const deleteSchedule = (uuid: string, schedule: number): Promise<void> => {
return new Promise((resolve, reject) => {
http.delete(`/api/client/servers/${uuid}/schedules/${schedule}`)
.then(() => resolve())
.catch(reject);
});
};
const deleteScheduleTask = (uuid: string, scheduleId: number, taskId: number): Promise<void> => {
return new Promise((resolve, reject) => {
http.delete(`/api/client/servers/${uuid}/schedules/${scheduleId}/tasks/${taskId}`)
.then(() => resolve())
.catch(reject);
});
};
const getServerSchedule = (uuid: string, schedule: number): Promise<Schedule> => {
return new Promise((resolve, reject) => {
http.get(`/api/client/servers/${uuid}/schedules/${schedule}`, {
params: {
include: [ 'tasks' ],
},
})
.then(({ data }) => resolve(Transformers.toServerSchedule(data)))
.catch(reject);
});
};
const getServerSchedules = async (uuid: string): Promise<Schedule[]> => {
const { data } = await http.get(`/api/client/servers/${uuid}/schedules`, {
params: {
include: [ 'tasks' ],
},
});
return (data.data || []).map(Transformers.toServerSchedule);
};
const triggerScheduleExecution = async (server: string, schedule: number): Promise<void> =>
await http.post(`/api/client/servers/${server}/schedules/${schedule}/execute`);
export {
deleteSchedule,
deleteScheduleTask,
createOrUpdateSchedule,
createOrUpdateScheduleTask,
getServerSchedule,
getServerSchedules,
triggerScheduleExecution,
};
@@ -1,19 +0,0 @@
import { rawDataToServerSchedule, Schedule } from '@/api/server/schedules/getServerSchedules';
import http from '@/api/http';
type Data = Pick<Schedule, 'cron' | 'name' | 'onlyWhenOnline' | 'isActive'> & { id?: number }
export default async (uuid: string, schedule: Data): Promise<Schedule> => {
const { data } = await http.post(`/api/client/servers/${uuid}/schedules${schedule.id ? `/${schedule.id}` : ''}`, {
is_active: schedule.isActive,
only_when_online: schedule.onlyWhenOnline,
name: schedule.name,
minute: schedule.cron.minute,
hour: schedule.cron.hour,
day_of_month: schedule.cron.dayOfMonth,
month: schedule.cron.month,
day_of_week: schedule.cron.dayOfWeek,
});
return rawDataToServerSchedule(data.attributes);
};
@@ -1,20 +0,0 @@
import { rawDataToServerTask, Task } from '@/api/server/schedules/getServerSchedules';
import http from '@/api/http';
interface Data {
action: string;
payload: string;
timeOffset: string | number;
continueOnFailure: boolean;
}
export default async (uuid: string, schedule: number, task: number | undefined, data: Data): Promise<Task> => {
const { data: response } = await http.post(`/api/client/servers/${uuid}/schedules/${schedule}/tasks${task ? `/${task}` : ''}`, {
action: data.action,
payload: data.payload,
continue_on_failure: data.continueOnFailure,
time_offset: data.timeOffset,
});
return rawDataToServerTask(response.attributes);
};
@@ -1,9 +0,0 @@
import http from '@/api/http';
export default (uuid: string, schedule: number): Promise<void> => {
return new Promise((resolve, reject) => {
http.delete(`/api/client/servers/${uuid}/schedules/${schedule}`)
.then(() => resolve())
.catch(reject);
});
};
@@ -1,9 +0,0 @@
import http from '@/api/http';
export default (uuid: string, scheduleId: number, taskId: number): Promise<void> => {
return new Promise((resolve, reject) => {
http.delete(`/api/client/servers/${uuid}/schedules/${scheduleId}/tasks/${taskId}`)
.then(() => resolve())
.catch(reject);
});
};
@@ -1,14 +0,0 @@
import http from '@/api/http';
import { rawDataToServerSchedule, Schedule } from '@/api/server/schedules/getServerSchedules';
export default (uuid: string, schedule: number): Promise<Schedule> => {
return new Promise((resolve, reject) => {
http.get(`/api/client/servers/${uuid}/schedules/${schedule}`, {
params: {
include: [ 'tasks' ],
},
})
.then(({ data }) => resolve(rawDataToServerSchedule(data.attributes)))
.catch(reject);
});
};
@@ -1,77 +0,0 @@
import http from '@/api/http';
export interface Schedule {
id: number;
name: string;
cron: {
dayOfWeek: string;
month: string;
dayOfMonth: string;
hour: string;
minute: string;
};
isActive: boolean;
isProcessing: boolean;
onlyWhenOnline: boolean;
lastRunAt: Date | null;
nextRunAt: Date | null;
createdAt: Date;
updatedAt: Date;
tasks: Task[];
}
export interface Task {
id: number;
sequenceId: number;
action: string;
payload: string;
timeOffset: number;
isQueued: boolean;
continueOnFailure: boolean;
createdAt: Date;
updatedAt: Date;
}
export const rawDataToServerTask = (data: any): Task => ({
id: data.id,
sequenceId: data.sequence_id,
action: data.action,
payload: data.payload,
timeOffset: data.time_offset,
isQueued: data.is_queued,
continueOnFailure: data.continue_on_failure,
createdAt: new Date(data.created_at),
updatedAt: new Date(data.updated_at),
});
export const rawDataToServerSchedule = (data: any): Schedule => ({
id: data.id,
name: data.name,
cron: {
dayOfWeek: data.cron.day_of_week,
month: data.cron.month,
dayOfMonth: data.cron.day_of_month,
hour: data.cron.hour,
minute: data.cron.minute,
},
isActive: data.is_active,
isProcessing: data.is_processing,
onlyWhenOnline: data.only_when_online,
lastRunAt: data.last_run_at ? new Date(data.last_run_at) : null,
nextRunAt: data.next_run_at ? new Date(data.next_run_at) : null,
createdAt: new Date(data.created_at),
updatedAt: new Date(data.updated_at),
tasks: (data.relationships?.tasks?.data || []).map((row: any) => rawDataToServerTask(row.attributes)),
});
export default async (uuid: string): Promise<Schedule[]> => {
const { data } = await http.get(`/api/client/servers/${uuid}/schedules`, {
params: {
include: [ 'tasks' ],
},
});
return (data.data || []).map((row: any) => rawDataToServerSchedule(row.attributes));
};
@@ -1,4 +0,0 @@
import http from '@/api/http';
export default async (server: string, schedule: number): Promise<void> =>
await http.post(`/api/client/servers/${server}/schedules/${schedule}/execute`);