diff --git a/server/AyaNova/Controllers/KPIController.cs b/server/AyaNova/Controllers/KPIController.cs new file mode 100644 index 00000000..d24782ff --- /dev/null +++ b/server/AyaNova/Controllers/KPIController.cs @@ -0,0 +1,85 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Authorization; +using Microsoft.EntityFrameworkCore; +using AyaNova.Models; +using AyaNova.Api.ControllerHelpers; +using AyaNova.Biz; +using AyaNova.KPI; +using System.Threading.Tasks; +using System.Linq; + +namespace AyaNova.Api.Controllers +{ + + [ApiController] + [ApiVersion("8.0")] + [Route("api/v{version:apiVersion}/kpi")] + [Produces("application/json")] + [Authorize] + public class KPIController : ControllerBase + { + private readonly AyContext ct; + private readonly ILogger log; + private readonly ApiServerState serverState; + + + + /// + /// ctor + /// + /// + /// + /// + public KPIController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState) + { + ct = dbcontext; + log = logger; + serverState = apiServerState; + } + + + + /// + /// Get KPI data + /// + /// Parameters for pick list see api docs for details + /// Filtered list + [HttpPost] + public async Task PostList([FromBody] KPIRequestOptions options) + { + if (serverState.IsClosed) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); + //UserTypeFromContext.Type(HttpContext.Items) + return Ok(await KPIFetcher.GetResponseAsync(ct, options, UserRolesFromContext.Roles(HttpContext.Items), log, UserIdFromContext.Id(HttpContext.Items))); + } + + + + + //SAVE FOR LATER IF WANT TO RETURN LIST OF ALL KPI'S (likely something to do with report editing??) + // /// + // /// List of all DataList keys available + // /// + // /// List of strings + // [HttpGet("listkeys")] + // public ActionResult GetDataListKeys() + // { + // //NOTE: not used by AyaNova Client, convenience method for developers api usage + // if (!serverState.IsOpen) + // return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + + // return Ok(ApiOkResponse.Response(DataListFactory.GetListOfAllDataListKeyNames())); + // } + + + + + + + }//eoc +}//ens \ No newline at end of file diff --git a/server/AyaNova/kpi/IAyaKPI.cs b/server/AyaNova/kpi/IAyaKPI.cs index d6dbf660..10d4be48 100644 --- a/server/AyaNova/kpi/IAyaKPI.cs +++ b/server/AyaNova/kpi/IAyaKPI.cs @@ -8,7 +8,7 @@ namespace AyaNova.KPI AuthorizationRoles AllowedRoles { get; } //build the data and meta queries based on the criteria and this kpi's standard query - void BuildQuery(string criteria, DateTimeOffset clientTimeStamp); + void BuildQuery(KPIRequestOptions options); string MetaQuery{get;}//Query to fetch json meta data for report purposes mainly (lookup stuff like names etc where applicable) string DataQuery{get;}//Query to fetch json format data for result set string ErrorMessage{get;}//if there was a problem then this is set with the error message diff --git a/server/AyaNova/kpi/KPIFetcher.cs b/server/AyaNova/kpi/KPIFetcher.cs index e9810964..b14c4ffe 100644 --- a/server/AyaNova/kpi/KPIFetcher.cs +++ b/server/AyaNova/kpi/KPIFetcher.cs @@ -18,7 +18,7 @@ namespace AyaNova.KPI // Get the kpi data requested // // - internal static async Task GetResponseAsync(AyContext ct, string kpiName, string criteria, DateTimeOffset clientTimeStamp, AuthorizationRoles userRoles, ILogger log, long userId) + internal static async Task GetResponseAsync(AyContext ct, KPIRequestOptions options, AuthorizationRoles userRoles, ILogger log, long userId) { /* @@ -30,8 +30,17 @@ namespace AyaNova.KPI } */ //instantiate the list - var kpi = KPIFactory.GetAyaKPI(kpiName); - kpi.BuildQuery(criteria, clientTimeStamp); + var kpi = KPIFactory.GetAyaKPI(options.KPIName); + + if (!AyaNova.Api.ControllerHelpers.Authorized.HasAnyRole(userRoles, kpi.AllowedRoles)) + { + return JObject.FromObject(new + { + error = "NOT AUTHORIZED" + }); + } + + kpi.BuildQuery(options); if (!string.IsNullOrWhiteSpace(kpi.ErrorMessage)) { diff --git a/server/AyaNova/kpi/KPIRequestOptions.cs b/server/AyaNova/kpi/KPIRequestOptions.cs new file mode 100644 index 00000000..050f87e3 --- /dev/null +++ b/server/AyaNova/kpi/KPIRequestOptions.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Mvc; +using System; + +namespace AyaNova.KPI +{ + + //string kpiName, string criteria, DateTimeOffset clientTimeStamp + + + public sealed class KPIRequestOptions + { + + [FromBody] + public string KPIName { get; set; } + + [FromBody] + public string Criteria { get; set; } + + [FromBody] + public DateTimeOffset Template { get; set; } + + public KPIRequestOptions() + { + KPIName=string.Empty; + Criteria=string.Empty; + } + } + +} \ No newline at end of file diff --git a/server/AyaNova/kpi/WorkOrderItemLaborQuantitySummary.cs b/server/AyaNova/kpi/WorkOrderItemLaborQuantitySummary.cs index 7af87df0..45ad620b 100644 --- a/server/AyaNova/kpi/WorkOrderItemLaborQuantitySummary.cs +++ b/server/AyaNova/kpi/WorkOrderItemLaborQuantitySummary.cs @@ -10,17 +10,31 @@ namespace AyaNova.KPI { private string _metaQuery = null; private string _dataQuery = null; - private string _errorMessage=null; + private string _errorMessage = null; public AuthorizationRoles AllowedRoles { get => BizRoles.GetRoleSet(AyaType.WorkOrderItemLabor).ReadFullRecord; } - + public string MetaQuery => _metaQuery; public string DataQuery => _dataQuery; public string ErrorMessage => _errorMessage; - public void BuildQuery(string criteria, DateTimeOffset clientTimeStamp) + public void BuildQuery(KPIRequestOptions options) { //build data and meta queries + + _dataQuery = @"SELECT row_to_json(t) as res from ( +select SUM(AWORKORDERITEMLABOR.serviceratequantity) SERVICERATESUM, date_trunc('month',AWORKORDERITEMLABOR.servicestartdate) timeframe +FROM AWORKORDER +LEFT JOIN AWORKORDERITEM ON AWORKORDER.ID = AWORKORDERITEM.WORKORDERID +LEFT JOIN AWORKORDERITEMLABOR ON AWORKORDERITEM.ID = AWORKORDERITEMLABOR.WORKORDERITEMID +WHERE AWORKORDERITEMLABOR.userid = 10 +GROUP BY timeframe +ORDER BY timeframe ASC +) t"; + + _metaQuery = @"SELECT row_to_json(t) as res from ( + select name from auser where id = 10 +) t"; } // public string GetVariantCriteria(string variant)