Skip to content

Commit 3d33a79

Browse files
authored
Merge pull request #1105 from hargata/Hargata/cost.data.fix
Version 1.5.4 Changes
2 parents b3283fc + b8a568b commit 3d33a79

56 files changed

Lines changed: 2629 additions & 172 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CarCareTracker.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
<ItemGroup>
1414
<PackageReference Include="CsvHelper" Version="33.1.0" />
1515
<PackageReference Include="LiteDB" Version="5.0.17" />
16-
<PackageReference Include="MailKit" Version="4.13.0" />
16+
<PackageReference Include="MailKit" Version="4.14.1" />
1717
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.14.0" />
18-
<PackageReference Include="Npgsql" Version="9.0.3" />
18+
<PackageReference Include="Npgsql" Version="9.0.4" />
1919
</ItemGroup>
2020

2121
</Project>

Controllers/APIController.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class APIController : Controller
2424
private readonly ISupplyRecordDataAccess _supplyRecordDataAccess;
2525
private readonly IPlanRecordDataAccess _planRecordDataAccess;
2626
private readonly IPlanRecordTemplateDataAccess _planRecordTemplateDataAccess;
27+
private readonly IInspectionRecordDataAccess _inspectionRecordDataAccess;
28+
private readonly IInspectionRecordTemplateDataAccess _inspectionRecordTemplateDataAccess;
2729
private readonly IUserAccessDataAccess _userAccessDataAccess;
2830
private readonly IUserRecordDataAccess _userRecordDataAccess;
2931
private readonly IReminderHelper _reminderHelper;
@@ -49,6 +51,8 @@ public APIController(IVehicleDataAccess dataAccess,
4951
ISupplyRecordDataAccess supplyRecordDataAccess,
5052
IPlanRecordDataAccess planRecordDataAccess,
5153
IPlanRecordTemplateDataAccess planRecordTemplateDataAccess,
54+
IInspectionRecordDataAccess inspectionRecordDataAccess,
55+
IInspectionRecordTemplateDataAccess inspectionRecordTemplateDataAccess,
5256
IUserAccessDataAccess userAccessDataAccess,
5357
IUserRecordDataAccess userRecordDataAccess,
5458
IMailHelper mailHelper,
@@ -71,6 +75,8 @@ public APIController(IVehicleDataAccess dataAccess,
7175
_supplyRecordDataAccess = supplyRecordDataAccess;
7276
_planRecordDataAccess = planRecordDataAccess;
7377
_planRecordTemplateDataAccess = planRecordTemplateDataAccess;
78+
_inspectionRecordDataAccess = inspectionRecordDataAccess;
79+
_inspectionRecordTemplateDataAccess = inspectionRecordTemplateDataAccess;
7480
_userAccessDataAccess = userAccessDataAccess;
7581
_userRecordDataAccess = userRecordDataAccess;
7682
_mailHelper = mailHelper;
@@ -533,7 +539,8 @@ public IActionResult AddServiceRecord(int vehicleId, GenericRecordExportModel in
533539
VehicleId = vehicleId,
534540
Date = DateTime.Parse(input.Date),
535541
Notes = string.IsNullOrWhiteSpace(input.Notes) ? "" : input.Notes,
536-
Mileage = int.Parse(input.Odometer)
542+
Mileage = int.Parse(input.Odometer),
543+
Files = StaticHelper.CreateAttachmentFromRecord(ImportMode.ServiceRecord, serviceRecord.Id, serviceRecord.Description)
537544
};
538545
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
539546
}
@@ -728,7 +735,8 @@ public IActionResult AddRepairRecord(int vehicleId, GenericRecordExportModel inp
728735
VehicleId = vehicleId,
729736
Date = DateTime.Parse(input.Date),
730737
Notes = string.IsNullOrWhiteSpace(input.Notes) ? "" : input.Notes,
731-
Mileage = int.Parse(input.Odometer)
738+
Mileage = int.Parse(input.Odometer),
739+
Files = StaticHelper.CreateAttachmentFromRecord(ImportMode.RepairRecord, repairRecord.Id, repairRecord.Description)
732740
};
733741
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
734742
}
@@ -925,7 +933,8 @@ public IActionResult AddUpgradeRecord(int vehicleId, GenericRecordExportModel in
925933
VehicleId = vehicleId,
926934
Date = DateTime.Parse(input.Date),
927935
Notes = string.IsNullOrWhiteSpace(input.Notes) ? "" : input.Notes,
928-
Mileage = int.Parse(input.Odometer)
936+
Mileage = int.Parse(input.Odometer),
937+
Files = StaticHelper.CreateAttachmentFromRecord(ImportMode.UpgradeRecord, upgradeRecord.Id, upgradeRecord.Description)
929938
};
930939
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
931940
}
@@ -1546,7 +1555,8 @@ public IActionResult AddGasRecord(int vehicleId, GasRecordExportModel input)
15461555
VehicleId = vehicleId,
15471556
Date = DateTime.Parse(input.Date),
15481557
Notes = string.IsNullOrWhiteSpace(input.Notes) ? "" : input.Notes,
1549-
Mileage = int.Parse(input.Odometer)
1558+
Mileage = int.Parse(input.Odometer),
1559+
Files = StaticHelper.CreateAttachmentFromRecord(ImportMode.GasRecord, gasRecord.Id, $"Gas Record - {gasRecord.Mileage.ToString()}")
15501560
};
15511561
_odometerLogic.AutoInsertOdometerRecord(odometerRecord);
15521562
}
@@ -1652,7 +1662,7 @@ public IActionResult UpdateGasRecord(GasRecordExportModel input)
16521662
[TypeFilter(typeof(CollaboratorFilter))]
16531663
[HttpGet]
16541664
[Route("/api/vehicle/reminders")]
1655-
public IActionResult Reminders(int vehicleId, List<ReminderUrgency> urgencies)
1665+
public IActionResult Reminders(int vehicleId, List<ReminderUrgency> urgencies, string tags)
16561666
{
16571667
if (vehicleId == default)
16581668
{
@@ -1668,6 +1678,11 @@ public IActionResult Reminders(int vehicleId, List<ReminderUrgency> urgencies)
16681678
var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId);
16691679
var reminderResults = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now);
16701680
reminderResults.RemoveAll(x => !urgencies.Contains(x.Urgency));
1681+
if (!string.IsNullOrWhiteSpace(tags))
1682+
{
1683+
var tagsFilter = tags.Split(' ').Distinct();
1684+
reminderResults.RemoveAll(x => !x.Tags.Any(y => tagsFilter.Contains(y)));
1685+
}
16711686
var results = reminderResults.Select(x=> new ReminderAPIExportModel { Id = x.Id.ToString(), Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), UserMetric = x.UserMetric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString(), DueDays = x.DueDays.ToString(), DueDistance = x.DueMileage.ToString(), Tags = string.Join(' ', x.Tags) });
16721687
if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
16731688
{
@@ -1904,7 +1919,7 @@ public IActionResult UploadDocument(List<IFormFile> documents)
19041919
[Authorize(Roles = nameof(UserData.IsRootUser))]
19051920
[HttpGet]
19061921
[Route("/api/vehicle/reminders/send")]
1907-
public IActionResult SendReminders(List<ReminderUrgency> urgencies)
1922+
public IActionResult SendReminders(List<ReminderUrgency> urgencies, string tags)
19081923
{
19091924
if (!urgencies.Any())
19101925
{
@@ -1914,6 +1929,7 @@ public IActionResult SendReminders(List<ReminderUrgency> urgencies)
19141929
var vehicles = _dataAccess.GetVehicles();
19151930
List<OperationResponse> operationResponses = new List<OperationResponse>();
19161931
var defaultEmailAddress = _config.GetDefaultReminderEmail();
1932+
List<string> tagsFilter = !string.IsNullOrWhiteSpace(tags) ? tags.Split(' ').Distinct().ToList() : new List<string>();
19171933
foreach(Vehicle vehicle in vehicles)
19181934
{
19191935
var vehicleId = vehicle.Id;
@@ -1922,6 +1938,10 @@ public IActionResult SendReminders(List<ReminderUrgency> urgencies)
19221938
var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId);
19231939
var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).OrderByDescending(x => x.Urgency).ToList();
19241940
results.RemoveAll(x => !urgencies.Contains(x.Urgency));
1941+
if (tagsFilter.Any())
1942+
{
1943+
results.RemoveAll(x => !x.Tags.Any(y => tagsFilter.Contains(y)));
1944+
}
19251945
if (!results.Any())
19261946
{
19271947
continue;
@@ -2004,6 +2024,8 @@ public IActionResult CleanUp(bool deepClean = false)
20042024
vehicleDocuments.AddRange(_supplyRecordDataAccess.GetSupplyRecordsByVehicleId(vehicle.Id).SelectMany(x => x.Files).Select(y => Path.GetFileName(y.Location)));
20052025
vehicleDocuments.AddRange(_planRecordDataAccess.GetPlanRecordsByVehicleId(vehicle.Id).SelectMany(x => x.Files).Select(y => Path.GetFileName(y.Location)));
20062026
vehicleDocuments.AddRange(_planRecordTemplateDataAccess.GetPlanRecordTemplatesByVehicleId(vehicle.Id).SelectMany(x => x.Files).Select(y => Path.GetFileName(y.Location)));
2027+
vehicleDocuments.AddRange(_inspectionRecordDataAccess.GetInspectionRecordsByVehicleId(vehicle.Id).SelectMany(x => x.Files).Select(y => Path.GetFileName(y.Location)));
2028+
vehicleDocuments.AddRange(_inspectionRecordTemplateDataAccess.GetInspectionRecordTemplatesByVehicleId(vehicle.Id).SelectMany(x => x.Files).Select(y => Path.GetFileName(y.Location)));
20072029
}
20082030
//shop supplies
20092031
vehicleDocuments.AddRange(_supplyRecordDataAccess.GetSupplyRecordsByVehicleId(0).SelectMany(x => x.Files).Select(y => Path.GetFileName(y.Location)));

Controllers/FilesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public IActionResult UploadCoordinates(List<string> coordinates)
102102
string filePath = Path.Combine(uploadPath, fileName);
103103
string fileData = string.Join("\r\n", coordinates);
104104
System.IO.File.WriteAllText(filePath, fileData);
105-
var uploadedFile = new UploadedFiles { Name = "coordinates.csv", Location = Path.Combine("/", uploadDirectory, fileName) };
105+
var uploadedFile = new UploadedFiles { Name = "coordinates.csv", Location = Path.Combine("/", uploadDirectory, fileName), IsPending = true };
106106
return Json(uploadedFile);
107107
}
108108
public IActionResult PreviewFile(string fileName, string fileLocation)

Controllers/HomeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,13 @@ public IActionResult SaveTranslation(IFormFile file)
321321
[HttpPost]
322322
public IActionResult ExportTranslation(IFormFile file)
323323
{
324-
var translationData = new Dictionary<string, string>();
324+
var result = string.Empty;
325325
using (var sReader = new StreamReader(file.OpenReadStream()))
326326
{
327+
var fileName = $"{Guid.NewGuid()}.json";
327328
var sData = sReader.ReadToEnd();
328-
translationData = JsonSerializer.Deserialize<Dictionary<string, string>>(sData);
329+
result = _fileHelper.WriteFileToTemp(fileName, sData);
329330
}
330-
var result = _translationHelper.ExportTranslation(translationData);
331331
return Json(result);
332332
}
333333
[Authorize(Roles = nameof(UserData.IsRootUser))]

Controllers/MigrationController.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ private void InitializeTables(NpgsqlDataSource conn)
5252
"CREATE TABLE IF NOT EXISTS app.tokenrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, body TEXT not null, emailaddress TEXT not null)",
5353
"CREATE TABLE IF NOT EXISTS app.userconfigrecords (id INT primary key, data jsonb not null)",
5454
"CREATE TABLE IF NOT EXISTS app.useraccessrecords (userId INT, vehicleId INT, PRIMARY KEY(userId, vehicleId))",
55-
"CREATE TABLE IF NOT EXISTS app.extrafields (id INT primary key, data jsonb not null)"
55+
"CREATE TABLE IF NOT EXISTS app.extrafields (id INT primary key, data jsonb not null)",
56+
"CREATE TABLE IF NOT EXISTS app.inspectionrecords (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)",
57+
"CREATE TABLE IF NOT EXISTS app.inspectionrecordtemplates (id INT GENERATED BY DEFAULT AS IDENTITY primary key, vehicleId INT not null, data jsonb not null)"
5658
};
5759
foreach(string cmd in cmds)
5860
{
@@ -99,6 +101,8 @@ public IActionResult Export()
99101
var useraccessrecords = new List<UserAccess>();
100102

101103
var extrafields = new List<RecordExtraField>();
104+
var inspectionrecords = new List<InspectionRecord>();
105+
var inspectionrecordtemplates = new List<InspectionRecordInput>();
102106
#region "Part1"
103107
string cmd = $"SELECT data FROM app.vehicles";
104108
using (var ctext = pgDataSource.CreateCommand(cmd))
@@ -416,6 +420,41 @@ public IActionResult Export()
416420
table.Upsert(record);
417421
};
418422
}
423+
cmd = $"SELECT data FROM app.inspectionrecords";
424+
using (var ctext = pgDataSource.CreateCommand(cmd))
425+
{
426+
using (NpgsqlDataReader reader = ctext.ExecuteReader())
427+
while (reader.Read())
428+
{
429+
inspectionrecords.Add(JsonSerializer.Deserialize<InspectionRecord>(reader["data"] as string));
430+
}
431+
}
432+
foreach (var record in inspectionrecords)
433+
{
434+
using (var db = new LiteDatabase(fullFileName))
435+
{
436+
var table = db.GetCollection<InspectionRecord>("inspectionrecords");
437+
table.Upsert(record);
438+
}
439+
;
440+
}
441+
cmd = $"SELECT data FROM app.inspectionrecordtemplates";
442+
using (var ctext = pgDataSource.CreateCommand(cmd))
443+
{
444+
using (NpgsqlDataReader reader = ctext.ExecuteReader())
445+
while (reader.Read())
446+
{
447+
inspectionrecordtemplates.Add(JsonSerializer.Deserialize<InspectionRecordInput>(reader["data"] as string));
448+
}
449+
}
450+
foreach (var record in inspectionrecordtemplates)
451+
{
452+
using (var db = new LiteDatabase(fullFileName))
453+
{
454+
var table = db.GetCollection<InspectionRecordInput>("inspectionrecordtemplates");
455+
table.Upsert(record);
456+
};
457+
}
419458
#endregion
420459
var destFilePath = $"{fullFolderPath}.zip";
421460
ZipFile.CreateFromDirectory(fullFolderPath, destFilePath);
@@ -464,6 +503,8 @@ public IActionResult Import(string fileName)
464503
var useraccessrecords = new List<UserAccess>();
465504

