From 9d9064d25f3a1ad90905ef7156c5faa02fcf75ab Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Wed, 29 Jan 2020 14:12:39 -0800 Subject: [PATCH] Added creation of owner object. Vehicle addition now dynamic based on presence of VIN. --- BodyshopUploader/BodyshopUploader.csproj | 1 + BodyshopUploader/Utils/Auth.cs | 23 ++++++---- .../Utils/Decoder/EstimateDecoder.cs | 31 ++++++++++--- BodyshopUploader/Utils/JobProcessingQueue.cs | 18 +++++--- .../Utils/Queries/VehicleQueries.cs | 45 +++++++++++++++++++ 5 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 BodyshopUploader/Utils/Queries/VehicleQueries.cs diff --git a/BodyshopUploader/BodyshopUploader.csproj b/BodyshopUploader/BodyshopUploader.csproj index a1d28c0..3607c1d 100644 --- a/BodyshopUploader/BodyshopUploader.csproj +++ b/BodyshopUploader/BodyshopUploader.csproj @@ -282,6 +282,7 @@ + diff --git a/BodyshopUploader/Utils/Auth.cs b/BodyshopUploader/Utils/Auth.cs index 80f1ff8..059211e 100644 --- a/BodyshopUploader/Utils/Auth.cs +++ b/BodyshopUploader/Utils/Auth.cs @@ -14,9 +14,13 @@ namespace BodyshopUploader.Utils public static FirebaseAuthLink authlink; static FirebaseAuthProvider ap = new FirebaseAuthProvider(new FirebaseConfig(Utils.AppMetaData.FirebaseAPIKey_DEV)); //TODO: Update this to be a dynamic key. Perhaps a function that fetches? private static Timer tokenTimer = new Timer(); + private static string U; + private static string P; public async static Task<(bool, string)> LoginAsync(string Username, string Password) { + U = Username; + P = Password; try { authlink = await ap.SignInWithEmailAndPasswordAsync(Username, Password); @@ -26,7 +30,6 @@ namespace BodyshopUploader.Utils logger.Trace("Firebase Refresh Token {0}.", authlink.RefreshToken); logger.Trace("Firebase Auth Token expires in {0} seconds.", authlink.ExpiresIn); tokenTimer.Interval = (authlink.ExpiresIn - 600) * 1000; //Set the token to refresh 10 minutes before it has to. - //tokenTimer.Interval = 10000; //Set the token to refresh 10 minutes before it has to. logger.Trace("Refresh timer interval set to {0}ms", (authlink.ExpiresIn - 600) * 1000); tokenTimer.Elapsed += TokenTimer_Tick; tokenTimer.Start(); @@ -73,17 +76,21 @@ namespace BodyshopUploader.Utils logger.Debug("New token: {0}", e.FirebaseAuth.FirebaseToken); } - private static void TokenTimer_Tick(object sender, ElapsedEventArgs e) + private static async Task RefreshToken() { //Gotta do some stuff now that i got a new token! //Maybe the token auto refreshes? logger.Info("Timer Old Token {0}", authlink.FirebaseToken); - //await authlink.RefreshUserDetails(); - //logger.Info("new Token {0}", authlink.FirebaseToken); - //tokenTimer.Stop(); - //tokenTimer.Interval = (authlink.ExpiresIn - 600) * 1000; //Set the token to refresh 10 minutes before it has to. - //tokenTimer.Interval = 10000; - //tokenTimer.Start(); + authlink = await ap.SignInWithEmailAndPasswordAsync(U, P); + logger.Info("new Token {0}", authlink.FirebaseToken); + tokenTimer.Stop(); + tokenTimer.Interval = (authlink.ExpiresIn - 600) * 1000; //Set the token to refresh 10 minutes before it has to. + tokenTimer.Start(); + } + + private static async void TokenTimer_Tick(object sender, ElapsedEventArgs e) + { + await RefreshToken(); } } } diff --git a/BodyshopUploader/Utils/Decoder/EstimateDecoder.cs b/BodyshopUploader/Utils/Decoder/EstimateDecoder.cs index c95b476..c594ade 100644 --- a/BodyshopUploader/Utils/Decoder/EstimateDecoder.cs +++ b/BodyshopUploader/Utils/Decoder/EstimateDecoder.cs @@ -70,6 +70,9 @@ namespace BodyshopUploader.Utils.Decoder }); var readValues = reader.NextRecord(); + dynamic o = new JObject(); + + j.ins_co_id = readValues[0]?.ToString(); j.ins_co_nm = readValues[1]?.ToString(); j.ins_addr1 = readValues[2]?.ToString(); @@ -166,11 +169,11 @@ namespace BodyshopUploader.Utils.Decoder j.ownr_zip = readValues[93]?.ToString(); j.ownr_ctry = readValues[94]?.ToString(); j.ownr_ph1 = readValues[95]?.ToString(); - j.ownr_ph1x = readValues[96]?.ToString(); + //j.ownr_ph1x = readValues[96]?.ToString(); j.ownr_ph2 = readValues[97]?.ToString(); - j.ownr_ph2x = readValues[98]?.ToString(); - j.ownr_fax = readValues[99]?.ToString(); - j.ownr_faxx = readValues[100]?.ToString(); + // j.ownr_ph2x = readValues[98]?.ToString(); + //j.ownr_fax = readValues[99]?.ToString(); + //j.ownr_faxx = readValues[100]?.ToString(); j.ownr_ea = readValues[101]?.ToString(); j.ins_ph1 = readValues[102]?.ToString(); j.ins_ph1x = readValues[103]?.ToString(); @@ -186,6 +189,24 @@ namespace BodyshopUploader.Utils.Decoder j.loss_cat = readValues[113]?.ToString(); j.shopid = AppMetaData.ActiveShopId; + //Set Owner Record + o.ownr_ln = readValues[85]?.ToString(); + o.ownr_fn = readValues[86]?.ToString(); + o.ownr_title = readValues[87]?.ToString(); + o.ownr_co_nm = readValues[88]?.ToString(); + o.ownr_addr1 = readValues[89]?.ToString(); + o.ownr_addr2 = readValues[90]?.ToString(); + o.ownr_city = readValues[91]?.ToString(); + o.ownr_st = readValues[92]?.ToString(); + o.ownr_zip = readValues[93]?.ToString(); + o.ownr_ctry = readValues[94]?.ToString(); + o.ownr_ph1 = readValues[95]?.ToString(); + o.ownr_ph2 = readValues[97]?.ToString(); + o.ownr_ea = readValues[101]?.ToString(); + o.shopid = AppMetaData.ActiveShopId; + + j.owner = new JObject(); + j.owner.data = o; return; } } @@ -390,7 +411,7 @@ namespace BodyshopUploader.Utils.Decoder "V_MAKECODE", "V_MAKEDESC", "V_MODEL", - "V_TYPE", // + "V_TYPE", "V_BSTYLE", "V_TRIMCODE", "TRIM_COLOR", diff --git a/BodyshopUploader/Utils/JobProcessingQueue.cs b/BodyshopUploader/Utils/JobProcessingQueue.cs index 2ca9ac0..1618782 100644 --- a/BodyshopUploader/Utils/JobProcessingQueue.cs +++ b/BodyshopUploader/Utils/JobProcessingQueue.cs @@ -103,15 +103,23 @@ namespace BodyshopUploader.Utils //}; //Add to Holding Queue - dynamic newJob = new JObject(); newJob.uploaded_by = Auth.authlink.User.Email; newJob.bodyshopid = AppMetaData.ActiveShopId; newJob.cieca_id = item.Job.ciecaid; newJob.est_data = item.Job; - newJob.ownr_name = item.Job.ownr_fn + " " + item.Job.ownr_ln; - newJob.vehicle_info = item.Job.vehicle.v_model_yr + " " + item.Job.vehicle.v_make_desc + " " + item.Job.vehicle.v_model_desc; - newJob.clm_no = item.Job.clm_no; + newJob.ownr_name = item.Job.ownr_fn.Value + " " + item.Job.ownr_ln.Value; + newJob.vehicle_info = item.Job.vehicle.data.v_model_yr.Value + " " + item.Job.vehicle.data.v_make_desc.Value + " " + item.Job.vehicle.data.v_model_desc.Value; + newJob.clm_no = item.Job.clm_no.Value; + var vv = item.Job.vehicle.data.v_vin.Value; + + //TODO: This should perform some sort of upsert to update the vehicle with more up to date information. + var vehuuid = await Utils.Queries.VehicleQueries.GetVehicleUuidByVin(vv) ; + if (!string.IsNullOrEmpty(vehuuid)) + { + newJob.est_data.vehicle = null; + newJob.est_data.vehicleid = vehuuid; + } var r = new GraphQLRequest { @@ -139,7 +147,7 @@ namespace BodyshopUploader.Utils { Title = Properties.Resources.Msg_NewJobUploaded, Subtitle = item.Job?.ownr_fn?.Value + " " + item.Job?.ownr_ln?.Value + " | " + item.Job?.clm_no?.Value, - Message = item.Job?.vehicle?.data.v_model_yr?.Value + " " + item.Job?.vehicle?.data.v_make_desc?.Value + " " + item.Job?.vehicle?.data.v_model_desc?.Value + //Message = item.Job?.vehicle?.data?.v_model_yr?.Value + " " + item.Job?.vehicle?.data?.v_make_desc?.Value + " " + item.Job?.vehicle?.data?.v_model_desc?.Value }); }); } diff --git a/BodyshopUploader/Utils/Queries/VehicleQueries.cs b/BodyshopUploader/Utils/Queries/VehicleQueries.cs new file mode 100644 index 0000000..cbb649b --- /dev/null +++ b/BodyshopUploader/Utils/Queries/VehicleQueries.cs @@ -0,0 +1,45 @@ +using GraphQL.Common.Request; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BodyshopUploader.Utils.Queries +{ + public static class VehicleQueries + { + private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); + + public static async Task GetVehicleUuidByVin(string vin) + { + if (string.IsNullOrEmpty(vin)) + return null; + + var r = new GraphQLRequest + { + Query = @"query QUERY_VEHICLE_BY_VIN($vin: String!) { + vehicles(where: {v_vin: {_eq: $vin}}) { + id + } + }", + Variables = new + { + vin = vin + } + }; + + try + { + var d = await Utils.GraphQL.ExecuteQuery(r); + return d.vehicles?[0].id?.Value; + + } + catch (Exception Ex) + { + logger.Error(Ex, "Querying for vehicle by VIN failed."); + return null; + } + } + } +}