Skip to content

Commit

Permalink
Merge pull request #126 from bcgov/feature/JASPER-219v2
Browse files Browse the repository at this point in the history
JASPER-219: Create the connection between JASPER and the PCSS APIs
  • Loading branch information
JTraill authored Jan 9, 2025
2 parents 4953fee + 2c745f5 commit fdc52b3
Show file tree
Hide file tree
Showing 59 changed files with 4,073 additions and 3,123 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/publish-infra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ jobs:
- name: Terraform Init
id: init
run: |
terraform init -input=false -backend-config=backend.tfvars -var-file=${{ inputs.environment }}.tfvars
terraform init -input=false -backend-config=backend.tfvars -var-file=${{ env.environment }}.tfvars
working-directory: ${{ env.WORKING_DIRECTORY }}

- name: Terraform Plan (Initial Stack)
id: plan-initial
run: |
terraform plan -target=module.initial -no-color -input=false -var-file=${{ inputs.environment }}.tfvars
terraform plan -target=module.initial -no-color -input=false -var-file=${{ env.environment }}.tfvars
continue-on-error: true
working-directory: ${{ env.WORKING_DIRECTORY }}

Expand All @@ -103,7 +103,7 @@ jobs:

- name: Terraform Apply (Initial Stack)
run: |
terraform apply -target=module.initial --auto-approve -input=false -var-file=${{ inputs.environment }}.tfvars
terraform apply -target=module.initial --auto-approve -input=false -var-file=${{ env.environment }}.tfvars
working-directory: ${{ env.WORKING_DIRECTORY }}

- name: Log in to the GHCR
Expand Down Expand Up @@ -144,7 +144,7 @@ jobs:
shell: bash
run: |
IMAGE_TAG=${{ env.DUMMY_IMAGE_NAME }}
REPOSITORY_NAME=${{ vars.APP_NAME }}-app-repo-${{ inputs.environment }}
REPOSITORY_NAME=${{ vars.APP_NAME }}-app-repo-${{ env.environment }}
IMAGE_EXISTS=$(aws ecr describe-images --repository-name $REPOSITORY_NAME --query "imageDetails[?contains(imageTags, '$IMAGE_TAG')]" --output text)
Expand All @@ -169,7 +169,7 @@ jobs:
shell: bash
run: |
IMAGE_TAG=${{ env.DUMMY_IMAGE_NAME }}
REPOSITORY_NAME=${{ vars.APP_NAME }}-lambda-repo-${{ inputs.environment }}
REPOSITORY_NAME=${{ vars.APP_NAME }}-lambda-repo-${{ env.environment }}
IMAGE_EXISTS=$(aws ecr describe-images --repository-name $REPOSITORY_NAME --query "imageDetails[?contains(imageTags, '$IMAGE_TAG')]" --output text)
Expand All @@ -192,7 +192,7 @@ jobs:
- name: Terraform Plan (Main Stack)
id: plan-main
run: |
terraform plan -no-color -input=false -var-file=${{ inputs.environment }}.tfvars
terraform plan -no-color -input=false -var-file=${{ env.environment }}.tfvars
continue-on-error: true
working-directory: ${{ env.WORKING_DIRECTORY }}

Expand All @@ -202,5 +202,5 @@ jobs:

- name: Terraform Apply (Main Stack)
run: |
terraform apply --auto-approve -input=false -var-file=${{ inputs.environment }}.tfvars
terraform apply --auto-approve -input=false -var-file=${{ env.environment }}.tfvars
working-directory: ${{ env.WORKING_DIRECTORY }}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sonarlint.connectedMode.project": {
"connectionId": "bcgov-sonarcloud",
"projectKey": "bcgov_jasper"
}
}
170 changes: 113 additions & 57 deletions api/Controllers/DashboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Scv.Api.Helpers.Extensions;
using Scv.Api.Infrastructure.Authorization;
using Scv.Api.Services;
using Scv.Api.Models.Lookup;
using PCSS.Models.REST.JudicialCalendar;
using Microsoft.VisualBasic;
using Scv.Api.Helpers;
using Scv.Api.Infrastructure.Authorization;
using Scv.Api.Models.Calendar;
using Scv.Api.Models.Lookup;
using Scv.Api.Services;

