@fleetbase/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
175 lines (163 loc) • 5.06 kB
JavaScript
import Service, { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { task } from 'ember-concurrency';
export default class DriverSchedulingService extends Service {
store;
fetch;
notifications;
currentSchedule = null;
scheduleItems = [];
constraints = [];
/**
* Load a schedule by ID
*/
*loadSchedule(scheduleId) {
try {
const schedule = yield this.store.findRecord('schedule', scheduleId, {
include: 'items',
reload: true,
});
this.currentSchedule = schedule;
return schedule;
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Create a new schedule
*/
*createSchedule(data) {
try {
const schedule = this.store.createRecord('schedule', {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
...data,
});
yield schedule.save();
this.notifications.success('Schedule created successfully');
return schedule;
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Create a new schedule item
*/
*createScheduleItem(data) {
try {
const item = this.store.createRecord('schedule-item', data);
yield item.save();
this.notifications.success('Schedule item created successfully');
return item;
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Update a schedule item
*/
*updateScheduleItem(item, data) {
try {
item.setProperties(data);
yield item.save();
this.notifications.success('Schedule item updated successfully');
return item;
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Delete a schedule item
*/
*deleteScheduleItem(item) {
try {
yield item.destroyRecord();
this.notifications.success('Schedule item deleted successfully');
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Get schedule items for an assignee
*/
*getScheduleItemsForAssignee(assigneeType, assigneeUuid, filters = {}) {
try {
const items = yield this.store.query('schedule-item', {
assignee_type: assigneeType,
assignee_uuid: assigneeUuid,
...filters,
});
this.scheduleItems = items.toArray();
return items;
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Check availability for a subject
*/
*checkAvailability(subjectType, subjectUuid, startAt, endAt) {
try {
const response = yield this.fetch.get('schedule-availability/check', {
subject_type: subjectType,
subject_uuid: subjectUuid,
start_at: startAt,
end_at: endAt,
});
return response.available;
} catch (error) {
this.notifications.serverError(error);
return false;
}
}
/**
* Set availability for a subject
*/
*setAvailability(data) {
try {
const availability = this.store.createRecord('schedule-availability', data);
yield availability.save();
this.notifications.success('Availability set successfully');
return availability;
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Load constraints for a subject
*/
*loadConstraints(subjectType, subjectUuid) {
try {
const constraints = yield this.store.query('schedule-constraint', {
subject_type: subjectType,
subject_uuid: subjectUuid,
is_active: true,
});
this.constraints = constraints.toArray();
return constraints;
} catch (error) {
this.notifications.serverError(error);
throw error;
}
}
/**
* Validate a schedule item against constraints
*/
*validateScheduleItem(item) {
try {
const response = yield this.fetch.post('schedule-items/validate', {
schedule_item: item.serialize(),
});
return response.violations || [];
} catch (error) {
this.notifications.serverError(error);
return [];
}
}
}