public int CreateCustomerLoginOAuth(IDbConnection db, CustomerLoginOAuth customerLoginOauth)
        {
            string sql = @"INSERT INTO customer_login_oauth
                (service, identity, customer_username)
                VALUES
                (@service, @identity, @customer_username)
                 RETURNING id";

            return db.Query<int>(sql, customerLoginOauth).FirstOrDefault();
        }
        public object LoginOAuth([FromBody] OauthModel param)
        {
            //prepare data
            var api = Api.Instance;
            string partnerUID = param.partner_id;
            Partner partner = api.GetPartnerById(partnerUID).Data;
            string service = !string.IsNullOrEmpty(param.service) ? param.service.ToLower() : string.Empty;
            //This token is returned from login facebook
            string token = param.token;
            string oauthAccountID = string.Empty;
            FacebookProfile fbProfile = api.IDRetriever(service, token);
            oauthAccountID = fbProfile == null
                ? token
                : fbProfile.id;
            string ip_address = !string.IsNullOrEmpty(param.ip_address)
                ? param.ip_address
                : IpHelper.GetClientIp(Request);

            ErrorCodes? message = null;
            CustomerAccount user = null;
            CustomerLoginOAuth userOAuth = null;

            if (partner == null)
                message = ErrorCodes.INVALID_PARTNER_ID;
            else if (string.IsNullOrEmpty(service) && string.IsNullOrEmpty(token))
                message = ErrorCodes.MISSING_FIELDS;
            else
            {
                userOAuth = api.GetCustomerLoginOAuthByIdentity(service, oauthAccountID).Data;
                if (userOAuth == null)
                {
                    if (fbProfile != null && !string.IsNullOrEmpty(fbProfile.email))
                    {
                        var customerAccount = api.GetUserByEmail(fbProfile.email).Data;
                        if (customerAccount != null)
                        {
                            int newId = api.CreateLoginOAuth(new CustomerLoginOAuth
                            {
                                customer_username = customerAccount.username,
                                service = service,
                                identity = fbProfile.id
                            });
                            userOAuth = new CustomerLoginOAuth()
                            {
                                customer_username = customerAccount.username
                            };
                        }
                        else
                        {
                            message = ErrorCodes.NON_EXISTING_OAUTH;
                        }
                    }
                    else
                    {
                        message = ErrorCodes.NON_EXISTING_OAUTH;
                    }
                }
                else
                {
                    user = api.GetUserByUserName(userOAuth.customer_username).Data;
                    if (user != null && (string.IsNullOrEmpty(user.country_code)))
                    {
                        IPAddress ip = IPAddress.Parse(ip_address);
                        ip.GetCountryCode(c => user.country_code = c, n => user.country_name = n);
                        api.UpdateCustomerAccount(user.id, user.country_code, user.country_name, DateTime.UtcNow);
                    }
                }
            }

            //Return result
            api.LogApi("1", "/account/login-oauth", message == null,
                Request.Headers.UserAgent != null ? Request.Headers.UserAgent.ToString() : string.Empty,
                param.partner_id, user == null ? string.Empty : user.username,
                IpHelper.GetClientIp(Request), message.ToErrorCode(), JsonConvert.SerializeObject(param));

            if (message == null)
            {
                user = api.GetUserByUserName(userOAuth.customer_username).Data;
                api.UpdateCustomerAccount(user.id, user.country_code, user.country_name, DateTime.UtcNow);

                ErrorCodes? errorInReferralCampaign = api.HandleDataReferralCampaign(user.id, user.username, param.game_id, param.device_id, user.inviter_username, ip_address, partner.identifier);
                if (errorInReferralCampaign != null)
                    api.LogApi("1", "/account/login-oauth", true,
                        Request.Headers.UserAgent != null ? Request.Headers.UserAgent.ToString() : string.Empty,
                        param.partner_id, user == null ? string.Empty : user.username,
                        IpHelper.GetClientIp(Request), errorInReferralCampaign.ToErrorCode(), errorInReferralCampaign.ToErrorMessage());
            }

            return new
            {
                success = message == null,
                message = message.ToErrorMessage(),
                error_code = message.ToErrorCode(),
                session = message == null ? api.GetAccessToken(user.id, user.username, partner) : string.Empty,
                profile = message == null ? Profile.GetFrom(user) : null
            };
        }