This commit is contained in:
@@ -112,7 +112,8 @@ namespace AyaNova.Biz
|
||||
UnitMeterReading = 53,
|
||||
CustomerServiceRequest = 54,
|
||||
ServiceBank = 55,
|
||||
OpsNotificationSettings = 56
|
||||
OpsNotificationSettings = 56,
|
||||
Report = 57
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -98,7 +98,8 @@ namespace AyaNova.Biz
|
||||
return await ct.WorkOrderTemplate.AnyAsync(z => z.Id == id);
|
||||
case AyaType.WorkOrderTemplateItem:
|
||||
return await ct.WorkOrderTemplateItem.AnyAsync(z => z.Id == id);
|
||||
|
||||
case AyaType.Report:
|
||||
return await ct.Report.AnyAsync(z => z.Id == id);
|
||||
default:
|
||||
throw new System.NotSupportedException($"AyaNova.Biz.BizObjectExistsInDatabase::ExistsAsync type {objectType.ToString()} is not supported");
|
||||
}
|
||||
|
||||
@@ -544,7 +544,15 @@ namespace AyaNova.Biz
|
||||
ReadFullRecord = AuthorizationRoles.All
|
||||
});
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
//REPORT
|
||||
//
|
||||
roles.Add(AyaType.Report, new BizRoleSet()
|
||||
{
|
||||
Change = AuthorizationRoles.BizAdminFull | AuthorizationRoles.BizAdminLimited,
|
||||
ReadFullRecord = AuthorizationRoles.BizAdminLimited | AuthorizationRoles.BizAdminLimited,
|
||||
Select = AuthorizationRoles.All
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
#endregion all roles init
|
||||
|
||||
239
server/AyaNova/biz/ReportBiz.cs
Normal file
239
server/AyaNova/biz/ReportBiz.cs
Normal file
@@ -0,0 +1,239 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using AyaNova.Util;
|
||||
using AyaNova.Api.ControllerHelpers;
|
||||
using AyaNova.Models;
|
||||
|
||||
namespace AyaNova.Biz
|
||||
{
|
||||
internal class ReportBiz : BizObject, ISearchAbleObject
|
||||
{
|
||||
internal ReportBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||||
{
|
||||
ct = dbcontext;
|
||||
UserId = currentUserId;
|
||||
UserTranslationId = userTranslationId;
|
||||
CurrentUserRoles = UserRoles;
|
||||
BizType = AyaType.Report;
|
||||
}
|
||||
|
||||
internal static ReportBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||||
{
|
||||
if (httpContext != null)
|
||||
return new ReportBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||
else
|
||||
return new ReportBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//EXISTS
|
||||
internal async Task<bool> ExistsAsync(long id)
|
||||
{
|
||||
return await ct.Report.AnyAsync(z => z.Id == id);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//CREATE
|
||||
//
|
||||
internal async Task<Report> CreateAsync(Report newObject)
|
||||
{
|
||||
await ValidateAsync(newObject, null);
|
||||
if (HasErrors)
|
||||
return null;
|
||||
else
|
||||
{
|
||||
await ct.Report.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||
await SearchIndexAsync(newObject, true);
|
||||
return newObject;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//DUPLICATE
|
||||
//
|
||||
internal async Task<Report> DuplicateAsync(long id)
|
||||
{
|
||||
Report dbObject = await GetAsync(id, false);
|
||||
if (dbObject == null)
|
||||
{
|
||||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
return null;
|
||||
}
|
||||
Report newObject = new Report();
|
||||
string newUniqueName = string.Empty;
|
||||
bool NotUnique = true;
|
||||
long l = 1;
|
||||
do
|
||||
{
|
||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||
NotUnique = await ct.Report.AnyAsync(z => z.Name == newUniqueName);
|
||||
} while (NotUnique);
|
||||
newObject.Name = newUniqueName;
|
||||
newObject.Id = 0;
|
||||
newObject.Concurrency = 0;
|
||||
await ct.Report.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||
await SearchIndexAsync(newObject, true);
|
||||
return newObject;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//GET
|
||||
//
|
||||
internal async Task<Report> GetAsync(long id, bool logTheGetEvent = true)
|
||||
{
|
||||
var ret = await ct.Report.SingleOrDefaultAsync(z => z.Id == id);
|
||||
if (logTheGetEvent && ret != null)
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//UPDATE
|
||||
//
|
||||
internal async Task<Report> PutAsync(Report putObject)
|
||||
{
|
||||
Report dbObject = await ct.Report.SingleOrDefaultAsync(z => z.Id == putObject.Id);
|
||||
if (dbObject == null)
|
||||
{
|
||||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
return null;
|
||||
}
|
||||
Report SnapshotOfOriginalDBObj = new Report();
|
||||
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||
CopyObject.Copy(putObject, dbObject, "Id");
|
||||
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||
if (HasErrors) return null;
|
||||
try
|
||||
{
|
||||
await ct.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!await ExistsAsync(putObject.Id))
|
||||
AddError(ApiErrorCode.NOT_FOUND);
|
||||
else
|
||||
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||
return null;
|
||||
}
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||
await SearchIndexAsync(dbObject, false);
|
||||
return dbObject;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//DELETE
|
||||
//
|
||||
internal async Task<bool> DeleteAsync(long id)
|
||||
{
|
||||
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||||
{
|
||||
try
|
||||
{
|
||||
Report dbObject = await ct.Report.SingleOrDefaultAsync(z => z.Id == id);
|
||||
ValidateCanDelete(dbObject);
|
||||
if (HasErrors)
|
||||
return false;
|
||||
if (HasErrors)
|
||||
return false;
|
||||
ct.Report.Remove(dbObject);
|
||||
await ct.SaveChangesAsync();
|
||||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||||
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType, ct);
|
||||
await FileUtil.DeleteAttachmentsForObjectAsync(BizType, dbObject.Id, ct);
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||||
throw;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//SEARCH
|
||||
//
|
||||
private async Task SearchIndexAsync(Report obj, bool isNew)
|
||||
{
|
||||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||
if (isNew)
|
||||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||||
else
|
||||
await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams);
|
||||
}
|
||||
|
||||
public async Task<Search.SearchIndexProcessObjectParameters> GetSearchResultSummary(long id)
|
||||
{
|
||||
var obj = await ct.Report.SingleOrDefaultAsync(z => z.Id == id);
|
||||
var SearchParams = new Search.SearchIndexProcessObjectParameters();
|
||||
if (obj != null)
|
||||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||||
return SearchParams;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//VALIDATION
|
||||
//
|
||||
|
||||
private async Task ValidateAsync(Report proposedObj, Report currentObj)
|
||||
{
|
||||
|
||||
bool isNew = currentObj == null;
|
||||
|
||||
//Name required
|
||||
if (string.IsNullOrWhiteSpace(proposedObj.Name))
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name");
|
||||
|
||||
//Name must be less than 255 characters
|
||||
if (proposedObj.Name.Length > 255)
|
||||
AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 max");
|
||||
|
||||
//If name is otherwise OK, check that name is unique
|
||||
if (!PropertyHasErrors("Name"))
|
||||
{
|
||||
//Use Any command is efficient way to check existance, it doesn't return the record, just a true or false
|
||||
if (await ct.Report.AnyAsync(z => z.Name == proposedObj.Name && z.Id != proposedObj.Id))
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async void ValidateCanDelete(Report inObj)
|
||||
{
|
||||
|
||||
//TODO: this is shitty, needs to mention notifications to be maximally useful
|
||||
if (await ct.NotifySubscription.AnyAsync(z => z.AttachReportId == inObj.Id) == true)
|
||||
{
|
||||
AddError(ApiErrorCode.INVALID_OPERATION, null, "LT:ErrorDBForeignKeyViolation");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//JOB / OPERATIONS
|
||||
//
|
||||
|
||||
|
||||
//Other job handlers here...
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
}//eoc
|
||||
|
||||
|
||||
}//eons
|
||||
|
||||
Reference in New Issue
Block a user