public async Task <ServiceResponseResult> CreateAdmin(long locationId, long userId, AdministratorReqModel model) { Logger.WriteInformation("Creating admin data."); var tempPwd = Guid.NewGuid().ToString(); var user = UserMapper.ToUser(model, tempPwd, _appSettings.Secret); user = user.UpdateLastUpdatedBy(userId); _context.Add(user); await _context.SaveChangesAsync(); var userLocation = new UserLocation { CreatedBy = userId, CreatedOn = DateTime.UtcNow, LocationId = locationId, LastUpdatedBy = userId, LastUpdatedOn = DateTime.UtcNow, UserId = user.Id, State = (int)AdministratorState.Invited }; _context.Add(userLocation); await _context.SaveChangesAsync(); var response = new AdministratorResult(user, userLocation); if (model.Permissions != null) { var userPermission = UserPermissionMapper.ToUserPermission(model.Permissions); userPermission = userPermission.UpdateLastUpdatedBy(userId) .UpdateLastUpdatedOn(DateTime.UtcNow) .UpdateUserLocationId(userLocation.Id); _context.Add(userPermission); await _context.SaveChangesAsync(); response.AddPermission(userPermission); } var key = await _context.KeyHolder.FirstOrDefaultAsync(x => x.KeySerialNumber == model.KeySerialNumber); if (key != null) { var userKeyMapping = new UserKeyMapping { AppliedOn = DateTime.UtcNow, KeySerialNumber = key.KeySerialNumber, LocationId = locationId, UserId = user.Id }; userLocation.UpdateIsToolKitEnabled(true); _context.Update(userLocation); _context.Add <UserKeyMapping>(userKeyMapping); await _context.SaveChangesAsync(); response.AddToolkit(key); } var emailData = await _emailService.ConstructResetPassword(tempPwd); await _emailSender.SendMailViaSmtpClientAsync(new string[] { user.Email }, new string[] { }, new string[] { }, emailData); Logger.WriteInformation("Creating admin data completed."); return(new ServiceResponseResult { Result = response, StatusCode = System.Net.HttpStatusCode.OK }); }