195 lines
7.5 KiB
TypeScript
Raw Normal View History

import { Resource, TriggerDefinition, TriggerOperation, TriggerType } from "@azure/cosmos";
import * as Constants from "../../Common/Constants";
import { createTrigger } from "../../Common/dataAccess/createTrigger";
import { updateTrigger } from "../../Common/dataAccess/updateTrigger";
import editable from "../../Common/EditableUtility";
import * as ViewModels from "../../Contracts/ViewModels";
import { Action } from "../../Shared/Telemetry/TelemetryConstants";
2020-09-08 12:44:46 -05:00
import * as TelemetryProcessor from "../../Shared/Telemetry/TelemetryProcessor";
2020-07-21 13:50:51 -05:00
import Trigger from "../Tree/Trigger";
import ScriptTabBase from "./ScriptTabBase";
import { getErrorMessage, getErrorStack } from "../../Common/ErrorHandlingUtils";
2020-07-27 16:05:25 -05:00
export default class TriggerTab extends ScriptTabBase {
public collection: ViewModels.Collection;
2020-07-21 13:50:51 -05:00
public node: Trigger;
public triggerType: ViewModels.Editable<string>;
public triggerOperation: ViewModels.Editable<string>;
constructor(options: ViewModels.ScriptTabOption) {
super(options);
super.onActivate.bind(this);
this.ariaLabel("Trigger Body");
this.triggerType = editable.observable<string>(options.resource.triggerType);
this.triggerOperation = editable.observable<string>(options.resource.triggerOperation);
this.formFields([this.id, this.triggerType, this.triggerOperation, this.editorContent]);
super.buildCommandBarOptions.bind(this);
super.buildCommandBarOptions();
}
public onSaveClick = (): Promise<TriggerDefinition & Resource> => {
return this._createTrigger({
id: this.id(),
body: this.editorContent(),
triggerOperation: this.triggerOperation() as TriggerOperation,
2021-01-20 09:15:01 -06:00
triggerType: this.triggerType() as TriggerType,
});
};
public onUpdateClick = (): Promise<any> => {
const data = this._getResource();
this.isExecutionError(false);
this.isExecuting(true);
const startKey: number = TelemetryProcessor.traceStart(Action.UpdateTrigger, {
databaseAccountName: this.collection && this.collection.container.databaseAccount().name,
defaultExperience: this.collection && this.collection.container.defaultExperience(),
2021-01-20 09:15:01 -06:00
tabTitle: this.tabTitle(),
});
return updateTrigger(this.collection.databaseId, this.collection.id(), {
id: this.id(),
body: this.editorContent(),
triggerOperation: this.triggerOperation() as TriggerOperation,
2021-01-20 09:15:01 -06:00
triggerType: this.triggerType() as TriggerType,
})
.then(
2021-01-20 09:15:01 -06:00
(createdResource) => {
this.resource(createdResource);
this.tabTitle(createdResource.id);
this.node.id(createdResource.id);
this.node.body(createdResource.body as string);
this.node.triggerType(createdResource.triggerOperation);
this.node.triggerOperation(createdResource.triggerOperation);
TelemetryProcessor.traceSuccess(
Action.UpdateTrigger,
{
databaseAccountName: this.collection && this.collection.container.databaseAccount().name,
defaultExperience: this.collection && this.collection.container.defaultExperience(),
dataExplorerArea: Constants.Areas.Tab,
2021-01-20 09:15:01 -06:00
tabTitle: this.tabTitle(),
},
startKey
);
this.setBaselines();
const editorModel = this.editor().getModel();
editorModel.setValue(createdResource.body as string);
this.editorContent.setBaseline(createdResource.body as string);
},
(createError: any) => {
this.isExecutionError(true);
TelemetryProcessor.traceFailure(
Action.UpdateTrigger,
{
databaseAccountName: this.collection && this.collection.container.databaseAccount().name,
defaultExperience: this.collection && this.collection.container.defaultExperience(),
dataExplorerArea: Constants.Areas.Tab,
tabTitle: this.tabTitle(),
error: getErrorMessage(createError),
2021-01-20 09:15:01 -06:00
errorStack: getErrorStack(createError),
},
startKey
);
}
)
.finally(() => this.isExecuting(false));
};
public setBaselines() {
super.setBaselines();
const resource = this.resource();
this.triggerOperation.setBaseline(resource.triggerOperation);
this.triggerType.setBaseline(resource.triggerType);
}
protected updateSelectedNode(): void {
if (this.collection == null) {
return;
}
const database: ViewModels.Database = this.collection.getDatabase();
if (!database.isDatabaseExpanded()) {
this.collection.container.selectedNode(database);
} else if (!this.collection.isCollectionExpanded() || !this.collection.isTriggersExpanded()) {
this.collection.container.selectedNode(this.collection);
} else {
this.collection.container.selectedNode(this.node);
}
}
private _createTrigger(resource: TriggerDefinition): Promise<TriggerDefinition & Resource> {
this.isExecutionError(false);
this.isExecuting(true);
const startKey: number = TelemetryProcessor.traceStart(Action.CreateTrigger, {
databaseAccountName: this.collection && this.collection.container.databaseAccount().name,
defaultExperience: this.collection && this.collection.container.defaultExperience(),
dataExplorerArea: Constants.Areas.Tab,
2021-01-20 09:15:01 -06:00
tabTitle: this.tabTitle(),
});
return createTrigger(this.collection.databaseId, this.collection.id(), resource)
.then(
2021-01-20 09:15:01 -06:00
(createdResource) => {
this.tabTitle(createdResource.id);
this.isNew(false);
this.resource(createdResource);
this.hashLocation(
`${Constants.HashRoutePrefixes.collectionsWithIds(
this.collection.databaseId,
this.collection.id()
)}/triggers/${createdResource.id}`
);
this.setBaselines();
const editorModel = this.editor().getModel();
editorModel.setValue(createdResource.body as string);
this.editorContent.setBaseline(createdResource.body as string);
this.node = this.collection.createTriggerNode(createdResource);
TelemetryProcessor.traceSuccess(
Action.CreateTrigger,
{
databaseAccountName: this.collection && this.collection.container.databaseAccount().name,
defaultExperience: this.collection && this.collection.container.defaultExperience(),
dataExplorerArea: Constants.Areas.Tab,
2021-01-20 09:15:01 -06:00
tabTitle: this.tabTitle(),
},
startKey
);
this.editorState(ViewModels.ScriptEditorState.exisitingNoEdits);
return createdResource;
},
(createError: any) => {
this.isExecutionError(true);
TelemetryProcessor.traceFailure(
Action.CreateTrigger,
{
databaseAccountName: this.collection && this.collection.container.databaseAccount().name,
defaultExperience: this.collection && this.collection.container.defaultExperience(),
dataExplorerArea: Constants.Areas.Tab,
tabTitle: this.tabTitle(),
error: getErrorMessage(createError),
2021-01-20 09:15:01 -06:00
errorStack: getErrorStack(createError),
},
startKey
);
return Promise.reject(createError);
}
)
.finally(() => this.isExecuting(false));
}
private _getResource() {
return {
id: this.id(),
body: this.editorContent(),
triggerOperation: this.triggerOperation(),
2021-01-20 09:15:01 -06:00
triggerType: this.triggerType(),
};
}
}