diff --git a/server/AyaNova/Controllers/NameController.cs b/server/AyaNova/Controllers/NameController.cs index c3b00473..de91b95e 100644 --- a/server/AyaNova/Controllers/NameController.cs +++ b/server/AyaNova/Controllers/NameController.cs @@ -56,11 +56,9 @@ namespace AyaNova.Api.Controllers return BadRequest(new ApiErrorResponse(ModelState)); if (id == 0) return Ok(ApiOkResponse.Response(ayType.ToString())); - using (var command = ct.Database.GetDbConnection().CreateCommand()) - { - ct.Database.OpenConnection(); - return Ok(ApiOkResponse.Response(BizObjectNameFetcherDirect.Name(ayType, id, command))); - } + + return Ok(ApiOkResponse.Response(BizObjectNameFetcherDirect.Name(ayType, id, ct))); + } diff --git a/server/AyaNova/biz/BizObjectExistsInDatabase.cs b/server/AyaNova/biz/BizObjectExistsInDatabase.cs index 44325e7f..cf0ac82f 100644 --- a/server/AyaNova/biz/BizObjectExistsInDatabase.cs +++ b/server/AyaNova/biz/BizObjectExistsInDatabase.cs @@ -108,6 +108,14 @@ namespace AyaNova.Biz return await ct.Reminder.AnyAsync(z => z.Id == id); case AyaType.Review: return await ct.Review.AnyAsync(z => z.Id == id); + case AyaType.ServiceRate: + return await ct.ServiceRate.AnyAsync(z => z.Id == id); + case AyaType.TravelRate: + return await ct.TravelRate.AnyAsync(z => z.Id == id); + case AyaType.TaxCode: + return await ct.TaxCode.AnyAsync(z => z.Id == id); + case AyaType.ServiceBank: + return await ct.ServiceBank.AnyAsync(z => z.Id == id); default: throw new System.NotSupportedException($"AyaNova.Biz.BizObjectExistsInDatabase::ExistsAsync type {objectType.ToString()} is not supported"); } diff --git a/server/AyaNova/biz/BizObjectFactory.cs b/server/AyaNova/biz/BizObjectFactory.cs index 743aee72..d5be9c6b 100644 --- a/server/AyaNova/biz/BizObjectFactory.cs +++ b/server/AyaNova/biz/BizObjectFactory.cs @@ -98,6 +98,8 @@ namespace AyaNova.Biz return new TravelRateBiz(ct, userId, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, roles); case AyaType.TaxCode: return new TaxCodeBiz(ct, userId, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, roles); + case AyaType.ServiceBank: + return new ServiceBankBiz(ct, userId, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, roles); diff --git a/server/AyaNova/biz/BizObjectNameFetcherDirect.cs b/server/AyaNova/biz/BizObjectNameFetcherDirect.cs index 33bdffcf..911c54fe 100644 --- a/server/AyaNova/biz/BizObjectNameFetcherDirect.cs +++ b/server/AyaNova/biz/BizObjectNameFetcherDirect.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; namespace AyaNova.Biz { @@ -10,6 +11,7 @@ namespace AyaNova.Biz { try { + cmd.CommandText = $"select PUBLIC.AYGETNAME({id}, {(int)ayaType}) as m"; // cmd.CommandText = $"SELECT m.{COLUMN} FROM {TABLE} AS m WHERE m.id = {id} LIMIT 1"; using (var dr = cmd.ExecuteReader()) @@ -21,6 +23,18 @@ namespace AyaNova.Biz throw; } } + + //warning: use the above in a loop, not this one + internal static string Name(AyaType ayaType, long id, AyaNova.Models.AyContext ct) + { + using (var command = ct.Database.GetDbConnection().CreateCommand()) + { + ct.Database.OpenConnection(); + return Name(ayaType, id, command); + + } + } + }//eoc }//eons diff --git a/server/AyaNova/biz/NotifyEventHelper.cs b/server/AyaNova/biz/NotifyEventHelper.cs index d0ae9c03..7d7ec054 100644 --- a/server/AyaNova/biz/NotifyEventHelper.cs +++ b/server/AyaNova/biz/NotifyEventHelper.cs @@ -132,7 +132,7 @@ namespace AyaNova.Biz ///////////////////////////////////////// - // PROCESS STANDARD CREATE NOTIFICATION + // PROCESS STANDARD MODIFIED NOTIFICATION // // public static async Task ProcessStandardObjectModifiedEvents(ICoreBizObjectModel newObject, AyContext ct) diff --git a/server/AyaNova/biz/ServiceBankBiz.cs b/server/AyaNova/biz/ServiceBankBiz.cs index b45597c2..88faa535 100644 --- a/server/AyaNova/biz/ServiceBankBiz.cs +++ b/server/AyaNova/biz/ServiceBankBiz.cs @@ -110,8 +110,18 @@ namespace AyaNova.Biz //Name required if (string.IsNullOrWhiteSpace(proposedObj.Name)) + { AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name"); + return; + } + + //Object exists? + if (!await BizObjectExistsInDatabase.ExistsAsync(proposedObj.ObjectType, proposedObj.ObjectId, ct)) + { + AddError(ApiErrorCode.NOT_FOUND, "generalerror", $"Bankable source object specified doesn't exist [type:{proposedObj.ObjectType}, id:{proposedObj.ObjectId}]"); + return; + } /* "CONSTRAINT UNQ_ServiceBank UNIQUE (entrydate, objectid, objecttype, incidentsbalance, hoursbalance, currencybalance), " + "CONSTRAINT UNQ_ServiceBank_Previous_values UNIQUE (lastentrydate, objectid, objecttype, lastincidentsbalance, lasthoursbalance, lastcurrencybalance), " + @@ -241,6 +251,45 @@ namespace AyaNova.Biz await NotifyEventHelper.ProcessStandardObjectEvents(ayaEvent, proposedObj, ct); //SPECIFIC EVENTS FOR THIS OBJECT + var o = (ServiceBank)proposedObj; + + + //SERVICE BANK DEPLETED + { + var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ServiceBankDepleted).ToListAsync(); + string SourceName = string.Empty; + if (subs.Count > 0) + SourceName = BizObjectNameFetcherDirect.Name(o.ObjectType, o.ObjectId, ct); + + foreach (var sub in subs) + { + //not for inactive users + if (!await UserBiz.UserIsActive(sub.UserId)) continue; + + //Has any of the balances changed and is that changed balance within this users selected threshold? + //decvalue here refers to the balance left + if ( + (o.LastCurrencyBalance != null && o.CurrencyBalance != o.LastCurrencyBalance && o.CurrencyBalance <= sub.DecValue) + || (o.LastHoursBalance != null && o.HoursBalance != o.LastHoursBalance && o.HoursBalance <= sub.DecValue) + || (o.LastIncidentsBalance != null && o.IncidentsBalance != o.LastIncidentsBalance && o.IncidentsBalance <= sub.DecValue) + ) + { + + NotifyEvent n = new NotifyEvent() + { + EventType = NotifyEventType.ServiceBankDepleted, + UserId = sub.UserId, + AyaType = o.ObjectType, + ObjectId = o.ObjectId, + NotifySubscriptionId = sub.Id, + Name = SourceName + }; + await ct.NotifyEvent.AddAsync(n); + log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); + await ct.SaveChangesAsync(); + } + } + } }//end of process notifications