Exemple #1
0
        public async Task <IActionResult> CreatePushNotification([ModelBinder(typeof(JsonModelBinder <PushNotificationDto>))] Delta <PushNotificationDto> pushNotificationDelta)
        {
            if (!ModelState.IsValid)
            {
                return(await Error());
            }

            var newPushNotification = new PushNotification();

            pushNotificationDelta.Merge(newPushNotification);

            await _pushNotificationService.InsertPushNotification(newPushNotification);

            //stores
            if (pushNotificationDelta.Dto.StoreIds.Count > 0)
            {
                await AddValidStores(pushNotificationDelta, newPushNotification);

                _pushNotificationService.UpdatePushNotification(newPushNotification);
            }

            await UserActivityService.InsertActivityAsync("AddNewPushNotification", $"Added a new push notification (ID = {newPushNotification.Id})", newPushNotification);

            var rootObj = new PushNotificationRootObject();

            rootObj.PushNotifications.Add(newPushNotification.ToDto());

            var json = JsonFieldsSerializer.Serialize(rootObj, string.Empty);

            return(new RawJsonActionResult(json));
        }
        public async Task <IActionResult> AddNotification(PushNotificationModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManagePushNotification))
            {
                return(AccessDeniedView());
            }

            PushNotification pushNotification = new PushNotification();

            #region Validation

            //validate stores
            var allStores = await _storeService.GetStores();

            var newStores = new List <Store>();
            foreach (var store in allStores)
            {
                if (model.SelectedStoreIds.Contains(store.P_BranchNo))
                {
                    newStores.Add(store);
                }
            }

            if (!model.SelectedNotificationCategoryIds.Any())
            {
                _notificationService.ErrorNotification("Category is required");
                model = await _pushNotificationModelFactory.PreparePushNotificationModel(model, pushNotification);

                model = GetStore(model);
                model = GetRepeat(model);
                model.SelectedNotificationCategoryIds = new List <int?>();

                return(View(model));
            }
            else if (model.SelectedNotificationCategoryIds.Count() == 1)
            {
                var catId = model.SelectedNotificationCategoryIds.FirstOrDefault();
                if (catId == null)
                {
                    _notificationService.ErrorNotification("Category is required");
                    model = await _pushNotificationModelFactory.PreparePushNotificationModel(model, pushNotification);

                    model = GetStore(model);
                    model = GetRepeat(model);
                    model.SelectedNotificationCategoryIds = new List <int?>();

                    return(View(model));
                }
            }

            if (model.SelectedNotificationCategoryIds.FirstOrDefault() == 1)
            {
                if (model.SelectedStockTake == null)
                {
                    _notificationService.ErrorNotification("Stock Take # is required");
                    model = await _pushNotificationModelFactory.PreparePushNotificationModel(model, pushNotification);

                    model             = GetStore(model);
                    model             = GetRepeat(model);
                    model.StockTakeNo = null;

                    return(View(model));
                }
                else if (model.SelectedStockTake != null && model.SelectedStockTake.Count() == 1)
                {
                    if (model.SelectedStockTake.FirstOrDefault() == null)
                    {
                        _notificationService.ErrorNotification("Stock Take # is required");
                        model = await _pushNotificationModelFactory.PreparePushNotificationModel(model, pushNotification);

                        model             = GetStore(model);
                        model             = GetRepeat(model);
                        model.StockTakeNo = null;

                        return(View(model));
                    }
                    else
                    {
                        model.StockTakeNo = model.SelectedStockTake.FirstOrDefault();
                    }
                }
                else
                {
                    model.StockTakeNo = model.SelectedStockTake.FirstOrDefault();
                }
            }
            else
            {
                model.StockTakeNo = null;

                if (model.SelectedStoreIds.Count == 0)
                {
                    _notificationService.ErrorNotification("Store is required");
                    model = await _pushNotificationModelFactory.PreparePushNotificationModel(model, pushNotification);

                    model.SelectedStoreIds = new List <int>();

                    return(View(model));
                }
            }

            #endregion

            if (ModelState.IsValid)
            {
                try
                {
                    DateTime currentTime = DateTime.Now;

                    pushNotification.Title                  = model.Title;
                    pushNotification.Desc                   = model.Description;
                    pushNotification.StockTakeNo            = model.StockTakeNo == null ? 0 : model.StockTakeNo;
                    pushNotification.NotificationCategoryId = Convert.ToInt32(model.SelectedNotificationCategoryIds.FirstOrDefault());
                    pushNotification.Interval               = model.RemindMe ? model.SelectedRepeat : null;
                    pushNotification.RemindMe               = model.RemindMe;
                    pushNotification.StartTime              = model.RemindMe ? model.StartTime : null;
                    pushNotification.EndTime                = model.RemindMe ? model.EndTime : null;

                    if (model.SelectedNotificationCategoryIds.FirstOrDefault() == 1)
                    {
                        model.SelectedStoreIds = new List <int>();
                        List <StockTakeHeader> stList = GetStockTakeStore(model.StockTakeNo);
                        foreach (var parent in stList)
                        {
                            foreach (var child in parent.Stores)
                            {
                                model.SelectedStoreIds.Add(Convert.ToInt32(child.StoreNo));
                            }
                        }
                    }

                    //stores
                    List <PushNotificationStore> pushNotificationStoreList = new List <PushNotificationStore>();
                    pushNotification.PushNotificationStores = new List <PushNotificationStore>();

                    List <PushNotificationDevice> pushNotificationDeviceList = new List <PushNotificationDevice>();
                    pushNotification.PushNotificationDevices = new List <PushNotificationDevice>();
                    int i = 0;

                    foreach (var item in model.SelectedStoreIds)
                    {
                        if (allStores.Any(x => x.P_BranchNo == item))
                        {
                            i = 0;
                            PushNotificationStore pushNotificationStore =
                                new PushNotificationStore
                            {
                                PushNotificationId = pushNotification.Id,
                                StoreId            = item
                            };
                            pushNotification.PushNotificationStores.Add(pushNotificationStore);

                            //device
                            var deviceResult = _deviceModelFactory.PrepareDeviceListbyStoreModel(item);

                            if (deviceResult.Result?.Devices?.Count > 0)
                            {
                                foreach (var devices in deviceResult.Result.Devices)
                                {
                                    i++;
                                    PushNotificationDevice pushNotificationDevice =
                                        new PushNotificationDevice
                                    {
                                        PushNotificationId = pushNotification.Id,
                                        JobName            = model.RemindMe ? "Job" + currentTime.ToString("ddMMyyyyHHmmssfff") + i : null,
                                        JobGroup           = model.RemindMe ? "Group" + currentTime.ToString("ddMMyyyyHHmmssfff") + i : null,
                                        DeviceId           = devices.Id
                                    };
                                    pushNotification.PushNotificationDevices.Add(pushNotificationDevice);
                                }
                            }
                        }
                        else if (item == -99) //All stores
                        {
                            i = 0;
                            foreach (var store in allStores)
                            {
                                PushNotificationStore pushNotificationStore =
                                    new PushNotificationStore
                                {
                                    PushNotificationId = pushNotification.Id,
                                    StoreId            = store.P_BranchNo
                                };
                                pushNotification.PushNotificationStores.Add(pushNotificationStore);
                            }
                            //device
                            var deviceResult = _deviceModelFactory.PrepareDeviceListbyStoreModel(item);

                            if (deviceResult.Result?.Devices?.Count > 0)
                            {
                                foreach (var devices in deviceResult.Result.Devices)
                                {
                                    i++;
                                    PushNotificationDevice pushNotificationDevice =
                                        new PushNotificationDevice
                                    {
                                        PushNotificationId = pushNotification.Id,
                                        JobName            = model.RemindMe ? "Job" + currentTime.ToString("ddMMyyyyHHmmssfff") + i : null,
                                        JobGroup           = model.RemindMe ? "Group" + currentTime.ToString("ddMMyyyyHHmmssfff") + i : null,
                                        DeviceId           = devices.Id
                                    };
                                    pushNotification.PushNotificationDevices.Add(pushNotificationDevice);
                                }
                            }
                        }
                    }

                    await _pushNotificationService.InsertPushNotification(pushNotification);

                    _notificationService.SuccessNotification("Push Notification has been added successfully.");

                    //selected tab
                    SaveSelectedTabName();

                    //Others
                    if (model.RemindMe)
                    {
                        string title = string.Empty;
                        if (pushNotification.StockTakeNo != 0)
                        {
                            title = model.Title + "(Stock Take #" + pushNotification.StockTakeNo + ")";
                        }
                        else
                        {
                            title = model.Title;
                        }

                        foreach (var item in pushNotification.PushNotificationDevices)
                        {
                            if (item.Device != null && !string.IsNullOrEmpty(item.Device.TokenId))
                            {
                                //why not use IScheduledTask? this is repetitive = performance.
                                Scheduler.Start(
                                    item.JobName,
                                    item.JobGroup,
                                    model.Title,
                                    model.Description,
                                    item.Device.TokenId,
                                    model.StartTime.GetValueOrDefault(),
                                    model.EndTime.GetValueOrDefault().Add(new TimeSpan(23, 59, 59)),
                                    model.SelectedRepeat,
                                    _iconfiguration["APIKey"].ToString()
                                    /*,_iconfiguration["FirebaseSenderID"].ToString()*/); //not required
                            }
                        }
                    }

                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    _notificationService.ErrorNotification(e.Message);
                }
            }

            model = await _pushNotificationModelFactory.PreparePushNotificationModel(model, pushNotification);

            return(View(model));
        }