Skip to content

Commit

Permalink
Use singular route handler (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigridge authored Nov 30, 2023
1 parent abe35b2 commit dba983d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 204 deletions.
6 changes: 3 additions & 3 deletions backend/Api/StaffingController/ConsultantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ActionResult<List<ConsultantReadModel>> Get(

[HttpPut]
[Route("staffing/update")]
public ActionResult<ConsultantReadModelSingleWeek> Put(
public ActionResult<ConsultantReadModel> Put(
[FromRoute] string orgUrlKey,
[FromBody] StaffingWriteModel staffingWriteModel
)
Expand All @@ -52,7 +52,7 @@ [FromBody] StaffingWriteModel staffingWriteModel

if (!StaffingControllerValidator.ValidateStaffingWriteModel(staffingWriteModel, service, orgUrlKey))
return BadRequest();
var selectedWeek = new Week(staffingWriteModel.Year, staffingWriteModel.Week);
var selectedWeek = new Week(staffingWriteModel.StartYear, staffingWriteModel.StartWeek);
try
{
switch (staffingWriteModel.Type)
Expand Down Expand Up @@ -134,7 +134,7 @@ [FromBody] SeveralStaffingWriteModel severalStaffingWriteModel
}
}

public record StaffingWriteModel(BookingType Type, int ConsultantId, int EngagementId, int Year, int Week,
public record StaffingWriteModel(BookingType Type, int ConsultantId, int EngagementId, int StartYear, int StartWeek,
[property: LongValidator(MinValue = 0, MaxValue = 100)]
double Hours);

Expand Down
6 changes: 3 additions & 3 deletions backend/Api/StaffingController/ReadModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public List<ConsultantReadModel> GetConsultantReadModelsForWeeks(string orgUrlKe
}


public ConsultantReadModelSingleWeek GetConsultantReadModelForWeek(int consultantId, Week week)
public ConsultantReadModel GetConsultantReadModelForWeek(int consultantId, Week week)
{
var consultant = _storageService.LoadConsultantForSingleWeek(consultantId, week);
var readModel = MapToReadModelList(consultant, new List<Week> { week });

return new ConsultantReadModelSingleWeek(consultant, readModel.Bookings.First(),
readModel.DetailedBooking.First(), readModel.IsOccupied);
return new ConsultantReadModel(consultant, new List<BookedHoursPerWeek> { readModel.Bookings.First() },
new List<DetailedBooking> { readModel.DetailedBooking.First() }, readModel.IsOccupied);
}

public ConsultantReadModel GetConsultantReadModelForWeeks( int consultantId, List<Week> weeks)
Expand Down
40 changes: 22 additions & 18 deletions frontend/src/app/[organisation]/bemanning/api/updateHours/route.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import { putWithToken } from "@/data/apiCallsWithToken";
import { parseYearWeekFromString } from "@/data/urlUtils";
import { StaffingWriteModel } from "@/types";
import { StaffingWriteModel, updateBookingHoursBody } from "@/types";
import { NextResponse } from "next/server";

export async function PUT(
request: Request,
{ params }: { params: { organisation: string } },
) {
const orgUrlKey = params.organisation;
const { searchParams } = new URL(request.url);
const consultantID = searchParams.get("consultantID") || "";
const engagementID = searchParams.get("engagementID") || "";
const hours = searchParams.get("hours") || "";
const bookingType = searchParams.get("bookingType") || "";
const selectedWeek = parseYearWeekFromString(
searchParams.get("selectedWeek") || undefined,
const requestBody = (await request.json()) as updateBookingHoursBody;

const startWeek = parseYearWeekFromString(
requestBody.startWeek.toString() || undefined,
);

const endWeek = parseYearWeekFromString(
requestBody.endWeek?.toString() || undefined,
);

const body: StaffingWriteModel = {
type: bookingType,
consultantId: Number(consultantID),
engagementId: Number(engagementID),
year: selectedWeek?.year ?? 0,
week: selectedWeek?.weekNumber ?? 0,
hours: Number(hours),
type: requestBody.bookingType,
consultantId: Number(requestBody.consultantId),
engagementId: Number(requestBody.bookingId),
startYear: startWeek?.year ?? 0,
startWeek: startWeek?.weekNumber ?? 0,
endYear: endWeek?.year ?? 0,
endWeek: endWeek?.weekNumber ?? 0,
hours: Number(requestBody.hours),
};

const url = endWeek
? `${orgUrlKey}/consultants/staffing/update/several`
: `${orgUrlKey}/consultants/staffing/update`;

const staffing =
(await putWithToken<never, StaffingWriteModel>(
`${orgUrlKey}/consultants/staffing/update`,
body,
)) ?? [];
(await putWithToken<never, StaffingWriteModel>(url, body)) ?? [];

return NextResponse.json(staffing);
}

This file was deleted.

134 changes: 41 additions & 93 deletions frontend/src/components/ConsultantRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
BookedHoursPerWeek,
BookingType,
Consultant,
ConsultantReadModelMultipleWeeks,
ConsultantReadModelSingleWeek,
DetailedBooking,
WeeklyHours,
updateBookingHoursBody,
} from "@/types";
import { ReactElement, useContext, useEffect, useRef, useState } from "react";
import {
Expand Down Expand Up @@ -479,42 +478,33 @@ function DetailedBookingRows(props: {
);
}

async function setSingularDetailedBookingHours(
hours: number,
bookingType: string,
organisationName: string,
consultantId: string,
engagementId: string,
week: number,
) {
const url = `/${organisationName}/bemanning/api/updateHours?hours=${hours}&bookingType=${bookingType}&consultantID=${consultantId}&engagementID=${engagementId}&selectedWeek=${week}`;

try {
const data = await fetch(url, {
method: "put",
});
return (await data.json()) as ConsultantReadModelSingleWeek;
} catch (e) {
console.error("Error updating staffing", e);
}
interface updateBookingHoursProps {
hours: number;
bookingType: BookingType;
organisationUrl: string;
consultantId: string;
bookingId: string;
startWeek: number;
endWeek?: number;
}

async function setSeveralDetailedBookingHours(
hours: number,
bookingType: string,
organisationName: string,
consultantId: string,
engagementId: string,
startWeek: number,
endWeek: number,
) {
const url = `/${organisationName}/bemanning/api/updateHours/several?hours=${hours}&bookingType=${bookingType}&consultantID=${consultantId}&engagementID=${engagementId}&startWeek=${startWeek}&endWeek=${endWeek}`;
async function setDetailedBookingHours(props: updateBookingHoursProps) {
const url = `/${props.organisationUrl}/bemanning/api/updateHours`;
const body: updateBookingHoursBody = {
hours: props.hours,
bookingType: props.bookingType,
consultantId: props.consultantId,
bookingId: props.bookingId,
startWeek: props.startWeek,
endWeek: props.endWeek,
};

try {
const data = await fetch(url, {
method: "put",
body: JSON.stringify(body),
});
return (await data.json()) as ConsultantReadModelMultipleWeeks;
return (await data.json()) as Consultant;
} catch (e) {
console.error("Error updating staffing", e);
}
Expand Down Expand Up @@ -559,17 +549,17 @@ function DetailedBookingCell({
function updateSingularHours() {
setIsDisabledHotkeys(false);
if (oldHours != hours && hourDragValue == undefined) {
setSingularDetailedBookingHours(
hours,
detailedBooking.bookingDetails.type,
organisationName,
consultant.id,
detailedBooking.bookingDetails.projectId,
detailedBookingHours.week,
).then((res) => {
setDetailedBookingHours({
hours: hours,
bookingType: detailedBooking.bookingDetails.type,
organisationUrl: organisationName,
consultantId: consultant.id,
bookingId: detailedBooking.bookingDetails.projectId,
startWeek: detailedBookingHours.week,
}).then((res) => {
setConsultants((old) => [
// Use spread to make a new list, forcing a re-render
...upsertConsultantWithSingleWeekBooking(old, res),
...upsertConsultantBooking(old, res),
]);
});
}
Expand All @@ -596,18 +586,18 @@ function DetailedBookingCell({
) {
return;
}
setSeveralDetailedBookingHours(
hourDragValue,
detailedBooking.bookingDetails.type,
organisationName,
consultant.id,
detailedBooking.bookingDetails.projectId,
startDragWeek,
currentDragWeek,
).then((res) => {
setDetailedBookingHours({
hours: hourDragValue,
bookingType: detailedBooking.bookingDetails.type,
organisationUrl: organisationName,
consultantId: consultant.id,
bookingId: detailedBooking.bookingDetails.projectId,
startWeek: startDragWeek,
endWeek: currentDragWeek,
}).then((res) => {
setConsultants((old) => [
// Use spread to make a new list, forcing a re-render
...upsertConsultantWithMultipleWeeksBooking(old, res),
...upsertConsultantBooking(old, res),
]);
});
}
Expand Down Expand Up @@ -741,49 +731,7 @@ function getInfopillVariantByColumnCount(count: number): InfoPillVariant {
}
}

function upsertConsultantWithSingleWeekBooking(
old: Consultant[],
res?: ConsultantReadModelSingleWeek,
) {
if (!res) return old;

const consultantToUpdate = old.find((c) => c.id === res.id);
if (!consultantToUpdate || !res) return old;

consultantToUpdate.bookings = consultantToUpdate.bookings ?? [];
const bookingIndex = consultantToUpdate.bookings.findIndex(
(b) =>
b.year == res.bookings?.year && b.weekNumber == res.bookings.weekNumber,
);
if (bookingIndex !== -1 && res.bookings) {
consultantToUpdate.bookings[bookingIndex] = res.bookings;
}

consultantToUpdate.detailedBooking = consultantToUpdate.detailedBooking ?? [];

if (bookingIndex !== -1 && res.detailedBooking) {
const detailedBookingIndex = consultantToUpdate.detailedBooking.findIndex(
(db) =>
db.bookingDetails.projectId ==
res.detailedBooking?.bookingDetails.projectId,
);
const hoursIndex = consultantToUpdate.detailedBooking[
detailedBookingIndex
].hours.findIndex((h) => h.week == res.detailedBooking?.hours[0].week);
consultantToUpdate.detailedBooking[detailedBookingIndex].hours[hoursIndex] =
res.detailedBooking.hours[0];
}

const consultantIndex = old.findIndex((c) => c.id === `${res.id}`);
old[consultantIndex] = consultantToUpdate;

return [...old];
}

function upsertConsultantWithMultipleWeeksBooking(
old: Consultant[],
res?: ConsultantReadModelMultipleWeeks,
) {
function upsertConsultantBooking(old: Consultant[], res?: Consultant) {
if (!res) return old;

const consultantToUpdate = old.find((c) => c.id === res.id);
Expand Down
Loading

0 comments on commit dba983d

Please sign in to comment.