using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; using System.Linq; using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using System.Threading.Tasks; using System; namespace AyaNova.Api.Controllers { [ApiController] [Asp.Versioning.ApiVersion("8.0")] [Route("api/v{version:apiVersion}/global-biz-setting")] [Produces("application/json")] [Authorize] public class GlobalBizSettingsController : ControllerBase { private readonly AyContext ct; private readonly ILogger log; private readonly ApiServerState serverState; /// /// ctor /// /// /// /// public GlobalBizSettingsController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) { ct = dbcontext; log = logger; serverState = apiServerState; } /// /// Get GlobalBizSettings /// /// Global settings object [HttpGet] public async Task GetGlobalBizSettings() { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); //Instantiate the business object handler GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.GetBiz(ct, HttpContext); if (!Authorized.HasReadFullRole(HttpContext.Items, biz.BizType)) return StatusCode(403, new ApiNotAuthorizedResponse()); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); var o = await biz.GetAsync(); if (o == null) return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND)); return Ok(ApiOkResponse.Response(o)); } /// /// PUT Global biz settings /// /// /// New concurrency token [HttpPut] public async Task ReplaceGlobalBizSettings([FromBody] GlobalBizSettings updatedObject) { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); //Instantiate the business object handler GlobalBizSettingsBiz biz = GlobalBizSettingsBiz.GetBiz(ct, HttpContext); if (!Authorized.HasModifyRole(HttpContext.Items, biz.BizType)) return StatusCode(403, new ApiNotAuthorizedResponse()); var o = await biz.PutAsync(updatedObject); if (o == null) return StatusCode(409, new ApiErrorResponse(biz.Errors)); return Ok(ApiOkResponse.Response(new { Concurrency = o.Concurrency })); } /// /// Get Client app relevant GlobalBizSettings /// /// Global settings object [HttpGet("client")] public ActionResult GetClientGlobalBizSettings() { //## NOTE: these are settings that the Client needs to see for standard operations //NOT the settings that the user changes in the global settings form which is fetched above //so do not include anything here unless the client needs it if (serverState.IsClosed) { //Exception for SuperUser account to handle licensing issues if (UserIdFromContext.Id(HttpContext.Items) != 1) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); } bool IsSubscriptionBuild = false; #if (SUBSCRIPTION_BUILD) IsSubscriptionBuild=true; #else IsSubscriptionBuild = false;//yes redundant, but defensive all the same #endif var ret = new { //Actual global settings: FilterCaseSensitive = AyaNova.Util.ServerGlobalBizSettings.Cache.FilterCaseSensitive, UseInventory = AyaNova.Util.ServerGlobalBizSettings.Cache.UseInventory, DefaultTaxPartSaleId = AyaNova.Util.ServerGlobalBizSettings.Cache.TaxPartSaleId, DefaultTaxPartPurchaseId = AyaNova.Util.ServerGlobalBizSettings.Cache.TaxPartPurchaseId, DefaultTaxRateSaleId = AyaNova.Util.ServerGlobalBizSettings.Cache.TaxRateSaleId, WorkOrderTravelDefaultMinutes = AyaNova.Util.ServerGlobalBizSettings.Cache.WorkOrderTravelDefaultMinutes, WorkLaborScheduleDefaultMinutes = AyaNova.Util.ServerGlobalBizSettings.Cache.WorkLaborScheduleDefaultMinutes, SignatureTitle = AyaNova.Util.ServerGlobalBizSettings.Cache.SignatureTitle, SignatureHeader = AyaNova.Util.ServerGlobalBizSettings.Cache.SignatureHeader, SignatureFooter = AyaNova.Util.ServerGlobalBizSettings.Cache.SignatureFooter, CSRInfoText = AyaNova.Util.ServerGlobalBizSettings.Cache.CustomerServiceRequestInfoText, //used to drive UI in case of unlicensed or attention required LicenseStatus = AyaNova.Core.License.ActiveKey.Status, MaintenanceExpired = AyaNova.Core.License.ActiveKey.MaintenanceExpired, ServerDbId = AyaNova.Core.License.ServerDbId, Company = AyaNova.Core.License.ActiveKey.RegisteredTo, SBuild = IsSubscriptionBuild, //used to drive UI in case of out of date release message to be shown to staff users non subscription only ShowUpdateAvailable = (Util.AyaNovaVersion.VersionString != AyaNova.Util.ServerGlobalOpsSettingsCache.LATEST_VERSION && !IsSubscriptionBuild), LatestVersion = AyaNova.Util.ServerGlobalOpsSettingsCache.LATEST_VERSION, ChangeLogUrl = AyaNova.Util.ServerGlobalOpsSettingsCache.CHANGE_LOG_URL, LicenseExpirationDays = AyaNova.Core.License.ActiveKey.LicenseExpirationDays // , // TestTSDaysWMS=new TimeSpan(22,10,15,22,33), // TestTSHMS=new TimeSpan(5,16,33), // TestTS_DHMS=new TimeSpan(5,10,15,33), // TestTS_MS=new TimeSpan(0,15,33), // TestTS_S=new TimeSpan(0,0,33), // TestTS_D=new TimeSpan(22,0,0,0,0) }; return Ok(ApiOkResponse.Response(ret)); } /// /// Get next serial numbers for serialized objects /// /// [HttpGet("seeds")] public ActionResult GetSeeds() { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!Authorized.HasReadFullRole(HttpContext.Items, AyaType.Global)) return StatusCode(403, new ApiNotAuthorizedResponse()); //select last_value from aworkorder_serial_seq long WONext = 0, PONext = 0, QuoteNext = 0, PMNext = 0; using (var command = ct.Database.GetDbConnection().CreateCommand()) { ct.Database.OpenConnection(); command.CommandText = "select last_value from apurchaseorder_serial_seq;"; using (var dr = command.ExecuteReader()) if (dr.HasRows) PONext = dr.Read() ? dr.GetInt64(0) : 0; command.CommandText = "select last_value from aworkorder_serial_seq;"; using (var dr = command.ExecuteReader()) if (dr.HasRows) WONext = dr.Read() ? dr.GetInt64(0) : 0; command.CommandText = "select last_value from aquote_serial_seq;"; using (var dr = command.ExecuteReader()) if (dr.HasRows) QuoteNext = dr.Read() ? dr.GetInt64(0) : 0; command.CommandText = "select last_value from apm_serial_seq;"; using (var dr = command.ExecuteReader()) if (dr.HasRows) PMNext = dr.Read() ? dr.GetInt64(0) : 0; ct.Database.CloseConnection(); } var ret = new { //Actual global settings: PurchaseOrderNextSerial = ++PONext, WorkorderNextSerial = ++WONext, QuoteNextSerial = ++QuoteNext, PMNextSerial = ++PMNext }; return Ok(ApiOkResponse.Response(ret)); } /// /// PUT New serial start seed /// /// one of PurchaseOrder, Workorder, PM, Quote serialized object types /// Next value to use /// OK or error response if not a valid value (seed number already in use) [HttpPut("seeds/{aType}/{nextSeed}")] public async Task SetNextSeed([FromRoute] AyaType aType, [FromRoute] long nextSeed) { if (serverState.IsClosed) return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); if (!ModelState.IsValid) return BadRequest(new ApiErrorResponse(ModelState)); if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.Global)) return StatusCode(403, new ApiNotAuthorizedResponse()); //this is random and arbitrary but is meant as a sanity check //in v7 and int identity was used for serial numbers so the maximum possible value could be 2,147,483,647 //assuming there is some weird edge case where people want to move beyond that max value for some reason I'm settling on a sanity check of 3 billion 3,000,000,000 if (nextSeed == 0 || nextSeed > 3000000000) return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "nextSeed", "> 0 or < 3,000,000,000")); string SequenceName = string.Empty; //confirm this number is not in use and set correct sequence name to alter switch (aType) { case AyaType.PurchaseOrder: if (await ct.PurchaseOrder.AnyAsync(z => z.Serial == nextSeed)) return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); SequenceName = "apurchaseorder_serial_seq"; break; case AyaType.WorkOrder: if (await ct.WorkOrder.AnyAsync(z => z.Serial == nextSeed)) return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); SequenceName = "aworkorder_serial_seq"; break; case AyaType.Quote: if (await ct.Quote.AnyAsync(z => z.Serial == nextSeed)) return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); SequenceName = "aquote_serial_seq"; break; case AyaType.PM: if (await ct.PM.AnyAsync(z => z.Serial == nextSeed)) return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); SequenceName = "apm_serial_seq"; break; default: return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "aType", "Not a serialized type")); } try { using (var command = ct.Database.GetDbConnection().CreateCommand()) { command.CommandText = $"ALTER SEQUENCE {SequenceName} RESTART WITH {nextSeed};"; await ct.Database.OpenConnectionAsync(); await command.ExecuteNonQueryAsync(); await ct.Database.CloseConnectionAsync(); } } catch (Exception ex) { return BadRequest(new ApiErrorResponse(ApiErrorCode.API_SERVER_ERROR, "generalerror", ex.Message)); } return Ok(ApiOkResponse.Response(new { AType = aType, NextSeed = nextSeed })); } }//eoc }//ens