Skip to content

Commit

Permalink
Merge pull request #624 from bcgov/2.0.6
Browse files Browse the repository at this point in the history
2.0.6
  • Loading branch information
bcgov-brwang authored Aug 12, 2024
2 parents 02c17c1 + 9ffa20c commit 2692bef
Show file tree
Hide file tree
Showing 39 changed files with 1,557 additions and 871 deletions.
10 changes: 5 additions & 5 deletions .pipeline/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const options = require("@bcgov/pipeline-cli").Util.parseArguments();

const changeId = options.pr; //aka pull-request
const version = "2.0.0";
const version = "2.0.6";
const name = "sbi"; //project name prefix

Object.assign(options.git, { owner: "ychung-mot", repository: "schoolbus" });
Expand Down Expand Up @@ -30,11 +30,11 @@ const phases = {
host: `sbi-e82e9a-dev.apps.silver.devops.gov.bc.ca`,
dotnet_env: "Development",
dbUser: "userUXN",
dbSize: "5Gi",
dbSize: "10Gi",
transient: true,
backupVolume: "schoolbus",
backupVolumeSize: "5Gi",
verificationVolumeSize: "5Gi",
backupVolumeSize: "10Gi",
verificationVolumeSize: "10Gi",
},
test: {
namespace: "e82e9a-test",
Expand All @@ -47,7 +47,7 @@ const phases = {
tag: `test-${version}`,
host: `sbi-e82e9a-test.apps.silver.devops.gov.bc.ca`,
dbUser: "user7KU",
dbSize: "5Gi",
dbSize: "6Gi",
dotnet_env: "Staging",
backupVolume: "schoolbus",
backupVolumeSize: "5Gi",
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The application is being developed as an open source solution.

## Prerequisites

- .Net Core 3.1 SDK
- .Net 7
- Node.JS v13.7.0 or newer
- PostgreSQL 10

Expand Down Expand Up @@ -130,5 +130,4 @@ Please note that this project is released with a [Contributor Code of Conduct](C
## Maintenance

This repository is maintained by [BC Ministry of Transportation](http://www.th.gov.bc.ca/).
Click [here](https://github.com/orgs/bcgov/teams/tran/repositories) for a complete list of our repositories on GitHub.

Click [here](https://github.com/orgs/bcgov/teams/tran/repositories) for a complete list of our repositories on GitHub.
4 changes: 1 addition & 3 deletions Server/SchoolBusAPI/Authentication/SbJwtBearerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ namespace SchoolBusAPI.Authentication
public class SbJwtBearerEvents : JwtBearerEvents
{
private DbAppContext _dbContext;
private ILogger<SbJwtBearerEvents> _logger;

public SbJwtBearerEvents(DbAppContext dbContext, ILogger<SbJwtBearerEvents> logger) : base()
public SbJwtBearerEvents(DbAppContext dbContext) : base()
{
_dbContext = dbContext;
_logger = logger;
}

public override async Task AuthenticationFailed(AuthenticationFailedContext context)
Expand Down
6 changes: 2 additions & 4 deletions Server/SchoolBusAPI/Authorization/PermissionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public static IServiceCollection RegisterPermissionHandler(this IServiceCollecti
/// </remarks>
public class PermissionHandler : AuthorizationHandler<PermissionRequirement>
{
private ILogger _logger;
private IHttpContextAccessor _httpContextAccessor;

public PermissionHandler(ILogger logger, IHttpContextAccessor httpContextAccessor)
public PermissionHandler(IHttpContextAccessor httpContextAccessor)
{
_logger = logger;
_httpContextAccessor = httpContextAccessor;
}

Expand All @@ -53,7 +51,7 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte
}
else
{
_logger.Information("RequiresPermission - {user} - {url}", user.Identity.Name, _httpContextAccessor.HttpContext.Request.Path);
Log.Information("RequiresPermission - {user} - {url}", user.Identity.Name, _httpContextAccessor.HttpContext.Request.Path);
}

return Task.CompletedTask;
Expand Down
51 changes: 40 additions & 11 deletions Server/SchoolBusAPI/DbAppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using Serilog;
using SchoolBusAPI.Extensions;
using SchoolBusAPI.ViewModels;
using SchoolBusCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;

namespace SchoolBusAPI.Models
{
Expand All @@ -33,18 +32,16 @@ public class DbAppContextFactory : IDbAppContextFactory
{
DbContextOptions<DbAppContext> _options;
IHttpContextAccessor _httpContextAccessor;
ILogger<DbAppContext> _logger;

public DbAppContextFactory(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options, ILogger<DbAppContext> logger)
public DbAppContextFactory(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options)
{
_options = options;
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}

public IDbAppContext Create()
{
return new DbAppContext(_httpContextAccessor, _options, _logger);
return new DbAppContext(_httpContextAccessor, _options);
}
}

Expand Down Expand Up @@ -91,16 +88,16 @@ public interface IDbAppContext
public class DbAppContext : DbContext, IDbAppContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<DbAppContext> _logger;

/// <summary>
/// Constructor for Class used for Entity Framework access.
/// </summary>
public DbAppContext(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options, ILogger<DbAppContext> logger)
public DbAppContext(IHttpContextAccessor httpContextAccessor, DbContextOptions<DbAppContext> options)
: base(options)
{
_httpContextAccessor = httpContextAccessor;
_logger = logger;
// To fix datetime utc issue after upgraded to .net 7.0
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
}

/// <summary>
Expand All @@ -114,6 +111,37 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Contact>()
.HasOne(s => s.SchoolBusOwner)
.WithMany(g => g.Contacts);

/*
// Npgsql 6+ datetime type issue
var dateTimeConverter = new ValueConverter<DateTime, DateTime>(
v => v.ToUniversalTime(),
v => DateTime.SpecifyKind(v, DateTimeKind.Utc));
var nullableDateTimeConverter = new ValueConverter<DateTime?, DateTime?>(
v => v.HasValue ? v.Value.ToUniversalTime() : v,
v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : v);
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (entityType.IsKeyless)
{
continue;
}
foreach (var property in entityType.GetProperties())
{
if (property.ClrType == typeof(DateTime))
{
property.SetValueConverter(dateTimeConverter);
}
else if (property.ClrType == typeof(DateTime?))
{
property.SetValueConverter(nullableDateTimeConverter);
}
}
}
*/
}

public virtual DbSet<Audit> Audits { get; set; }
Expand Down Expand Up @@ -252,7 +280,8 @@ public override int SaveChanges()
var modifiedEntries = ChangeTracker.Entries()
.Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted).ToList();

DateTime currentTime = DateTime.UtcNow;
DateTime utcCurrentTime = DateTime.UtcNow;
DateTime currentTime = DateTime.SpecifyKind(utcCurrentTime, DateTimeKind.Unspecified);

foreach (var entry in modifiedEntries)
{
Expand Down Expand Up @@ -341,7 +370,7 @@ public override int SaveChanges()
catch (Exception e)
{
string exceptionMessage = e.ToString();
_logger.LogError($"DbAppContext exception: {exceptionMessage}");
Log.Error($"DbAppContext exception: {exceptionMessage}");
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Server/SchoolBusAPI/Extensions/IDbAppContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using SchoolBusAPI.Models;
using SchoolBusCommon.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -251,7 +252,8 @@ private static void AddInitialUser(this IDbAppContext context, User initialUser)
user.UserRoles.Add(
new UserRole
{
EffectiveDate = DateTime.UtcNow.Date,
EffectiveDate = DateUtils.ConvertPacificToUtcTime(
new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0)),
Role = role
});
}
Expand Down
22 changes: 9 additions & 13 deletions Server/SchoolBusAPI/Hangfire/CcwJobService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Hangfire;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Serilog;
using SchoolBusAPI.Models;
using SchoolBusAPI.Services;
using SchoolBusCcw;
Expand All @@ -21,19 +21,15 @@ public class CcwJobService : ICcwJobService
private readonly DbAppContext _context;
private readonly IConfiguration _configuration;
private readonly ICCWDataService _ccwDataService;
private readonly ILogger _logger;

private readonly string _userId;
private readonly string _userGuid;
private readonly string _userDir;

public CcwJobService(DbAppContext context, IConfiguration configuration, ICCWDataService ccwDataService, ILogger<CcwJobService> logger)
public CcwJobService(DbAppContext context, IConfiguration configuration, ICCWDataService ccwDataService)
{
_context = context;
_configuration = configuration;
_ccwDataService = ccwDataService;
_logger = logger;

_userId = _configuration["CCW_USER_ID"];
_userGuid = _configuration["CCW_USER_GUID"];
_userDir = _configuration["CCW_USER_DIR"];
Expand Down Expand Up @@ -76,17 +72,17 @@ public async Task PopulateCCWJob()

if (cCWData == null)
{
_logger.LogInformation($"[Hangfire] PopulateCCWJob - No data from CCW for regi: {regi} vin: {vin}, plate: {plate}.");
Log.Information($"[Hangfire] PopulateCCWJob - No data from CCW for regi: {regi} vin: {vin}, plate: {plate}.");
_context.SaveChanges();
_logger.LogInformation($"[Hangfire] PopulateCCWJob - Updated bus record timestamp with the ID {data.Id}.");
Log.Information($"[Hangfire] PopulateCCWJob - Updated bus record timestamp with the ID {data.Id}.");
return;
}

data.CCWData = cCWData;

// ensure that the record is touched in the database
_context.SaveChanges();
_logger.LogInformation($"[Hangfire] PopulateCCWJob - Saved bus record with the ID {data.Id}.");
Log.Information($"[Hangfire] PopulateCCWJob - Saved bus record with the ID {data.Id}.");
}
}

Expand All @@ -110,7 +106,7 @@ public async Task UpdateCCWJob()

if (maxUpdateCount > 500)
{
_logger.LogInformation($"[Hangfire] UpdateCCWJob - Today's max update count({maxUpdateCount}) reached.");
Log.Information($"[Hangfire] UpdateCCWJob - Today's max update count({maxUpdateCount}) reached.");
return;
}

Expand Down Expand Up @@ -138,9 +134,9 @@ public async Task UpdateCCWJob()
// fetch did not work, but we don't want it to fire again, so update the timestamp.
if (cCWData == null)
{
_logger.LogInformation($"[Hangfire] UpdateCCWJob - No data from CCW for regi: {regi} vin: {vin}, plate: {plate}.");
Log.Information($"[Hangfire] UpdateCCWJob - No data from CCW for regi: {regi} vin: {vin}, plate: {plate}.");
_context.SaveChanges();
_logger.LogInformation($"[Hangfire] UpdateCCWJob - Updated bus record timestamp with the ID {data.Id}.");
Log.Information($"[Hangfire] UpdateCCWJob - Updated bus record timestamp with the ID {data.Id}.");
return;
}

Expand All @@ -165,7 +161,7 @@ public async Task UpdateCCWJob()
}

_context.SaveChanges();
_logger.LogInformation($"[Hangfire] UpdateCCWJob - Saved bus record with the ID {data.Id}.");
Log.Information($"[Hangfire] UpdateCCWJob - Saved bus record with the ID {data.Id}.");
}
}

Expand Down
8 changes: 3 additions & 5 deletions Server/SchoolBusAPI/Middlewares/ExceptionMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Serilog;
using SchoolBusAPI.Extensions;
using System;
using System.Net;
Expand All @@ -11,11 +11,9 @@ namespace SchoolBusAPI.Middlewares
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;

public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger)
public ExceptionMiddleware(RequestDelegate next)
{
_logger = logger;
_next = next;
}

Expand All @@ -31,7 +29,7 @@ public async Task InvokeAsync(HttpContext httpContext)
return;

var guid = Guid.NewGuid();
_logger.LogError($"HMCR Exception{guid}: {ex}");
Log.Error($"HMCR Exception{guid}: {ex}");
await HandleExceptionAsync(httpContext, guid);
}
}
Expand Down
Loading

0 comments on commit 2692bef

Please sign in to comment.