Files
rockfish/Controllers/RvlController.cs
2020-06-17 17:59:34 +00:00

171 lines
6.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("rvl")]
public class RvlController : Controller //RAVEN license controller
{
private readonly rockfishContext ct;
public RvlController(rockfishContext context)
{
ct = context;
}
public class dtoRavLicense
{
[Required]
public string RegisteredTo { get; set; }
[Required]
public Guid DbId { get; set; }
[Required]
public long LicenseExpiration { get; set; }
[Required]
public long MaintenanceExpiration { get; set; }
[Required]
public List<dtoLicenseFeature> Features { get; set; }
[Required]
public long SiteId { get; set; }
}
public class dtoLicenseFeature
{
dtoLicenseFeature()
{
Count = 0;
}
//name of feature / product
public string Feature { get; set; }
//Optional count for items that require it
public long Count { get; set; }
}
#if (DEBUG)
private const string LICENSE_SERVER_URL = "http://localhost:3001/";
public const string SUPPORT_EMAIL = "cardjohn@ayanova.com";
#else
private const string LICENSE_SERVER_URL = "https://rockfish.ayanova.com/";
public const string SUPPORT_EMAIL="support@ayanova.com";
#endif
[HttpPost]
public async Task<IActionResult> Post([FromBody] dtoRavLicense l)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//Get customer from site id
var CustomerId = await ct.Site.AsNoTracking().Where(z => z.Id == l.SiteId).Select(z => z.CustomerId).FirstOrDefaultAsync();
if (CustomerId == 0)
{
return BadRequest($"Customer could not be found for site id {l.SiteId}");
}
var Customer = await ct.Customer.AsNoTracking().Where(z => z.Id == CustomerId).FirstOrDefaultAsync();
if (Customer == null)
{
return BadRequest($"Customer could not be found for customer id {CustomerId}");
}
if (string.IsNullOrWhiteSpace(Customer.AdminEmail))
{
return BadRequest($"Customer does not have an Admin / License email address, no where to send key");
}
if (!Customer.Active)
{
return BadRequest($"Customer {Customer.Name} is not Active, set to active and try again");
}
if (Customer.DoNotContact)
{
return BadRequest($"Customer {Customer.Name} is set to DO NOT CONTACT, unable to process and send key");
}
var newLicense = new RavenKeyFactory.AyaNovaLicenseKey();
newLicense.RegisteredTo = l.RegisteredTo;
newLicense.DbId = l.DbId;
newLicense.LicenseExpiration = DateUtil.EpochToDate(l.LicenseExpiration);
newLicense.MaintenanceExpiration = DateUtil.EpochToDate(l.MaintenanceExpiration);
foreach (dtoLicenseFeature f in l.Features)
{
newLicense.Features.Add(new RavenKeyFactory.LicenseFeature() { Feature = f.Feature, Count = f.Count });
}
//Everything seems to be in order generate the license, save it and send it
var NewRequest = new TrialRequest();
NewRequest.Email = r.Email;
NewRequest.DbId = r.DbId;
NewRequest.CompanyName = r.Company;
NewRequest.ContactName = r.Contact;
await ct.TrialRequest.AddAsync(NewRequest);
await ct.SaveChangesAsync();
NewRequest.EmailConfirmCode = NewRequest.Id.ToString() + FetchKeyCode.generate();
await ct.SaveChangesAsync();
var verifyUrl = LICENSE_SERVER_URL + $"rvr/verify/{NewRequest.EmailConfirmCode}";
var body = $"Please verify your email address by clicking the link below or copy and pasting into a browser\r\n{verifyUrl}\r\n(If you did not request this you can ignore this message)";
//send confirmation email
RfMail.SendMessage("support@ayanova.com", NewRequest.Email, "AyaNova trial request email verification", body, false);
//return Ok(new { Accepted = true });
return Accepted();
}
[HttpGet("verify/{code}")]
public async Task<IActionResult> GetVerify([FromRoute] string code)
{
//is there a valid trial request
var req = await ct.TrialRequest.Where(z => z.EmailConfirmCode == code && z.DtFetched == null && z.Status == TrialRequest.TrialRequestStatus.NotSet).FirstOrDefaultAsync();
if (req == null)
{
return new ContentResult
{
ContentType = "text/html",
StatusCode = 200,
Content = "<html><body><h4>Request not found</h4><p>There was no evaluation request associated with this verification code.</p></body></html>"
};
}
req.EmailValidated = true;
await ct.SaveChangesAsync();
//notify *us*
//http://localhost:3001/default.htm#!/trials/[id]
var rfUrl = LICENSE_SERVER_URL + $"default.htm#!/trialEdit/{req.Id}";
var body = $"Email address {req.Email} was just verified for {req.ContactName} at {req.CompanyName}.\r\nTrial key is ready to be processed now:\r\n{rfUrl}";
//send confirmation email
RfMail.SendMessage("support@ayanova.com", SUPPORT_EMAIL, "AyaNova trial request requiring action", body, false);
return new ContentResult
{
ContentType = "text/html",
StatusCode = 200,
Content = "<html><body><h4>Email validated!</h4><p>Your request is being processed and you will receive an email with instructions for starting your AyaNova evaluation.</p></body></html>"
};
}
}//eoc
}//eons