Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/update project name backend #501

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions backend/Api/Projects/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,41 @@ public ActionResult<List<StaffingReadModel>> Put([FromRoute] string orgUrlKey,
}
}

[HttpPut]
[Route("updateProjectName")]
public ActionResult<List<EngagementReadModel>> Put([FromRoute] string orgUrlKey,
[FromBody] UpdateProjectNameWriteModel projectWriteModel)
{
// Merk: Service kommer snart via Dependency Injection, da slipper å lage ny hele tiden
var service = new StorageService(_cache, _context);

if (!ProjectControllerValidator.ValidateUpdateProjectNameWriteModel(projectWriteModel, service, orgUrlKey))
return BadRequest("Error in data");
if (ProjectControllerValidator.ValidateUpdateProjectNameAlreadyExist(projectWriteModel, service, orgUrlKey))
{return BadRequest("Name already in use");}

try
{
Engagement engagement;
engagement = _context.Project
.Include(p => p.Consultants)
.Include(p => p.Staffings)
.Single(p => p.Id == projectWriteModel.EngagementId);

engagement.Name = projectWriteModel.EngagementName;
_context.SaveChanges();

service.ClearConsultantCache(orgUrlKey);

return Ok();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

private bool EngagementHasSoftMatch(int id)
{
var engagementToChange = _context.Project
Expand Down
22 changes: 22 additions & 0 deletions backend/Api/Projects/ProjectControllerValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ public static bool ValidateUpdateProjectWriteModel(UpdateProjectWriteModel updat
CheckIfEngagementIsInOrganisation(updateProjectWriteModel.EngagementId, storageService, orgUrlKey);
}

public static bool ValidateUpdateProjectNameWriteModel(UpdateProjectNameWriteModel updateProjectNameWriteModel, StorageService storageService, string orgUrlKey)
{
return CheckIfEngagementExists(updateProjectNameWriteModel.EngagementId, storageService) &&
CheckIfEngagementIsInOrganisation(updateProjectNameWriteModel.EngagementId, storageService, orgUrlKey) && !string.IsNullOrWhiteSpace(updateProjectNameWriteModel.EngagementName);
}

public static bool ValidateUpdateProjectNameAlreadyExist(UpdateProjectNameWriteModel updateProjectNameWriteModel,
StorageService storageService, string orgUrlKey)
{
var updatedEngagement = storageService.GetProjectById(updateProjectNameWriteModel.EngagementId);
if (updatedEngagement is not null)
{
var customer = storageService.GetCustomerFromId(orgUrlKey, updatedEngagement.CustomerId);
if (customer is not null)
{
return customer.Projects.Any(engagement => string.Equals(engagement.Name,
updateProjectNameWriteModel.EngagementName, StringComparison.OrdinalIgnoreCase));
}
}
return false;
}

private static bool CheckIfEngagementExists(int engagementId, StorageService storageService)
{
return storageService.GetProjectById(engagementId) is not null;
Expand Down
2 changes: 2 additions & 0 deletions backend/Api/Projects/ProjectModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public record ProjectWithCustomerModel(
public record UpdateProjectWriteModel(int EngagementId, EngagementState ProjectState, int StartYear, int StartWeek,
int WeekSpan);

public record UpdateProjectNameWriteModel(int EngagementId, string EngagementName);


public record CustomersWithProjectsReadModel(
[property: Required] int CustomerId,
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ export interface UpdateProjectWriteModel {
weekSpan?: number;
}

export interface UpdateProjectNameWriteModel {
/** @format int32 */
engagementId?: number;
/** @minLength 1 */
projectName: string;
}

export interface VacationMetaData {
/** @format int32 */
daysTotal?: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { putWithToken } from "@/data/apiCallsWithToken";
import { updateProjectNameBody } from "@/types";
import { NextResponse } from "next/server";
import { EngagementReadModel } from "@/api-types";

export async function PUT(
request: Request,
{ params }: { params: { organisation: string } },
) {
const orgUrlKey = params.organisation;
const requestBody = (await request.json()) as updateProjectNameBody;

const project =
(await putWithToken<EngagementReadModel, updateProjectNameBody>(
`${orgUrlKey}/projects/updateProjectName`,
requestBody,
)) ?? [];

return NextResponse.json(project);
}
5 changes: 5 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface updateProjectStateBody {
weekSpan: number;
}

export interface updateProjectNameBody {
engagementId: string;
engagementName: string;
}

export interface WeekWithHours {
week: number;
hours: number;
Expand Down
Loading