Exemplo n.º 1
0
        public async Task <ActionResult <OrdersSynchronizationPushNotification> > SynchronizeOrders([FromBody] OrdersSynchronizationRequest request)
        {
            var notification = await Enqueue(request);

            await _pushNotificationManager.SendAsync(notification);

            return(Ok(notification));
        }
Exemplo n.º 2
0
        public async Task SampleDataImportBackgroundAsync(Uri url, SampleDataImportPushNotification pushNotification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            void progressCallback(ExportImportProgressInfo x)
            {
                pushNotification.Path(x);
                pushNotification.JobId = context.BackgroundJob.Id;
                _pushNotifier.Send(pushNotification);
            }

            try
            {
                pushNotification.Description = "Start downloading from " + url;

                await _pushNotifier.SendAsync(pushNotification);

                var tmpPath = Path.GetFullPath(_platformOptions.LocalUploadFolderPath);
                if (!Directory.Exists(tmpPath))
                {
                    Directory.CreateDirectory(tmpPath);
                }

                var tmpFilePath = Path.Combine(tmpPath, Path.GetFileName(url.ToString()));
                using (var client = new WebClient())
                {
                    client.DownloadProgressChanged += async(sender, args) =>
                    {
                        pushNotification.Description = string.Format("Sample data {0} of {1} downloading...", args.BytesReceived.ToHumanReadableSize(), args.TotalBytesToReceive.ToHumanReadableSize());
                        await _pushNotifier.SendAsync(pushNotification);
                    };
                    var task = client.DownloadFileTaskAsync(url, tmpFilePath);
                    task.Wait();
                }
                using (var stream = new FileStream(tmpFilePath, FileMode.Open))
                {
                    var manifest = _platformExportManager.ReadExportManifest(stream);
                    if (manifest != null)
                    {
                        await _platformExportManager.ImportAsync(stream, manifest, progressCallback, new JobCancellationTokenWrapper(cancellationToken));
                    }
                }
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                _settingsManager.SetValue(PlatformConstants.Settings.Setup.SampleDataState.Name, SampleDataState.Completed);
                pushNotification.Description = "Import finished";
                pushNotification.Finished    = DateTime.UtcNow;
                await _pushNotifier.SendAsync(pushNotification);
            }
        }
        public async Task <ActionResult <ImportNotification> > ImportCouponsAsync([FromBody] ImportRequest request)
        {
            var notification = new ImportNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Import coupons from CSV",
                Description = "Starting import..."
            };

            await _notifier.SendAsync(notification);

            BackgroundJob.Enqueue(() => BackgroundImportAsync(request, notification));

            return(Ok(notification));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> DownloadSitemap(string storeId, string baseUrl)
        {
            var notification = new SitemapDownloadNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Download sitemaps",
                Description = "Processing download sitemaps..."
            };

            await _notifier.SendAsync(notification);

            BackgroundJob.Enqueue(() => BackgroundDownload(storeId, baseUrl, notification));

            return(Ok(notification));
        }
        public async Task ExportBackgroundAsync(ExportDataRequest request, ExportPushNotification pushNotification, IJobCancellationToken jobCancellationToken, PerformContext context)
        {
            ValidateParameters(pushNotification);

            try
            {
                await _customerDataExporter.ExportAsync(request,
                                                        progressInfo => ProgressCallback(progressInfo, pushNotification, context),
                                                        new JobCancellationTokenWrapper(jobCancellationToken));
            }
            catch (JobAbortedException)
            {
                // job is aborted, do nothing
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                pushNotification.Description = "Export finished";
                pushNotification.Finished    = DateTime.UtcNow;

                await _pushNotificationManager.SendAsync(pushNotification);
            }
        }
Exemplo n.º 6
0
        public async Task <ActionResult <ImportPushNotification> > RunImport([FromBody] ImportDataRequest request)
        {
            var notification = new ImportPushNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Prices import",
                Description = "Starting import task..."
            };
            await _pushNotificationManager.SendAsync(notification);

            notification.JobId = BackgroundJob.Enqueue <ImportJob>(importJob => importJob.ImportBackgroundAsync(request, notification, JobCancellationToken.Null, null));

            return(Ok(notification));
        }
