private async Task SendEmail(Event ev, SignupRecord signupRecord, byte[] pdfAsBytes) { var client = new SendGridClient(_configuration.GetValue <string>(Constants.AppSettingSendGridApiKey)); var from = new EmailAddress("*****@*****.**", "Ingunn"); var subject = "Your start number for " + ev.Name; var to = new EmailAddress(signupRecord.Email, signupRecord.FirstName + " " + signupRecord.SurName); var plainTextContent = "Your start number for " + ev.Name + " attached."; var htmlContent = "Your start number for <strong>" + ev.Name + "</strong> attached. "; var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent); var ms = new MemoryStream(pdfAsBytes); await msg.AddAttachmentAsync(signupRecord.GetStartNumberPdfFileName(), contentStream : ms); var response = await client.SendEmailAsync(msg); var bodyAsString = await response.Body.ReadAsStringAsync(); var listOfUpdates = new List <UpdateDefinition <SignupRecord> >(); listOfUpdates.Add(Builders <SignupRecord> .Update.Set(x => x.EmailSendStatusCode, (int)response.StatusCode)); listOfUpdates.Add(Builders <SignupRecord> .Update.Set(x => x.EmailSendResponseBody, bodyAsString)); var finalUpd = Builders <SignupRecord> .Update.Combine(listOfUpdates); await _db.SignupRecords.FindOneAndUpdateAsync(x => x.Id == signupRecord.Id, finalUpd); }
public async Task <CommandResultDto <string> > SignUp(SignUpDto signUpData) { try { var tenant = await(await _db.Tenants.FindAsync(x => x.Key == signUpData.TenantKey)).SingleAsync(); var currentEvent = await(await _db.Events.FindAsync(x => x.Id == tenant.CurrentlyActiveEventId)).SingleAsync(); var compareEmail = signUpData.Email.ToLower().Trim(); var compareFirstName = signUpData.FirstName.ToLower().Trim(); var compareSurName = signUpData.SurName.ToLower().Trim(); var existingPerson = await(await _db.Persons.FindAsync(x => x.TenantId == tenant.Id && x.Email == compareEmail && x.FirstName.ToLower() == compareFirstName && x.SurName.ToLower() == compareSurName)).SingleOrDefaultAsync(); if (existingPerson == null) { await _db.Persons.InsertOneAsync(new Person { TenantId = tenant.Id, FirstName = signUpData.FirstName.Trim(), SurName = signUpData.SurName.Trim(), Email = compareEmail, AllowUsToContactPersonByEmail = signUpData.AllowUsToContactPersonByEmail }); existingPerson = _db.Persons.Find(x => x.TenantId == tenant.Id && x.FirstName == signUpData.FirstName.Trim() && x.SurName == signUpData.SurName.Trim()).Single(); } else { var listOfUpdates = new List <UpdateDefinition <Person> >(); listOfUpdates.Add(Builders <Person> .Update.Set(x => x.AllowUsToContactPersonByEmail, signUpData.AllowUsToContactPersonByEmail)); var finalUpd = Builders <Person> .Update.Combine(listOfUpdates); await _db.Persons.UpdateOneAsync(x => x.Id == existingPerson.Id, finalUpd, new UpdateOptions { IsUpsert = true }); } if (!currentEvent.Signups.Any(x => x.PersonId == existingPerson.Id)) { var newSignupsArray = currentEvent.Signups.Concat(new[] { new EventSignup { PersonId = existingPerson.Id, PersonName = existingPerson.FirstName + " " + existingPerson.SurName } }); var listOfUpdates = new List <UpdateDefinition <Event> >(); listOfUpdates.Add(Builders <Event> .Update.Set(x => x.Signups, newSignupsArray)); var finalUpd = Builders <Event> .Update.Combine(listOfUpdates); await _db.Events.FindOneAndUpdateAsync(x => x.Id == currentEvent.Id && x.Signups.Length == currentEvent.Signups.Length, finalUpd); } var newlyReservedStartNumber = currentEvent.Signups.Length + 1; var signupRecord = new SignupRecord { EventId = currentEvent.Id, FirstName = signUpData.FirstName.Trim(), SurName = signUpData.SurName.Trim(), Email = signUpData.Email.Trim(), AllowUsToContactPersonByEmail = signUpData.AllowUsToContactPersonByEmail, PreviouslyParticipated = signUpData.PreviouslyParticipated, IPAddress = _ctx.HttpContext.Connection.RemoteIpAddress.ToString(), SignupUTC = DateTime.UtcNow, PersonId = existingPerson.Id, ActualStartNumber = newlyReservedStartNumber }; await _db.SignupRecords.InsertOneAsync(signupRecord); var startNumberPdf = GetStartNumberPdf(newlyReservedStartNumber); await SendEmail(currentEvent, signupRecord, startNumberPdf); return(new CommandResultDto <string> { Success = true, Data = signupRecord.Id }); } catch (Exception ex) { return(new CommandResultDto <string> { Success = false, ErrorMessages = new[] { ex.Message }, Data = null }); } }
public async Task <QueryResult> Query(QueryInput queryInput) { QueryResult queryResult = new QueryResult() { Status = OperationStatus.NoError, Records = new List <SignupRecord>() }; int PageSize = 20; // We defined this in here. bool skipFirstName = string.IsNullOrWhiteSpace(queryInput.FirstName); bool skipLastName = string.IsNullOrWhiteSpace(queryInput.LastName); bool AllEvents = queryInput.FunEvent == 0; try { using (HomeworkEntities context = new HomeworkEntities()) { var query = from sign in context.Signups join funEvent in context.FunEvents on sign.FunEventId equals funEvent.FunEventId where (skipFirstName || sign.FirstName.Contains(queryInput.FirstName)) && (skipLastName || sign.LastName.Contains(queryInput.LastName)) && (AllEvents || sign.FunEventId == queryInput.FunEvent) orderby sign.CreatedDatetime ascending select new { funEvent.Name, funEvent.StartDateTime, funEvent.FunEventId, sign.FirstName, sign.LastName, sign.Email, sign.Comments }; var data = await query.Skip(queryInput.CurrentPage *PageSize).Take(PageSize).ToListAsync(); if (data == null && data.Count == 0) { return(queryResult); } foreach (var item in data) { SignupRecord r = new SignupRecord() { FirstName = item.FirstName, LastName = item.LastName, Email = item.Email, Comments = item.Comments, FunEventName = item.Name, EventStartDate = item.StartDateTime, FunEventId = item.FunEventId }; queryResult.Records.Add(r); } } } catch (Exception ex) { } return(queryResult); }
public byte[] GetStartNumberPdf(SignupRecord signup) { return(GetStartNumberPdf(signup.ActualStartNumber)); }
public static string GetStartNumberPdfFileName(this SignupRecord signupRecord) { return($"Start_Number_{signupRecord.ActualStartNumber}.pdf"); }