Ejemplo n.º 1
0
        //Send my actions push notifications
        public static async Task <EbNFResponse> SendMobileNotification(EbWebForm _this, EbConnectionFactory ConnFactory, Service service)
        {
            EbNFResponse resp = new EbNFResponse("0");

            try
            {
                if (_this.MyActNotification?.SendPushNotification == true && ConnFactory.MobileAppConnection != null)
                {
                    List <int> userIds = new List <int>();
                    if (_this.MyActNotification.ApproverEntity == ApproverEntityTypes.Users)
                    {
                        userIds = _this.MyActNotification.UserIds;
                    }
                    else if (_this.MyActNotification.ApproverEntity == ApproverEntityTypes.Role ||
                             _this.MyActNotification.ApproverEntity == ApproverEntityTypes.DynamicRole ||
                             _this.MyActNotification.ApproverEntity == ApproverEntityTypes.UserGroup)
                    {
                        string Qry;
                        if (_this.MyActNotification.ApproverEntity == ApproverEntityTypes.UserGroup)
                        {
                            Qry = $"SELECT userid FROM eb_user2usergroup WHERE groupid = {_this.MyActNotification.UserGroupId} AND COALESCE(eb_del, 'F') = 'F'; ";
                        }
                        else// static/dynamic role
                        {
                            Qry = $"SELECT user_id FROM eb_role2user WHERE role_id = ANY(STRING_TO_ARRAY('{_this.MyActNotification.RoleIds.Join(",")}'::TEXT, ',')::INT[]) AND COALESCE(eb_del, 'F') = 'F'; ";
                        }

                        EbDataTable dt = ConnFactory.DataDB.DoQuery(Qry);
                        foreach (EbDataRow dr in dt.Rows)
                        {
                            int.TryParse(dr[0].ToString(), out int temp);
                            if (!userIds.Contains(temp))
                            {
                                userIds.Add(temp);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid approver entity: " + _this.MyActNotification.ApproverEntity);
                    }

                    if (userIds.Count == 0)
                    {
                        throw new Exception("User Id collection is empty");
                    }

                    List <string> userAuthIds = new List <string>();
                    EbNFData      Data        = new EbNFData()
                    {
                        Title   = _this.MyActNotification.Title,
                        Message = _this.MyActNotification.Description,
                        Link    = new EbNFLink()
                        {
                            LinkType = EbNFLinkTypes.Action,
                            ActionId = _this.MyActNotification.MyActionId
                        }
                    };

                    EbAzureNFClient client = EbAzureNFClient.Create(ConnFactory.MobileAppConnection);
                    foreach (int uid in userIds)
                    {
                        string authId = _this.SolutionObj.SolutionID + CharConstants.COLON + uid + CharConstants.COLON + TokenConstants.MC;
                        User   user   = service.Redis.Get <User>(authId);
                        if (user != null)
                        {
                            if (user.LocationIds.Contains(-1) || user.LocationIds.Contains(_this.LocationId))
                            {
                                userAuthIds.Add(client.ConvertToAuthTag(authId));
                            }
                        }
                    }
                    if (userAuthIds.Count > 0)
                    {
                        EbNFRequest req = new EbNFRequest()
                        {
                            Platform = PNSPlatforms.GCM,
                            Tags     = userAuthIds
                        };
                        req.SetPayload(new EbNFDataTemplateAndroid()
                        {
                            Data = Data
                        });
                        resp = await client.Send(req);
                    }
                }
            }
            catch (Exception ex)
            {
                resp.Message = ex.Message;
                Console.WriteLine("Exception in SendMobileNotification: " + ex.Message);
            }
            Console.WriteLine("SendMobileNotification response: " + resp.Message);
            return(resp);
        }
Ejemplo n.º 2
0
        public override void SendNotification(EbWebForm _this, EbConnectionFactory ConnFactory, Service service, FG_Root globals, ref int resp)
        {
            if (ConnFactory.MobileAppConnection == null)
            {
                return;
            }

            List <int> uids = new List <int>();

            if (this.IsDirectNotification)
            {
                uids.Add(this.NotifyUserId);
            }
            else
            {
                string        Qry = null;
                DbParameter[] _p  = null;
                if (this.NotifyBy == EbFnSys_NotifyBy.Roles)
                {
                    Qry = $"SELECT user_id FROM eb_role2user WHERE role_id = ANY(STRING_TO_ARRAY('{this.Roles.Join(",")}'::TEXT, ',')::INT[]) AND COALESCE(eb_del, 'F') = 'F'; ";
                }
                else if (this.NotifyBy == EbFnSys_NotifyBy.UserGroup)
                {
                    Qry = $"SELECT userid FROM eb_user2usergroup WHERE groupid = {this.UserGroup} AND COALESCE(eb_del, 'F') = 'F'; ";
                }
                else if (this.NotifyBy == EbFnSys_NotifyBy.Users)
                {
                    Qry = this.Users.Code;
                    _p  = this.GetParameters(_this, ConnFactory.DataDB, this.QryParams);
                }

                if (!string.IsNullOrWhiteSpace(Qry))
                {
                    EbDataTable dt;
                    if (_p == null)
                    {
                        dt = ConnFactory.DataDB.DoQuery(Qry);
                    }
                    else
                    {
                        dt = ConnFactory.DataDB.DoQuery(Qry, _p);
                    }
                    foreach (EbDataRow dr in dt.Rows)
                    {
                        int.TryParse(dr[0].ToString(), out int temp);
                        if (!uids.Contains(temp))
                        {
                            uids.Add(temp);
                        }
                    }
                    if (uids.Count == 0)
                    {
                        return;
                    }

                    this.ProcessedMsgTitle = _this.DisplayName;
                    if (!string.IsNullOrEmpty(this.MessageTitle?.Code))
                    {
                        object msg = _this.ExecuteCSharpScriptNew(this.MessageTitle.Code, globals);
                        this.ProcessedMsgTitle = msg.ToString();
                    }
                    this.ProcessedMessage = string.Empty;
                    if (!string.IsNullOrEmpty(this.Message?.Code))
                    {
                        object msg = _this.ExecuteCSharpScriptNew(this.Message.Code, globals);
                        this.ProcessedMessage = msg.ToString();
                    }
                }
            }

            if (uids.Count > 0)
            {
                List <string> userAuthIds = new List <string>();
                EbNFData      Data        = new EbNFData()
                {
                    Title   = this.ProcessedMsgTitle,
                    Message = this.ProcessedMessage
                };

                EbAzureNFClient client = EbAzureNFClient.Create(ConnFactory.MobileAppConnection);
                foreach (int uid in uids)
                {
                    userAuthIds.Add(client.ConvertToAuthTag(_this.SolutionObj.SolutionID + CharConstants.COLON + uid + CharConstants.COLON + TokenConstants.MC));
                }

                EbNFRequest req = new EbNFRequest()
                {
                    Platform = PNSPlatforms.GCM,
                    Tags     = userAuthIds
                };
                req.SetPayload(new EbNFDataTemplateAndroid()
                {
                    Data = Data
                });

                try
                {
                    client.Send(req);
                }
                catch (Exception ex)
                {
                    string temp = $"Exception when tried to send EbFnMobile\n Message: ${ex.Message} \nStackTrace: ${ex.StackTrace}";
                    throw new FormException($"Unable to process notification.", (int)HttpStatusCode.InternalServerError, ex.Message, temp);
                }
                resp++;
            }
        }