From 7e0fa69d4a6c924a6dd29c326b51c8af74f08dd2 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 9 Dec 2020 00:46:29 +0000 Subject: [PATCH] --- server/AyaNova/biz/CustomerBiz.cs | 4 ++-- server/AyaNova/biz/HeadOfficeBiz.cs | 35 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/server/AyaNova/biz/CustomerBiz.cs b/server/AyaNova/biz/CustomerBiz.cs index 5ac84f53..923831a0 100644 --- a/server/AyaNova/biz/CustomerBiz.cs +++ b/server/AyaNova/biz/CustomerBiz.cs @@ -156,7 +156,7 @@ namespace AyaNova.Biz AddError(ApiErrorCode.NOT_FOUND); return false; } - await ValidateCanDelete(dbObject); + await ValidateCanDeleteAsync(dbObject); if (HasErrors) return false; @@ -286,7 +286,7 @@ namespace AyaNova.Biz } - private async Task ValidateCanDelete(Customer inObj) + private async Task ValidateCanDeleteAsync(Customer inObj) { //## NOTE: contact isn't so important, this could be changed to check only more important things like workorders etc //and just attempt to delete all the contacts if possible, but for now.... diff --git a/server/AyaNova/biz/HeadOfficeBiz.cs b/server/AyaNova/biz/HeadOfficeBiz.cs index a8e9e849..96902993 100644 --- a/server/AyaNova/biz/HeadOfficeBiz.cs +++ b/server/AyaNova/biz/HeadOfficeBiz.cs @@ -151,15 +151,31 @@ namespace AyaNova.Biz try { HeadOffice dbObject = await ct.HeadOffice.SingleOrDefaultAsync(z => z.Id == id); - if (dbObject == null){ + if (dbObject == null) + { AddError(ApiErrorCode.NOT_FOUND); return false; } ValidateCanDelete(dbObject); if (HasErrors) return false; - if (HasErrors) - return false; + + + //DELETE DIRECT CHILD OBJECTS + { + var ContactIds = await ct.User.AsNoTracking().Where(z => z.HeadOfficeId == id).Select(z => z.Id).ToListAsync(); + if (ContactIds.Count() > 0) + { + UserBiz b = new UserBiz(ct, UserId, UserTranslationId, CurrentUserRoles); + foreach (long ItemId in ContactIds) + if (!await b.DeleteAsync(ItemId, transaction)) + { + AddError(ApiErrorCode.CHILD_OBJECT_ERROR, null, $"HeadOfficeContact [{ItemId}]: {b.GetErrorsAsString()}"); + return false; + } + } + } + ct.HeadOffice.Remove(dbObject); await ct.SaveChangesAsync(); @@ -173,6 +189,8 @@ namespace AyaNova.Biz } catch { + //NOTE: no need to rollback the transaction, it will auto-rollback if not committed and it is disposed when it goes out of scope either way + //Just re-throw for now, let exception handler deal, but in future may want to deal with this more here throw; } @@ -246,9 +264,16 @@ namespace AyaNova.Biz } - private void ValidateCanDelete(HeadOffice inObj) + + private async Task ValidateCanDeleteAsync(HeadOffice inObj) { - //whatever needs to be check to delete this object + //Referential integrity + if (await ct.Customer.AnyAsync(z => z.HeadOfficeId == inObj.Id) == true) + { + //Note: errorbox will ensure it appears in the general errror box and not field specific + //the translation key is to indicate what the linked object is that is causing the error + AddError(ApiErrorCode.VALIDATION_REFERENTIAL_INTEGRITY, "errorbox", "LT:Customer"); + } }