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

Justin/60 sorting filtering #64

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions server/mongodb/actions/Issue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Types } from "mongoose";
import IssueModel from "server/mongodb/models/Issue";
import ProjectModel from "server/mongodb/models/Project";
import dbConnect from "server/utils/dbConnect";
import { projectGetIssuesFilter } from "src/utils/filter/Issue";
import { projectGetIssuesSorter } from "src/utils/sorting/Issue";
import { ProjectGetIssues } from "src/utils/types";

export async function projectGetIssues(
projectId: Types.ObjectId,
issuesGet: ProjectGetIssues
) {
await dbConnect();

const filter = projectGetIssuesFilter(projectId, issuesGet);

const sorter = projectGetIssuesSorter(issuesGet);

const issues = await IssueModel.find(filter).sort(sorter);

return issues;
}
80 changes: 40 additions & 40 deletions server/mongodb/actions/Project.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { FilterQuery, Types } from "mongoose";
import { Types } from "mongoose";
import ChapterModel from "server/mongodb/models/Chapter";
import NonprofitModel from "server/mongodb/models/Nonprofit";
import ProjectModel from "server/mongodb/models/Project";
import UserModel from "server/mongodb/models/User";
import dbConnect from "server/utils/dbConnect";
import { displayableProjectStageToProjectStages } from "src/utils/stages";
import {
chapterGetProjectsFilter,
nonprofitGetProjectsFilter,
} from "src/utils/filter/Project";
import {
chapterGetProjectsSorter,
nonprofitGetProjectsSorter,
projectStageToInt,
} from "src/utils/sorting/Project";
import {
ChapterUpdateProject,
DisplayableProjectStage,
NonprofitUpdateProject,
Project,
NonprofitCreateProject,
ProjectStage,
ChapterGetProjects,
NonprofitGetProjects,
ChapterGetProject,
NonprofitGetProject,
SortingOption,
} from "src/utils/types";

export async function nonprofitCreateProject(
Expand All @@ -38,10 +45,13 @@ export async function chapterGetProjects(
) {
await dbConnect();

const status = projectsGet.status;
const filter = chapterGetProjectsFilter(chapterId, projectsGet);

if (status === ProjectStage.APPLICATION_REVIEW) {
const projects = await ProjectModel.find({ status }).populate({
const sorter = chapterGetProjectsSorter(projectsGet);

const projects = await ProjectModel.find(filter)
.sort(sorter)
.populate({
path: "nonprofit",
model: NonprofitModel,
populate: {
Expand All @@ -50,13 +60,18 @@ export async function chapterGetProjects(
},
});

return projects;
if (projectsGet.sortStatus != undefined) {
projects.sort((a, b) => {
if (projectsGet.sortStatus === SortingOption.ASCENDING) {
return projectStageToInt(a.status) - projectStageToInt(b.status);
} else if (projectsGet.sortStatus === SortingOption.DESCENDING) {
return projectStageToInt(b.status) - projectStageToInt(a.status);
} else {
return projectStageToInt(a.status) - projectStageToInt(b.status);
}
});
}

const projects = await ProjectModel.find({
chapter: chapterId,
});

return projects;
}

Expand Down Expand Up @@ -115,35 +130,20 @@ export async function nonprofitGetProjects(
) {
await dbConnect();

const filter: FilterQuery<Project> = {
nonprofit: nonprofitId,
};

const active = projectsGet.active;

// will filter by active or inactive only if filter specified
if (active != undefined) {
filter["status"] = active
? {
$nin: displayableProjectStageToProjectStages(
DisplayableProjectStage.COMPLETE
),
}
: {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.COMPLETE
),
};
}
const filter = nonprofitGetProjectsFilter(nonprofitId, projectsGet);

const projects = await ProjectModel.find(filter).populate({
path: "chapter",
model: ChapterModel,
populate: {
path: "contact",
model: UserModel,
},
});
const sorter = nonprofitGetProjectsSorter(projectsGet);

const projects = await ProjectModel.find(filter)
.sort(sorter)
.populate({
path: "chapter",
model: ChapterModel,
populate: {
path: "contact",
model: UserModel,
},
});

return projects;
}
Expand Down
10 changes: 8 additions & 2 deletions src/pages/chapter/applications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ import {
import { useEffect, useState } from "react";
import { chapterGetProjects } from "src/actions/Project";
import { showError } from "src/utils/notifications";
import { User, Nonprofit, Project, ProjectStage } from "src/utils/types";
import {
User,
Nonprofit,
Project,
ProjectStage,
SortingOption,
} from "src/utils/types";

function ChapterApplicationsPage() {
const [projects, setProjects] = useState<Project[]>();

useEffect(() => {
async function loadProjects() {
const newProjects: Project[] = await chapterGetProjects({
status: ProjectStage.APPLICATION_REVIEW,
filterStatus: ProjectStage.APPLICATION_REVIEW,
});

setProjects(newProjects);
Expand Down
4 changes: 3 additions & 1 deletion src/pages/nonprofit/projects/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
DisplayableProjectStage,
Project,
User,
SortingOption,
} from "src/utils/types";
import urls from "src/utils/urls";

Expand All @@ -50,7 +51,8 @@ function NonprofitProjectsPage() {
useEffect(() => {
async function fetchProjects() {
const projects: Project[] = await nonprofitGetProjects({
active,
active: active,
sortCreatedAt: SortingOption.ASCENDING,
});

if (projects.length !== 0 && active) {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/filter/Issue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FilterQuery, Types } from "mongoose";
import { ProjectGetIssues, Issue, IssueStatus } from "src/utils/types";

export function projectGetIssuesFilter(
projectId: Types.ObjectId,
issuesGet: ProjectGetIssues
) {
const filter: FilterQuery<Issue> = {
project: projectId,
};

const filterStatus = issuesGet.filterStatus;
if (filterStatus != undefined) {
if (filterStatus === IssueStatus.CLOSED) {
filter["status"] = IssueStatus.CLOSED;
} else if (filterStatus === IssueStatus.IN_PROGRESS) {
filter["status"] = IssueStatus.IN_PROGRESS;
} else if (filterStatus === IssueStatus.PENDING) {
filter["status"] = IssueStatus.PENDING;
} else if (filterStatus === IssueStatus.RESOLVED) {
filter["status"] = IssueStatus.RESOLVED;
}
}

return filter;
}
115 changes: 115 additions & 0 deletions src/utils/filter/Project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { FilterQuery, Types } from "mongoose";
import { displayableProjectStageToProjectStages } from "src/utils/stages";
import {
Project,
ProjectStage,
DisplayableProjectStage,
ChapterGetProjects,
NonprofitGetProjects,
} from "src/utils/types";

export function chapterGetProjectsFilter(
chapterId: Types.ObjectId,
projectsGet: ChapterGetProjects
) {
const filter: FilterQuery<Project> = {
chapter: chapterId,
};

const filterStatus = projectsGet.filterStatus;
if (filterStatus != undefined) {
if (filterStatus === ProjectStage.APPLICATION_REVIEW) {
filter["status"] = ProjectStage.APPLICATION_REVIEW;
}
}

const filterDisplayableStatus = projectsGet.filterDisplayableStatus;
if (filterDisplayableStatus != undefined) {
if (filterDisplayableStatus === DisplayableProjectStage.APPLICATION) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.APPLICATION
),
};
} else if (filterDisplayableStatus === DisplayableProjectStage.COMPLETE) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.COMPLETE
),
};
} else if (filterDisplayableStatus === DisplayableProjectStage.INTERVIEW) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.INTERVIEW
),
};
} else if (
filterDisplayableStatus === DisplayableProjectStage.IN_PROGRESS
) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.IN_PROGRESS
),
};
}
}

return filter;
}

