private static SaveRewardResponse SaveRewardTrx(RewardsRequest item, ICollector <string> errormessage, TraceWriter log) { SaveRewardResponse saveRewardResponse = new SaveRewardResponse(); saveRewardResponse.RewardTrxID = 0; try { using (var connection = MSSQLConnection.CreateConnection()) { connection.Open(); // Call stored procedure to Save Customer Customer Customers = new Customer(); GetRewardsRequest SaveRewardTrx = new GetRewardsRequest(); if (item.IsValid) { RewardTrxStatus = RewardTrxStatusEnum.ReadyForFulfillmentImmediate.GetDescription(); } if (string.IsNullOrEmpty(item.Customer.SourceSystemUniqueIDType)) { RewardTrxStatus = "Validation error"; item.ValidationMessage += " Source system is not supported."; } if (item.Reward != null && !string.IsNullOrEmpty(item.Reward.ProductValue) && !Decimal.TryParse(item.Reward.ProductValue, out ProductValue)) { RewardTrxStatus = "Validation error"; item.ValidationMessage += " Required numeric value for ProductValue field."; } if (!string.IsNullOrEmpty(item.Customer.SourceSystemID)) { int.TryParse(item.Customer.SourceSystemID, out SourceSystemID); if (SourceSystemID <= 0) { item.IsValid = false; RewardTrxStatus = RewardTrxStatusEnum.ValidationError.GetDescription(); item.ValidationMessage += "Validation Failed : SourceSystemID must be numeric;"; } } if (SourceSystemID > 0 || !string.IsNullOrEmpty(item.Customer.SourceSystemName)) { List <SqlParameter> SSIDprm = new List <SqlParameter>(); SSIDprm.Add(new SqlParameter("@SourceSystemShortName", item.Customer.SourceSystemName)); SSIDprm.Add(new SqlParameter("@SourceSystemID", item.Customer.SourceSystemID)); SourceSystemID = MSSQLConnection.ExecuteStoredProcedure <int>(USPContstants.GetSourceSystemID, SSIDprm).FirstOrDefault(); if (SourceSystemID == 0) { item.IsValid = false; RewardTrxStatus = RewardTrxStatusEnum.ValidationError.GetDescription(); item.ValidationMessage += "Validation Failed : Please provide valid SourceSystemID or SourceSystemName (if both are provided they must match);"; } } else if (item.IsOrder == false) { item.IsValid = false; RewardTrxStatus = RewardTrxStatusEnum.ValidationError.GetDescription(); item.ValidationMessage += "Validation Failed : Required value for at least one of 'SourceSystemID' or 'SourceSystemName'. Path: SourceSystemID or SourceSystemName;"; } item.ValidationMessage = item.ValidationMessage.TrimStart(';'); DynamicParameters CustomerTrxParams = new DynamicParameters(); CustomerTrxParams.Add("@SourceSystemID", SourceSystemID.ToString()); CustomerTrxParams.Add("@SourceSystemUniqueID", item.Customer.SourceSystemUniqueID); CustomerTrxParams.Add("@SourceSystemUniqueIDType", item.Customer.SourceSystemUniqueIDType); CustomerTrxParams.Add("@MasterID", item.Customer.MasterID); CustomerTrxParams.Add("@Email", item.Customer.Email); CustomerTrxParams.Add("@FirstName", item.Customer.FirstName); CustomerTrxParams.Add("@LastName", item.Customer.LastName); CustomerTrxParams.Add("@CompanyName", item.Customer.CompanyName); CustomerTrxParams.Add("@AddressLine1", item.Customer.AddressLine1); CustomerTrxParams.Add("@AddressLine2", item.Customer.AddressLine2); CustomerTrxParams.Add("@City", item.Customer.City); CustomerTrxParams.Add("@StateProvince", item.Customer.StateProvince); CustomerTrxParams.Add("@ZipPostalCode", item.Customer.ZipPostalCode); CustomerTrxParams.Add("@Phone1", item.Customer.Phone1); CustomerTrxParams.Add("@Product", item.Customer.Product); CustomerTrxParams.Add("@Language", item.Customer.Language); CustomerTrxParams.Add("@RequestID", item.RequestId); CustomerTrxParams.Add("@TransactionType", item.TransactionType); CustomerTrxParams.Add("@ProductCode", (item.Reward != null && !string.IsNullOrEmpty(item.Reward.ProductCode)) ? item.Reward.ProductCode : string.Empty); CustomerTrxParams.Add("@ProductValue", ProductValue); CustomerTrxParams.Add("@ProgramName", (item.Reward != null && !string.IsNullOrEmpty(item.Reward.ProgramName)) ? item.Reward.ProgramName : string.Empty); if (item.Reward != null) { CustomerTrxParams.Add("@EffectiveDate", item.Reward.EffectiveDate); } else { CustomerTrxParams.Add("@EffectiveDate", string.Empty); } CustomerTrxParams.Add("@SourceIP", item.SourceIP); CustomerTrxParams.Add("@RewardsRequestReceiveTimestamp", item.RewardsRequestReceiveTimestamp); CustomerTrxParams.Add("@RMSRewardID", item.RMSRewardID); var additionalDataJSON = JsonConvert.SerializeObject(item.AdditionalData); CustomerTrxParams.Add("@AdditionalData", additionalDataJSON); CustomerTrxParams.Add("@RewardType", (!string.IsNullOrEmpty(item.Reward.RewardType)) ? item.Reward.RewardType : string.Empty); CustomerTrxParams.Add("@MessageType", MessageType.RewardRequest.GetDescription()); CustomerTrxParams.Add("@Message", Message); CustomerTrxParams.Add("@RewardTrxStatus", RewardTrxStatus); CustomerTrxParams.Add("@Comment", item.ValidationMessage); SaveRewardTrx = connection.Query <GetRewardsRequest>(Common.Constants.USPContstants.SaveRewards, CustomerTrxParams, null, true, null, CommandType.StoredProcedure).FirstOrDefault(); log.Verbose($"SaveRewardTrx sucessfully ={SaveRewardTrx.RewardTrxID}", "JE.RMS.Services.SaveRewardsTrx"); saveRewardResponse.Status = SaveRewardTrx.RewardTrxStatus; saveRewardResponse.RewardTrxID = SaveRewardTrx.RewardTrxID; saveRewardResponse.IsValid = item.IsValid; //Hack : SP returns validation message in Reward type field (this is internal) saveRewardResponse.ValidationMessage = item.ValidationMessage + SaveRewardTrx.RewardType; } return(saveRewardResponse); } catch (Exception ex) { log.Error($"Exception={ex}", ex, "JE.RMS.Services.ApplyReward"); errormessage.Add(JsonConvert.SerializeObject(ex).ToString()); saveRewardResponse.IsValid = false; saveRewardResponse.ValidationMessage = ex.Message; saveRewardResponse.Status = RewardTrxStatusEnum.Error.GetDescription(); return(saveRewardResponse); } }
public static async Task <HttpResponseMessage> Run(HttpRequestMessage req, ICollector <string> errormessage, TraceWriter log) { log.Verbose($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}", "JE.RMS.Services.SaveRewardsTrx"); try { string Message = await req.Content.ReadAsStringAsync(); var item = JsonConvert.DeserializeObject <RewardsRequest>(await req.Content.ReadAsStringAsync()); using (var connection = MSSQLConnection.CreateConnection()) { connection.Open(); // Call stored procedure to Save Customer Customer Customers = new Customer(); GetRewardsRequest SaveRewardTrx = new GetRewardsRequest(); log.Verbose($"In SaveRewardTrx, Message={Message}", "JE.RMS.Services.SaveRewardsTrx"); log.Verbose($"In SaveRewardTrx, IsValid={item.IsValid}", "JE.RMS.Services.SaveRewardsTrx"); if (item.IsValid == true && item.IsValid.ToString().ToLower() == "true") { RewardTrxStatus = "Received"; } else { RewardTrxStatus = "Validation error"; } log.Verbose($"In SaveRewardTrx, RewardTrxStatus={RewardTrxStatus}", "JE.RMS.Services.SaveRewardsTrx"); if (item.Reward != null && !string.IsNullOrEmpty(item.Reward.ProductValue) && !Decimal.TryParse(item.Reward.ProductValue, out ProductValue)) { RewardTrxStatus = "Validation error"; item.ValidationMessage += " Required numeric value for ProductValue field."; } if (!item.IsOrder) { if (string.IsNullOrEmpty(item.Customer.SourceSystemUniqueIDType)) { RewardTrxStatus = "Validation error"; item.ValidationMessage += " Source system is not supported."; } if (!string.IsNullOrEmpty(item.Customer.SourceSystemID)) { int.TryParse(item.Customer.SourceSystemID, out SourceSystemID); if (SourceSystemID <= 0) { item.IsValid = false; RewardTrxStatus = RewardTrxStatusEnum.ValidationError.GetDescription(); item.ValidationMessage += "Validation Failed : SourceSystemID must be numeric;"; } } if (SourceSystemID > 0 || !string.IsNullOrEmpty(item.Customer.SourceSystemName)) { List <SqlParameter> SSIDprm = new List <SqlParameter>(); SSIDprm.Add(new SqlParameter("@SourceSystemShortName", item.Customer.SourceSystemName)); SSIDprm.Add(new SqlParameter("@SourceSystemID", item.Customer.SourceSystemID)); SourceSystemID = MSSQLConnection.ExecuteStoredProcedure <int>(USPContstants.GetSourceSystemID, SSIDprm).FirstOrDefault(); if (SourceSystemID == 0) { item.IsValid = false; RewardTrxStatus = RewardTrxStatusEnum.ValidationError.GetDescription(); item.ValidationMessage += "Validation Failed : Please provide valid SourceSystemID or SourceSystemName (if both are provided they must match);"; } } else { item.IsValid = false; RewardTrxStatus = RewardTrxStatusEnum.ValidationError.GetDescription(); item.ValidationMessage += "Validation Failed : Required value for at least one of 'SourceSystemID' or 'SourceSystemName'. Path: SourceSystemID or SourceSystemName;"; } } item.ValidationMessage = item.ValidationMessage.TrimStart(';'); DynamicParameters CustomerTrxParams = new DynamicParameters(); CustomerTrxParams.Add("@SourceSystemID", SourceSystemID.ToString()); CustomerTrxParams.Add("@SourceSystemUniqueID", item.Customer.SourceSystemUniqueID); CustomerTrxParams.Add("@SourceSystemUniqueIDType", item.Customer.SourceSystemUniqueIDType); CustomerTrxParams.Add("@MasterID", item.Customer.MasterID); CustomerTrxParams.Add("@Email", item.Customer.Email); CustomerTrxParams.Add("@FirstName", item.Customer.FirstName); CustomerTrxParams.Add("@LastName", item.Customer.LastName); CustomerTrxParams.Add("@CompanyName", item.Customer.CompanyName); CustomerTrxParams.Add("@AddressLine1", item.Customer.AddressLine1); CustomerTrxParams.Add("@AddressLine2", item.Customer.AddressLine2); CustomerTrxParams.Add("@City", item.Customer.City); CustomerTrxParams.Add("@StateProvince", item.Customer.StateProvince); CustomerTrxParams.Add("@ZipPostalCode", item.Customer.ZipPostalCode); CustomerTrxParams.Add("@Phone1", item.Customer.Phone1); CustomerTrxParams.Add("@Product", item.Customer.Product); CustomerTrxParams.Add("@Language", item.Customer.Language); CustomerTrxParams.Add("@RequestID", item.RequestId); CustomerTrxParams.Add("@TransactionType", item.TransactionType); CustomerTrxParams.Add("@ProductCode", (item.Reward != null && !string.IsNullOrEmpty(item.Reward.ProductCode)) ? item.Reward.ProductCode : string.Empty); CustomerTrxParams.Add("@ProductValue", ProductValue); CustomerTrxParams.Add("@ProgramName", (item.Reward != null && !string.IsNullOrEmpty(item.Reward.ProgramName)) ? item.Reward.ProgramName : string.Empty); if (item.Reward != null) { CustomerTrxParams.Add("@EffectiveDate", item.Reward.EffectiveDate); } else { CustomerTrxParams.Add("@EffectiveDate", string.Empty); } CustomerTrxParams.Add("@SourceIP", item.SourceIP); CustomerTrxParams.Add("@RewardsRequestReceiveTimestamp", item.RewardsRequestReceiveTimestamp); CustomerTrxParams.Add("@RMSRewardID", item.RMSRewardID); var additionalDataJSON = JsonConvert.SerializeObject(item.AdditionalData); CustomerTrxParams.Add("@AdditionalData", additionalDataJSON); CustomerTrxParams.Add("@RewardType", (!string.IsNullOrEmpty(item.Reward.RewardType)) ? item.Reward.RewardType : string.Empty); CustomerTrxParams.Add("@MessageType", MessageType.RewardRequest.GetDescription()); CustomerTrxParams.Add("@Message", Message); CustomerTrxParams.Add("@RewardTrxStatus", RewardTrxStatus); CustomerTrxParams.Add("@Comment", item.ValidationMessage); if (item.AdditionalData != null && item.AdditionalData.Where(p => p.Name == "OldEmailAddress") != null && item.AdditionalData.Where(p => p.Name == "OldEmailAddress").Count() > 0) { CustomerTrxParams.Add("@OldEmailAddress", item.AdditionalData.Where(p => p.Name == "OldEmailAddress").FirstOrDefault().Value); } CustomerTrxParams.Add("@IsOrder", item.IsOrder); log.Verbose($"Before calling SaveRewards stored proc, RewardTrxStatus={RewardTrxStatus}", "JE.RMS.Services.SaveRewardsTrx"); SaveRewardTrx = connection.Query <GetRewardsRequest>(Common.Constants.USPContstants.SaveRewards, CustomerTrxParams, null, true, null, CommandType.StoredProcedure).FirstOrDefault(); log.Verbose($"SaveRewardTrx sucessfully ={SaveRewardTrx.RewardTrxID}", "JE.RMS.Services.SaveRewardsTrx"); } return(req.CreateResponse(HttpStatusCode.OK, true)); } catch (Exception ex) { log.Error($"Exception={ex}", ex, "JE.RMS.Services.SaveRewardsTrx"); errormessage.Add(JsonConvert.SerializeObject(ex).ToString()); return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public static async void Run(TimerInfo getPointTransactionTimer, ICollector <string> errormessage, TraceWriter log) { log.Verbose($"C# Timer trigger function processed a request", "JE.RMS.Services.ScheduledGetPointTransactionLog"); try { List <SqlParameter> objParams = new List <SqlParameter>(); objParams.Add(new SqlParameter("@ProcessName", "GetPointTransactionLog")); ProcessLog DateLog = MSSQLConnection.ExecuteStoredProcedure <ProcessLog>(Common.Constants.USPContstants.GetLastDateOfTransactionLog, objParams).FirstOrDefault(); log.Verbose($"Get Points transaction called with start date = {DateLog.StartDate.Date.ToString()}, end date = {DateLog.EndDate.Date.ToString()}", "JE.RMS.Services.GetTransactionLogFromEnergyEarth"); //Check if start date & end date is provided if (DateLog.StartDate != null && DateLog.EndDate != null) { if (DateLog.StartDate > DateLog.EndDate) { log.Verbose($"Function seems to be already executed before. Provided startdate : {DateLog.StartDate} and EndDate : {DateLog.EndDate}"); } using (HttpClient httpClient = new HttpClient()) { //Add Basic Authentication header httpClient.BaseAddress = new Uri(ConfigurationManager.AppSettings["EnergyEarthBaseUrl"].ToString()); var auth = Encoding.ASCII.GetBytes(ConfigurationManager.AppSettings["EEUserName"].ToString() + ":" + ConfigurationManager.AppSettings["EEPassword"].ToString()); httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(auth)); startDate = DateLog.StartDate.ToString("MM/dd/yyyy"); endDate = DateLog.EndDate.ToString("MM/dd/yyyy"); string GetPointTransactionsUrl = ConfigurationManager.AppSettings["GetTransactionLogFromEnergyEarthUrl"].ToString() + "?startDate=" + startDate + "&endDate=" + endDate; var response = await httpClient.GetStringAsync(GetPointTransactionsUrl); log.Verbose($"Success : Get Points transaction start date={DateLog.StartDate.Date.ToString()}, end date = {DateLog.EndDate.Date.ToString()}.", "JE.RMS.Services.GetTransactionLogFromEnergyEarth"); //Convert Users from Energy earth to Customer List Model using (var conn = MSSQLConnection.CreateConnection()) { log.Verbose("Started migration of EE users to Customer Table", "JE.RMS.Services.DMEEUserToCustomer"); conn.Open(); string deleteQuery = "DELETE FROM [dbo].[FulfillmentChannelTransactionLog] WHERE RecordDate BetWeen @StartDate AND @EndDate"; conn.Execute(deleteQuery, new { StartDate = DateLog.StartDate, EndDate = DateLog.EndDate }); string getFulfillmentChannelIDQuery = "SELECT FulfillmentChannelID FROM [dbo].[FulfillmentChannel] WHERE ChannelName = 'EnergyEarth'"; int FID = conn.Query <int>(getFulfillmentChannelIDQuery).FirstOrDefault(); //Convert Users from Energy earth to Customer List Model var transactionList = JObject.Parse(response); List <FulfillmentChannelTransactionLog> transactionLogList = new List <FulfillmentChannelTransactionLog>(); foreach (JObject x in transactionList["Transactions"]) { // Where 'obj' and 'obj["otherObject"]' are both JObjects FulfillmentChannelTransactionLog transactionLog = new FulfillmentChannelTransactionLog(); transactionLog.CreatedDate = DateTime.Now; transactionLog.TransactionData = x.ToString(); transactionLog.FulfillmentChannelID = FID; string date = x.SelectToken("OrderDate").ToString(); string date1 = x.SelectToken("ShipDate").ToString(); transactionLog.RecordDate = string.IsNullOrEmpty(date) ? DateTime.Now : Convert.ToDateTime(date); transactionLogList.Add(transactionLog); } string processQuery = "INSERT INTO [dbo].[FulfillmentChannelTransactionLog] (FulfillmentChannelID,TransactionData,RecordDate,CreatedDate) VALUES (@FulfillmentChannelID, @TransactionData, @RecordDate, @CreatedDate)"; conn.Execute(processQuery, transactionLogList); var Log = MSSQLConnection.ExecuteStoredProcedure <ProcessLog>(Common.Constants.USPContstants.UpdateProcessLog, objParams).FirstOrDefault(); log.Verbose("Completed migration of EE users to Customer Table", "JE.RMS.Services.ScheduledGetPointTransactionLog"); } } } else //Bad request : Start Date or End Date is not provided. { log.Error("Missing Parameter for GetTransactionLogFromEnergyEarth : startDate/endDate", null, "JE.RMS.Services.ScheduledGetPointTransactionLog"); } } catch (Exception ex) { log.Error("Something went wrong while GetTransactionLogFromEnergyEarth", ex, "JE.RMS.Services.ScheduledGetPointTransactionLog"); errormessage.Add(JsonConvert.SerializeObject(ex).ToString()); } }