Beispiel #1
0
        public async Task <IActionResult> AssignRecordToDriver(RecordAssignInput input)
        {
            Record record = await _db.Records.FirstOrDefaultAsync(x => x.RecordGuid == input.RecordId);

            record.DriverId = input.DriverId;
            long userId = _db.Drivers.FirstOrDefault(y => y.Id == input.DriverId).UserId;

            _db.Entry(record).State = EntityState.Modified;

            var result = await _db.SaveChangesAsync();

            if (result == 1)
            {
                PushNotificationInput pushNotificationInput = new PushNotificationInput
                {
                    UserId = userId,
                    title  = "كشف جديد",
                    body   = "تم تعيين كشف جديد لك",
                };
                await _pushNotificationService.PushNotification(pushNotificationInput);

                return(new OkObjectResult(1));
            }
            throw new Exception("حصل خطأ");
        }
        public async Task <bool> PushNotification(PushNotificationInput input)
        {
            try
            {
                ApplicationUser userDeviceToken = _userManager.Users.FirstOrDefault(x => x.Id == input.UserId);
                if (userDeviceToken != null)
                {
                    if (!string.IsNullOrEmpty(userDeviceToken.RegId))
                    {
                        var message = new FirebaseAdmin.Messaging.Message()
                        {
                            Notification = new FirebaseAdmin.Messaging.Notification()
                            {
                                Title = input.title,
                                Body  = input.body,
                            },
                            Token = userDeviceToken.RegId,
                        };

                        string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
                    }
                }
                return(true);
            }
            catch (Exception exc)
            {
                return(false);
            }
        }
 public async Task <BaseResponse> SendNotificationByRole(PushNotificationInput input)
 {
     try
     {
         var allusers = _userManager.GetUsersInRoleAsync(input.role).Result.Select(x => x.Id).ToList();
         foreach (var user in allusers)
         {
             input.UserId = user;
             await PushNotification(input);
         }
         return(new BaseResponse
         {
             Code = RsponseCode.Success,
             Message = "تم إرسال الإشعارات بنجاح"
         });
     }
     catch (Exception exc)
     {
         return(new BaseResponse
         {
             Code = RsponseCode.DataBaseError,
             Message = "حصل خطأ"
         });
     }
 }
Beispiel #4
0
        public async Task <IActionResult> SendPushNotification([FromBody] PushNotificationInput input)
        {
            try
            {
                var result = await _pushNotificationService.PushNotification(input);

                return(Ok(result));
            }
            catch (Exception)
            {
                return(BadRequest("حصل خطأ"));
            }
        }
