private static SuccessFailCode WriteSessions(Options opts) { var client = new DocumentClient(new Uri(opts.Cosmos.Endpoint), opts.Cosmos.Key); var title = opts.QuestionVersionKey.Split('-').Last(); var questionSetRepository = new QuestionSetRepository(client, new OptionsWrapper <CosmosSettings>(opts.Cosmos)); var sessionRepository = new UserSessionRepository(client, new OptionsWrapper <CosmosSettings>(opts.Cosmos)); var questionSet = questionSetRepository.GetLatestQuestionSetByTypeAndKey("short", title) .GetAwaiter().GetResult(); using (var fs = File.OpenWrite(opts.FileName + ".csv")) using (var sw = new StreamWriter(fs)) using (var csv = new CsvWriter(sw)) { csv.WriteHeader <UserSessionEnty>(); csv.NextRecord(); for (var i = 0; i < opts.NumberOfSessions; i++) { var session = CreateSession(questionSet.QuestionSetVersion, questionSet.MaxQuestions); Console.WriteLine($"Creating User Session: {i} {session.UserSessionId}"); sessionRepository.CreateUserSession(session).GetAwaiter().GetResult(); csv.WriteRecord(new UserSessionEnty { SessionId = session.UserSessionId, UserName = "" }); csv.NextRecord(); } } return(SuccessFailCode.Succeed); }
private async Task <IActionResult> AuthoriseUser(string authToken, AuthType authType, string firstName, string lastName) { if (authType == AuthType.Unknown) { return(BadRequest()); } var metadata = await tokenUtil.GetMetadataForToken(authToken, authType).ConfigureAwait(false); if (metadata == null || string.IsNullOrWhiteSpace(metadata.AuthIdentifier)) { return(BadRequest()); } var user = await users.FetchUserForAuthIdentifier(metadata.AuthIdentifier, authType).ConfigureAwait(false); if (user.IsFailure) { return(StatusCode(500)); } if (user.Value.HasValue) { var userId = user.Value.Value.UserId; if (string.IsNullOrWhiteSpace(user.Value.Value.ExternalPaymentProcessorId)) { try { var billingId = await billingManager.CreateCustomer(user.Value.Value).ConfigureAwait(false); await users.UpdateUser(new UserPatch { ResourceId = userId, ExternalPaymentProcessorId = new PatchOperation <string> { Operation = OperationKind.Update, Value = billingId } }).ConfigureAwait(false); } catch (Exception) { return(StatusCode(500)); } } if (!string.IsNullOrWhiteSpace(firstName) || !string.IsNullOrWhiteSpace(lastName)) { await users.UpdateUser(new UserPatch { ResourceId = userId, FirstName = new PatchOperation <string> { Operation = OperationKind.Update, Value = firstName ?? "Anonymous" }, LastName = new PatchOperation <string> { Operation = OperationKind.Update, Value = lastName }, }).ConfigureAwait(false); } var existingSession = await userSessions.FetchUserSessionAndUserFromToken(authToken, (int)authType) .Ensure(s => s.HasValue, "Data found") .OnSuccess(s => s.Value) .ConfigureAwait(false); if (existingSession.IsFailure) { var session = new UserSession { AuthToken = authToken, UserId = userId, TokenType = (int)authType, AuthTokenExpiry = metadata.Expiry }; var sessionId = await userSessions.CreateUserSession(session).ConfigureAwait(false); if (sessionId.IsFailure) { return(StatusCode(500)); } } } else { var newUser = new User { IsVerified = true, WantAdvertising = false, RegistrationId = tokenUtil.GenerateToken(), FirstName = firstName ?? "Anonymous", LastName = lastName }; switch (authType) { case AuthType.Apple: newUser.AppleUserIdentifier = metadata.AuthIdentifier; break; case AuthType.Google: newUser.GoogleUserIdentifier = metadata.AuthIdentifier; break; case AuthType.Facebook: newUser.FacebookUserIdentifier = metadata.AuthIdentifier; break; case AuthType.Unknown: break; } var result = await users.CreateUser(newUser).ConfigureAwait(false); if (result.IsFailure) { return(StatusCode(500)); } var userId = result.Value.Value; try { var billingId = await billingManager.CreateCustomer(newUser).ConfigureAwait(false); await users.UpdateUser(new UserPatch { ResourceId = userId, ExternalPaymentProcessorId = new PatchOperation <string> { Operation = OperationKind.Update, Value = billingId } }).ConfigureAwait(false); } catch (Exception) { return(StatusCode(500)); } var existingSession = await userSessions.FetchUserSessionAndUserFromToken(authToken, (int)authType) .Ensure(s => s.HasValue, "Data found") .OnSuccess(s => s.Value) .ConfigureAwait(false); if (existingSession.IsFailure) { var session = new UserSession { AuthToken = authToken, UserId = userId, TokenType = (int)authType, AuthTokenExpiry = metadata.Expiry }; var sessionId = await userSessions.CreateUserSession(session).ConfigureAwait(false); if (sessionId.IsFailure) { return(StatusCode(500)); } } } return(Ok()); }