Exemplo n.º 7
0
        public async Task ExportBackgroundAsync(ExportDataRequest request, ExportPushNotification notification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            void progressCallback(ExportProgressInfo x)
            {
                notification.Patch(x);
                notification.JobId = context.BackgroundJob.Id;
                _pushNotificationManager.Send(notification);
            }

            try
            {
                if (string.IsNullOrEmpty(_platformOptions.DefaultExportFolder))
                {
                    throw new PlatformException($"{nameof(_platformOptions.DefaultExportFolder)} should be set.");
                }

                var fileName = string.Format(FileNameTemplate, DateTime.UtcNow);

                // Do not like provider creation here to get file extension, maybe need to pass created provider to Exporter.
                // Create stream inside Exporter is not good as it is not Exporter resposibility to decide where to write.
                var provider = _exportProviderFactory.CreateProvider(request);

                if (!string.IsNullOrEmpty(provider.ExportedFileExtension))
                {
                    fileName = Path.ChangeExtension(fileName, provider.ExportedFileExtension);
                }

                var url = UrlHelperExtensions.Combine(_platformOptions.DefaultExportFolder, fileName);
                using (var blobStream = _blobStorageProvider.OpenWrite(url))
                {
                    _dataExporter.Export(blobStream, request, progressCallback, new JobCancellationTokenWrapper(cancellationToken));
                }

                notification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(url);
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                notification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                notification.Description = "Export finished";
                notification.Finished    = DateTime.UtcNow;
                await _pushNotificationManager.SendAsync(notification);
            }
        }
Exemplo n.º 8
0
        public async Task <IActionResult> MarkAllAsRead()
        {
            var criteria = new PushNotificationSearchCriteria {
                OnlyNew = true, Skip = 0, Take = int.MaxValue
            };
            var retVal = _pushNotifier.SearchNotifies(User.Identity.Name, criteria);

            foreach (var notifyEvent in retVal.NotifyEvents)
            {
                notifyEvent.IsNew = false;
                await _pushNotifier.SendAsync(notifyEvent);
            }

            return(Ok(retVal));
        }
Exemplo n.º 9
0
        public async Task <ActionResult <ExportNotification> > DoExport([FromBody] CsvExportInfo exportInfo)
        {
            var hasPermissions = true;

            if (!exportInfo.ProductIds.IsNullOrEmpty())
            {
                var items = await _itemService.GetByIdsAsync(exportInfo.ProductIds, ItemResponseGroup.ItemInfo.ToString());

                hasPermissions = await CheckCatalogPermission(items, CatalogModuleConstants.Security.Permissions.Read);
            }

            if (hasPermissions && !exportInfo.CategoryIds.IsNullOrEmpty())
            {
                var categories = await _categoryService.GetByIdsAsync(exportInfo.CategoryIds, CategoryResponseGroup.Info.ToString());

                hasPermissions = await CheckCatalogPermission(categories, CatalogModuleConstants.Security.Permissions.Read);
            }

            if (hasPermissions && !exportInfo.CatalogId.IsNullOrEmpty())
            {
                var catalogs = await _catalogService.GetByIdsAsync(new[] { exportInfo.CatalogId }, CategoryResponseGroup.Info.ToString());

                if (!catalogs.IsNullOrEmpty())
                {
                    hasPermissions = await CheckCatalogPermission(catalogs.First(), CatalogModuleConstants.Security.Permissions.Read);
                }
            }

            if (!hasPermissions)
            {
                return(Unauthorized());
            }

            var notification = new ExportNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Catalog export task",
                Description = "starting export...."
            };
            await _notifier.SendAsync(notification);


            BackgroundJob.Enqueue(() => BackgroundExport(exportInfo, notification));

            return(Ok(notification));
        }