export function nonprofitGetProjectsFilter(
nonprofitId: Types.ObjectId,
projectsGet: NonprofitGetProjects
) {
const filter: FilterQuery<Project> = {
nonprofit: nonprofitId,
};

const active = projectsGet.active;

// will filter by active or inactive only if filter specified
if (active != undefined) {
filter["status"] = active
? {
$nin: displayableProjectStageToProjectStages(
DisplayableProjectStage.COMPLETE
),
}
: {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.COMPLETE
),
};
}

const filterStatus = projectsGet.filterStatus;
if (filterStatus != undefined) {
if (filterStatus === DisplayableProjectStage.APPLICATION) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.APPLICATION
),
};
} else if (filterStatus === DisplayableProjectStage.COMPLETE) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.COMPLETE
),
};
} else if (filterStatus === DisplayableProjectStage.INTERVIEW) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.INTERVIEW
),
};
} else if (filterStatus === DisplayableProjectStage.IN_PROGRESS) {
filter["status"] = {
$in: displayableProjectStageToProjectStages(
DisplayableProjectStage.IN_PROGRESS
),
};
}
}

return filter;
}
25 changes: 25 additions & 0 deletions src/utils/sorting/Issue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ProjectGetIssues, SortingOption } from "src/utils/types";

export function projectGetIssuesSorter(issuesGet: ProjectGetIssues) {
const sorter: { [key: string]: string } = {};

const createdAt = issuesGet.sortCreatedAt;
if (createdAt != undefined) {
if (createdAt == SortingOption.ASCENDING) {
sorter["createdAt"] = SortingOption.ASCENDING;
} else if (createdAt == SortingOption.DESCENDING) {
sorter["createdAt"] = SortingOption.DESCENDING;
}
}

const updatedAt = issuesGet.sortUpdatedAt;
if (updatedAt != undefined) {
if (updatedAt == SortingOption.ASCENDING) {
sorter["updatedAt"] = SortingOption.ASCENDING;
} else if (updatedAt == SortingOption.DESCENDING) {
sorter["updatedAt"] = SortingOption.DESCENDING;
}
}

return sorter;
}
Loading