Пример #1
0
 public IHttpActionResult GetEmployeesToBeLoggedIN(DateTime date)
 {
     try
     {
         using (MaxMasterDbEntities db = new MaxMasterDbEntities())
         {
             var employees = db.GetEmployeesToBeLogin(date).ToList();
             return(Content(HttpStatusCode.OK, new { employees }));
         }
     }
     catch (Exception ex)
     {
         new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace);
         return(Content(HttpStatusCode.InternalServerError, "An error occured, please try again later"));
     }
 }
Пример #2
0
        public IHttpActionResult SendAttendanceNotification()
        {
            try
            {
                using (MaxMasterDbEntities db = new MaxMasterDbEntities())
                {
                    var form    = HttpContext.Current.Request.Form;
                    var title   = form.Get("notificationTitle");
                    var message = form.Get("message");
                    //get all employees (working)

                    DateTime  date     = DateTime.Now;
                    var       dayToday = " " + date.ToString("d");
                    DayOfWeek day      = DateTime.Now.DayOfWeek;
                    dayToday = " " + day.ToString();

                    if ((dayToday != "Sunday"))
                    {
                        var employees = db.GetEmployeesToBeLogin(date).ToList();

                        foreach (var emp   in employees)
                        {
                            var employeeInLeave = db.LeaveRecords.Where(x => x.LeaveDate == DateTime.Now.Date && x.EmpId == emp.AspNetUserId).FirstOrDefault();
                            var employee        = db.Employees.Where(x => x.AspNetUserId == emp.AspNetUserId).FirstOrDefault();
                            if (employee == null || employee.NotificationToken == null || employeeInLeave != null)
                            {
                                continue;
                            }

                            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                            tRequest.Method = "post";
                            tRequest.Headers.Add(string.Format("Authorization: key={0}", "AIzaSyAqm4FRdcn7b-rn2jRfy-2EwTyFUFPzIKQ"));
                            tRequest.ContentType = "application/json";
                            var payload = new
                            {
                                to               = employee.NotificationToken,
                                time_to_live     = 200,
                                collapse_key     = "test_type_b",
                                delay_while_idle = false,
                                notification     = new { },
                                data             = new
                                {
                                    subText          = "Attendance Reminder",
                                    title            = "Attendance Reminder",
                                    message          = "Please Clock In to TMS",
                                    bigText          = "Please Clock In to TMS, if clocked in or holiday please ignore",
                                    color            = "#3F81C5",
                                    notificationType = "Attendance"
                                }
                            };

                            string postbody  = JsonConvert.SerializeObject(payload).ToString();
                            Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
                            tRequest.ContentLength = byteArray.Length;
                            using (Stream dataStream = tRequest.GetRequestStream())
                            {
                                dataStream.Write(byteArray, 0, byteArray.Length);
                                tRequest.GetResponse();
                            }
                        }
                    }
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace);
                return(Content(HttpStatusCode.InternalServerError, "An error occured, please try again later"));
            }
        }