466505
var extrafields = new List<RecordExtraField>();
506+
var inspectionrecords = new List<InspectionRecord>();
507+
var inspectionrecordtemplates = new List<InspectionRecordInput>();
467508
#region "Part1"
468509
using (var db = new LiteDatabase(fullFileName))
469510
{
@@ -743,6 +784,38 @@ public IActionResult Import(string fileName)
743784
ctext.ExecuteNonQuery();
744785
}
745786
}
787+
using (var db = new LiteDatabase(fullFileName))
788+
{
789+
var table = db.GetCollection<InspectionRecord>("inspectionrecords");
790+
inspectionrecords = table.FindAll().ToList();
791+
};
792+
foreach (var record in inspectionrecords)
793+
{
794+
string cmd = $"INSERT INTO app.inspectionrecords (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb)); SELECT setval('app.inspectionrecords_id_seq', (SELECT MAX(id) from app.inspectionrecords));";
795+
using (var ctext = pgDataSource.CreateCommand(cmd))
796+
{
797+
ctext.Parameters.AddWithValue("id", record.Id);
798+
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
799+
ctext.Parameters.AddWithValue("data", JsonSerializer.Serialize(record));
800+
ctext.ExecuteNonQuery();
801+
}
802+
}
803+
using (var db = new LiteDatabase(fullFileName))
804+
{
805+
var table = db.GetCollection<InspectionRecordInput>("inspectionrecordtemplates");
806+
inspectionrecordtemplates = table.FindAll().ToList();
807+
};
808+
foreach (var record in inspectionrecordtemplates)
809+
{
810+
string cmd = $"INSERT INTO app.inspectionrecordtemplates (id, vehicleId, data) VALUES(@id, @vehicleId, CAST(@data AS jsonb)); SELECT setval('app.inspectionrecordtemplates_id_seq', (SELECT MAX(id) from app.inspectionrecordtemplates));";
811+
using (var ctext = pgDataSource.CreateCommand(cmd))
812+
{
813+
ctext.Parameters.AddWithValue("id", record.Id);
814+
ctext.Parameters.AddWithValue("vehicleId", record.VehicleId);
815+
ctext.Parameters.AddWithValue("data", JsonSerializer.Serialize(record));
816+
ctext.ExecuteNonQuery();
817+
}
818+
}
746819
#endregion
747820
return Json(OperationResponse.Succeed("Data Imported Successfully"));
748821
}

