public async Task CreateAndSave(LivechatUser livechatUser) { using (var db = sqlFactory.GetNewConnection()) { string insertStatement; if (livechatUser.CustomerId.HasValue) { insertStatement = $"insert {nameof(LivechatUser)}(Id, CustomerId, Name, Email, Phone) values (@Id, @CustomerId, @Name, @Email, @Phone)"; } else { insertStatement = $"insert {nameof(LivechatUser)}(Id, Name, Email, Phone) values (@Id, @Name, @Email, @Phone)"; } await db.ExecuteAsync( insertStatement, new { Id = livechatUser.Id, CustomerId = livechatUser.CustomerId, Name = livechatUser.Name, Email = livechatUser.Email, Phone = livechatUser.Phone } ); } }
public async Task <LivechatChannel> CreateChannel(int storeId, LivechatUser livechatUser) { using (var db = sqlFactory.GetNewConnection()) { var newChannel = new LivechatChannel { Name = livechatUser.Name, StoreId = storeId }; // Creates a new channel and add user into it await db.ExecuteAsync( $"insert {nameof(LivechatChannel)}(Id, Name, StoreId, StoreName) values (@Id, @Name, @StoreId, @StoreName);" + $"insert {nameof(LivechatChannelUser)}(Id, LivechatUserId, LivechatChannelId) values (@IdCUser, @LivechatUserId, @LivechatChannelId)", new { Id = newChannel.Id, Name = newChannel.Name, StoreId = newChannel.StoreId, StoreName = "Sem Alias", IdCUser = Guid.NewGuid().ToString(), LivechatUserId = livechatUser.Id, LivechatChannelId = newChannel.Id }); return(newChannel); } }
public async Task <IActionResult> Login([FromBody] LoginUserModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check in the NopCommerce if user exists var user = await livechatRules.FindUserByPassword(model); if (user == null) { await logRepository.RecAuthLog($"Invalid chat login attempt using {model.Email}."); return(Unauthorized()); } var hasPermission = await livechatRules.UserHasLivechatPermission(user.Id); if (!hasPermission) { await logRepository.RecAuthLog($"User {model.Email} doesn't have access to this chat as agent."); return(Unauthorized()); } // Check if user exists inside LivechatUser table var livechatUser = await livechatRules.FindLivechatUserByCustomerId(user.Id); // When not found, creates a new register inside LivechatUser table if (livechatUser == null) { // Get customer's name var customerName = await livechatRules.GetCustomerNameFromNopAttributes(user.Id); var newToken = GenerateToken(customerName); await livechatRules.CreateAndSave(new LivechatUser { Id = newToken, CustomerId = user.Id, Email = user.Email, Name = customerName, Phone = "" }); // Update livechatUser to its new attributes livechatUser = new LivechatUser { Id = newToken, Name = customerName }; } var authorizedStores = await livechatRules.GetStoresByCustomerId(user.Id); if (authorizedStores.Count() == 0) { await logRepository.RecAuthLog($"User {model.Email} is registered at any store."); return(Unauthorized()); } return(SetClaimAndSignIn ( livechatUser.Name, user.Email, "LivechatAgent", livechatUser.Id, authorizedStores.ToArray(), customerId: user.Id, device: model.Device, version: model.Version )); }