Example #1
0
        public async Task <SocialAccount> AddFacebookPageAsync(SocialAccount socialAccount, string originalId)
        {
            if (IsDupliated(SocialUserSource.Facebook, originalId))
            {
                throw SocialExceptions.BadRequest($"'{socialAccount.SocialUser.Name}' has already been added.");
            }

            await _fbClient.SubscribeApp(originalId, socialAccount.Token);

            var socialUser = _socialUserRepo.FindAll()
                             .Where(t => t.OriginalId == originalId && t.Source == SocialUserSource.Facebook)
                             .OrderByDescending(t => t.Id)
                             .FirstOrDefault();

            if (socialUser == null)
            {
                // create a new integraton account.
                await this.InsertAsync(socialAccount);
            }
            else
            {
                // if the user was a customer or a deleted integration account.
                socialUser.Type          = SocialUserType.IntegrationAccount; // make sure convert a customer to integration account.
                socialUser.IsDeleted     = false;                             // make sure to restore a deleted integration account.
                socialUser.Avatar        = socialAccount.SocialUser.Avatar;
                socialUser.Email         = socialAccount.SocialUser.Email;
                socialUser.OriginalLink  = socialAccount.SocialUser.OriginalLink;
                socialAccount.Id         = socialUser.Id;
                socialAccount.SocialUser = socialUser;
                if (socialUser.SocialAccount == null)
                {
                    socialUser.SocialAccount = socialAccount;
                }
                else
                {
                    socialUser.SocialAccount.Token = socialAccount.Token;
                    socialUser.SocialAccount.FacebookPageCategory = socialAccount.FacebookPageCategory;
                    socialUser.SocialAccount.FacebookSignInAs     = socialAccount.FacebookSignInAs;
                }
                socialUser.SocialAccount.IsDeleted = false;
                socialUser.SocialAccount.IfEnable  = true;
                socialUser.SocialAccount.IfConvertMessageToConversation     = true;
                socialUser.SocialAccount.IfConvertVisitorPostToConversation = true;
                socialUser.SocialAccount.IfConvertWallPostToConversation    = true;
                _socialUserRepo.Update(socialUser);

                await this.InsertSocialAccountInGeneralDb(SocialUserSource.Facebook, originalId);
            }

            CurrentUnitOfWork.SaveChanges();
            return(this.Find(socialAccount.Id));
        }