This commit is contained in:
@@ -113,23 +113,45 @@ namespace AyaNova.Biz
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name");
|
||||
|
||||
/*
|
||||
"CONSTRAINT CHK_Servicebank_Valid_IncidentBalance CHECK(incidentsbalance >= 0 AND (incidentsbalance = COALESCE(lastincidentsbalance, 0) + incidents)), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_CurrencyBalance CHECK(currencybalance >= 0 AND (currencybalance = COALESCE(lastcurrencybalance, 0) + currency)), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_HoursBalance CHECK(hoursbalance >= 0 AND (hoursbalance = COALESCE(lasthoursbalance, 0) + hours)), " +
|
||||
"CONSTRAINT UNQ_ServiceBank UNIQUE (entrydate, objectid, objecttype, incidentsbalance, hoursbalance, currencybalance), " +
|
||||
"CONSTRAINT UNQ_ServiceBank_Previous_values UNIQUE (lastentrydate, objectid, objecttype, lastincidentsbalance, lasthoursbalance, lastcurrencybalance), " +
|
||||
"CONSTRAINT fk_ServiceBank_self FOREIGN KEY (lastentrydate, objectid, objecttype, lastincidentsbalance, lasthoursbalance, lastcurrencybalance) references aservicebank(entrydate, objectid, objecttype, incidentsbalance, hoursbalance, currencybalance), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_IncidentBalance CHECK(incidentsbalance = COALESCE(lastincidentsbalance, 0) + incidents), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_CurrencyBalance CHECK(currencybalance = COALESCE(lastcurrencybalance, 0) + currency), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_HoursBalance CHECK(hoursbalance = COALESCE(lasthoursbalance, 0) + hours), " +
|
||||
"CONSTRAINT CHK_ServiceBank_Valid_Dates_Sequence CHECK(lastentrydate < entrydate), " +
|
||||
"CONSTRAINT CHK_ServiceBank_Valid_Previous_Columns CHECK((lastentrydate IS NULL AND lastincidentsbalance IS NULL AND lastcurrencybalance IS NULL AND lasthoursbalance IS NULL) OR (lastentrydate IS NOT NULL AND lastincidentsbalance IS NOT NULL AND lastcurrencybalance IS NOT NULL AND lasthoursbalance IS NOT NULL)) "+
|
||||
*/
|
||||
|
||||
//New entry must have *something* to bank
|
||||
if(proposedObj.Incidents==0 && proposedObj.Hours==0 && proposedObj.Currency==0){
|
||||
AddError(ApiErrorCode.INVALID_OPERATION,null,"")
|
||||
}
|
||||
|
||||
if (!(
|
||||
(proposedObj.IncidentsBalance >= 0)
|
||||
&& (proposedObj.IncidentsBalance== proposedObj.LastIncidentsBalance + proposedObj.Incidents)))
|
||||
//New entry must have *something* to bank
|
||||
if (proposedObj.Incidents == 0 && proposedObj.Hours == 0 && proposedObj.Currency == 0)
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE);
|
||||
AddError(ApiErrorCode.INVALID_OPERATION, null, "Nothing to bank");
|
||||
return;
|
||||
}
|
||||
|
||||
//values must add up
|
||||
if (proposedObj.IncidentsBalance != proposedObj.LastIncidentsBalance + proposedObj.Incidents)
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "generalerror", "LT:ServiceBankIncidentsBalance");
|
||||
return;
|
||||
}
|
||||
if (proposedObj.CurrencyBalance != proposedObj.LastCurrencyBalance + proposedObj.Currency)
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "generalerror", "LT:ServiceBankCurrencyBalance");
|
||||
return;
|
||||
}
|
||||
if (proposedObj.HoursBalance != proposedObj.LastHoursBalance + proposedObj.Hours)
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "generalerror", "LT:ServiceBankHoursBalance");
|
||||
return;
|
||||
}
|
||||
|
||||
//date is newer than last entry date?
|
||||
if (proposedObj.LastEntryDate != null && proposedObj.LastEntryDate > proposedObj.EntryDate)
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_STARTDATE_AFTER_ENDDATE, "generalerror", "LastEntryDate is newer than EntryDate");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace AyaNova.Models
|
||||
public string Name { get; set; }
|
||||
[Required]
|
||||
public DateTime EntryDate { get; set; }
|
||||
public DateTime LastEntryDate { get; set; }
|
||||
public DateTime? LastEntryDate { get; set; }
|
||||
[Required]
|
||||
public long ObjectId { get; set; }
|
||||
[Required]
|
||||
@@ -34,17 +34,17 @@ namespace AyaNova.Models
|
||||
public decimal Incidents { get; set; }
|
||||
[Required]
|
||||
public decimal IncidentsBalance { get; set; }
|
||||
public decimal LastIncidentsBalance { get; set; }
|
||||
public decimal? LastIncidentsBalance { get; set; }
|
||||
[Required]
|
||||
public decimal Currency { get; set; }
|
||||
[Required]
|
||||
public decimal CurencyBalance { get; set; }
|
||||
public decimal LastCurencyBalance { get; set; }
|
||||
public decimal CurrencyBalance { get; set; }
|
||||
public decimal? LastCurrencyBalance { get; set; }
|
||||
[Required]
|
||||
public decimal Hours { get; set; }
|
||||
[Required]
|
||||
public decimal HoursBalance { get; set; }
|
||||
public decimal LastHoursBalance { get; set; }
|
||||
public decimal? LastHoursBalance { get; set; }
|
||||
|
||||
|
||||
public ServiceBank()
|
||||
|
||||
@@ -618,19 +618,19 @@ $BODY$ LANGUAGE PLPGSQL STABLE");
|
||||
await ExecQueryAsync("CREATE INDEX areview_tags ON areview using GIN(tags)");
|
||||
|
||||
|
||||
//SERVICE BANK
|
||||
//SERVICE BANK
|
||||
//Note: I'm allowing negative balances so this code differs slightly from the example it was drawn from https://dba.stackexchange.com/a/19368
|
||||
await ExecQueryAsync("CREATE TABLE aservicebank (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name text not null, " +
|
||||
"entrydate timestamp not null, lastentrydate timestamp, objecttype integer not null, objectid bigint not null, sourcetype integer not null, sourceid bigint not null, " +
|
||||
"incidents decimal(19,4) not null, incidentsbalance decimal(19,4) not null, lastincidentsbalance decimal(19,4), " +
|
||||
"currency decimal(19,4) not null, currencybalance decimal(19,4) not null, lastcurrencybalance decimal(19,4), " +
|
||||
"hours decimal(19,4) not null, hoursbalance decimal(19,4) not null, lasthoursbalance decimal(19,4), "+
|
||||
"UNIQUE (entrydate, objectid, objecttype), " +
|
||||
"UNIQUE (entrydate, objectid, objecttype, incidentsbalance, hoursbalance, currencybalance), " +
|
||||
"UNIQUE (lastentrydate, objectid, objecttype, lastincidentsbalance, lasthoursbalance, lastcurrencybalance), " +
|
||||
"entrydate timestamp not null, lastentrydate timestamp null, objecttype integer not null, objectid bigint not null, sourcetype integer not null, sourceid bigint not null, " +
|
||||
"incidents decimal(19,4) not null, incidentsbalance decimal(19,4) not null, lastincidentsbalance decimal(19,4) null, " +
|
||||
"currency decimal(19,4) not null, currencybalance decimal(19,4) not null, lastcurrencybalance decimal(19,4) null, " +
|
||||
"hours decimal(19,4) not null, hoursbalance decimal(19,4) not null, lasthoursbalance decimal(19,4) null, "+
|
||||
"CONSTRAINT UNQ_ServiceBank UNIQUE (entrydate, objectid, objecttype, incidentsbalance, hoursbalance, currencybalance), " +
|
||||
"CONSTRAINT UNQ_ServiceBank_Previous_values UNIQUE (lastentrydate, objectid, objecttype, lastincidentsbalance, lasthoursbalance, lastcurrencybalance), " +
|
||||
"CONSTRAINT fk_ServiceBank_self FOREIGN KEY (lastentrydate, objectid, objecttype, lastincidentsbalance, lasthoursbalance, lastcurrencybalance) references aservicebank(entrydate, objectid, objecttype, incidentsbalance, hoursbalance, currencybalance), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_IncidentBalance CHECK(incidentsbalance >= 0 AND (incidentsbalance = COALESCE(lastincidentsbalance, 0) + incidents)), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_CurrencyBalance CHECK(currencybalance >= 0 AND (currencybalance = COALESCE(lastcurrencybalance, 0) + currency)), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_HoursBalance CHECK(hoursbalance >= 0 AND (hoursbalance = COALESCE(lasthoursbalance, 0) + hours)), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_IncidentBalance CHECK(incidentsbalance = COALESCE(lastincidentsbalance, 0) + incidents), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_CurrencyBalance CHECK(currencybalance = COALESCE(lastcurrencybalance, 0) + currency), " +
|
||||
"CONSTRAINT CHK_Servicebank_Valid_HoursBalance CHECK(hoursbalance = COALESCE(lasthoursbalance, 0) + hours), " +
|
||||
"CONSTRAINT CHK_ServiceBank_Valid_Dates_Sequence CHECK(lastentrydate < entrydate), " +
|
||||
"CONSTRAINT CHK_ServiceBank_Valid_Previous_Columns CHECK((lastentrydate IS NULL AND lastincidentsbalance IS NULL AND lastcurrencybalance IS NULL AND lasthoursbalance IS NULL) OR (lastentrydate IS NOT NULL AND lastincidentsbalance IS NOT NULL AND lastcurrencybalance IS NOT NULL AND lasthoursbalance IS NOT NULL)) "+
|
||||
" )");
|
||||
|
||||
Reference in New Issue
Block a user