namespace Scv.Api.Controllers
{
Expand All @@ -20,67 +19,117 @@ namespace Scv.Api.Controllers
public class DashboardController : ControllerBase
{
#region Variables
private readonly LocationService _locationService;

private readonly JudicialCalendarService _judicialCalendarService;

#endregion Variables

#region Constructor
public DashboardController(LocationService locationService, JudicialCalendarService judicialCalendarService)

public DashboardController(JudicialCalendarService judicialCalendarService)
{
_locationService = locationService;
_judicialCalendarService = judicialCalendarService;
}

#endregion Constructor

/// <summary>
/// Returns list of assignemnts for a given month and year for current user.
/// Retrieves the events for the specified month.
/// </summary>
/// <param name="year">selected year</param>
/// <param name="month">selected month</param>
/// <param name="locationId">selected month</param>
/// <param name="locationIds">List of location ids</param>
/// <returns></returns>
// [HttpGet("monthly-schedule/{year}/{month}")]
[HttpGet]
[Route("monthly-schedule/{year}/{month}")]
public async Task<ActionResult<CalendarSchedule>> GetMonthlySchedule(int year, int month, [FromQuery] string locationId = "")
[Route("monthly-schedule")]
public async Task<ActionResult<CalendarSchedule>> GetMonthlySchedule([FromQuery] int year, [FromQuery] int month, [FromQuery] string locationIds = "")
{
try
{
#region Calculate Start and End Dates of the calendar month

// could be replaced if found on a front end in calendar properties
var startMonthDifference = GetWeekFirstDayDifference(month, year);
var endMonthDifference = GetLastDayOfMonthWeekDifference(month, year);
// first day of the month and a week before the first day of the month
var startDate = new DateTime(year, month, 1).AddDays(-7);
var startDate = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Local).AddDays(-startMonthDifference);
// last day of the month and a week after the last day of the month
var endDate = startDate.AddMonths(1).AddDays(-1).AddDays(7);
var calendars = await _judicialCalendarService.JudicialCalendarsGetAsync(locationId, startDate, endDate);
var endDate = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Local).AddMonths(1).AddDays(6).AddDays(endMonthDifference);
#endregion Calculate Start and End Dates

CalendarSchedule calendarSchedule = new CalendarSchedule();
var isMySchedule = string.IsNullOrWhiteSpace(locationIds);

// Test Judge Id
var judgeId = 190;

// Get "Judge's Calendar" when no LocationIds provided. Otherwise, get all judge's calendar for the provided LocationIds.
var calendars = isMySchedule
? [await _judicialCalendarService.GetJudgeCalendarAsync(judgeId, startDate, endDate)]
: await _judicialCalendarService.JudicialCalendarsGetAsync(locationIds, startDate, endDate);

// check if the calendar is empty and return empty schedule - do we need it at all?
if (calendars == null)
{
return Ok(calendarSchedule);
}

var calendarDays = MapperHelper.CalendarToDays(calendars.ToList());
if (calendarDays == null)
{
calendarSchedule.Schedule = new List<CalendarDay>();
calendarSchedule.Schedule = new List<CalendarDay>();
}
else
else
{
if (isMySchedule)
calendarDays = calendarDays.Where(t => t.Assignment != null && t.Assignment.JudgeId == judgeId).ToList();
calendarSchedule.Schedule = calendarDays;
}

calendarSchedule.Presiders = calendars.Where(t => t.IsPresider).Select(presider => new FilterCode
{
Text = $"{presider.RotaInitials} - {presider.Name}",
Value = $"{presider.ParticipantId}",
}).DistinctBy(t => t.Value).OrderBy(x => x.Value).ToList();

var assignmentsList = calendars.Where(t => t.IsPresider)
.Where(t => t.Days?.Count > 0)
.SelectMany(t => t.Days).Where(day => day.Assignment != null && (day.Assignment.ActivityAm !=null || day.Assignment.ActivityPm != null))
.Select(day => day.Assignment)
.ToList();
var activitiesList = assignmentsList
.SelectMany(t => new[] { t.ActivityAm, t.ActivityPm })
.Where(activity => activity != null)
calendarSchedule.Presiders = calendars
.Where(t => t.IsPresider && t.Days.Any())
.Select(presider => new FilterCode
{
Text = $"{presider.RotaInitials} - {presider.Name}",
Value = $"{presider.Days[0].JudgeId}",
})
.DistinctBy(t => t.Value)
.OrderBy(x => x.Value)
.ToList();

// check if it should isJudge or IsPresider
var assignmentsListFull = calendars.Where(t => t.IsPresider)
.Where(t => t.Days?.Count > 0)
.SelectMany(t => t.Days).Where(day => day.Assignment != null)
.Select(day => day.Assignment)
.ToList();

var activitiesList = assignmentsListFull
.Where(activity => activity != null && activity.ActivityCode != null && activity.ActivityDescription != null)
.Select(activity => new FilterCode
{
Text = activity.ActivityDescription,
Value = activity.ActivityCode
})
}).ToList();


