2018-03-16 14:06:42 +00:00
|
|
|
import ngModule from '../module';
|
|
|
|
|
2018-03-22 09:57:55 +00:00
|
|
|
class Controller {
|
2018-03-16 14:06:42 +00:00
|
|
|
constructor($stateParams, $scope, $http, $translate, vnApp) {
|
|
|
|
this.params = $stateParams;
|
|
|
|
this.$scope = $scope;
|
|
|
|
this.$http = $http;
|
|
|
|
this.$translate = $translate;
|
|
|
|
this.vnApp = vnApp;
|
|
|
|
|
|
|
|
this.ticketObservations = [];
|
|
|
|
this.oldObservations = {};
|
|
|
|
this.removedObservations = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
_setIconAdd() {
|
|
|
|
if (this.ticketObservations.length) {
|
|
|
|
this.ticketObservations.map(element => {
|
|
|
|
element.showAddIcon = false;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
this.ticketObservations[this.ticketObservations.length - 1].showAddIcon = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setDirtyForm() {
|
|
|
|
if (this.$scope.form) {
|
|
|
|
this.$scope.form.$setDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_unsetDirtyForm() {
|
|
|
|
if (this.$scope.form) {
|
|
|
|
this.$scope.form.$setPristine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addObservation() {
|
|
|
|
this.ticketObservations.push({description: null, ticketFk: this.params.id, showAddIcon: true});
|
|
|
|
this._setIconAdd();
|
|
|
|
}
|
|
|
|
|
|
|
|
removeObservation(index) {
|
|
|
|
let item = this.ticketObservations[index];
|
|
|
|
if (item) {
|
|
|
|
this.ticketObservations.splice(index, 1);
|
|
|
|
this._setIconAdd();
|
|
|
|
if (item.id) {
|
|
|
|
this.removedObservations.push(item.id);
|
|
|
|
this._setDirtyForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_equalObservations(oldObservation, newObservation) {
|
|
|
|
return oldObservation.id === newObservation.id && oldObservation.observationTypeFk === newObservation.observationTypeFk && oldObservation.description === newObservation.description;
|
|
|
|
}
|
|
|
|
|
|
|
|
setOldObservations(response) {
|
|
|
|
this._setIconAdd();
|
|
|
|
response.data.forEach(observation => {
|
|
|
|
this.oldObservations[observation.id] = Object.assign({}, observation);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getObservations() {
|
|
|
|
let filter = {
|
|
|
|
where: {ticketFk: this.params.id},
|
|
|
|
include: ['observationType']
|
|
|
|
};
|
|
|
|
|
|
|
|
this.$http.get(`/ticket/api/TicketObservations?filter=${JSON.stringify(filter)}`).then(response => {
|
|
|
|
this.ticketObservations = response.data;
|
|
|
|
this.setOldObservations(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
submit() {
|
|
|
|
let typesDefined = [];
|
|
|
|
let repeatedType = false;
|
|
|
|
let canSubmit;
|
|
|
|
let observationsObj = {
|
|
|
|
delete: this.removedObservations,
|
|
|
|
create: [],
|
|
|
|
update: []
|
|
|
|
};
|
|
|
|
|
|
|
|
this.ticketObservations.forEach(observation => {
|
|
|
|
let isNewObservation = !observation.id;
|
|
|
|
|
|
|
|
delete observation.showAddIcon;
|
|
|
|
|
|
|
|
if (typesDefined.indexOf(observation.observationTypeFk) !== -1) {
|
|
|
|
repeatedType = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
typesDefined.push(observation.observationTypeFk);
|
|
|
|
|
|
|
|
if (isNewObservation && observation.description && observation.observationTypeFk) {
|
|
|
|
observationsObj.create.push(observation);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNewObservation && !this._equalObservations(this.oldObservations[observation.id], observation)) {
|
|
|
|
observationsObj.update.push(observation);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.$scope.form.$invalid) {
|
2018-07-09 11:01:46 +00:00
|
|
|
return this.vnApp.showError(this.$translate.instant('Some fields are invalid'));
|
2018-03-16 14:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (repeatedType) {
|
2018-07-09 11:01:46 +00:00
|
|
|
return this.vnApp.showError(this.$translate.instant('The observation type must be unique'));
|
2018-03-16 14:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canSubmit = observationsObj.update.length > 0 || observationsObj.create.length > 0 || observationsObj.delete.length > 0;
|
|
|
|
|
|
|
|
if (canSubmit) {
|
2018-03-22 09:57:55 +00:00
|
|
|
return this.$http.post(`/ticket/api/TicketObservations/crudTicketObservation`, observationsObj).then(() => {
|
2018-03-16 14:06:42 +00:00
|
|
|
this.getObservations();
|
|
|
|
this._unsetDirtyForm();
|
2018-05-31 09:52:39 +00:00
|
|
|
this.$scope.watcher.notifySaved();
|
2018-03-16 14:06:42 +00:00
|
|
|
});
|
|
|
|
}
|
2018-07-09 11:01:46 +00:00
|
|
|
this.vnApp.showError(this.$translate.instant('No changes to save'));
|
2018-03-16 14:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
|
|
|
this.getObservations();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 09:57:55 +00:00
|
|
|
Controller.$inject = ['$stateParams', '$scope', '$http', '$translate', 'vnApp'];
|
2018-03-16 14:06:42 +00:00
|
|
|
|
2018-03-22 09:57:55 +00:00
|
|
|
ngModule.component('vnTicketObservation', {
|
2018-05-25 08:03:45 +00:00
|
|
|
template: require('./index.html'),
|
2018-03-22 09:57:55 +00:00
|
|
|
controller: Controller,
|
2018-03-16 14:06:42 +00:00
|
|
|
bindings: {
|
|
|
|
ticket: '<'
|
|
|
|
}
|
|
|
|
});
|