Skip to content

Commit

Permalink
implemented sub actions : new, unpublish, retract (#2933)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinamudura authored Apr 18, 2022
1 parent 7bd4df2 commit 1d56c64
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 28 deletions.
35 changes: 35 additions & 0 deletions src/api/Elsa.Api/Endpoints/Workflows/Publish.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Management.Contracts;
using Elsa.Persistence.Models;
using Elsa.Runtime.Contracts;
using Elsa.Serialization;
using Microsoft.AspNetCore.Http;

namespace Elsa.Api.Endpoints.Workflows;

public static partial class Workflows
{
public static async Task<IResult> PublishAsync(string definitionId, WorkflowSerializerOptionsProvider serializerOptionsProvider, IWorkflowRegistry workflowRegistry,
IWorkflowPublisher workflowPublisher, CancellationToken cancellationToken)
{
var workflow = await workflowRegistry.FindByIdAsync(definitionId, VersionOptions.Latest, cancellationToken);
if (workflow == null)
{
return Results.NotFound();
}

if (workflow.Publication.IsPublished)
{
return Results.BadRequest(new
{
Message = $"Workflow with id {definitionId} is already published"
});
}

await workflowPublisher.PublishAsync(workflow, cancellationToken);
var serializerOptions = serializerOptionsProvider.CreateApiOptions();

return Results.Json(workflow, serializerOptions, statusCode: StatusCodes.Status202Accepted);
}
}
6 changes: 5 additions & 1 deletion src/api/Elsa.Api/Endpoints/Workflows/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public WorkflowsResource(IEndpointRouteBuilder endpoints) : base(endpoints)
public WorkflowsResource Get() => MapGet("api/workflows/{definitionId}", Workflows.Get);
public WorkflowsResource Execute() => MapPost("api/workflows/{definitionId}/execute", Workflows.ExecuteAsync);
public WorkflowsResource Dispatch() => MapPost("api/workflows/{definitionId}/dispatch", Workflows.DispatchAsync);
public void Delete() => MapDelete("api/workflows/{definitionId}", Workflows.DeleteAsync);
public WorkflowsResource Delete() => MapDelete("api/workflows/{definitionId}", Workflows.DeleteAsync);
public WorkflowsResource Retract() => MapPost("api/workflows/{definitionId}/retract", Workflows.RetractAsync);
public WorkflowsResource Publish() => MapPost("api/workflows/{definitionId}/publish", Workflows.PublishAsync);
}

public static class WorkflowsResourceExtensions
Expand All @@ -34,5 +36,7 @@ public static IEndpointRouteBuilder MapWorkflows(this IEndpointRouteBuilder endp
.GetMany()
.Get()
.Delete()
.Retract()
.Publish()
);
}
27 changes: 27 additions & 0 deletions src/api/Elsa.Api/Endpoints/Workflows/Retract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Management.Contracts;
using Elsa.Persistence.Models;
using Elsa.Runtime.Contracts;
using Elsa.Serialization;
using Microsoft.AspNetCore.Http;

namespace Elsa.Api.Endpoints.Workflows;

