Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #768 from Northeastern-Electric-Racing/#767-get-risks
Browse files Browse the repository at this point in the history
#767 - Risks.ts file and getRisks
  • Loading branch information
anthonybernardi authored Jul 27, 2022
2 parents e55d0fd + 58fcf1a commit cc01dca
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 1 deletion.
103 changes: 103 additions & 0 deletions src/backend/functions/risks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* This file is part of NER's PM Dashboard and licensed under GNU AGPLv3.
* See the LICENSE file in the repository root folder for details.
*/

import { Handler } from '@netlify/functions';
import { Prisma, PrismaClient, WBS_Element } from '@prisma/client';
import {
ApiRoute,
ApiRouteFunction,
apiRoutes,
API_URL,
buildServerFailureResponse,
buildNotFoundResponse,
buildSuccessResponse,
routeMatcher,
Risk,
WbsNumber,
UserPreview
} from 'utils';

const prisma = new PrismaClient();

const wbsNumOf = (element: WBS_Element): WbsNumber => ({
carNumber: element.carNumber,
projectNumber: element.projectNumber,
workPackageNumber: element.workPackageNumber
});

const riskQueryArgs = Prisma.validator<Prisma.RiskArgs>()({
include: {
project: { include: { wbsElement: true } },
createdBy: true,
resolvedBy: true,
deletedBy: true
}
});

const userTransformer = (user: Prisma.UserGetPayload<null>): UserPreview => {
return {
userId: user.userId,
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
role: user.role
};
};

const riskTransformer = (risk: Prisma.RiskGetPayload<typeof riskQueryArgs>): Risk => {
return {
id: risk.id,
project: {
id: risk.project.projectId,
name: risk.project.wbsElement.name,
wbsNum: wbsNumOf(risk.project.wbsElement)
},
detail: risk.detail,
isResolved: risk.isResolved,
dateDeleted: risk.dateDeleted ?? undefined,
dateCreated: risk.dateCreated,
createdBy: userTransformer(risk.createdBy),
resolvedBy: risk.resolvedBy ? userTransformer(risk.resolvedBy) : undefined,
resolvedAt: risk.resolvedAt ?? undefined,
deletedBy: risk.deletedBy ? userTransformer(risk.deletedBy) : undefined
};
};

const getRisksForProject: ApiRouteFunction = async (params: { projectId: string }) => {
const projectId = parseInt(params.projectId);
const requestedProject = await prisma.project.findUnique({
where: { projectId }
});

if (!requestedProject) return buildNotFoundResponse('project', `#${projectId}`);

const risks = await prisma.risk.findMany({
where: { projectId },
...riskQueryArgs
});

return buildSuccessResponse(risks.map(riskTransformer));
};

// Define all valid routes for the endpoint
const routes: ApiRoute[] = [
{
path: `${API_URL}${apiRoutes.RISKS_BY_PROJECT}`,
httpMethod: 'GET',
func: getRisksForProject
}
];

// Handler for incoming requests
const handler: Handler = async (event, context) => {
try {
return routeMatcher(routes, event, context);
} catch (error: any) {
console.error(error);
return buildServerFailureResponse(error.message);
}
};

export { handler };
9 changes: 8 additions & 1 deletion src/utils/src/api-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const CHANGE_REQUESTS_NEW: string = `${CHANGE_REQUESTS}-new`;
const CHANGE_REQUESTS_BY_ID: string = `${CHANGE_REQUESTS}/:id`;
const CHANGE_REQUESTS_REVIEW: string = `${CHANGE_REQUESTS}-review`;

/**************** Risks Endpoint ****************/
const RISKS: string = `/risks`;
const RISKS_BY_PROJECT: string = `${RISKS}/:projectId`;

export const apiRoutes = {
USERS,
USERS_BY_ID,
Expand All @@ -44,5 +48,8 @@ export const apiRoutes = {
CHANGE_REQUESTS,
CHANGE_REQUESTS_NEW,
CHANGE_REQUESTS_BY_ID,
CHANGE_REQUESTS_REVIEW
CHANGE_REQUESTS_REVIEW,

RISKS,
RISKS_BY_PROJECT
};
1 change: 1 addition & 0 deletions src/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './types/project-types';
export * from './types/user-types';
export * from './types/work-package-types';
export * from './types/api-utils-types';
export * from './types/risk-types';

export * from './backend-supports/project-supports';

Expand Down
2 changes: 2 additions & 0 deletions src/utils/src/types/project-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export interface Project extends WbsElement {
workPackages: WorkPackage[];
}

export type ProjectPreview = Pick<Project, 'id' | 'name' | 'wbsNum'>;

export interface WorkPackage extends WbsElement {
orderInProject: number;
progress: number;
Expand Down
20 changes: 20 additions & 0 deletions src/utils/src/types/risk-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This file is part of NER's PM Dashboard and licensed under GNU AGPLv3.
* See the LICENSE file in the repository root folder for details.
*/

import { ProjectPreview } from './project-types';
import { UserPreview } from './user-types';

export interface Risk {
id: String;
project: ProjectPreview;
detail: string;
isResolved: boolean;
dateDeleted?: Date;
dateCreated: Date;
createdBy: UserPreview;
resolvedBy?: UserPreview;
resolvedAt?: Date;
deletedBy?: UserPreview;
}
2 changes: 2 additions & 0 deletions src/utils/src/types/user-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export interface User {
role: Role;
}

export type UserPreview = Pick<User, 'userId' | 'firstName' | 'lastName' | 'email' | 'role'>;

export type Role = 'APP_ADMIN' | 'ADMIN' | 'LEADERSHIP' | 'MEMBER' | 'GUEST';

0 comments on commit cc01dca

Please sign in to comment.