Skip to content

Commit

Permalink
Revert "Improve agreement status (#539)" (#540)
Browse files Browse the repository at this point in the history
Co-authored-by: md <[email protected]>
  • Loading branch information
Dahly96 and md authored Nov 21, 2024
1 parent 3ea7bf6 commit bad43c4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 160 deletions.
6 changes: 0 additions & 6 deletions backend/Api/Common/StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public Consultant LoadConsultantForSingleWeek(int consultantId, Week week)
var consultant = _dbContext.Consultant
.Include(c => c.Department)
.ThenInclude(d => d.Organization)
.Include(c => c.Projects)
.ThenInclude(p => p.Agreements)
.Single(c => c.Id == consultantId);

consultant.Staffings = _dbContext.Staffing.Where(staffing =>
Expand All @@ -68,8 +66,6 @@ public Consultant LoadConsultantForWeekSet(int consultantId, List<Week> weeks)
var consultant = _dbContext.Consultant
.Include(c => c.Department)
.ThenInclude(d => d.Organization)
.Include(c => c.Projects)
.ThenInclude(p => p.Agreements)
.Single(c => c.Id == consultantId);


Expand Down Expand Up @@ -100,8 +96,6 @@ private List<Consultant> LoadConsultantsFromDb(string orgUrlKey)
.ThenInclude(department => department.Organization)
.Include(c => c.CompetenceConsultant)
.ThenInclude(cc => cc.Competence)
.Include(c => c.Projects)
.ThenInclude(p => p.Agreements)
.Where(consultant => consultant.Department.Organization.UrlKey == orgUrlKey)
.OrderBy(consultant => consultant.Name)
.ToList();
Expand Down
28 changes: 2 additions & 26 deletions backend/Api/StaffingController/ReadModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,6 @@ private static List<DetailedBooking> DetailedBookings(Consultant consultant,
{
weekSet.Sort();

var projectsStatuses = consultant.Projects.Select(p => {
// Get the endDate furthest away for the engagement
var endDate = p.Agreements
.Where(a => a.EngagementId == p.Id)
.Select(a => a?.EndDate)
.DefaultIfEmpty((DateTime?)null)
.Max();

var dateToday = DateOnly.FromDateTime(DateTime.Today);
var status = endDate.HasValue
? endDate < dateToday.ToDateTime(TimeOnly.MinValue)
? AgreementStatus.Expired
: AgreementStatus.Active
: AgreementStatus.None;

return new {
p.Id,
status
};
}).ToList();




// var billableProjects = UniqueWorkTypes(projects, billableStaffing);
var billableBookings = consultant.Staffings
.Where(staffing => staffing.Engagement.State == EngagementState.Order)
Expand All @@ -125,7 +101,7 @@ private static List<DetailedBooking> DetailedBookings(Consultant consultant,
.Select(grouping => new DetailedBooking(
new BookingDetails(grouping.First().Engagement.Name, BookingType.Booking,
grouping.First().Engagement.Customer.Name,
grouping.Key, false, grouping.First().Engagement.IsBillable, projectsStatuses.First(p => p.Id == grouping.Key).status),
grouping.Key, false, grouping.First().Engagement.IsBillable),
weekSet.Select(week =>
new WeeklyHours(
week.ToSortableInt(), grouping
Expand All @@ -141,7 +117,7 @@ private static List<DetailedBooking> DetailedBookings(Consultant consultant,
.Select(grouping => new DetailedBooking(
new BookingDetails(grouping.First().Engagement.Name, BookingType.Offer,
grouping.First().Engagement.Customer.Name,
grouping.Key, false, grouping.First().Engagement.IsBillable, projectsStatuses.First(p => p.Id == grouping.Key).status),
grouping.Key, false, grouping.First().Engagement.IsBillable),
weekSet.Select(week =>
new WeeklyHours(
week.ToSortableInt(),
Expand Down
10 changes: 1 addition & 9 deletions backend/Api/StaffingController/StaffingReadModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,7 @@ public record BookingDetails(
[property: Required] string CustomerName,
[property: Required] int ProjectId,
[property: Required] bool ExcludeFromBilling = false,
[property: Required] bool IsBillable = false,
[property: Required] AgreementStatus AgreementStatus = AgreementStatus.None);

public enum AgreementStatus
{
Active,
Expired,
None
}
[property: Required] bool IsBillable = false);

public record WeeklyHours([property: Required] int Week, [property: Required] double Hours);

Expand Down
52 changes: 10 additions & 42 deletions backend/Tests/AbsenceTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Api.StaffingController;
using Core.Absences;
using Core.Agreements;
using Core.Consultants;
using Core.Customers;
using Core.DomainModels;
Expand Down Expand Up @@ -57,39 +56,16 @@ public void AvailabilityCalculation(
Consultants = Substitute.For<List<Consultant>>()
};

var customer = new Customer
{
Id = 1,
Name = "TestCustomer",
Organization = org,
Projects = new List<Engagement>()
};

var engagement = new Engagement
{
Id = 1,
Customer = customer,
State = EngagementState.Order,
IsBillable = true,
Staffings = new List<Staffing>(),
Name = "TestProject",

};

customer.Projects.Add(engagement);

Consultant consultant = new()
{
Id = 1,
Name = "Test Variant",
Email = "[email protected]",
GraduationYear = 2010,
Department = department,
Projects = new List<Engagement>()
Department = department
};

consultant.Projects.Add(engagement);

var mondayDateOnly = numberOfHolidays switch
{
0 => new DateOnly(2023, 9, 4), // Week 36, 4th Sept 2023, (no public holidays)
Expand All @@ -100,17 +76,12 @@ public void AvailabilityCalculation(
};

var week = Week.FromDateOnly(mondayDateOnly);



Agreement agreement = new()
{
Id = 1,
EndDate = new DateTime(2023, 12, 31),
StartDate = new DateTime(2023, 1, 1),
EngagementId = 1,
Engagement = engagement,
};
var project = Substitute.For<Engagement>();
var customer = Substitute.For<Customer>();
customer.Name = "TestCustomer";
project.Customer = customer;
project.State = EngagementState.Order;
project.IsBillable = true;


// TODO: Change this to update consultant data
Expand All @@ -137,11 +108,11 @@ public void AvailabilityCalculation(
if (staffedHours > 0)
consultant.Staffings.Add(new Staffing
{
Engagement = engagement,
Engagement = project,
Consultant = consultant,
Hours = staffedHours,
Week = week,
EngagementId = engagement.Id,
EngagementId = project.Id,
ConsultantId = consultant.Id
});

Expand Down Expand Up @@ -191,12 +162,9 @@ public void MultiplePlannedAbsences()
Name = "Test Variant",
Email = "[email protected]",
GraduationYear = 2010,
Department = department,
Projects = new List<Engagement>()
Department = department
};

consultant.Projects.Add(Substitute.For<Engagement>());

var week = new Week(2000, 1);

consultant.PlannedAbsences.Add(new PlannedAbsence
Expand Down
7 changes: 0 additions & 7 deletions frontend/src/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export interface BookingDetails {
/** @format int32 */
projectId: number;
isBillable: boolean;
agreementStatus?: string;
}

export enum AgreementStatus {
Active,
Expired,
None,
}

export enum BookingType {
Expand Down
43 changes: 3 additions & 40 deletions frontend/src/components/Staffing/ConsultantRow.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
"use client";
import {
AgreementStatus,
ConsultantReadModel,
ProjectWithCustomerModel,
} from "@/api-types";
import { ConsultantReadModel, ProjectWithCustomerModel } from "@/api-types";
import React, { useContext, useEffect, useState } from "react";
import { AlertCircle, ChevronDown, Plus } from "react-feather";
import { ChevronDown, Plus } from "react-feather";
import { DetailedBookingRows } from "@/components/Staffing/DetailedBookingRows";
import { WeekCell } from "@/components/Staffing/WeekCell";
import { useModal } from "@/hooks/useModal";
Expand Down Expand Up @@ -82,32 +78,6 @@ export default function ConsultantRows({

const organisationUrl = usePathname().split("/")[1];

function getStatusConsultant(consultant: ConsultantReadModel) {
const statuses = consultant.detailedBooking.map((e) =>
stringToAgreementStatus(e.bookingDetails.agreementStatus),
);

if (statuses) {
if (statuses.includes(AgreementStatus.None)) {
return "red";
} else if (statuses.includes(AgreementStatus.Expired)) {
return "orange";
} else {
return null;
}
}
}

function stringToAgreementStatus(status: string | undefined) {
if (status === "Active") {
return AgreementStatus.Active;
} else if (status === "Expired") {
return AgreementStatus.Expired;
} else {
return AgreementStatus.None;
}
}

useEffect(() => {
async function fetchProject() {
const url = `/${organisationUrl}/bemanning/api/projects?projectId=${selectedProjectId}`;
Expand Down Expand Up @@ -211,14 +181,7 @@ export default function ConsultantRows({
</button>
</td>
<td>
<div className="flex gap-1 items-center">
{getStatusConsultant(currentConsultant)! && (
<AlertCircle
color={getStatusConsultant(currentConsultant)!}
size={20}
/>
)}

<div className="flex gap-2">
<div className="flex flex-row align-center self-center gap-2">
{consultant.imageThumbUrl ? (
<Image
Expand Down
52 changes: 22 additions & 30 deletions frontend/src/components/Staffing/DetailedBookingRows.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
AgreementStatus,
BookingType,
ConsultantReadModel,
DetailedBooking,
Expand Down Expand Up @@ -30,6 +29,7 @@ import { updateBookingHoursBody, updateProjectStateBody } from "@/types";
import { useOutsideClick } from "@/hooks/useOutsideClick";
import { parseYearWeekFromString } from "@/data/urlUtils";
import Link from "next/link";
import { getAgreementsForProject } from "@/actions/agreementActions";

async function updateProjectState(
organisationName: string,
Expand Down Expand Up @@ -67,30 +67,10 @@ export function DetailedBookingRows(props: {
openEngagementAndSetID: (id: number) => void;
numWorkHours: number;
}) {
const colors: {
color: string;
text: string;
icon: Icon;
status: AgreementStatus;
}[] = [
{
color: "green",
text: "Avtale aktiv",
icon: CheckCircle,
status: AgreementStatus.Active,
},
{
color: "red",
text: "Ingen avtaler funnet",
icon: AlertCircle,
status: AgreementStatus.None,
},
{
color: "orange",
text: "Avtale utgått",
icon: AlertCircle,
status: AgreementStatus.Expired,
},
const colors: { color: string; text: string; icon: Icon }[] = [
{ color: "green", text: "Avtale aktiv", icon: CheckCircle },
{ color: "red", text: "Ingen avtaler funnet", icon: AlertCircle },
{ color: "orange", text: "Avtale utgått", icon: AlertCircle },
];
const { setConsultants } = useContext(FilteredContext);

Expand Down Expand Up @@ -132,10 +112,22 @@ export function DetailedBookingRows(props: {
}, []);

async function getColorIcon() {
const status = detailedBooking.bookingDetails?.agreementStatus;
if (status) {
const color = colors.find((c) => AgreementStatus[c.status] === status);
setAlertColor(color);
const agreements = await getAgreementsForProject(
detailedBooking.bookingDetails.projectId,
organisationName,
);
if (agreements) {
const endDate = Math.max(
...agreements.map((p) => new Date(p.endDate).getTime()),
);
const today = new Date().getTime();
if (today > endDate) {
return setAlertColor(colors.find((c) => c.color == "orange"));
} else {
return setAlertColor(colors.find((c) => c.color == "green"));
}
} else {
return setAlertColor(colors.find((c) => c.color == "red"));
}
}

Expand All @@ -146,7 +138,7 @@ export function DetailedBookingRows(props: {
>
<td className=" border-l-secondary border-l-2 w-full">
<div className="relative flex justify-center group items-center">
{alert && <alert.icon color={alert.color} />}
{alert ? <alert.icon color={alert.color} /> : <Loader />}
<div className="absolute left-full ml-2 hidden group-hover:block bg-black text-white text-xs rounded py-1 px-2 w-max z-30">
{alert?.text}
<div className="absolute top-1/2 transform -translate-y-1/2 left-0 ml-[-8px] w-0 h-0 border-r-8 border-r-black border-y-8 border-y-transparent border-l-0"></div>
Expand Down

0 comments on commit bad43c4

Please sign in to comment.