Files
rockfish/Controllers/RvrController.cs
2020-06-11 21:28:36 +00:00

94 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using rockfishCore.Models;
using rockfishCore.Util;
using System.ComponentModel.DataAnnotations;
namespace rockfishCore.Controllers
{
[Produces("text/plain")]
[Route("rvr")]
public class RvrController : Controller //RAVEN trial license request
{
private readonly rockfishContext ct;
public RvrController(rockfishContext context)
{
ct = context;
}
public class dtoRequest
{
[Required]
public Guid DbId { get; set; }
[Required, EmailAddress]
public string Email { get; set; }
[Required]
public string Company { get; set; }
[Required]
public string Contact { get; set; }
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] dtoRequest r)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (r.DbId == Guid.Empty)
{
return BadRequest("E1000 - DBId invalid");
}
//can't do this if there is a purchased license with this dbid already
if (await ct.License.Where(z => z.DbId == r.DbId).AnyAsync())
{
return BadRequest("E1000 - Can't trial; there is already a purchased license issued for this database Id");
}
System.Diagnostics.Debug.WriteLine("RvRController:Post - TODO: Test MustBeOlderThan date code");
//if there is an active trial for this db then can't do this they must request we re-release it or completely zap the database instead
var MustBeOlderThan = DateUtil.DateToEpoch(DateTime.Now.AddDays(-45));
if (await ct.TrialRequest.Where(z => z.DbId == r.DbId && z.DtProcessed != null && z.DtProcessed > MustBeOlderThan).AnyAsync())
{
return BadRequest("E1000 - Can't trial; there is already an active trial license issued for this database Id");
}
//Everything seems to be in order, save the request and return ok
var NewRequest=new TrialRequest();
NewRequest.Email=r.Email;
//TODO: closer to release as ROCKFISH might change before then
//Attempt to match customer by dbid if not then create new customer of trial type for the indicated dbid and regto
//ensure this email goes into the adminEmail array if not already there
//Flag customer record has having an unfulfilled trial request
//If it's a new customer or an existing one with non-matching email then send a verification email to the customer
//When a response is spotted in email then Rockfish should see it and flag the customer as verified
//Probably some general purpose email Verify code would be helpful here as it would likely be useful for all manner of things
//When user releases trial in Rockfish a license key will be generated ready for the customers RAVEN to retrieve
return Ok("Request accepted. Awaiting email verification and approval.");
}
}//eoc
}//eons