// merging activities information form activityAm and activityPm, and assignmentsListFull
var assignmentsList = calendars.Where(t => t.IsPresider)
.Where(t => t.Days?.Count > 0)
.SelectMany(t => t.Days).Where(day => day.Assignment != null && (day.Assignment.ActivityAm != null || day.Assignment.ActivityPm != null))
.Select(day => day.Assignment)
.ToList();

activitiesList.AddRange(assignmentsList
.SelectMany(t => new[] { t.ActivityAm, t.ActivityPm })
.Where(activity => activity != null && activity.ActivityCode != null && activity.ActivityDescription != null)
.Select(activity => new FilterCode
{
Text = activity.ActivityDescription,
Value = activity.ActivityCode
}));

activitiesList = activitiesList
.DistinctBy(t => t.Value)
.OrderBy(x => x.Text)
.ToList();
Expand All @@ -90,38 +139,45 @@ public async Task<ActionResult<CalendarSchedule>> GetMonthlySchedule(int year, i
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());

// Log the exception
return StatusCode(500, "Internal server error");
}
}

//public async Task<ActionResult<List<FilterCode>>> LocationList(int a)
/// <summary>
/// Provides locations.
/// </summary>
/// <returns>IEnumerable{FilterCode}</returns>
[HttpGet]
[Route("locations")]
public async Task<ActionResult<IEnumerable<FilterCode>>> LocationList()
#region Helpers

//calcluate the difference between the first day of the month and the first day of the week for the calendar

private static int GetWeekFirstDayDifference(int month, int year)
{
try
{
var locations = await _locationService.GetLocations();
var locationList = locations.Where(t => t.Flex?.Equals("Y") == true).Select(location => new FilterCode
{
Text = location.LongDesc,
Value = location.ShortDesc
}).OrderBy(x => x.Text);
var firstDayOfMonth = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Local);
return (int)firstDayOfMonth.DayOfWeek - (int)FirstDayOfWeek.Sunday + 1;
}

return Ok(locationList);
}
catch (Exception ex)
private static int GetLastDayOfMonthWeekDifference(int month, int year)
{
var lastDayOfMonth = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Local).AddMonths(1).AddDays(-1);
int difference = (int)FirstDayOfWeek.Saturday - (int)lastDayOfMonth.DayOfWeek;
// calendar seems to add a week if the difference is 0
if (difference <= 0)
difference = 7 + difference;
// if calendar is 5 weeks we need to add a week
var firstDayOfMonth = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Local);
var totalDays = (lastDayOfMonth - firstDayOfMonth).Days + 1;
var fullWeeks = totalDays / 7;
if (totalDays % 7 > 0)
{
// Log the exception
return StatusCode(500, "Internal server error" + ex.Message);
fullWeeks++;
}
}
}
if (fullWeeks == 5)
_ = difference + 7;


}
return difference;
}

#endregion Helpers
}
}
16 changes: 15 additions & 1 deletion api/Controllers/LocationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,24 @@ public async Task<ActionResult<List<Location>>> GetLocationsAndCourtRooms()
foreach (var location in locationList)
{
location.CourtRooms = courtRooms.Where(cr => cr.Flex == location.LocationId && (cr.ShortDesc == "CRT" || cr.ShortDesc == "HGR"))
.Select(cr => new CourtRoom {LocationId = cr.Flex, Room = cr.Code, Type = cr.ShortDesc}).ToList();
.Select(cr => new CourtRoom { LocationId = cr.Flex, Room = cr.Code, Type = cr.ShortDesc }).ToList();
}

return Ok(locationList);
}


/// <summary>
/// Returns the list of locations used in PCSS
/// </summary>
/// <returns>PCSS Locations</returns>
[HttpGet]
[Route("pcss")]
public async Task<ActionResult<List<Location>>> GetPCSSLocations()
{
var locations = await _locationService.GetPCSSLocations();

return Ok(locations);
}
}
}
10 changes: 5 additions & 5 deletions api/Helpers/MapperHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using AutoMapper;
using System.Collections.Generic;
using Scv.Api.Models.Calendar;
using Scv.Api.Mappers;
using PCSS.Models.REST.JudicialCalendar;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using PCSSCommon.Models;
using Scv.Api.Mappers;
using Scv.Api.Models.Calendar;

namespace Scv.Api.Helpers
{
Expand Down
Loading

0 comments on commit fdc52b3

Please sign in to comment.