This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#767 - Risks.ts file and getRisks
- Loading branch information
Showing
6 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters