128 lines
5.0 KiB
C#
128 lines
5.0 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;//now there's at least 'two problems' :)
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using rockfishCore.Models;
|
|
|
|
namespace rockfishCore.Util
|
|
{
|
|
/*
|
|
This class handles formatting and sending various notifications
|
|
*/
|
|
|
|
public static class RfNotify
|
|
{
|
|
//template key magic strings
|
|
public const string KEY_SUB_RENEW_NOTICE_TEMPLATE = "SubRenewNotice";
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
//SUBSCRIPTION RENEWAL NOTICE
|
|
//
|
|
public static object SendSubscriptionRenewalNotice(rockfishContext ct, List<long> purchaseidlist)
|
|
{
|
|
|
|
try
|
|
{
|
|
var template = ct.TextTemplate.SingleOrDefault(m => m.Name == KEY_SUB_RENEW_NOTICE_TEMPLATE);
|
|
|
|
if (template == null)
|
|
{
|
|
return RfResponse.fail("couldn't find template SubRenewNotice");
|
|
}
|
|
|
|
|
|
//parse out tokens
|
|
//we expect TITLE and BODY tokens here
|
|
Dictionary<string, string> tempTokens = parseTemplateTokens(template.Template);
|
|
|
|
//get a list of all the products
|
|
var products = ct.Product.ToList();
|
|
|
|
//Get a list of all purchases that are in the list of purchase id's
|
|
var purchases = ct.Purchase.Where(c => purchaseidlist.Contains(c.Id)).ToList();
|
|
|
|
//rf6
|
|
// string emailTO = purchases[0].Email;
|
|
var cust = ct.Customer.AsNoTracking().First(r => r.Id == purchases[0].CustomerId);
|
|
string emailTO = cust.AdminEmail;
|
|
|
|
//get company name
|
|
//rf6
|
|
// string companyName = ct.Customer.Select(r => new { r.Id, r.Name })
|
|
// .Where(r => r.Id == purchases[0].CustomerId)
|
|
// .First().Name;
|
|
string companyName=cust.Name;
|
|
|
|
//TAGS EXPECTED: {{BODY=}}, {{TITLE=}}
|
|
//REPLACEMENT TOKENS EXPECTED: {{SUBLIST}}
|
|
StringBuilder sublist = new StringBuilder();
|
|
foreach (Purchase pc in purchases)
|
|
{
|
|
var pr = products.Where(p => p.ProductCode == pc.ProductCode).First();
|
|
decimal dRenew = Convert.ToDecimal(pr.RenewPrice) / 100m;
|
|
decimal dMonthly = Convert.ToDecimal(pr.RenewPrice) / 1200m;
|
|
string sRenew = String.Format("{0:C}", dRenew);
|
|
string sMonthly = String.Format("{0:C}", dMonthly);
|
|
string sRenewDate = DateUtil.EpochToDate(pc.ExpireDate).ToString("D");
|
|
|
|
sublist.Append("\t- ");
|
|
sublist.Append(pr.Name);
|
|
sublist.Append(" will renew on ");
|
|
sublist.Append(sRenewDate);
|
|
sublist.Append(" at US");
|
|
sublist.Append(sRenew);
|
|
sublist.Append(" for the year + taxes (which works out to only ");
|
|
sublist.Append(sMonthly);
|
|
sublist.Append(" per month)");
|
|
sublist.AppendLine();
|
|
}
|
|
|
|
|
|
string emailFrom = "support@ayanova.com";
|
|
string emailSubject = companyName + " " + tempTokens["TITLE"];
|
|
string emailBody = tempTokens["BODY"].Replace("<<SUBLIST>>", sublist.ToString());
|
|
|
|
//put email in drafts
|
|
RfMail.DraftMessage(emailFrom, emailTO, emailSubject, emailBody, "MsgType", "SubRenewNotice");
|
|
|
|
//tag purchase as notified
|
|
foreach (Purchase pc in purchases)
|
|
{
|
|
pc.RenewNoticeSent = true;
|
|
}
|
|
ct.SaveChanges();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return RfResponse.fail(ex.Message);
|
|
}
|
|
|
|
return RfResponse.ok();
|
|
}
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
|
//parse out all tokens and values in the template
|
|
private static Dictionary<string, string> parseTemplateTokens(string src)
|
|
{
|
|
Dictionary<string, string> ret = new Dictionary<string, string>();
|
|
var roughmatches = Regex.Matches(src, @"{{.*?}}", RegexOptions.Singleline);
|
|
foreach (Match roughmatch in roughmatches)
|
|
{
|
|
//capture the token name which is a regex group between {{ and =
|
|
var token = Regex.Match(roughmatch.Value, @"{{(.*?)=", RegexOptions.Singleline).Groups[1].Value;
|
|
//capture the token value which is a regex group between {{TOKENNAME= and }}
|
|
var tokenValue = Regex.Match(roughmatch.Value, @"{{" + token + "=(.*?)}}", RegexOptions.Singleline).Groups[1].Value;
|
|
ret.Add(token, tokenValue);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
//eoc
|
|
}
|
|
//eons
|
|
} |