using System; using System.Data; using Amazon.Lambda.Core; using classes; using Model.ResponseModel; using MySqlConnector; namespace PMF { public class CognitoMail: Pmfbase { public CognitoObjectData GenerateCustomEmail(CognitoObjectData input, ILambdaContext context) { // Documentation: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html //check on trigger source from cognito.....All models set up in ResponseModel and cognito details. try { if (input.triggerSource == "CustomMessage_SignUp") //SEARCH REMOVE for the AWS Credits Fields { input.response.emailSubject = "Pay My Fines: Sign Up"; input.response.emailMessage = ReplaceEmailPlaceHolders("CustomMessage_SignUp", input.request.codeParameter, input.request.userAttributes.email); } if (input.triggerSource == "CustomMessage_ResendCode") //SEARCH REMOVE for the AWS Credits Fields { input.response.emailSubject = "Pay My Fines: Resent code"; input.response.emailMessage = ReplaceEmailPlaceHolders("CustomMessage_ResendCode", input.request.codeParameter, input.request.userAttributes.email); } if (input.triggerSource == "CustomMessage_ForgotPassword") //SEARCH REMOVE for the AWS Credits Fields { input.response.emailSubject = "Pay My Fines: Forgot Password"; input.response.emailMessage = ReplaceEmailPlaceHolders("CustomMessage_ForgotPassword", input.request.codeParameter, string.Empty); } return input; } catch (Exception ex) { _logger.LogMessage(context, $"Processing request failed - Cognito reset Failed: {ex.Message.ToString()}"); _logger.WriteLog("Generate email fires with exception", ex.Message.ToString(), "exception", "DB"); return input; } //On fail should bounnce back to cognito } private string ReplaceEmailPlaceHolders(string emailType, string codeParameter, string email) { string emailBody = ""; MySqlDataReader _reader = null; try { if (_con.State == ConnectionState.Closed) _con.Open(); string statement = $"SELECT * FROM setting WHERE strKey = '{emailType}'"; var cmd = new MySqlCommand(statement, _con); _reader = cmd.ExecuteReader(); if (_reader.HasRows) { while (_reader.Read()) { emailBody = _reader["strValue"].ToString(); } emailBody = emailBody.Replace("{code}", codeParameter); if (email != string.Empty) { emailBody = emailBody.Replace("{email}", email); } } } catch (Exception ex) { throw ex; } finally { if (_reader != null) _reader.Close(); if (_con != null || _con.State == ConnectionState.Open) _con.Close(); } return emailBody; } } }