public async Task <IActionResult> StartMailMerge(RunMailMergeOptions options,
                                                         CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var mergeTemplate = await _mergeTemplateRepository.GetMergeTemplate(options.MergeTemplateId);

            if (mergeTemplate == null)
            {
                return(NotFound($"Merge template with id {options.MergeTemplateId} not found"));
            }

            await _servicesProxy.StartMailMergeAsync(options, cancellationToken);

            return(Ok());
        }
Example #2
0
        public async Task <RunMergeTemplateProgress> RunMailMergeAsync(
            RunMailMergeOptions options,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var mergeTemplate = await _mergeTemplateRepository.GetMergeTemplate(options.MergeTemplateId);

            if (mergeTemplate == null)
            {
                throw new KeyNotFoundException($"MergeTemplate with Id {options.MergeTemplateId} not found");
            }

            Task notifyProgressUpdatedTask = Task.CompletedTask;

            var result = await _mergeTemplateService.RunMergeTemplateAsync(mergeTemplate,
                                                                           progress =>
            {
                // only notify is the last one is done already.
                if (!string.IsNullOrWhiteSpace(options.ConnectionId) &&
                    notifyProgressUpdatedTask.IsCompleted)
                {
                    notifyProgressUpdatedTask =
                        NotifyMailMergeUpdatedAsync(
                            options.MergeTemplateId,
                            options.ConnectionId,
                            progress,
                            cancellationToken);
                }
            },
                                                                           cancellationToken);

            if (!string.IsNullOrWhiteSpace(options.ConnectionId))
            {
                await NotifyMailMergeCompletedAsync(options.MergeTemplateId,
                                                    options.ConnectionId,
                                                    result,
                                                    cancellationToken);
            }
            return(result);
        }
Example #3
0
        public async Task <IActionResult> Run([FromBody] RunMailMergeOptions options,
                                              CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (string.IsNullOrWhiteSpace(options.MergeTemplateId))
            {
                return(BadRequest("MergeTemplateId cannot be null"));
            }

            var mergeTemplate = await _mergeTemplateRepository.GetMergeTemplate(options.MergeTemplateId);

            if (mergeTemplate == null)
            {
                return(NotFound($"MergeTemplate with id {options.MergeTemplateId} was not found"));
            }

            Task notifyProgressUpdatedTask = Task.CompletedTask;

            var result = await _mergeTemplateService.RunMergeTemplateAsync(mergeTemplate,
                                                                           progress =>
            {
                // only notify is the last one is done already.
                if (!string.IsNullOrWhiteSpace(options.ConnectionId) &&
                    notifyProgressUpdatedTask.IsCompleted)
                {
                    notifyProgressUpdatedTask = _servicesProxy
                                                .NotifyMailMergeUpdatedAsync(
                        options.MergeTemplateId,
                        options.ConnectionId,
                        progress,
                        cancellationToken);
                }
            },
                                                                           cancellationToken);

            if (!string.IsNullOrWhiteSpace(options.ConnectionId))
            {
                await _servicesProxy
                .NotifyMailMergeCompletedAsync(options.MergeTemplateId,
                                               options.ConnectionId,
                                               result,
                                               cancellationToken);
            }

            return(Ok(result));
        }