using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Logging; using AyaNova.Models; using AyaNova.Util; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using Newtonsoft.Json.Linq; using System.ComponentModel.DataAnnotations; namespace AyaNova.Api.Controllers { /// ///Test controller class used during development /// [ApiController] [ApiVersion("8.0")] [Route("api/v{version:apiVersion}/trial")] [Produces("application/json")] [Authorize] public class TrialController : ControllerBase { private readonly AyContext ct; private readonly ILogger log; private readonly ApiServerState serverState; /// /// ctor /// /// /// /// public TrialController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) { ct = dbcontext; log = logger; serverState = apiServerState; } /// /// Seed a trial database with sample data. /// NOTE: the database must be erased prior to seeding /// You can control the size and scope of the seeded data with the passed in size value /// "Small" - a small one man shop dataset /// "Medium" - Local service company with multiple employees and departments dataset /// "Large" - Large corporate multi regional dataset /// "Huge" - Used for automated testing and development, if you choose this it will take a very long time (15 minutes to overnight) /// TimeZoneOffset - Value in hours of local time zone offset from UTC / GMT. This ensures that data is generated relative to the desired time zone /// E2e - End to end testing mode for development automated testing; false is default and faster /// ForceEmail - set every generated object with an email address to this email address /// AppendPassword - append this string to the stock passwords, useful when testing on the public internet /// /// /// Job Id [HttpPost("seed")] public async Task SeedTrialDatabase([FromBody] SeedOptions seedOptions) { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!ModelState.IsValid) { return BadRequest(new ApiErrorResponse(ModelState)); } if (!AyaNova.Core.License.ActiveKey.TrialLicense) { return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "Current license is not a trial license key. Only a trial can be seeded.")); } //if db not empty then can't seed if (!await DbUtil.DBIsEmptyAsync(ct, log)) { return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "Current database is not empty. Only an empty database can be seeded.")); } Seeder.Level.SeedLevel seedLevel = Seeder.Level.StringToSeedLevel(seedOptions.SeedLevel); if (seedLevel == Seeder.Level.SeedLevel.NotValid) return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "size", "Valid values are \"small\", \"medium\", \"large\", \"huge\"")); //Create the job here JObject o = JObject.FromObject(new { seedLevel = seedLevel, timeZoneOffset = seedOptions.TimeZoneOffset, e2e = seedOptions.E2e, forceEmail = seedOptions.ForceEmail, appendPassword = seedOptions.AppendPassword }); OpsJob j = new OpsJob(); j.Name = $"Seed test data (size={seedOptions.SeedLevel})"; j.JobType = JobType.SeedTestData; j.Exclusive = true;//don't run other jobs, this will erase the db j.JobInfo = o.ToString(); await JobsBiz.AddJobAsync(j); //Log await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.TrialSeeder, AyaEvent.Created, seedOptions.SeedLevel), ct); return Accepted(new { JobId = j.GId });//202 accepted } public class SeedOptions { [Required] public string SeedLevel { get; set; } [Required] public decimal TimeZoneOffset { get; set; } public bool E2e { get; set; } = false; [EmailAddress] public string ForceEmail { get; set; } public string AppendPassword { get; set; } } //------------ }//eoc }//eons