This commit is contained in:
2020-06-12 23:41:42 +00:00
parent 8be276b5b6
commit 7b452670f0
7 changed files with 184 additions and 74 deletions

View File

@@ -5,7 +5,7 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using rockfishCore.Util;
namespace rockfishCore.Controllers
{
@@ -65,44 +65,74 @@ namespace rockfishCore.Controllers
return Ok(rec);
}
// PUT: api/TrialRequest/5
[HttpPut("{id}")]
public async Task<IActionResult> PutTrialRequest([FromRoute] long id, [FromBody] TrialRequest TrialRequest)
//Approve
[HttpPost("approve/{id}")]
public async Task<IActionResult> ApproveTrial([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
var trial = await ct.TrialRequest.SingleOrDefaultAsync(m => m.Id == id);
if (trial == null)
return NotFound();
//DO APPROVE
//check not already processed and ignore if so
if(trial.DtProcessed!=null){
//already processed, nothing to do here
return BadRequest("Already processed");
}
if (id != TrialRequest.Id)
{
return BadRequest();
}
//generate license key and insert in record
trial.Key = RavenKeyFactory.GetRavenTrialKey(trial.DbId, trial.CompanyName);
trial.Status = TrialRequest.TrialRequestStatus.Approved;
trial.DtProcessed = DateUtil.NowAsEpoch();
await ct.SaveChangesAsync();
ct.Entry(TrialRequest).State = EntityState.Modified;
//send approved email to user
var body = $"Your trial license request has been approved.\r\nThe license will fetch and install automatically shortly or you can fetch it now in the License form menu.";
//send confirmation email
RfMail.SendMessage("support@ayanova.com", trial.Email, "AyaNova trial request approved", body, false);
try
{
await ct.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!await TrialRequestExistsAsync(id))
{
return NotFound();
}
else
{
throw;
}
}
return Ok(trial);
return NoContent();
}
//Reject
[HttpPost("reject/{id}")]
public async Task<IActionResult> RejectTrial([FromRoute] long id, [FromQuery] string rejectReason)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
var trial = await ct.TrialRequest.SingleOrDefaultAsync(m => m.Id == id);
if (trial == null)
return NotFound();
//DO REJECT
//check not already processed and ignore if so
if(trial.DtProcessed!=null){
//already processed, nothing to do here
return BadRequest("Already processed");
}
//generate license key and insert in record
trial.RejectReason=rejectReason;
trial.Status = TrialRequest.TrialRequestStatus.Rejected;
trial.DtProcessed = DateUtil.NowAsEpoch();
await ct.SaveChangesAsync();
//send approved email to user
string reason=string.Empty;
if(!string.IsNullOrWhiteSpace(rejectReason)){
reason=$"The request was rejected due to:\r\n{rejectReason}";
}
var body = $"Your trial license request was not approved.\r\n{reason}";
//send confirmation email
RfMail.SendMessage("support@ayanova.com", trial.Email, "AyaNova trial request approved", body, false);
return Ok(trial);
}
private async Task<bool> TrialRequestExistsAsync(long id)
@@ -110,7 +140,7 @@ namespace rockfishCore.Controllers
return await ct.TrialRequest.AnyAsync(e => e.Id == id);
}
//------------------------------------------------------
}//eoc