using System; using Amazon.Lambda.Core; using Amazon.Lambda.APIGatewayEvents; using Newtonsoft.Json; using MySqlConnector; using Newtonsoft.Json.Linq; using System.Data; using classes; namespace PMF { public class UpdateSettings: Pmfbase { public APIGatewayProxyResponse PostUpdateSettings(APIGatewayProxyRequest request, ILambdaContext context) { APIGatewayProxyResponse response; string statement = "DB::PostSettings"; string userId = ""; bool paramProvided = false; try { string validationMessage = string.Empty; if (request != null && request.Body != null) { var data = JsonConvert.DeserializeObject(request.Body); var validRequest = Helper.ValidateRequestParameters(ref validationMessage, data, "userId"); if (validRequest) { if (_con.State == ConnectionState.Closed) _con.Open(); string resendCode = ""; string forgotPassword = ""; string signUp = ""; string contactUs = ""; string convenience = ""; string result = string.Empty; userId = data["userId"].ToString(); foreach (var key in data.AsJEnumerable()) { string value = key.Path; switch (value) { case "CustomMessage_SignUp": signUp = (data["CustomMessage_SignUp"].ToString()); //Not cleaned to allow html from FE result += $" {UpdateSettingsKey(signUp, "CustomMessage_SignUp")}"; paramProvided = true; break; case "CustomMessage_ResendCode": resendCode = (data["CustomMessage_ResendCode"].ToString()); result += $" {UpdateSettingsKey(resendCode, "CustomMessage_ResendCode")}"; paramProvided = true; break; case "CustomMessage_ForgotPassword": forgotPassword = (data["CustomMessage_ForgotPassword"].ToString()); result += $" {UpdateSettingsKey(forgotPassword, "CustomMessage_ForgotPassword")}"; paramProvided = true; break; case "CustomMessage_ContactUs": contactUs = (data["CustomMessage_ContactUs"].ToString()); result += $" {UpdateSettingsKey(contactUs, "ContactUs_CustomMessage")}"; paramProvided = true; break; case "convenience": convenience = Helper.CleanInput(data["convenience"].ToString()); try { double val = Convert.ToDouble(convenience); } catch (System.Exception) { throw new Exception("convience amount must be a numerical value"); } result += $" {UpdateSettingsKey(convenience, "convenience")}"; paramProvided = true; break; } } if (!string.IsNullOrWhiteSpace(result)) return HttpService.CreateResponse(400, payload: result); if (!paramProvided) { response = HttpService.CreateResponse(400, payload: "Body must contain a valid Email type"); } else { response = HttpService.CreateResponse(200, payload: "Settings successfully updated"); } } else { response = HttpService.CreateResponse(400, payload: validationMessage); } } else { response = HttpService.CreateResponse(400, payload: "Body can not be empty"); } _logger.WriteLog(userId, statement, response.Body, "DB"); } catch (Exception ex) { _logger.LogMessage(context, "Processing request failed: " + ex.Message); _logger.WriteLog(userId, statement, ex.Message, "DB"); return HttpService.CreateResponse(500, payload: ex.Message); } finally { if (_con != null || _con.State == ConnectionState.Open) _con.Close(); } return response; } private string UpdateSettingsKey(string source, string key) { //check if email temp contains code placeholder; string result = string.Empty; string statement = ""; try { var cmd = new MySqlCommand(); cmd.Connection = _con; if (key.Contains("CustomMessage")) { if (source.Contains(Constants.CognitoCodePlaceHolder, StringComparison.CurrentCultureIgnoreCase)) { source = Uri.UnescapeDataString(source); statement = $"Update setting set strValue = '{source}' WHERE strKey = '{key}'"; cmd.CommandText = statement; cmd.ExecuteNonQuery(); } else { result = $"{key} does not contain a placeholder for the code parameter"; } } else { statement = $"Update setting set strValue = '{source}' WHERE strKey = '{key}'"; cmd.CommandText = statement; cmd.ExecuteNonQuery(); } } catch (Exception ex) { throw ex; } return result; } } }