Skip to content

Commit

Permalink
Merge search/sort/filter branch
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminJohnson2204 committed May 27, 2024
2 parents a584520 + 795ef94 commit 9c3f0fa
Show file tree
Hide file tree
Showing 60 changed files with 6,301 additions and 2,180 deletions.
37 changes: 16 additions & 21 deletions backend/src/controllers/vsr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
sendVSRConfirmationEmailToVeteran,
sendVSRNotificationEmailToStaff,
} from "src/services/emails";
import { retrieveVSRs } from "src/services/vsrs";
import validationErrorParser from "src/util/validationErrorParser";
import ExcelJS from "exceljs";
import { ObjectId } from "mongodb";
Expand All @@ -19,7 +20,13 @@ type FurnitureItemEntry = FurnitureItem & { _id: ObjectId };
*/
export const getAllVSRS: RequestHandler = async (req, res, next) => {
try {
const vsrs = await VSRModel.find();
const vsrs = await retrieveVSRs(
req.query.search as string | undefined,
req.query.status as string | undefined,
req.query.incomeLevel as string | undefined,
req.query.zipCode ? (req.query.zipCode as string).split(",") : undefined,
undefined,
);

res.status(200).json({ vsrs });
} catch (error) {
Expand Down Expand Up @@ -289,32 +296,20 @@ const writeSpreadsheet = async (plainVsrs: VSR[], res: Response) => {

export const bulkExportVSRS: RequestHandler = async (req, res, next) => {
try {
const filename = "vsrs.xlsx";
const filename = `vsrs_${new Date().toISOString()}.xlsx`;
// Set some headers on the response so the client knows that a file is attached
res.set({
"Content-Disposition": `attachment; filename="${filename}"`,
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});

let vsrs: VSR[];

if (req.query.vsrIds && ((req.query.vsrIds.length ?? 0) as number) > 0) {
// If the "vsrIds" query parameter exists and is non-empty, then find & export all VSRs
// with an _id in the vsrIds list

// Need to convert each ID string to an ObjectId object
const vsrObjectIds = (req.query.vsrIds as string)?.split(",").map((_id) => new ObjectId(_id));
vsrs = (
await VSRModel.find({
_id: {
$in: vsrObjectIds,
},
})
).map((doc) => doc.toObject());
} else {
// If the "vsrIds" query parameter is not provided or is empty, export all VSRs in the database
vsrs = (await VSRModel.find()).map((doc) => doc.toObject());
}
const vsrs = await retrieveVSRs(
req.query.search as string | undefined,
req.query.status as string | undefined,
req.query.incomeLevel as string | undefined,
req.query.zipCode ? (req.query.zipCode as string).split(",") : undefined,
req.query.vsrIds ? (req.query.vsrIds as string).split(",") : undefined,
);

await writeSpreadsheet(vsrs, res);
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/models/vsr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ const vsrSchema = new Schema({
export type FurnitureInput = InferSchemaType<typeof furntitureInputSchema>;
export type VSR = InferSchemaType<typeof vsrSchema>;

vsrSchema.index({ name: "text" });

export default model<VSR>("VSR", vsrSchema);
65 changes: 65 additions & 0 deletions backend/src/services/vsrs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import VSRModel from "src/models/vsr";

export const retrieveVSRs = async (
search: string | undefined,
status: string | undefined,
incomeLevel: string | undefined,
zipCodes: string[] | undefined,
vsrIds: string[] | undefined,
) => {
let vsrs = await VSRModel.aggregate([
...(search
? [
{
$match: { name: { $regex: new RegExp(search) } },
},
]
: []),
{
$addFields: {
statusOrder: {
$switch: {
branches: [
{ case: { $eq: ["$status", "Received"] }, then: 1 },
{ case: { $eq: ["$status", "Approved"] }, then: 2 },
{ case: { $eq: ["$status", "Appointment Scheduled"] }, then: 3 },
{ case: { $eq: ["$status", "Complete"] }, then: 4 },
{ case: { $eq: ["$status", "No-show / Incomplete"] }, then: 5 },
{ case: { $eq: ["$status", "Archived"] }, then: 6 },
],
default: 99,
},
},
},
},
{ $sort: { statusOrder: 1, dateReceived: -1 } },
]);

if (vsrIds && vsrIds.length > 0) {
const vsrIdsSet = new Set(vsrIds);
vsrs = vsrs.filter((vsr) => vsrIdsSet.has(vsr._id.toString()));
}

if (status) {
vsrs = vsrs.filter((vsr) => vsr.status === status);
}

if (incomeLevel) {
const incomeMap: { [key: string]: string } = {
"50000": "$50,001 and over",
"25000": "$25,001 - $50,000",
"12500": "$12,501 - $25,000",
"0": "$12,500 and under",
};

vsrs = vsrs.filter((vsr) => {
return vsr.incomeLevel === incomeMap[incomeLevel];
});
}

if (zipCodes && zipCodes.length > 0) {
vsrs = vsrs.filter((vsr) => zipCodes.includes(vsr.zipCode.toString()));
}

return vsrs;
};
Loading

0 comments on commit 9c3f0fa

Please sign in to comment.