// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.ComponentModel.DataAnnotations; using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.Rules.Deprecated; using Squidex.Domain.Apps.Core.Rules.EnrichedEvents; using Squidex.Domain.Apps.Core.Schemas; using Squidex.Domain.Apps.Entities; using Squidex.Flows; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Reflection; using Squidex.Infrastructure.Validation; using Command = Squidex.Domain.Apps.Entities.Contents.Commands.CreateContent; namespace Squidex.Extensions.Actions.CreateContent; [FlowStep( Title = "CreateContent", IconImage = "", IconColor = "#3389ff", Display = "Create content", Description = "Create a a new content item for any schema.")] #pragma warning disable CS0618 // Type or member is obsolete public sealed record CreateContentFlowStep : FlowStep, IConvertibleToAction #pragma warning restore CS0618 // Type or member is obsolete { [LocalizedRequired] [Display(Name = "Data", Description = "The content data.")] [Editor(FlowStepEditor.TextArea)] [Expression] public string Data { get; set; } [LocalizedRequired] [Display(Name = "Schema", Description = "The name of the schema.")] [Editor(FlowStepEditor.Text)] public string Schema { get; set; } [Display(Name = "Client", Description = "An optional client name.")] [Editor(FlowStepEditor.Text)] public string? Client { get; set; } [Display(Name = "Publish", Description = "Publish the content.")] [Editor(FlowStepEditor.Text)] public bool Publish { get; set; } public override async ValueTask ExecuteAsync(FlowExecutionContext executionContext, CancellationToken ct) { var @event = ((FlowEventContext)executionContext.Context).Event; var command = new Command { AppId = @event.AppId, }; var schema = await executionContext.Resolve() .GetSchemaAsync(@event.AppId.Id, Schema, true, ct); if (schema == null) { executionContext.LogSkipped("Invalid Schema."); return Next(); } if (executionContext.IsSimulation) { executionContext.LogSkipSimulation(); return Next(); } command.SchemaId = schema.NamedId(); command.FromRule = true; command.Data = executionContext.DeserializeJson(Data); if (!string.IsNullOrEmpty(Client)) { command.Actor = RefToken.Client(Client); } else if (@event is EnrichedUserEventBase userEvent) { command.Actor = userEvent.Actor; } if (Publish) { command.Status = Status.Published; } await executionContext.Resolve() .PublishAsync(command, ct); executionContext.Log($"Content created for schema '{schema.Name}', ID: {command.ContentId}"); return Next(); } #pragma warning disable CS0618 // Type or member is obsolete public RuleAction ToAction() { return SimpleMapper.Map(this, new CreateContentAction()); } #pragma warning restore CS0618 // Type or member is obsolete }