Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/backend/custom.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Organization } from '@prisma/client';
import { Car, Organization } from '@prisma/client';
import { User as SharedUser } from 'shared';

declare global {
namespace Express {
export interface Request {
currentUser: SharedUser;
organization: Organization;
currentCar?: Car;
}
}
}
18 changes: 13 additions & 5 deletions src/backend/src/controllers/projects.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 projects: ProjectGantt[] = await ProjectsService.getAllProjectsGantt(req.organization, req.currentCar?.carId);
res.status(200).json(projects);
} catch (error: unknown) {
next(error);
Expand All @@ -25,7 +25,7 @@ export default class ProjectsController {

static async getAllProjects(req: Request, res: Response, next: NextFunction) {
try {
const projects: ProjectPreview[] = await ProjectsService.getAllProjects(req.organization);
const projects: ProjectPreview[] = await ProjectsService.getAllProjects(req.organization, req.currentCar?.carId);
res.status(200).json(projects);
} catch (error: unknown) {
next(error);
Expand All @@ -34,7 +34,11 @@ 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 projects: ProjectOverview[] = await ProjectsService.getUsersTeamsProjects(
req.currentUser,
req.organization,
req.currentCar?.carId
);
res.status(200).json(projects);
} catch (error: unknown) {
next(error);
Expand All @@ -43,7 +47,11 @@ 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 projects: ProjectOverview[] = await ProjectsService.getUsersLeadingProjects(
req.currentUser,
req.organization,
req.currentCar?.carId
);
res.status(200).json(projects);
} catch (error: unknown) {
next(error);
Expand All @@ -53,7 +61,7 @@ export default class ProjectsController {
static async getTeamsProjects(req: Request, res: Response, next: NextFunction) {
try {
const { teamId } = req.params as Record<string, string>;
const projects: Project[] = await ProjectsService.getTeamsProjects(req.organization, teamId);
const projects: Project[] = await ProjectsService.getTeamsProjects(req.organization, teamId, req.currentCar?.carId);
res.status(200).json(projects);
} catch (error: unknown) {
next(error);
Expand Down
28 changes: 18 additions & 10 deletions src/backend/src/services/projects.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectGantt[]> {
static async getAllProjectsGantt(organization: Organization, carId?: string): Promise<ProjectGantt[]> {
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)
});

Expand All @@ -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<ProjectPreview[]> {
static async getAllProjects(organization: Organization, carId?: string): Promise<ProjectPreview[]> {
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)
});

Expand All @@ -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<ProjectOverview[]> {
static async getUsersLeadingProjects(user: User, organization: Organization, carId?: string): Promise<ProjectOverview[]> {
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)
});
Expand All @@ -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<ProjectOverview[]> {
static async getUsersTeamsProjects(user: User, organization: Organization, carId?: string): Promise<ProjectOverview[]> {
const projects = await prisma.project.findMany({
where: {
wbsElement: {
Expand Down Expand Up @@ -128,7 +133,8 @@ export default class ProjectsService {
}
]
}
}
},
...(carId && { carId })
},
...getProjectOverviewQueryArgs(organization.organizationId)
});
Expand All @@ -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<Project[]> {
static async getTeamsProjects(organization: Organization, teamId: string, carId?: string): Promise<Project[]> {
const projects = await prisma.project.findMany({
where: {
wbsElement: {
Expand All @@ -153,7 +160,8 @@ export default class ProjectsService {
some: {
teamId
}
}
},
...(carId && { carId })
},
...getProjectQueryArgs(organization.organizationId)
});
Expand Down