Skip to content

Commit

Permalink
Add ability to start a workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Pasquier committed May 10, 2019
1 parent 8437978 commit b715d3d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
17 changes: 15 additions & 2 deletions src/ActionHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,27 @@ var ActionHandlers = {
* Display workflow card.
*/
displayWF: function(e) {
return displayWFCard(e.parameters.docId);
var suggestions = nuxeoClientWrapper().workflows();
var wfNames = suggestions.map(function(suggestion) {
return suggestion.name;
});
return displayWFCard(e.parameters, wfNames);
},

/**
* Execute a workflow for a given user on content
*/
executeWF: function(e) {
// TODO
var workflowId = e.formInputs.workflowName;
var docId = e.parameters.docId;
var link = e.parameters.link;
nuxeoClientWrapper().startWF(docId, workflowId[0]);
return showResultDoc(
"Done!",
"Your workflow has started on the document",
link,
docId
);
},

attachDocument: function(e) {
Expand Down
52 changes: 52 additions & 0 deletions src/NuxeoClientWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,58 @@ var NuxeoClientPrototype = {
return parsedResponse;
},

/**
* Fetch the workflows.
*/
workflows: function() {
if (!this.oauthService.hasAccess()) {
throw new AuthorizationRequiredException();
}
var headers = {
Authorization: Utilities.formatString("Bearer %s", this.oauthService.getAccessToken())
};
var url = this.apiEndpoint + "/workflowModel";
var response = UrlFetchApp.fetch(url, {
method: "get",
headers: headers,
muteHttpExceptions: true
});
var raw = response.getContentText();
var parsedResponse = JSON.parse(raw);
if (parsedResponse.status && parsedResponse.status === 500) {
throw new Error(parsedResponse.message);
}
return parsedResponse.entries;
},

startWF: function(docId, workflowId) {
// Building the payload
var json = {
context: {},
params: {
id: workflowId
},
input: docId
};
var headers = {
Authorization: Utilities.formatString("Bearer %s", this.oauthService.getAccessToken()),
"enrichers.document": "documentURL"
};
var url = this.apiEndpoint + "/automation/Context.StartWorkflow";
var response = UrlFetchApp.fetch(url, {
method: "post",
contentType: "application/json",
payload: JSON.stringify(json),
headers: headers,
muteHttpExceptions: true
});
var raw = response.getContentText();
var parsedResponse = JSON.parse(raw);
if (parsedResponse.status && parsedResponse.status === 500) {
throw new Error(parsedResponse.message);
}
},

/**
* De-authorizes the Nuxeo client.
*/
Expand Down
35 changes: 25 additions & 10 deletions src/Views.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ function showResultDoc(title, message, link, docId) {
.setOpenLink(createExternalLink(link))
);
var params = {
docId: docId
docId: docId,
link: link
};
var sectionTask = CardService.newCardSection();
sectionTask.addWidget(
Expand Down Expand Up @@ -443,21 +444,35 @@ function buildAttachCard(attachments) {
.build();
}

function displayWFCard(docId) {
function displayWFCard(params, suggestions) {
var card = CardService.newCardBuilder();
var sectionLogo = CardService.newCardSection().addWidget(
CardService.newKeyValue()
.setIconUrl(NUXEO_ICON)
.setMultiline(true)
.setContent("<b>Workflow</b>")
.setContent("<b>Please select the workflow to execute:</b>")
);
var sectionAction = CardService.newCardSection();
sectionAction.addWidget(
CardService.newButtonSet().addButton(
CardService.newTextButton()
.setText('<font color="#334CFF">Execute</font>')
.setOnClickAction(createAction_(EXECUTE_WF))
var suggestionsWidget = CardService.newSuggestions();
for (var i = 0; i < suggestions.length; i++) {
suggestionsWidget.addSuggestion(suggestions[i]);
}
sectionAction
.addWidget(
CardService.newTextInput()
.setFieldName("workflowName")
.setTitle("Workflow:")
.setSuggestions(suggestionsWidget)
)
);
var card = card.addSection(sectionLogo);
.addWidget(
CardService.newButtonSet().addButton(
CardService.newTextButton()
.setText('<font color="#334CFF">Execute</font>')
.setOnClickAction(createAction_(EXECUTE_WF, params))
)
);
return card
.addSection(sectionLogo)
.addSection(sectionAction)
.build();
}

0 comments on commit b715d3d

Please sign in to comment.