Skip to content

Commit

Permalink
Merge pull request #1742 from hackforla/1736-create-endpoint-for-list…
Browse files Browse the repository at this point in the history
…ings-wo-admin-fields

1736 Create endpoint for listings w/o admin fields
  • Loading branch information
VirginiaWu11 authored Jul 25, 2023
2 parents c71c2a7 + 92aab9f commit f7f8d57
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
19 changes: 19 additions & 0 deletions server/app/controllers/stakeholder-best-controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import stakeholderService from "../services/stakeholder-best-service";
import { RequestHandler } from "express";
import {
FoodSeekerStakeholder,
FoodSeekerStakeholderParams,
Stakeholder,
StakeholderBestSearchParams,
} from "../../types/stakeholder-types";

const selectAll: RequestHandler<
never,
FoodSeekerStakeholder[] | { error: string },
never,
FoodSeekerStakeholderParams
> = (req, res) => {
stakeholderService
.selectAll({ tenantId: req.query.tenantId })
.then((resp) => {
res.send(resp);
})
.catch((err) => {
res.status(500).json({ error: err.toString() });
});
};

const search: RequestHandler<
never,
Stakeholder[] | { error: string },
Expand Down Expand Up @@ -53,6 +71,7 @@ const getById: RequestHandler<
};

export default {
selectAll,
search,
getById,
};
1 change: 1 addition & 0 deletions server/app/routes/stakeholder-best-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import stakeholderBestController from "../controllers/stakeholder-best-controlle
const router = Router();

router.get("/", stakeholderBestController.search);
router.get("/select-all", stakeholderBestController.selectAll);
router.get("/:id", stakeholderBestController.getById);

export default router;
63 changes: 63 additions & 0 deletions server/app/services/stakeholder-best-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import {
StakeholderCategory,
StakeholderBestSearchParams,
StakeholderBest,
FoodSeekerStakeholder,
} from "../../types/stakeholder-types";
import db from "./db";
import stakeholderHelpers from "./stakeholder-helpers";
import camelcaseKeys from "camelcase-keys";

/*
Expand All @@ -31,6 +33,66 @@ const booleanEitherClause = (columnName: string, value?: string) => {
: "";
};

const selectAll = async ({ tenantId }: { tenantId: string }) => {
const sql = `
SELECT
s.id, s.name, s.address_1, s.address_2, s.city, s.state, s.zip,
s.phone, s.latitude, s.longitude, s.website, s.notes,
to_char(s.created_date at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS')
as created_date,
to_char(s.modified_date at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS')
as modified_date,
to_char(s.approved_date at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS')
as approved_date,
s.requirements, s.inactive,
s.parent_organization, s.physical_access, s.email,
s.items, s.services, s.facebook,
s.twitter, s.pinterest, s.linkedin, s.description,
s.review_notes, s.instagram, s.admin_contact_name,
s.admin_contact_phone, s.admin_contact_email,
s.covid_notes, s.food_types, s.languages,
s.verification_status_id, s.inactive_temporary,
array_to_json(s.hours) as hours, s.category_ids,
s.neighborhood_id, n.name as neighborhood_name, s.is_verified,
s.parent_organization_id,
s.allow_walkins, s.hours_notes, s.tags
FROM stakeholder_best s
LEFT JOIN neighborhood n on s.neighborhood_id = n.id
WHERE 1 = ANY(s.category_ids)
OR 9 = ANY(s.category_ids)
AND s.tenant_id = ${tenantId}
`;

const stakeholders: FoodSeekerStakeholder[] = [];
let categories: StakeholderCategory[] = [];

const rows = await db.manyOrNone(sql);
const stakeholder_ids = rows.map((a) => a.id);
if (stakeholder_ids.length) {
// Hoover up all the stakeholder categories
// for all of our stakeholder row results.
const categoriesSql = `select sc.stakeholder_id, c.id, c.name, c.display_order
from category c
join stakeholder_best_category sc on c.id = sc.category_id
where sc.stakeholder_id in (${stakeholder_ids.join(",")})
order by c.display_order, c.name`;
categories = await db.manyOrNone(categoriesSql);
}

rows.forEach((row) => {
stakeholders.push({
...camelcaseKeys(row),
isVerified: row.is_verified,
categories: categories
.filter((cr) => cr.stakeholder_id === row.id)
.map((c) => {
return { id: c.id, name: c.name, displayOrder: c.display_order };
}),
});
});
return stakeholders;
};

const search = async ({
categoryIds,
latitude,
Expand Down Expand Up @@ -449,6 +511,7 @@ const buildLoginSelectsClause = () => {
};

export default {
selectAll,
search,
selectById,
};
71 changes: 71 additions & 0 deletions server/types/stakeholder-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,74 @@ export interface NeedsVerificationParams {
preserveConfirmations: number;
message: string;
}

export interface FoodSeekerStakeholder {
address1: string;
address2: null | string;
adminContactEmail: null | string;
adminContactName: null | string;
adminContactPhone: null | string;
allowWalkins: boolean;
approvedDate: null | string;
categories: StakeholderCategory[];
categoryIds: number[];
city: string;
covidNotes: string;
createdDate: string;
description: string;
email: string;
facebook: string;
foodTypes: string;
hours: Hour[] | null;
hoursNotes: string;
id: number;
inactive: boolean;
inactiveTemporary: boolean;
instagram: string;
isVerified: boolean;
items: string;
languages: string;
latitude: string;
linkedin: string;
longitude: string;
modifiedDate: null | string;
name: string;
neighborhoodId: number | null;
neighborhoodName: null | string;
notes: string;
parentOrganization: string;
parentOrganizationId: null;
phone: string;
physicalAccess: string;
pinterest: string;
requirements: string;
reviewNotes: null | string;
services: string;
state: string;
tags: null | string[];
twitter: string;
verificationStatusId: number;
website: string;
zip: string;
}

export interface Hour {
close: string;
day_of_week: DayOfWeek;
open: string;
week_of_month: number;
}

export enum DayOfWeek {
Fri = "Fri",
Mon = "Mon",
Sat = "Sat",
Sun = "Sun",
Thu = "Thu",
Tue = "Tue",
Wed = "Wed",
}

export interface FoodSeekerStakeholderParams {
tenantId: string;
}

0 comments on commit f7f8d57

Please sign in to comment.