Beispiel #5
0
        public IActionResult Create(OrderReportReplaysCreate model)
        {
            long        userId   = _principalService.GetUserId();
            long        clientId = _db.Clients.FirstOrDefault(x => x.UserId == userId).Id;
            OrderReport report   = _db.OrderReports.Where(x => x.Id == model.OrderReportId)
                                   .Include(x => x.CreatedBy)
                                   .ThenInclude(y => y.User)
                                   .Include(x => x.Order)
                                   .ThenInclude(x => x.Record)
                                   .FirstOrDefault();
            Driver driver = _db.Drivers.FirstOrDefault(x => x.Id == report.Order.Record.DriverId);

            OrderReportReplaye orderReportReplay = new OrderReportReplaye
            {
                IsActive        = true,
                CreatedById     = clientId,
                CreatedAt       = DateTime.Now,
                ClientComment   = model.ClientComments,
                DriverLatitude  = (double)report.CreatedBy.User.Latitude,
                DriverLongitude = (double)report.CreatedBy.User.Longitude,
                OrderReportId   = model.OrderReportId
            };

            _db.OrderReportReplayes.Add(orderReportReplay);
            _db.SaveChanges();
            PushNotificationInput pushNotificationInput = new PushNotificationInput
            {
                UserId = driver.UserId,
                title  = "رد على البلاغ ",
                body   = "تم الرد على البلاغ",
                data   = new
                {
                    orderId = report.OrderId,
                }
            };

            _pushNotificationService.PushNotification(pushNotificationInput);
            DataBase.Entities.Notification notification = new DataBase.Entities.Notification
            {
                Title            = "رد على بلاغ",
                Body             = "تم تقديم رد على بلاغ على طلب رقم " + report.OrderId + " رقم الهاتف : " + report.Order.DeliveryPhoneNumber + "نص الرد : " + model.ClientComments,
                Target           = NotificationTarget.Tracker,
                NotificationGuid = Guid.NewGuid()
            };
            _db.Notifications.Add(notification);
            _db.SaveChanges();
            return(new OkObjectResult(true));
        }
        public OrderReport Create(OrderReportCreate model)
        {
            long userId = _principalService.GetUserId();
            //ApplicationUser user = _userManager.Users.Where(x => x.Id == userId).FirstOrDefault();
            long  driverId = _db.Drivers.FirstOrDefault(x => x.UserId == userId).Id;
            Order order    = _db.Orders.Where(x => x.Id == model.OrderId)
                             .Include(x => x.Client).ThenInclude(x => x.User).FirstOrDefault();
            //Order number not order guid
            OrderReport orderReport = new OrderReport
            {
                IsActive        = true,
                CreatedById     = driverId,
                CreatedAt       = DateTime.Now,
                DriverComments  = model.DriverComments,
                Latitude        = model.Latitude,
                Longitude       = model.Longitude,
                OrderId         = model.OrderId,
                OrderReportGuid = Guid.NewGuid()
            };

            _db.OrderReports.Add(orderReport);
            _db.SaveChanges();
            if (order != null)
            {
                PushNotificationInput pushNotificationInput = new PushNotificationInput
                {
                    UserId = order.Client.User.Id,
                    title  = "بلاغ جديد",
                    body   = "تم تقديم بلاغ على طلبك",
                    data   = new
                    {
                        orderId = orderReport.OrderId,
                    }
                };
                _pushNotificationService.PushNotification(pushNotificationInput);
                DataBase.Entities.Notification notification = new DataBase.Entities.Notification
                {
                    Title            = "بلاغ جديد",
                    Body             = "تم تقديم بلاغ على طلب رقم " + order.Id + " رقم الهاتف : " + order.DeliveryPhoneNumber + "نص البلاغ : " + orderReport.DriverComments,
                    Target           = NotificationTarget.Tracker,
                    NotificationGuid = Guid.NewGuid()
                };
                _db.Notifications.Add(notification);
                _db.SaveChanges();
            }

            return(orderReport);
        }
        public async Task <Result> SendPushNotificationAsync(PushNotificationInput input)
        {
            if (string.IsNullOrWhiteSpace(_settings.OneSignal.ApiKey) || string.IsNullOrWhiteSpace(_settings.OneSignal.AppId))
            {
                return(Result.Fail("Required settings not found"));
            }

            try
            {
                var currentUser = await _sessionAppService.GetCurrentLoginInformations();

                if (currentUser != null)
                {
                    var pushData = new
                    {
                        app_id   = _settings.OneSignal.AppId,
                        headings = new { en = input.Headings },
                        contents = new { en = $"{input.Contents}" },
                        data     = input.Data,
                        filters  = new object[] { new { field = "tag", key = "tenancyName", value = currentUser.Tenant.TenancyName.Trim().ToLowerInvariant(), relation = "=" } }
                    };

                    var result = await "https://onesignal.com/api/v1/notifications"
                                 .WithHeader("Authorization", $"Basic {_settings.OneSignal.ApiKey}")
                                 .PostJsonAsync(pushData);

                    if (result.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return(Result.Ok());
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO: log
            }

            return(Result.Fail("Something went wrong. Failed to sent push notification"));
        }
Beispiel #8
0
        public async Task <IActionResult> SendPushNotificationAllUsers([FromBody] PushNotificationInput input)
        {
            var result = await _pushNotificationService.SendNotificationAllUsers(input);

            return(Ok(result));
        }
        public Order Update(OrderUpdate model)
        {
            Order order = _db.Orders
                          .Include(c => c.Record)
                          .Include(c => c.Client)
                          .Include(a => a.Area)
                          .ThenInclude(a => a.AreaGroup)
                          .Where(x => x.Id == model.Id).FirstOrDefault();
            long userId = _principalService.GetUserId();

            order.Address                  = model.Address;
            order.AreaId                   = model.AreaId;
            order.ClientId                 = model.ClientId;
            order.ClientStatus             = model.ClientStatus;
            order.DeliveryPhoneNumber      = model.DeliveryNumber;
            order.OrderItemTypeDescription = model.OrderItemDescription;
            order.OrderStatusId            = model.OrderStatusId;
            order.OrderTotalPrice          = model.OrderTotalPrice;
            order.PolicyNumber             = model.PolicyNumber;
            order.OrderDate                = model.OrderDate;
            order.AddedPrice               = model.AddedPrice;
            CalculateRevenueForOrder(order);
            Record record = _db.Records.FirstOrDefault(x => x.AreaGroupId == order.Area.AreaGroupId && x.RecordDate == model.OrderDate);

            if (record != null)
            {
                order.RecordId = record.Id;
            }
            else
            {
                Record newRecord = new Record
                {
                    AreaGroupId = order.Area.AreaGroupId,
                    CreatedAt   = DateTime.Now,
                    CreatedById = userId,
                    RecordDate  = order.OrderDate,
                    RecordGuid  = Guid.NewGuid(),
                    IsActive    = true
                };
                _db.Records.Add(newRecord);
                _db.SaveChanges();
                order.RecordId = newRecord.Id;
            }
            _db.Entry(order).State = EntityState.Modified;
            _db.SaveChanges();

            if (order.OrderStatusId == (int)OrderStatusEnum.Completed)
            {
                PushNotificationInput pushNotificationInput = new PushNotificationInput();
                pushNotificationInput.UserId = order.Client.UserId;
                pushNotificationInput.title  = "تم توصيل طلبك ";
                pushNotificationInput.body   = "تم توصيل طلبك بنجاح";
                pushNotificationInput.data   = new
                {
                    orderId = order.Id
                };


                _pushNotificationService.PushNotification(pushNotificationInput);
            }
            return(order);
        }