diff --git a/src/backend/src/controllers/projects.controllers.ts b/src/backend/src/controllers/projects.controllers.ts index 438b02005b..c3a587cf84 100644 --- a/src/backend/src/controllers/projects.controllers.ts +++ b/src/backend/src/controllers/projects.controllers.ts @@ -16,7 +16,11 @@ import BillOfMaterialsService from '../services/boms.services.js'; export default class ProjectsController { static async getAllProjectsGantt(req: Request, res: Response, next: NextFunction) { try { - const projects: ProjectGantt[] = await ProjectsService.getAllProjectsGantt(req.organization); + const { carId } = req.query; + const projects: ProjectGantt[] = await ProjectsService.getAllProjectsGantt( + req.organization, + carId as string | undefined + ); res.status(200).json(projects); } catch (error: unknown) { next(error); @@ -25,7 +29,8 @@ export default class ProjectsController { static async getAllProjects(req: Request, res: Response, next: NextFunction) { try { - const projects: ProjectPreview[] = await ProjectsService.getAllProjects(req.organization); + const { carId } = req.query; + const projects: ProjectPreview[] = await ProjectsService.getAllProjects(req.organization, carId as string | undefined); res.status(200).json(projects); } catch (error: unknown) { next(error); @@ -34,7 +39,12 @@ export default class ProjectsController { static async getUsersTeamsProjects(req: Request, res: Response, next: NextFunction) { try { - const projects: ProjectOverview[] = await ProjectsService.getUsersTeamsProjects(req.currentUser, req.organization); + const { carId } = req.query; + const projects: ProjectOverview[] = await ProjectsService.getUsersTeamsProjects( + req.currentUser, + req.organization, + carId as string | undefined + ); res.status(200).json(projects); } catch (error: unknown) { next(error); @@ -43,7 +53,12 @@ export default class ProjectsController { static async getUsersLeadingProjects(req: Request, res: Response, next: NextFunction) { try { - const projects: ProjectOverview[] = await ProjectsService.getUsersLeadingProjects(req.currentUser, req.organization); + const { carId } = req.query; + const projects: ProjectOverview[] = await ProjectsService.getUsersLeadingProjects( + req.currentUser, + req.organization, + carId as string | undefined + ); res.status(200).json(projects); } catch (error: unknown) { next(error); @@ -53,7 +68,12 @@ export default class ProjectsController { static async getTeamsProjects(req: Request, res: Response, next: NextFunction) { try { const { teamId } = req.params as Record; - const projects: Project[] = await ProjectsService.getTeamsProjects(req.organization, teamId); + const { carId } = req.query; + const projects: Project[] = await ProjectsService.getTeamsProjects( + req.organization, + teamId, + carId as string | undefined + ); res.status(200).json(projects); } catch (error: unknown) { next(error); diff --git a/src/backend/src/services/projects.services.ts b/src/backend/src/services/projects.services.ts index 44cda2e7cc..b6cfd74db9 100644 --- a/src/backend/src/services/projects.services.ts +++ b/src/backend/src/services/projects.services.ts @@ -47,11 +47,12 @@ export default class ProjectsService { /** * Get all the non deleted projects in the database for the given organization * @param organization the organization the user is currently in + * @param carId optional car id to filter projects by * @returns all the projects with query args for use in the gantt chart */ - static async getAllProjectsGantt(organization: Organization): Promise { + static async getAllProjectsGantt(organization: Organization, carId?: string): Promise { const projects = await prisma.project.findMany({ - where: { wbsElement: { dateDeleted: null, organizationId: organization.organizationId } }, + where: { wbsElement: { dateDeleted: null, organizationId: organization.organizationId }, ...(carId && { carId }) }, ...getProjectGanttQueryArgs(organization.organizationId) }); @@ -61,11 +62,12 @@ export default class ProjectsService { /** * Get all projects for given organization * @param organization the organization the user is in + * @param carId optional car id to filter projects by * @returns all the projects with preview query args */ - static async getAllProjects(organization: Organization): Promise { + static async getAllProjects(organization: Organization, carId?: string): Promise { const projects = await prisma.project.findMany({ - where: { wbsElement: { dateDeleted: null, organizationId: organization.organizationId } }, + where: { wbsElement: { dateDeleted: null, organizationId: organization.organizationId }, ...(carId && { carId }) }, ...getProjectPreviewQueryArgs(organization.organizationId) }); @@ -76,16 +78,18 @@ export default class ProjectsService { * Get all projects that the user is the lead or manager of * @param user the user making the request * @param organization the oranization the user is in + * @param carId optional car id to filter projects by * @returns the projects the user is a lead or manager of with preview query args */ - static async getUsersLeadingProjects(user: User, organization: Organization): Promise { + static async getUsersLeadingProjects(user: User, organization: Organization, carId?: string): Promise { const projects = await prisma.project.findMany({ where: { wbsElement: { organizationId: organization.organizationId, dateDeleted: null, OR: [{ leadId: user.userId }, { managerId: user.userId }] - } + }, + ...(carId && { carId }) }, ...getProjectOverviewQueryArgs(organization.organizationId) }); @@ -97,9 +101,10 @@ export default class ProjectsService { * Get all projects related to teams the user is on * @param user the user making the request * @param organization the organization the user is in + * @param carId optional car id to filter projects by * @returns all projects associated with teams the user is on with overview card query args */ - static async getUsersTeamsProjects(user: User, organization: Organization): Promise { + static async getUsersTeamsProjects(user: User, organization: Organization, carId?: string): Promise { const projects = await prisma.project.findMany({ where: { wbsElement: { @@ -128,7 +133,8 @@ export default class ProjectsService { } ] } - } + }, + ...(carId && { carId }) }, ...getProjectOverviewQueryArgs(organization.organizationId) }); @@ -140,9 +146,10 @@ export default class ProjectsService { * Get the projects for a given team * @param organization * @param teamId + * @param carId optional car id to filter projects by * @returns all the projects for the given team with full project query args */ - static async getTeamsProjects(organization: Organization, teamId: string): Promise { + static async getTeamsProjects(organization: Organization, teamId: string, carId?: string): Promise { const projects = await prisma.project.findMany({ where: { wbsElement: { @@ -153,7 +160,8 @@ export default class ProjectsService { some: { teamId } - } + }, + ...(carId && { carId }) }, ...getProjectQueryArgs(organization.organizationId) }); diff --git a/src/frontend/src/apis/projects.api.ts b/src/frontend/src/apis/projects.api.ts index e5d3ce62a4..b9b2650ee3 100644 --- a/src/frontend/src/apis/projects.api.ts +++ b/src/frontend/src/apis/projects.api.ts @@ -29,8 +29,9 @@ import { CreateSingleProjectPayload, EditSingleProjectPayload } from '../utils/t /** * Fetches all projects with querry args needed for Gantt chart */ -export const getAllProjectsGantt = () => { +export const getAllProjectsGantt = (carId?: string) => { return axios.get(apiUrls.allProjectsGantt(), { + params: { carId }, transformResponse: (data) => JSON.parse(data).map(projectGanttTransformer) }); }; @@ -38,8 +39,9 @@ export const getAllProjectsGantt = () => { /** * Fetches all projects with preview querry args */ -export const getAllProjects = () => { +export const getAllProjects = (carId?: string) => { return axios.get(apiUrls.allProjectPreviews(), { + params: { carId }, transformResponse: (data) => JSON.parse(data).map(projectPreviewTransformer) }); }; @@ -47,8 +49,9 @@ export const getAllProjects = () => { /** * Fetches all the projects that are on the users teams */ -export const getUsersTeamsProjects = () => { +export const getUsersTeamsProjects = (carId?: string) => { return axios.get(apiUrls.usersTeamsProjects(), { + params: { carId }, transformResponse: (data) => JSON.parse(data).map(projectOverviewTransformer) }); }; @@ -56,8 +59,9 @@ export const getUsersTeamsProjects = () => { /** * Fetches all projects that the user is the manager or lead of. */ -export const getUsersLeadingProjects = () => { +export const getUsersLeadingProjects = (carId?: string) => { return axios.get(apiUrls.usersLeadingProjects(), { + params: { carId }, transformResponse: (data) => JSON.parse(data).map(projectOverviewTransformer) }); };