Controllers/Vehicle/GasController.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,24 @@ public IActionResult SaveGasRecordToVehicleId(GasRecordInput gasRecord)
4040
{
4141
return Json(false);
4242
}
43-
if (gasRecord.Id == default && _config.GetUserConfig(User).EnableAutoOdometerInsert)
43+
gasRecord.Files = gasRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList();
44+
var convertedRecord = gasRecord.ToGasRecord();
45+
var result = _gasRecordDataAccess.SaveGasRecordToVehicle(convertedRecord);
46+
if (result)
47+
{
48+
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), WebHookPayload.FromGasRecord(convertedRecord, gasRecord.Id == default ? "gasrecord.add" : "gasrecord.update", User.Identity.Name));
49+
}
50+
if (convertedRecord.Id != default && gasRecord.Id == default && _config.GetUserConfig(User).EnableAutoOdometerInsert)
4451
{
4552
_odometerLogic.AutoInsertOdometerRecord(new OdometerRecord
4653
{
4754
Date = DateTime.Parse(gasRecord.Date),
4855
VehicleId = gasRecord.VehicleId,
4956
Mileage = gasRecord.Mileage,
50-
Notes = $"Auto Insert From Gas Record. {gasRecord.Notes}"
57+
Notes = $"Auto Insert From Gas Record. {gasRecord.Notes}",
58+
Files = StaticHelper.CreateAttachmentFromRecord(ImportMode.GasRecord, convertedRecord.Id, $"Gas Record - {gasRecord.Mileage.ToString()}")
5159
});
5260
}
53-
gasRecord.Files = gasRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList();
54-
var result = _gasRecordDataAccess.SaveGasRecordToVehicle(gasRecord.ToGasRecord());
55-
if (result)
56-
{
57-
StaticHelper.NotifyAsync(_config.GetWebHookUrl(), WebHookPayload.FromGasRecord(gasRecord.ToGasRecord(), gasRecord.Id == default ? "gasrecord.add" : "gasrecord.update", User.Identity.Name));
58-
}
5961
return Json(result);
6062
}
6163
[TypeFilter(typeof(CollaboratorFilter))]

0 commit comments

Comments
 (0)