Exemple #1
0
        public async Task <IActionResult> Post([FromBody] object value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Party application data was invalid"));
            }

            var jPerm = value as JObject;

            if (jPerm == null)
            {
                return(BadRequest("Party application data was of the wrong type"));
            }

            var party = ExtractParty(jPerm);

            // Save the party
            var objId = await _partyRepository.AddOrUpdateAsync(party);

            if (objId == ObjectId.Empty)
            {
                return(BadRequest("Could not save permit application"));
            }
            party.id = objId;

            return(Ok(objId));
        }
Exemple #2
0
        /// <summary>
        /// Try a cascading series of attempts to get a matching entity
        /// </summary>
        /// <param name="party"></param>
        private async Task <Party> RefreshParty(Party party)
        {
            // First try by id
            if (party.id.HasValue && party.id.Value != ObjectId.Empty)
            {
                var partyById = await _partyRepository.GetAsync(party.id.Value);

                if (!ReferenceEquals(partyById, null))
                {
                    return(partyById);
                }
            }

            _entityRefreshNeeded = true;

            // Then try a 'full' match
            var partyFull = await _partyRepository.FindAsync(party, false);

            if (!ReferenceEquals(partyFull, null))
            {
                return(partyFull);
            }

            // Try a 'min' match
            var partyMin = await _partyRepository.FindAsync(party, true);

            if (!ReferenceEquals(partyMin, null))
            {
                // Merge any details from the supplied entity into the found one
                // This does NOT merge IsInfrastructureOwner or the child entities of Party
                return(partyMin.Merge(party));
            }

            // There is still no match then save the supplied entity first
            party.id = await _partyRepository.AddOrUpdateAsync(party);

            return(party);
        }
Exemple #3
0
        public async Task <IActionResult> Post([FromBody] object value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Penalty data was invalid"));
            }

            var jPenlt = value as JObject;

            if (jPenlt == null)
            {
                return(BadRequest("Penalty data was of the wrong type"));
            }

            var penalty = ExtractPenalty(jPenlt);

            // Save the permit
            var penaltyId = await _penaltyRepository.AddOrUpdateAsync(penalty);

            if (penaltyId == ObjectId.Empty)
            {
                return(BadRequest("Could not save penalty data"));
            }
            penalty.id = penaltyId;

            // Save the parties
            foreach (var party in penalty.parties)
            {
                var partyId = await _partyRepository.AddOrUpdateAsync(party);

                if (partyId != ObjectId.Empty)
                {
                    party.id = partyId;
                }
            }

            var oMem = BuildPenaltyPDF(penalty);

            var     apiKey = _appCodes.SendGridApiKey;
            dynamic sg     = new SendGridAPIClient(apiKey);

            var from    = new Email("*****@*****.**");
            var subject = "Test message from " + Constants.CisAbbr;
            var to      = new Email("*****@*****.**");

            Email[] cc   = { to, new Email("*****@*****.**"), new Email("*****@*****.**"), new Email("*****@*****.**"), new Email("*****@*****.**") };
            var     doco = new Attachment
            {
                Filename = "Penalty.pdf",
                Type     = "application/pdf",
                Content  = Convert.ToBase64String(oMem.ToArray())
            };

            var failedRecipients = new List <Email>();

            foreach (var email in cc)
            {
                try
                {
                    Content content;
                    if (email.Address == to.Address && email.Name == to.Name)
                    {
                        content = new Content("text/html", "<p>Dear Applicant,</p><p>This is the data you submitted via the " + Constants.UnaAbbr + " website.</p><p>Regards</p><p>UNA Support Team</p><pre>" + jPenlt.ToString(Formatting.Indented) + "</pre>");
                    }
                    else
                    {
                        content = new Content("text/html", "<p>This is the data the applicant submitted via the " + Constants.UnaAbbr + " website.</p><p>Regards</p><p>UNA Support Team</p><pre>" + jPenlt.ToString(Formatting.Indented) + "</pre>");
                    }
                    var mail = new Mail(from, subject, email, content)
                    {
                        Attachments = new List <Attachment> {
                            doco
                        }
                    };
                    dynamic response = sg.client.mail.send.post(requestBody: mail.Get());
                    var     respCode = response.StatusCode as HttpStatusCode?;
                    if (!respCode.HasValue)
                    {
                        failedRecipients.Add(email);
                    }
                    switch (respCode.Value)
                    {
                    case HttpStatusCode.Accepted:
                        break;

                    default:
                        failedRecipients.Add(email);
                        break;
                    }
                }
                catch (Exception)
                {
                    failedRecipients.Add(email);
                }
            }

            //if (failedRecipients.Any())
            //{
            //  var errorResult = "There were some issues sending emails to the intended recipients";
            //  return BadRequest(); // + failedRecipients.Select(f => f.Address));
            //}

            return(Ok(penaltyId));
        }