diff --git a/AyaNovaQBI/util.cs b/AyaNovaQBI/util.cs
index e85db31..7dd176d 100644
--- a/AyaNovaQBI/util.cs
+++ b/AyaNovaQBI/util.cs
@@ -566,20 +566,12 @@ namespace AyaNovaQBI
await PopulateAyaListCache();
if (string.IsNullOrWhiteSpace(QBIntegration.IntegrationData) || QBIntegration.Items.Count == 0)
- return true;//all ok, no maps
-
- //PFC - Validate settings, create if necessary (Util.ValidateSettings()) and save
- //TODO: if QBIntegration.IntegrationData==null then nothing is set yet and it's fresh so trigger the setup stuff
- // but perhaps this should be done / called by mainform as this block and function is strictly related to initialization and caching only
- // and mapping and setup is not really part of that
- // So, if there are no mappings and integrationData=null just return but if there are then they need to be validated here
-
-// next: if QBIntegration.IntegrationData==null then we're done for now let mainform handle initial setup
- //also if there are no mappings then same thing, let mainform handle setup
- //However, if there *are* integration data or mappings then they need to be validated here
-
+ return true;//nothing mapped or set to validate return to mainform for initial setup to be triggered
//PFC - verify integration mapped objects still exist at each end (Util.PreFlightCheck() line 199)
+ if (!await ValidateMap(initErrors))
+ return false;
+
//DONE
}
}
@@ -643,17 +635,59 @@ namespace AyaNovaQBI
return false;
}
-
}
-
- public static async Task IntegrationLog(string logLine)
+ ///
+ /// Ensure existance of QBI Integration object
+ ///
+ ///
+ ///
+ public static async Task ValidateMap(StringBuilder initErrors)
{
+ ApiResponse r = null;
+ try
+ {
+ r = await GetAsync($"integration/exists/{QBI_INTEGRATION_ID}");
- await PostAsync("integration/log", Newtonsoft.Json.JsonConvert.SerializeObject(new NameIdItem { Id = QBIntegration.Id, Name = logLine }));
+ if (r.ObjectResponse["data"].Value() == false)
+ {
+ //doesn't exist, need to create it now
+ QBIntegration = new Integration();
+ QBIntegration.IntegrationAppId = QBI_INTEGRATION_ID;
+ QBIntegration.Active = true;
+ QBIntegration.Name = "QBI - QuickBooks Desktop integration";
+ r = await PostAsync("integration", Newtonsoft.Json.JsonConvert.SerializeObject(QBIntegration));
+ QBIntegration.Id = IdFromResponse(r);
+ await IntegrationLog("AyaNova QBI Integration installed to AyaNova");
+ }
+ else
+ {
+ //Exists, fetch it check if active then we're done here
+ r = await GetAsync($"integration/{QBI_INTEGRATION_ID}");
+ QBIntegration = r.ObjectResponse["data"].ToObject();
+ if (!QBIntegration.Active)
+ {
+ initErrors.AppendLine("QBI Integration is currently deactivated and can not be used\r\nThis setting can be changed in AyaNova in the Administration section -> Integrations -> QuickBooks Desktop integration record\r\nSet to active and save to enable QBI");
+ return false;
+ }
+ }
+
+ return true;
+ }
+ catch (Exception ex)
+ {
+ initErrors.AppendLine("Error fetching QBI Integration object");
+ initErrors.AppendLine(ex.Message);
+ initErrors.AppendLine(r.CompactResponse);
+ return false;
+
+ }
}
+ public static async Task IntegrationLog(string logLine) => await PostAsync("integration/log", Newtonsoft.Json.JsonConvert.SerializeObject(new NameIdItem { Id = QBIntegration.Id, Name = logLine }));
+
+
#region PFC QB side
///
/// Open QB connection
@@ -812,10 +846,6 @@ namespace AyaNovaQBI
}
#endregion pfc qb side
-
-
-
-
#region QB Specific utils
@@ -935,13 +965,6 @@ namespace AyaNovaQBI
#endregion qb specific utils
- #region QB API helper methods/ attributes/cached lists
- ///
- /// Populate or repopulate the list of
- ///
-
- #endregion qb specific utils
-
#region QB API helper methods/ attributes/cached lists
///
/// Populate or repopulate the list of
@@ -2457,7 +2480,6 @@ namespace AyaNovaQBI
#endregion
-
#region AyaNova cached lists
public async static Task PopulateAyaListCache()
{
@@ -2590,8 +2612,7 @@ namespace AyaNovaQBI
#endregion
-
- #endregion qb stuff
+ #endregion qb specific non-api stuff
}