Skip to content

Commit

Permalink
Avenmia/fixpolis survey filter (#221)
Browse files Browse the repository at this point in the history
* Updating prisma schema to expect search term

* Updating survey rules and readme to add new Search term

* Adding new database migration

* Updating data import values and documentation

* Storing island and county in planning region

* Fixing survey filter to search by term

* Fixing error

* Fixing data import instructions

* Removing todo statement
  • Loading branch information
avenmia authored Sep 1, 2024
1 parent 068bf86 commit c9eb858
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 101 deletions.
12 changes: 6 additions & 6 deletions prisma/dataimport/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
- id,title,description
4. survey_rules.csv
- Required Format
- id,surveyId,questionId,requiredAnswerId
- id,surveyId,search
5. Add a .env file to the dataimport folder based on the .env.example
1. If the database uses a username and password use the following in dataimport conn_str = f'DRIVER={{SQL Server}};SERVER={server};DATABASE={database};UID={username};PWD={password}'

## Creating questions.csv, answers.csv, and polis_surveys.csv
## Creating questions.csv, answers.csv, polis_surveys.csv, and survey_rules.csv

1. Open a new google sheet
2. Rename one tab Questions
Expand Down Expand Up @@ -54,15 +55,14 @@
2. title: The title of the polis survey
3. description: A short description of the pol.is survey
7. In the SurveyRules sheet, add the following headers:
1. id surveyId questionId requiredAnswerId
1. id surveyId search
1. id: The ID of the survey rule
2. surveyId: The ID of the pol.is survey that requires the rule
3. questionId: The ID of the question in the pol.is survey that requires the specific answer
4. requiredAnswerId: The answerId that qualifies as a correct answer to show that pol.is survey
3. search: This is the term that will be used for filtering. If the planning region has that word, the survey will show
8. Add your questions to the questions sheet
9. Add your answers to the answers sheet
10. Add any survey rules to the survey rules sheet
1. **Note:** Any rules that are added will mean that the assoicated pol.is survey will not display unless that corresponding answer ID is selected
1. **Note:** Any rules that are added will mean that the associated pol.is survey will not display unless that corresponding answer ID is selected
11. Download the Questions sheet as a csv and name it "questions.csv"
12. Download the Answers sheet as a csv and name it "answers.csv"
13. Download the SurveyRules sheet as a csv and name it "survey_rules.csv"
Expand Down
49 changes: 1 addition & 48 deletions prisma/dataimport/answers.csv
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,4 @@ id,questionId,answer,position,answerType
68,15,"$100,000 but less than 150,000",8,option
69,15,"$150,000 but less than $200,000",9,option
70,15,"$200,000 or more",10,option
71,15,Don’t know/Prefer not to answer,11,option
72,16,Maui-Hana,1,option
73,16,Maui-Kahikinui (DHHL),2,option
74,16,Maui-Keokea-Waiohuli (DHHL),3,option
75,16,Maui-Lanai,4,option
76,16,Maui-Lanai (DHHL),5,option
77,16,Maui-Molokai (DHHL),6,option
78,16,Maui-Paukūkalo-Wai''ehu Koa (DHHL),7,option
79,16,Maui-Wailuku,8,option
80,16,Honolulu-Koolau Poko,9,option
81,16,Honolulu-Primary Urban Center,10,option
82,16,Honolulu-East Honolulu,11,option
83,16,Honolulu-Central Oahu,12,option
84,16,Honolulu-Ko''olau Loa,13,option
85,16,Honolulu-Papakolea (DHHL),14,option
86,16,Honolulu-Waimanalo (DHHL),15,option
87,16,Honolulu-Waianae Lualualei (DHHL),16,option
88,16,Honolulu-Waianae,17,option
89,16,Honolulu-Nanakuli (DHHL),18,option
90,16,Honolulu-Kapolei (DHLL),19,option
91,16,Honolulu-Ewa,20,option
92,16,Hawaii-Puna,21,option
93,16,Honolulu-North Shore,22,option
94,16,Kauai-Anahola (DHHL),23,option
95,16,Kauai-Wailua (DHHL),24,option
96,16,Kauai-West Kauai (DHHL),25,option
97,16,Kauai-Lihue,26,option
98,16,Kauai-Lihue Town Center,27,option
99,16,Kauai-South Kauai,28,option
100,16,Maui-Kahoolawe,29,option
101,16,Maui-Kihei,30,option
102,16,Maui-Leialii Honokowai (DHHL),31,option
103,16,Hawaii-Kau (DHHL),32,option
104,16,Hawaii-Kaumana Piihonua (DHHL),33,option
105,16,Hawaii-Kawaihae (DHHL),34,option
106,16,Hawaii-Kealakehe La''i ''Opua (DHHL),35,option
107,16,Hawaii-Keaukaha (DHHL),36,option
108,16,Hawaii-Makuu (DHHL),37,option
109,16,Hawaii-Panaewa (DHHL),38,option
110,16,Hawaii-Waimea (DHHL),39,option
111,16,Hawaii-Hamakua,40,option
112,16,Hawaii-Kona,41,option
113,16,Hawaii-North Kahala,42,option
114,16,Hawaii-South Kahala,43,option
115,16,Maui-Makawao,44,option
116,16,Maui-Paia,45,option
117,16,Maui-West Maui,46,option
118,16,Maui-Molokai,47,option
71,15,Don’t know/Prefer not to answer,11,option
3 changes: 1 addition & 2 deletions prisma/dataimport/dataimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
CREATE TABLE SurveyRules (
id NVARCHAR(255) PRIMARY KEY,
surveyId NVARCHAR(255),
questionId INT,
requiredAnswerId INT,
search NVARCHAR(255),
)
''')

Expand Down
3 changes: 1 addition & 2 deletions prisma/dataimport/questions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ id,question,position,questionType
12,"What is your ethnic identification? If more than one ethnicity, which do you identify most. If you cannot choose, please select mixed. If you have any Hawaiian in your ethnic mix, please select Hawaiian/Part Hawaiian",12,option
13,What is the highest level of education you have completed? (Select one answer),13,option
14,Do you identify with having a disability?,14,option
15,"What was your household income in 2022, before taxes? Please consider and include in your thinking your best estimate of the income of all persons living in your household. (Select one answer)",15,option
16,"In what region do you live? Note: for workshop participants, this information will be provided by your moderator.",16,dropdown
15,"What was your household income in 2022, before taxes? Please consider and include in your thinking your best estimate of the income of all persons living in your household. (Select one answer)",15,option
21 changes: 5 additions & 16 deletions prisma/dataimport/survey_rules.csv
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
id,surveyId,questionId,requiredAnswerId
1,6xnxstpwcb,16,72
2,6xnxstpwcb,16,73
3,6xnxstpwcb,16,74
4,6xnxstpwcb,16,75
5,6xnxstpwcb,16,76
6,6xnxstpwcb,16,77
7,6xnxstpwcb,16,78
8,6xnxstpwcb,16,79
9,6xnxstpwcb,16,100
10,6xnxstpwcb,16,101
11,6xnxstpwcb,16,102
12,6xnxstpwcb,16,115
13,6xnxstpwcb,16,116
14,6xnxstpwcb,16,117
15,6xnxstpwcb,16,118
id,surveyId,search
1,6xnxstpwcb,Maui
2,6xnxstpwcb,Honolulu
3,6xnxstpwcb,East Kauai
3,6xnxstpwcb,DHHL
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Warnings:
- You are about to drop the column `questionId` on the `SurveyRules` table. All the data in the column will be lost.
- You are about to drop the column `requiredAnswerId` on the `SurveyRules` table. All the data in the column will be lost.
- Added the required column `search` to the `SurveyRules` table without a default value. This is not possible if the table is not empty.
*/
BEGIN TRY

BEGIN TRAN;

-- AlterTable
ALTER TABLE [dbo].[SurveyRules] DROP COLUMN [questionId],
[requiredAnswerId];
ALTER TABLE [dbo].[SurveyRules] ADD [search] NVARCHAR(1000) NOT NULL;

COMMIT TRAN;

END TRY
BEGIN CATCH

IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRAN;
END;
THROW

END CATCH
3 changes: 1 addition & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ model PolisSurveys {
model SurveyRules {
id String @id @default(cuid())
surveyId String
questionId String
requiredAnswerId String
search String
}

// Necessary for Next auth
Expand Down
30 changes: 22 additions & 8 deletions src/components/AddressSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const AddressSearch: React.FC = () => {
const [censusTract, setCensusTract] = useState<string | null>(null);
const [zipCode, setZipCode] = useState<string | null>(null);
const [planningRegion, setPlanningRegion] = useState<string | null>(null);
const [island, setIsland] = useState<string | null>(null);
const [county, setCounty] = useState<string | null>(null);
const [dhhlRegion, setDhhlRegion] = useState<string | null>(null);
const [location, setLocation] = useState<Address | null>(null);
const [censusTractComplete, setCensusTractComplete] =
Expand Down Expand Up @@ -84,7 +86,11 @@ const AddressSearch: React.FC = () => {
}
if (planningRegionDB && planningRegionDB.data) {
if (planningRegionDB.data.planningRegion !== null) {
setPlanningRegion(planningRegionDB.data?.planningRegion);
const [islandData, countyData, planningRegionData] =
planningRegionDB.data.planningRegion.split(",");
setIsland(islandData ?? "");
setCounty(countyData ?? "");
setPlanningRegion(planningRegionData ?? "");
setPlanningRegionComplete(true);
}
}
Expand All @@ -109,9 +115,9 @@ const AddressSearch: React.FC = () => {

const handleSubmit = () => {
console.log("Submitted!");
const planningRegionDhhl = `${planningRegion ?? ""} ${
dhhlRegion === "Yes" ? "-DHHL" : ""
}`;
const planningRegionDhhl = `${island ? island + "," : ""}${
county ? county + "," : ""
}${planningRegion ?? ""}${dhhlRegion === "Yes" ? "-DHHL" : ""}`;
updateUserCensusTract.mutate({ censusTract: censusTract ?? "" });
updateUserZipCode.mutate({ zipcode: zipCode ?? "" });
updateUserPlanningRegion.mutate({
Expand Down Expand Up @@ -208,12 +214,16 @@ const AddressSearch: React.FC = () => {
let propertyValue = defaultValue;
turf.featureEach(geojson, (currentFeature) => {
if (turf.booleanPointInPolygon(point, currentFeature)) {
if (name !== "dhhl") {
if (name === "censustract") {
setIsland((currentFeature.properties?.island as string) ?? "");
setCounty((currentFeature.properties?.county as string) ?? "");
}
if (name === "dhhl") {
propertyValue = "Yes";
} else {
propertyValue =
(currentFeature.properties?.[propertyName] as string) ??
defaultValue;
} else {
propertyValue = "Yes";
}
}
});
Expand All @@ -226,7 +236,9 @@ const AddressSearch: React.FC = () => {
);
const censusTract = getFeatureProperty(
CensusTractGeojson as FeatureCollection<Polygon>,
"name20"
"name20",
"Not Found",
"censustract"
);
const featurePlanningRegion = getFeatureProperty(
PlanningAreaGeojson as FeatureCollection<Polygon>,
Expand Down Expand Up @@ -290,6 +302,8 @@ const AddressSearch: React.FC = () => {
{censusTract && (
<p className="text-white">Census Tract: {censusTract}</p>
)}
{island && <p className="text-white">Island: {island}</p>}
{county && <p className="text-white">County: {county}</p>}
{planningRegion && (
<p className="text-white">
Planning Region: {planningRegion}
Expand Down
28 changes: 11 additions & 17 deletions src/server/api/routers/polis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ export const polisRouter = createTRPCRouter({
}
const { id: userId } = ctx.session.user;

const userSurveyAnswers = await ctx.prisma.userSurveyAnswers.findMany({
where: { userId: userId },
const userPlanningRegion = await ctx.prisma.user.findUnique({
where: { id: userId },
select: {
questionId: true,
answerId: true,
planningRegion: true,
},
});

Expand All @@ -31,8 +30,7 @@ export const polisRouter = createTRPCRouter({
select: {
id: true,
surveyId: true,
questionId: true,
requiredAnswerId: true,
search: true,
},
});

Expand All @@ -44,23 +42,19 @@ export const polisRouter = createTRPCRouter({

surveyRules.map((rule) => {
const surveyId = rule.surveyId;
const questionId = rule.questionId;
const requiredAnswerId = rule.requiredAnswerId;

// Returns all the answers for a certain question
const userAnswer = userSurveyAnswers.find(
(answer) => answer.questionId === questionId
);
const filterTerm = rule.search;

if (
userAnswer &&
userAnswer.questionId == questionId &&
userAnswer.answerId === requiredAnswerId
userPlanningRegion &&
userPlanningRegion.planningRegion?.includes(filterTerm)
) {
const filteredSurvey = allSurveys.find(
(survey) => survey.id === surveyId
);
if (filteredSurvey !== undefined) {
if (
filteredSurvey !== undefined &&
!filteredSurveys.includes(filteredSurvey)
) {
filteredSurveys.push(filteredSurvey);
}
}
Expand Down

0 comments on commit c9eb858

Please sign in to comment.