public static partial class Workflows
{
public static async Task<IResult> RetractAsync(string definitionId, WorkflowSerializerOptionsProvider serializerOptionsProvider, IWorkflowRegistry workflowRegistry,
IWorkflowPublisher workflowPublisher, CancellationToken cancellationToken)
{
var workflow = await workflowRegistry.FindByIdAsync(definitionId, VersionOptions.LatestOrPublished, cancellationToken);
if (workflow == null)
{
return Results.NotFound();
}

await workflowPublisher.RetractAsync(workflow, cancellationToken);
var serializerOptions = serializerOptionsProvider.CreateApiOptions();

return Results.Json(workflow, serializerOptions, statusCode: StatusCodes.Status202Accepted);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component, Event, EventEmitter, h, Host, Method, Prop, State, Watch} from '@stencil/core';
import {DefaultActions, PagedList, VersionOptions, WorkflowSummary} from "../../../models";
import {DefaultActions, PagedList, VersionOptions, Workflow, WorkflowSummary} from "../../../models";
import {Container} from "typedi";
import {ElsaApiClientProvider, ElsaClient} from "../../../services";
import {ElsaApiClientProvider, ElsaClient, SaveWorkflowRequest} from "../../../services";
import {DeleteIcon, EditIcon, PublishIcon, UnPublishIcon} from "../../icons/tooling";

@Component({
Expand Down Expand Up @@ -33,30 +33,26 @@ export class WorkflowBrowser {
}

private async onPublishClick(e: MouseEvent, workflowDefinition: WorkflowSummary) {
// const elsaClient = await this.createClient();
// await elsaClient.workflowDefinitionsApi.publish(workflowDefinition.definitionId);
// await this.loadWorkflowDefinitions();
const elsaClient = this.elsaClient;
await elsaClient.workflows.publish(workflowDefinition);
await this.loadWorkflowDefinitions();
}

private async onUnPublishClick(e: MouseEvent, workflowDefinition: WorkflowSummary) {
// const elsaClient = await this.createClient();
// await elsaClient.workflowDefinitionsApi.retract(workflowDefinition.definitionId);
// await this.loadWorkflowDefinitions();
}

private async onDeleteClick(e: MouseEvent, workflowDefinition: WorkflowSummary) {
const elsaClient = this.elsaClient;
await elsaClient.workflows.delete(workflowDefinition);
await elsaClient.workflows.retract(workflowDefinition);
await this.loadWorkflowDefinitions();
}

private async onDeleteClick(e: MouseEvent, workflowDefinition: WorkflowSummary) {
// const result = await this.confirmDialog.show(t('DeleteConfirmationModel.Title'), t('DeleteConfirmationModel.Message'));
//
// if (!result)
// return;
//
// const elsaClient = await this.createClient();
// await elsaClient.workflowDefinitionsApi.delete(workflowDefinition.definitionId);
// await this.loadWorkflowDefinitions();

const elsaClient = this.elsaClient;
await elsaClient.workflows.delete(workflowDefinition);
await this.loadWorkflowDefinitions();
}

private onWorkflowDefinitionClick = async (e: MouseEvent, workflowDefinition: WorkflowSummary) => {
Expand Down Expand Up @@ -153,3 +149,4 @@ export class WorkflowBrowser {
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,15 @@ export class WorkflowInstanceBrowser {
};

private async onDeleteClick(e: MouseEvent, workflowInstance: WorkflowInstanceSummary) {

const elsaClient = this.elsaClient;

await elsaClient.workflowInstances.delete(workflowInstance);
await this.loadWorkflowInstances();

// const result = await this.confirmDialog.show(t('DeleteConfirmationModel.Title'), t('DeleteConfirmationModel.Message'));
//
// if (!result)
// return;
//
// const elsaClient = await this.createClient();
// await elsaClient.workflowDefinitionsApi.delete(workflowDefinition.definitionId);
// await this.loadWorkflowDefinitions();
const elsaClient = this.elsaClient;

await elsaClient.workflowInstances.delete(workflowInstance);
await this.loadWorkflowInstances();
}

private onWorkflowInstanceClick = async (e: MouseEvent, workflowInstance: WorkflowInstanceSummary) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {Component, Element, h, Listen, Prop, Watch} from '@stencil/core';
import {Component, Element, EventEmitter, h, Listen, Prop, Watch} from '@stencil/core';
import 'reflect-metadata';
import {Container} from 'typedi';
import {ElsaApiClientProvider, ElsaClient, SaveWorkflowRequest, ServerSettings} from '../../../services';
import {
ElsaApiClientProvider,
ElsaClient,
RetractWorkflowRequest,
SaveWorkflowRequest,
ServerSettings
} from '../../../services';
import {ActivityDescriptor, VersionOptions, Workflow, WorkflowInstanceSummary, WorkflowSummary} from '../../../models';
import {WorkflowUpdatedArgs} from '../../designer/workflow-editor/workflow-editor';
import {PublishClickedArgs} from "../../toolbar/workflow-publish-button/workflow-publish-button";
Expand Down Expand Up @@ -81,6 +87,33 @@ export class Studio {
e.detail.complete();
}

@Listen('unPublishClicked')
private async handleUnPublishClicked(e: Event) {
const workflowEditorElement = this.workflowEditorElement;

if (!workflowEditorElement)
return;

const workflow = await workflowEditorElement.getWorkflow();
await this.retractWorkflow(workflow);
}

@Listen('newClicked')
private async handleNewClick(e: EventEmitter) {
const workflowEditorElement = this.workflowEditorElement;

if (!workflowEditorElement)
return;

const request: SaveWorkflowRequest = {
definitionId: "",
publish: false
};

const updatedWorkflow = await this.elsaClient.workflows.post(request);
await this.workflowEditorElement.importWorkflowMetadata(updatedWorkflow);
}

public async componentWillLoad() {
this.handleServerUrl(this.serverUrl);

Expand Down Expand Up @@ -112,4 +145,14 @@ export class Studio {
await this.workflowEditorElement.importWorkflowMetadata(updatedWorkflow);
return updatedWorkflow;
}

private retractWorkflow = async (workflow: Workflow): Promise<Workflow> => {
const request: RetractWorkflowRequest = {
definitionId: workflow.identity.definitionId
};

const updatedWorkflow = await this.elsaClient.workflows.retract(request);
await this.workflowEditorElement.importWorkflowMetadata(updatedWorkflow);
return updatedWorkflow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export async function createElsaClient(serverUrl: string): Promise<ElsaClient> {
}
},
workflows: {
async publish(request: PublishWorkflowRequest) : Promise<Workflow>{
const response = await httpClient.post<Workflow>(`api/workflows/${request.definitionId}/publish`);
return response.data;
},
async retract(request: RetractWorkflowRequest) : Promise<Workflow>{
const response = await httpClient.post<Workflow>(`api/workflows/${request.definitionId}/retract`);
return response.data;
},
async delete(request: DeleteWorkflowRequest) : Promise<Workflow>{
const response = await httpClient.delete<Workflow>(`api/workflows/${request.definitionId}`);
return response.data;
Expand Down Expand Up @@ -161,6 +169,10 @@ export interface WorkflowsApi {
getMany(request: GetManyWorkflowsRequest): Promise<Array<WorkflowSummary>>;

delete(request: DeleteWorkflowRequest): Promise<Workflow>;

retract(request: RetractWorkflowRequest): Promise<Workflow>;

publish(request: PublishWorkflowRequest): Promise<Workflow>;
}

export interface WorkflowInstancesApi {
Expand Down Expand Up @@ -194,6 +206,14 @@ export interface DeleteWorkflowRequest {
definitionId: string;
}

export interface RetractWorkflowRequest {
definitionId: string;
}

export interface PublishWorkflowRequest {
definitionId: string;
}

export interface GetWorkflowRequest {
definitionId: string;
versionOptions?: VersionOptions;
Expand Down

0 comments on commit 1d56c64

Please sign in to comment.