// ========================================================================== // 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 ContentDataCommand = Squidex.Domain.Apps.Entities.Contents.Commands.ContentDataCommand; using CreateCommand = Squidex.Domain.Apps.Entities.Contents.Commands.CreateContent; using UpsertCommand = Squidex.Domain.Apps.Entities.Contents.Commands.UpsertContent; namespace Squidex.Extensions.Actions.CreateContent; [FlowStep( Title = "CreateContent", IconImage = "", IconColor = "#3389ff", Display = "Create content", Description = "Create a new content item for any schema, or update it if it already exists.")] #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.Checkbox)] public bool Publish { get; set; } [Display(Name = "ID (Optional)", Description = "The ID of the content. If set, the content is created when it does not exist and updated otherwise.")] [Editor(FlowStepEditor.Text)] [Expression] public string? Id { get; set; } [Display(Name = "Patch", Description = "Only update the fields from the data when the content already exists. Requires an ID.")] [Editor(FlowStepEditor.Checkbox)] public bool Patch { get; set; } public override ValueTask ValidateAsync(FlowValidationContext validationContext, AddStepError addError, CancellationToken ct) { if (Patch && string.IsNullOrWhiteSpace(Id)) { addError(nameof(Patch), "Patch requires an ID."); } return base.ValidateAsync(validationContext, addError, ct); } public override async ValueTask ExecuteAsync(FlowExecutionContext executionContext, CancellationToken ct) { var @event = ((FlowEventContext)executionContext.Context).Event; 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(); } var status = Publish ? Status.Published : (Status?)null; ContentDataCommand command; if (!string.IsNullOrWhiteSpace(Id)) { command = new UpsertCommand { ContentId = DomainId.Create(Id.Trim()), EnrichDefaults = true, EnrichRequiredFields = false, Patch = Patch, Status = status, }; } else { command = new CreateCommand { Status = status }; } command.AppId = @event.AppId; 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; } await executionContext.Resolve() .PublishAsync(command, ct); if (command is UpsertCommand) { executionContext.Log($"Content upserted for schema '{schema.Name}', ID: {command.ContentId}"); } else { 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 }