added web hooks

This commit is contained in:
DESKTOP-T0O5CDB\DESK-555BD
2024-03-15 14:59:30 -06:00
parent 5ae1628b7c
commit 44da393369
4 changed files with 123 additions and 0 deletions

View File

@@ -143,6 +143,7 @@ namespace CarCareTracker.Controllers
};
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
}
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"added service record via API {serviceRecord.Description}");
response.Success = true;
response.Message = "Service Record Added";
return Json(response);
@@ -210,6 +211,7 @@ namespace CarCareTracker.Controllers
};
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
}
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"added repair record via API {repairRecord.Description}");
response.Success = true;
response.Message = "Repair Record Added";
return Json(response);
@@ -277,6 +279,7 @@ namespace CarCareTracker.Controllers
};
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
}
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"added upgrade record via API {upgradeRecord.Description}");
response.Success = true;
response.Message = "Upgrade Record Added";
return Json(response);
@@ -330,6 +333,7 @@ namespace CarCareTracker.Controllers
Cost = decimal.Parse(input.Cost)
};
_taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord);
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"added tax record via API {taxRecord.Description}");
response.Success = true;
response.Message = "Tax Record Added";
return Json(response);
@@ -396,6 +400,7 @@ namespace CarCareTracker.Controllers
Mileage = int.Parse(input.Odometer)
};
_odometerRecordDataAccess.SaveOdometerRecordToVehicle(odometerRecord);
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"added odometer record via API {odometerRecord.Mileage.ToString()}");
response.Success = true;
response.Message = "Odometer Record Added";
return Json(response);
@@ -477,6 +482,7 @@ namespace CarCareTracker.Controllers
};
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
}
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, $"added gas record via API {gasRecord.Mileage.ToString()}");
response.Success = true;
response.Message = "Gas Record Added";
return Json(response);

View File

