public async Task<ActionResult> InitiateVolunteerFlow([FromBody] InitiateVolunteerModel model) { // record volunteer details using (var session = AsyncSessionFactory.Create(Guid.Empty, OrgId)) { var volunteer = new Volunteer { FirstName = model.FirstName, LastName = model.LastName }; session.Store(volunteer); await session.SaveChangesAsync(); return Json(volunteer); } }
public static void CreateOrganization(IApplicationBuilder app) { app.Run(async context => { if (!context.Request.Headers.ContainsKey("Authorization") || context.Request.Headers["Authorization"] != "DG 12345" || context.Request.Method != "POST") { context.Response.StatusCode = 404; return; } var store = app.ApplicationServices.GetRequiredService<IDocumentStore>(); JObject requestJson; using (var reader = new StreamReader(context.Request.Body)) { requestJson = JObject.Parse(await reader.ReadToEndAsync()); } var slug = (string)requestJson["slug"]; var name = (string)requestJson["name"]; var websiteUrl = (string)requestJson["websiteUrl"]; var theme = (string)requestJson["theme"]; Guid orgId; using (ProfileSession.Current.Step("database")) using (var session = store.LightweightSession()) { if ((await session.Query<Organization>() .CountAsync(x => x.Slug == slug)) > 0) { context.Response.StatusCode = 500; await context.Response.WriteAsync("Organization already exists"); return; } var o = new Organization { Slug = slug, Name = name, WebsiteUrl = websiteUrl, Theme = theme, CreatedUtc = new DateTime(2015, 01, 01), CreatedBy = Guid.Empty, LastModifiedUtc = DateTime.UtcNow, LastModifiedBy = Guid.Empty }; // this populates the Id column session.Store(o); orgId = o.Id; await session.SaveChangesAsync(); } var now = DateTime.UtcNow; using (var session = AsyncSessionFactory.Create(Guid.Empty, orgId)) { session.StoreObjects( new User { EmailAddress = "*****@*****.**", DisplayName = "Michael Third", Role = "User" }, new User { EmailAddress = "*****@*****.**", DisplayName = "Sarah Kidd", Role = "User" }, new User { EmailAddress = "*****@*****.**", DisplayName = "David Sweat", Role = "User" } ); session.StoreObjects( new Campaign { Name = "General Donation", Slug = "general", Goal = 250000 }, new Campaign { Name = "Back-to-School Blast", Slug = "backtoschool", Goal = 16000 }, new Campaign { Name = "Summer Camp", Slug = "summercamp", Goal = 20000 }, new Campaign { Name = "Thanksgiving Meals", Slug = "thanksgiving", Goal = 30000 }, new Campaign { Name = "Toys for Tots", Slug = "toysfortots", Goal = 15000 } ); session.StoreObjects( new Event { Slug = "event-a", Name = "Event A", Registration = new Event.RegistrationDetails { StartDateUtc = DateTime.UtcNow } }, new Event { OrganizationId = orgId, Slug = "event-b", Name = "Event B" } ); var random = new Random(); for (int i = random.Next(40, 400); i > 0; i--) { var donor = new Donor { FirstName = FirstNames[random.Next() % FirstNames.Length], LastName = LastNames[random.Next() % LastNames.Length], CreatedUtc = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(random.Next(86400, 3 * 31536000))) }; session.Store(donor); } await session.SaveChangesAsync(); for (int i = random.Next(40, 250); i > 0; i--) { var volunteer = new Volunteer { FirstName = FirstNames[random.Next() % FirstNames.Length], LastName = LastNames[random.Next() % LastNames.Length], Minor = random.Next() % 3 == 1, CreatedUtc = DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(random.Next(86400, 3 * 31536000))) }; // approve 80% if (random.Next() % 10 < 7) { volunteer.ApprovedUtc = volunteer.CreatedUtc.Add(TimeSpan.FromSeconds(random.Next(300, 900))); // 80% actually volunteered if (random.Next() % 10 < 7) { volunteer.LastVolunteeredUtc = volunteer.CreatedUtc.Add(TimeSpan.FromSeconds( random.Next(500, (int)now.Subtract(volunteer.CreatedUtc).TotalSeconds))); } } // reject 10% else if (random.Next() % 2 == 1) { volunteer.RejectedUtc = volunteer.CreatedUtc.Add(TimeSpan.FromSeconds(random.Next(300, 86400))); volunteer.RejectedReason = RejectionReasons[random.Next() % RejectionReasons.Length]; } // leave the remaining to be approved session.Store(volunteer); } await session.SaveChangesAsync(); var campaigns = (await session.Query<Campaign>().ToListAsync()).ToArray(); var donors = (await session.Query<Donor>().ToListAsync()).ToArray(); for (int i = random.Next(80, 2000); i > 0; i--) { var donor = donors[random.Next() % donors.Length]; var donation = new Donation { DonorId = donor.Id, CampaignId = campaigns[random.Next() % campaigns.Length].Id, Amount = random.Next(5, 500), }; donation.CreatedUtc = donor.CreatedUtc.Add(TimeSpan.FromSeconds(random.Next(500, (int)now.Subtract(donor.CreatedUtc).TotalSeconds))); var thisMonth = DateTime.UtcNow.Date.AddDays(-now.Day); // fund 80% if (random.Next() % 10 < 7) { donation.FundedUtc = donation.CreatedUtc.Add(TimeSpan.FromSeconds(random.Next(300, 900))); if (donation.FundedUtc < thisMonth) { var endOfMonth = donation.FundedUtc.Value.AddMonths(1); endOfMonth = new DateTime(endOfMonth.Year, endOfMonth.Month, 12); donation.ReconciledUtc = endOfMonth; } } session.Store(donation); } // create donations with random campaigns and donors await session.SaveChangesAsync(); } context.Response.StatusCode = 200; await context.Response.WriteAsync("OK"); }); }