public override async Task <ICommandResult> HandleAsync(ProjectCreateCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log) { if (command is null) { throw new ArgumentNullException(nameof(command)); } if (commandQueue is null) { throw new ArgumentNullException(nameof(commandQueue)); } var commandResult = command.CreateResult(); try { commandResult.Result = await projectRepository .AddAsync(command.Payload) .ConfigureAwait(false); await userRepository .AddProjectMembershipAsync(command.User, commandResult.Result.Id, ProjectUserRole.Owner, new Dictionary <string, string>()) .ConfigureAwait(false); await commandQueue .AddAsync(new ProjectDeployCommand(command.User, command.Payload)) .ConfigureAwait(false); var tenantId = await azureSessionService .GetTenantIdAsync() .ConfigureAwait(false); var message = NotificationMessage.Create <WelcomeMessage>(command.User); message.Merge(new WelcomeMessageData() { Organization = await organizationRepository.GetAsync(tenantId.ToString(), commandResult.Result.Organization, expand: true).ConfigureAwait(false), Project = commandResult.Result, User = await userRepository.ExpandAsync(command.User).ConfigureAwait(false), PortalUrl = endpointOptions.Portal }); await commandQueue .AddAsync(new NotificationSendMailCommand <WelcomeMessage>(command.User, message)) .ConfigureAwait(false); commandResult.RuntimeStatus = CommandRuntimeStatus.Completed; } catch (Exception exc) { commandResult.Errors.Add(exc); } return(commandResult); }
private async Task SendWelcomeMessageAsync(ICommand command, IAsyncCollector <ICommand> commandQueue, User userOld) { if (command.Payload is User userNew) { var projects = await userNew.ProjectMemberships .Select(pm => pm.ProjectId) .Except(userOld?.ProjectMemberships.Select(pm => pm.ProjectId) ?? Enumerable.Empty <string>(), StringComparer.OrdinalIgnoreCase) .Select(pid => projectRepository.GetAsync(userNew.Organization, pid, true)) .WhenAll() .ConfigureAwait(false); if (projects.Any()) { var tenantId = await azureSessionService .GetTenantIdAsync() .ConfigureAwait(false); var organization = await organizationRepository .GetAsync(tenantId.ToString(), userNew.Organization, expand : true) .ConfigureAwait(false); userNew = await userRepository .ExpandAsync(userNew) .ConfigureAwait(false); await projects .Select(project => SendWelcomeMessageAsync(command.User, organization, project, userNew)) .WhenAll() .ConfigureAwait(false); } } Task SendWelcomeMessageAsync(User sender, Organization organization, Project project, User user) { var message = NotificationMessage.Create <WelcomeMessage>(user); message.Merge(new WelcomeMessageData() { Organization = organization, Project = project, User = user, PortalUrl = endpointOptions.Portal }); return(commandQueue.AddAsync(new NotificationSendMailCommand <WelcomeMessage>(sender, message))); } }
private async Task SendAlternateIdentityMessageAsync(ICommand command, IAsyncCollector <ICommand> commandQueue, User userOld) { if (command.Payload is User userNew) { var alternateIdentities = userNew.AlternateIdentities.Keys .Except(userOld?.AlternateIdentities.Keys ?? Enumerable.Empty <DeploymentScopeType>()) .Select(deploymentScopeType => deploymentScopeType.ToString(prettyPrint: true)); if (alternateIdentities.Any()) { var tenantId = await azureSessionService .GetTenantIdAsync() .ConfigureAwait(false); var organization = await organizationRepository .GetAsync(tenantId.ToString(), userNew.Organization, expand : true) .ConfigureAwait(false); userNew = await userRepository .ExpandAsync(userNew) .ConfigureAwait(false); var message = NotificationMessage.Create <AlternateIdentityMessage>(userNew); message.Merge(new AlternateIdentityMessageData() { Organization = organization, Services = alternateIdentities.ToArray(), User = userNew, PortalUrl = endpointOptions.Portal }); await commandQueue .AddAsync(new NotificationSendMailCommand <AlternateIdentityMessage>(command.User, message)) .ConfigureAwait(false); } } }