@@ -130,6 +130,10 @@ namespace CarCareTracker.Controllers
if (isNewAddition)
{
_userLogic.AddUserAccessToVehicle(GetUserID(), vehicleInput.Id);
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleInput.Id, User.Identity.Name, "added new vehicle");
} else
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleInput.Id, User.Identity.Name, "edited vehicle");
}
return Json(result);
}
@@ -157,6 +161,10 @@ namespace CarCareTracker.Controllers
_odometerRecordDataAccess.DeleteAllOdometerRecordsByVehicleId(vehicleId) &&
_userLogic.DeleteAllAccessToVehicle(vehicleId) &&
_dataAccess.DeleteVehicle(vehicleId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), vehicleId, User.Identity.Name, "deleted vehicle");
}
return Json(result);
}
[HttpPost]
@@ -661,6 +669,10 @@ namespace CarCareTracker.Controllers
}
gasRecord.Files = gasRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList();
var result = _gasRecordDataAccess.SaveGasRecordToVehicle(gasRecord.ToGasRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), gasRecord.VehicleId, User.Identity.Name, $"saved gas record {gasRecord.Mileage.ToString()}");
}
return Json(result);
}
[HttpGet]
@@ -705,6 +717,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteGasRecordById(int gasRecordId)
{
var result = _gasRecordDataAccess.DeleteGasRecordById(gasRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted gas record id {gasRecordId}");
}
return Json(result);
}
[HttpPost]
@@ -759,6 +775,10 @@ namespace CarCareTracker.Controllers
PushbackRecurringReminderRecordWithChecks(serviceRecord.ReminderRecordId);
}
var result = _serviceRecordDataAccess.SaveServiceRecordToVehicle(serviceRecord.ToServiceRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), serviceRecord.VehicleId, User.Identity.Name, $"saved service record {serviceRecord.Description}");
}
return Json(result);
}
[HttpGet]
@@ -791,6 +811,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteServiceRecordById(int serviceRecordId)
{
var result = _serviceRecordDataAccess.DeleteServiceRecordById(serviceRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted service record id: {serviceRecordId}");
}
return Json(result);
}
#endregion
@@ -836,6 +860,10 @@ namespace CarCareTracker.Controllers
PushbackRecurringReminderRecordWithChecks(collisionRecord.ReminderRecordId);
}
var result = _collisionRecordDataAccess.SaveCollisionRecordToVehicle(collisionRecord.ToCollisionRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), collisionRecord.VehicleId, User.Identity.Name, $"saved repair record {collisionRecord.Description}");
}
return Json(result);
}
[HttpGet]
@@ -868,6 +896,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteCollisionRecordById(int collisionRecordId)
{
var result = _collisionRecordDataAccess.DeleteCollisionRecordById(collisionRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted repair record id {collisionRecordId}");
}
return Json(result);
}
#endregion
@@ -939,6 +971,10 @@ namespace CarCareTracker.Controllers
PushbackRecurringReminderRecordWithChecks(taxRecord.ReminderRecordId);
}
var result = _taxRecordDataAccess.SaveTaxRecordToVehicle(taxRecord.ToTaxRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), taxRecord.VehicleId, User.Identity.Name, $"saved tax record {taxRecord.Description}");
}
return Json(result);
}
[HttpGet]
@@ -972,6 +1008,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteTaxRecordById(int taxRecordId)
{
var result = _taxRecordDataAccess.DeleteTaxRecordById(taxRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted tax record {taxRecordId}");
}
return Json(result);
}
#endregion
@@ -1629,6 +1669,10 @@ namespace CarCareTracker.Controllers
PushbackRecurringReminderRecordWithChecks(upgradeRecord.ReminderRecordId);
}
var result = _upgradeRecordDataAccess.SaveUpgradeRecordToVehicle(upgradeRecord.ToUpgradeRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), upgradeRecord.VehicleId, User.Identity.Name, $"saved upgrade record {upgradeRecord.Description}");
}
return Json(result);
}
[HttpGet]
@@ -1661,6 +1705,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteUpgradeRecordById(int upgradeRecordId)
{
var result = _upgradeRecordDataAccess.DeleteUpgradeRecordById(upgradeRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted upgrade record id {upgradeRecordId}");
}
return Json(result);
}
#endregion
@@ -1685,6 +1733,10 @@ namespace CarCareTracker.Controllers
public IActionResult SaveNoteToVehicleId(Note note)
{
var result = _noteDataAccess.SaveNoteToVehicle(note);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), note.VehicleId, User.Identity.Name, $"saved note {note.Description}");
}
return Json(result);
}
[HttpGet]
@@ -1702,6 +1754,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteNoteById(int noteId)
{
var result = _noteDataAccess.DeleteNoteById(noteId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted note id {noteId}");
}
return Json(result);
}
[HttpPost]
@@ -1823,6 +1879,10 @@ namespace CarCareTracker.Controllers
//move files from temp.
supplyRecord.Files = supplyRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList();
var result = _supplyRecordDataAccess.SaveSupplyRecordToVehicle(supplyRecord.ToSupplyRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), supplyRecord.VehicleId, User.Identity.Name, $"saved supply record {supplyRecord.Description}");
}
return Json(result);
}
[HttpGet]
@@ -1857,6 +1917,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteSupplyRecordById(int supplyRecordId)
{
var result = _supplyRecordDataAccess.DeleteSupplyRecordById(supplyRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted supply record id {supplyRecordId}");
}
return Json(result);
}
#endregion
@@ -1884,6 +1948,10 @@ namespace CarCareTracker.Controllers
planRecord.RequisitionHistory = RequisitionSupplyRecordsByUsage(planRecord.Supplies, DateTime.Parse(planRecord.DateCreated), planRecord.Description);
}
var result = _planRecordDataAccess.SavePlanRecordToVehicle(planRecord.ToPlanRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), planRecord.VehicleId, User.Identity.Name, $"saved plan record {planRecord.Description}");
}
return Json(result);
}
[HttpPost]
@@ -2069,6 +2137,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeletePlanRecordById(int planRecordId)
{
var result = _planRecordDataAccess.DeletePlanRecordById(planRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted plan record {planRecordId}");
}
return Json(result);
}
#endregion
@@ -2108,6 +2180,10 @@ namespace CarCareTracker.Controllers
//move files from temp.
odometerRecord.Files = odometerRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList();
var result = _odometerRecordDataAccess.SaveOdometerRecordToVehicle(odometerRecord.ToOdometerRecord());
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), odometerRecord.VehicleId, User.Identity.Name, $"saved odometer record {odometerRecord.Mileage.ToString()}");
}
return Json(result);
}
[HttpGet]
@@ -2188,6 +2264,10 @@ namespace CarCareTracker.Controllers
public IActionResult DeleteOdometerRecordById(int odometerRecordId)
{
var result = _odometerRecordDataAccess.DeleteOdometerRecordById(odometerRecordId);
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted odometer record {odometerRecordId}");
}
return Json(result);
}
#endregion
@@ -2289,6 +2369,10 @@ namespace CarCareTracker.Controllers
}
}
}
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"moved multiple {source.ToString()} to {destination.ToString()} {string.Join(",", recordIds)}");
}
return Json(result);
}
public IActionResult DeleteRecords(List<int> recordIds, ImportMode importMode)
@@ -2327,6 +2411,10 @@ namespace CarCareTracker.Controllers
break;
}
}
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"deleted multiple {importMode.ToString()} {string.Join(",", recordIds)}");
}
return Json(result);
}
public IActionResult DuplicateRecords(List<int> recordIds, ImportMode importMode)
@@ -2401,6 +2489,10 @@ namespace CarCareTracker.Controllers
break;
}
}
if (result)
{
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), 0, User.Identity.Name, $"duplicated multiple {importMode.ToString()} {string.Join(",", recordIds)}");
}
return Json(result);
}
[HttpPost]

View File

@@ -12,6 +12,7 @@ namespace CarCareTracker.Helper
UserConfig GetUserConfig(ClaimsPrincipal user);
bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData);
bool AuthenticateRootUser(string username, string password);
string GetWebHookUrl();
string GetMOTD();
string GetLogoUrl();
string GetServerLanguage();
@@ -33,6 +34,15 @@ namespace CarCareTracker.Helper
_userConfig = userConfig;
_cache = memoryCache;
}
public string GetWebHookUrl()
{
var webhook = _config["LUBELOGGER_WEBHOOK"];
if (string.IsNullOrWhiteSpace(webhook))
{
webhook = "";
}
return webhook;
}
public string GetMOTD()
{
var motd = _config["LUBELOGGER_MOTD"];

View File

@@ -244,5 +244,20 @@ namespace CarCareTracker.Helper
var motd = config["LUBELOGGER_MOTD"] ?? "Not Configured";
Console.WriteLine($"Message Of The Day: {motd}");
}
public static async void NotifyAsync(string webhookURL, int vehicleId, string username, string action)
{
if (string.IsNullOrWhiteSpace(webhookURL))
{
return;
}
var httpClient = new HttpClient();
var httpParams = new Dictionary<string, string>
{
{ "vehicleId", vehicleId.ToString() },
{ "username", username },
{ "action", action },
};
httpClient.PostAsJsonAsync(webhookURL, httpParams);
}
}
}