new code snippets
This commit is contained in:
238
Code Snippets/alertsAction.js
Normal file
238
Code Snippets/alertsAction.js
Normal file
@@ -0,0 +1,238 @@
|
||||
let $injector = widgetContext.$scope.$injector;
|
||||
let customDialog = $injector.get(widgetContext.servicesMap.get("customDialog"));
|
||||
let entityService = $injector.get(widgetContext.servicesMap.get("entityService"));
|
||||
let assetService = $injector.get(widgetContext.servicesMap.get("assetService"));
|
||||
let deviceService = $injector.get(widgetContext.servicesMap.get("deviceService"));
|
||||
let attributeService = $injector.get(widgetContext.servicesMap.get("attributeService"));
|
||||
let entityRelationService = $injector.get(widgetContext.servicesMap.get("entityRelationService"));
|
||||
|
||||
let user = widgetContext.$scope.ctx.currentUser;
|
||||
//console.log(user);
|
||||
openEditEntityDialog();
|
||||
|
||||
function openEditEntityDialog() {
|
||||
customDialog.customDialog(htmlTemplate, EditEntityDialogController).subscribe();
|
||||
}
|
||||
|
||||
function EditEntityDialogController(instance) {
|
||||
let vm = instance;
|
||||
|
||||
vm.entityName = entityName;
|
||||
vm.entityType = entityId.entityType;
|
||||
vm.user = widgetContext.$scope.ctx.currentUser;
|
||||
vm.entitySearchDirection = {
|
||||
from: "FROM",
|
||||
to: "TO",
|
||||
};
|
||||
vm.attributes = {};
|
||||
vm.oldRelationsData = [];
|
||||
vm.relationsToDelete = [];
|
||||
vm.entity = {};
|
||||
|
||||
vm.editEntityFormGroup = vm.fb.group({
|
||||
entityName: ["", [vm.validators.required]],
|
||||
entityType: [null],
|
||||
entityLabel: [null],
|
||||
type: ["", [vm.validators.required]],
|
||||
attributes: vm.fb.group({
|
||||
alertsEmail: [false],
|
||||
alertsSMS: [false],
|
||||
alertsVoice: [false],
|
||||
}),
|
||||
oldRelations: vm.fb.array([]),
|
||||
relations: vm.fb.array([]),
|
||||
});
|
||||
|
||||
getEntityInfo();
|
||||
|
||||
vm.cancel = function () {
|
||||
vm.dialogRef.close(null);
|
||||
};
|
||||
|
||||
vm.relations = function () {
|
||||
return vm.editEntityFormGroup.get("relations");
|
||||
};
|
||||
|
||||
vm.oldRelations = function () {
|
||||
return vm.editEntityFormGroup.get("oldRelations");
|
||||
};
|
||||
|
||||
vm.addRelation = function () {
|
||||
vm.relations().push(
|
||||
vm.fb.group({
|
||||
relatedEntity: [null, [vm.validators.required]],
|
||||
relationType: [null, [vm.validators.required]],
|
||||
direction: [null, [vm.validators.required]],
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
function addOldRelation() {
|
||||
vm.oldRelations().push(
|
||||
vm.fb.group({
|
||||
relatedEntity: [{ value: null, disabled: true }, [vm.validators.required]],
|
||||
relationType: [{ value: null, disabled: true }, [vm.validators.required]],
|
||||
direction: [{ value: null, disabled: true }, [vm.validators.required]],
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
vm.removeRelation = function (index) {
|
||||
vm.relations().removeAt(index);
|
||||
vm.relations().markAsDirty();
|
||||
};
|
||||
|
||||
vm.removeOldRelation = function (index, relation) {
|
||||
vm.oldRelations().removeAt(index);
|
||||
vm.relationsToDelete.push(relation);
|
||||
vm.oldRelations().markAsDirty();
|
||||
};
|
||||
|
||||
vm.save = function () {
|
||||
vm.editEntityFormGroup.markAsPristine();
|
||||
widgetContext.rxjs.forkJoin([saveRelations(entityId)]).subscribe(function () {
|
||||
widgetContext.updateAliases();
|
||||
vm.dialogRef.close(null);
|
||||
});
|
||||
};
|
||||
|
||||
function getEntityAttributes(attributes) {
|
||||
for (var i = 0; i < attributes.length; i++) {
|
||||
vm.attributes[attributes[i].key] = attributes[i].value;
|
||||
}
|
||||
}
|
||||
|
||||
function getEntityRelations(relations) {
|
||||
//console.log(relations);
|
||||
let relationsFrom = relations[0];
|
||||
let relationsTo = relations[1];
|
||||
for (let i = 0; i < relationsFrom.length; i++) {
|
||||
if (relationsFrom[i].type == "getsAlertsEmail" && relationsFrom[i].to.id == user.userId) {
|
||||
//console.log(`found getsAlertsEmail relation for ${entityName}`);
|
||||
vm.attributes.alertsEmail = true;
|
||||
vm.oldRelationsData.push("alertsEmail");
|
||||
}
|
||||
if (relationsFrom[i].type == "getsAlertsSMS" && relationsFrom[i].to.id == user.userId) {
|
||||
//console.log(`found getsAlertsSMS relation for ${entityName}`);
|
||||
vm.attributes.alertsSMS = true;
|
||||
vm.oldRelationsData.push("alertsSMS");
|
||||
}
|
||||
if (relationsFrom[i].type == "getsAlertsVoice" && relationsFrom[i].to.id == user.userId) {
|
||||
//console.log(`found getsAlertsVoice relation for ${entityName}`);
|
||||
vm.attributes.alertsVoice = true;
|
||||
vm.oldRelationsData.push("alertsVoice");
|
||||
}
|
||||
}
|
||||
//console.log(vm.attributes);
|
||||
}
|
||||
|
||||
function getEntityInfo() {
|
||||
widgetContext.rxjs
|
||||
.forkJoin([entityRelationService.findInfoByFrom(entityId), entityRelationService.findInfoByTo(entityId), attributeService.getEntityAttributes(entityId, "SERVER_SCOPE"), entityService.getEntity(entityId.entityType, entityId.id)])
|
||||
.subscribe(function (data) {
|
||||
getEntityRelations(data.slice(0, 2));
|
||||
//getEntityAttributes(data[2]);
|
||||
//console.log(vm.attributes);
|
||||
vm.entity = data[3];
|
||||
vm.editEntityFormGroup.patchValue(
|
||||
{
|
||||
//I think this is where I'll be able to populate the toggles
|
||||
entityName: vm.entity.name,
|
||||
entityType: vm.entityType,
|
||||
entityLabel: vm.entity.label,
|
||||
type: vm.entity.type,
|
||||
attributes: vm.attributes,
|
||||
oldRelations: vm.oldRelationsData,
|
||||
},
|
||||
{ emitEvent: false }
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
function saveRelations(entityId) {
|
||||
//let relations = vm.editEntityFormGroup.get("relations").value;
|
||||
let attributes = vm.editEntityFormGroup.get("attributes").value;
|
||||
let tasks = [];
|
||||
//console.log(attributes);
|
||||
|
||||
if (attributes.alertsEmail == true) {
|
||||
let relation = {
|
||||
type: "getsAlertsEmail",
|
||||
typeGroup: "COMMON",
|
||||
to: { entityType: "USER", id: user.userId },
|
||||
from: entityId,
|
||||
};
|
||||
//console.log(`adding getsAlertsEmail FROM ${entityName} TO ${user.name}`);
|
||||
|
||||
tasks.push(entityRelationService.saveRelation(relation));
|
||||
} else {
|
||||
let relation = {
|
||||
type: "getsAlertsEmail",
|
||||
typeGroup: "COMMON",
|
||||
to: { entityType: "USER", id: user.userId },
|
||||
from: entityId,
|
||||
};
|
||||
if (vm.oldRelationsData.includes("alertsEmail")) {
|
||||
//console.log(`removing getsAlertsEmail FROM ${entityName} TO ${user.name}`);
|
||||
relation.type = "getsAlertsEmail";
|
||||
tasks.push(entityRelationService.deleteRelation(relation.from, relation.type, relation.to));
|
||||
}
|
||||
}
|
||||
|
||||
if (attributes.alertsSMS == true) {
|
||||
let relation = {
|
||||
type: "getsAlertsSMS",
|
||||
typeGroup: "COMMON",
|
||||
to: { entityType: "USER", id: user.userId },
|
||||
from: entityId,
|
||||
};
|
||||
//console.log(`adding getsAlertsSMS FROM ${entityName} TO ${user.name}`);
|
||||
relation.type = "getsAlertsSMS";
|
||||
tasks.push(entityRelationService.saveRelation(relation));
|
||||
} else {
|
||||
let relation = {
|
||||
type: "getsAlertsSMS",
|
||||
typeGroup: "COMMON",
|
||||
to: { entityType: "USER", id: user.userId },
|
||||
from: entityId,
|
||||
};
|
||||
if (vm.oldRelationsData.includes("alertsSMS")) {
|
||||
//console.log(`removing getsAlertsSMS FROM ${entityName} TO ${user.name}`);
|
||||
relation.type = "getsAlertsSMS";
|
||||
tasks.push(entityRelationService.deleteRelation(relation.from, relation.type, relation.to));
|
||||
}
|
||||
}
|
||||
|
||||
if (attributes.alertsVoice == true) {
|
||||
let relation = {
|
||||
type: "getsAlertsVoice",
|
||||
typeGroup: "COMMON",
|
||||
to: { entityType: "USER", id: user.userId },
|
||||
from: entityId,
|
||||
};
|
||||
//console.log(`adding getsAlertsVoice FROM ${entityName} TO ${user.name}`);
|
||||
relation.type = "getsAlertsVoice";
|
||||
tasks.push(entityRelationService.saveRelation(relation));
|
||||
} else {
|
||||
let relation = {
|
||||
type: "getsAlertsVoice",
|
||||
typeGroup: "COMMON",
|
||||
to: { entityType: "USER", id: user.userId },
|
||||
from: entityId,
|
||||
};
|
||||
if (vm.oldRelationsData.includes("alertsVoice")) {
|
||||
//console.log(`removing getsAlertsVoice FROM ${entityName} TO ${user.name}`);
|
||||
relation.type = "getsAlertsVoice";
|
||||
tasks.push(entityRelationService.deleteRelation(relation.from, relation.type, relation.to));
|
||||
}
|
||||
}
|
||||
|
||||
if (tasks.length > 0) {
|
||||
//console.log(tasks);
|
||||
return widgetContext.rxjs.forkJoin(tasks);
|
||||
}
|
||||
return widgetContext.rxjs.of([]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user