129 lines
4.3 KiB
C#
129 lines
4.3 KiB
C#
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using rockfishCore.Models;
|
|
using rockfishCore.Util;
|
|
using System.Linq;
|
|
using System;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace rockfishCore.Controllers
|
|
{
|
|
[Produces("application/json")]
|
|
[Route("api/subscription")]
|
|
[Authorize]
|
|
public class SubscriptionController : Controller
|
|
{
|
|
private readonly rockfishContext _context;
|
|
private readonly IConfiguration _configuration;
|
|
public SubscriptionController(rockfishContext context, IConfiguration configuration)//these two are injected, see startup.cs
|
|
{
|
|
_context = context;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
//**************************
|
|
//TODO: ASYNCIFY ALL OF THIS
|
|
|
|
///////////////////////////////////////////////////////
|
|
//Get notification list for expiring subscriptions
|
|
//
|
|
[HttpGet("notifylist")]
|
|
public JsonResult Get()
|
|
{
|
|
var customerList = _context.Customer.Select(p => new { p.Id, p.Name });
|
|
|
|
/*Query: purchases with no renewal warning flag set, not cancelled and that are expiring subs in next 30 day window grouped by customer and then by purchase date
|
|
Take that list show in UI, with button beside each customer group, press button, it generates a renewal warning email
|
|
and puts it into the drafts folder, tags all the purchases with renewal warning sent true. */
|
|
|
|
//from three days ago to a month from now
|
|
long windowStart = DateUtil.DateToEpoch(DateTime.Now.AddDays(-3));
|
|
long windowEnd = DateUtil.DateToEpoch(DateTime.Now.AddMonths(1));
|
|
|
|
var rawExpiresList = _context.Purchase
|
|
.Where(p => p.RenewNoticeSent == false)
|
|
.Where(p => p.ExpireDate != null)
|
|
.Where(p => p.ExpireDate > windowStart)
|
|
.Where(p => p.ExpireDate < windowEnd)
|
|
.Where(p => p.CancelDate == null)
|
|
.OrderBy(p => p.CustomerId)
|
|
.Select(p => new { id = p.Id, name = p.Name, customerId = p.CustomerId })
|
|
.ToList();
|
|
|
|
|
|
|
|
//Initiate an empty list for return
|
|
List<subnotifyResultItem> retList = new List<subnotifyResultItem>();
|
|
//Bail if nothing to see here
|
|
if (rawExpiresList.Count < 1)
|
|
{
|
|
return Json(retList);
|
|
}
|
|
|
|
|
|
long lastCustomerId = -1;
|
|
subnotifyResultItem sn = new subnotifyResultItem();
|
|
|
|
//flatten the list and project it into return format
|
|
foreach (var i in rawExpiresList)
|
|
{
|
|
//first loop?
|
|
if (lastCustomerId == -1) lastCustomerId = i.customerId;
|
|
|
|
//New customer?
|
|
if (i.customerId != lastCustomerId)
|
|
{
|
|
sn.purchasenames = sn.purchasenames.TrimStart(',').Trim();
|
|
retList.Add(sn);
|
|
sn = new subnotifyResultItem();
|
|
lastCustomerId = i.customerId;
|
|
}
|
|
|
|
if (sn.customerId == 0)
|
|
{
|
|
//get the full data
|
|
sn.Customer = customerList.First(p => p.Id == i.customerId).Name;
|
|
sn.customerId = i.customerId;
|
|
sn.purchaseidlist = new List<long>();
|
|
}
|
|
sn.purchasenames += ", " + i.name;
|
|
sn.purchaseidlist.Add(i.id);
|
|
}
|
|
//Add the last one
|
|
sn.purchasenames = sn.purchasenames.TrimStart(',').Trim();
|
|
retList.Add(sn);
|
|
|
|
return Json(retList);
|
|
}
|
|
|
|
//dto classes for route
|
|
public class subnotifyResultItem
|
|
{
|
|
public long id;
|
|
public string Customer;
|
|
public string purchasenames;
|
|
public List<long> purchaseidlist;
|
|
public long customerId;
|
|
|
|
}
|
|
|
|
//SEND SELECTED RENEWAL NOTIFICATIONS
|
|
[HttpPost("sendnotify")]
|
|
public JsonResult Generate([FromBody] List<long> purchaseidlist)
|
|
{
|
|
return Json(RfNotify.SendSubscriptionRenewalNotice(_context, purchaseidlist));
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
}//eoc
|
|
}//eons
|
|
|