Skip to content

Commit

Permalink
Update referral column type & add tests (#2458)
Browse files Browse the repository at this point in the history
* change rh referral field from char to foreignkey

* merge rh-referral

* add test for rent history request with referral

* add migration with foreign key from 0004

* frontend test

* new migrations to change metadata of column

---------

Co-authored-by: Maxwell Austensen <[email protected]>
  • Loading branch information
kiwansim and austensen authored Jan 22, 2024
1 parent f457e3d commit 4251fee
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 4 deletions.
6 changes: 5 additions & 1 deletion frontend/lib/rh/tests/example-rh-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { newSb } from "../../tests/session-builder";

export const exampleRentalHistoryInfo = newSb().withOnboardingScaffolding({
const sb = newSb();

export const exampleRentalHistoryInfo = sb.withOnboardingScaffolding({
firstName: "boop",
lastName: "jones",
street: "150 DOOMBRINGER STREET",
Expand All @@ -9,3 +11,5 @@ export const exampleRentalHistoryInfo = newSb().withOnboardingScaffolding({
borough: "MANHATTAN",
zipCode: "10001",
});

export const exampleRhWithReferral = exampleRentalHistoryInfo.withActivePartnerReferral();
15 changes: 14 additions & 1 deletion frontend/lib/rh/tests/routes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import {
Borough,
} from "../../queries/globalTypes";
import { BlankOnboardingInfo } from "../../queries/OnboardingInfo";
import { exampleRentalHistoryInfo } from "./example-rh-info";
import {
exampleRentalHistoryInfo,
exampleRhWithReferral,
} from "./example-rh-info";
import { preloadLingui } from "../../tests/lingui-preloader";
import { ClearAnonymousSessionMutation } from "../../queries/ClearAnonymousSessionMutation";

Expand Down Expand Up @@ -101,6 +104,16 @@ describe("Rental history frontend", () => {
pal.withFormMutation(ClearAnonymousSessionMutation).expect({});
});

it("deletes partner org referral on clicking cancel", () => {
const pal = new AppTesterPal(<RentalHistoryRoutes />, {
url: JustfixRoutes.locale.rh.form,
session: exampleRhWithReferral.value,
});

pal.clickButtonOrLink("Cancel request");
pal.withFormMutation(ClearAnonymousSessionMutation).expect({});
});

it("shows an anonymous users address from DDO in form", () => {
browserStorage.update({ latestAddress: "150 DOOMBRINGER STREET" });
browserStorage.update({ latestBorough: "MANHATTAN" });
Expand Down
10 changes: 10 additions & 0 deletions frontend/lib/tests/session-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ export class SessionBuilder {
lastQueriedPhoneNumberAccountStatus: status,
});
}

withActivePartnerReferral(): SessionBuilder {
return this.with({
activePartnerReferral: {
name: "Beep Council on Habitat",
slug: "bch",
website: "beepbeep.com",
},
});
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions rh/migrations/0005_alter_rentalhistoryrequest_referral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.13 on 2024-01-18 21:17

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('partnerships', '0001_initial'),
('rh', '0004_rentalhistoryrequest_referral'),
]

operations = [
migrations.AlterField(
model_name='rentalhistoryrequest',
name='referral',
field=models.ForeignKey(blank=True, db_column='referral', null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='partnerships.partnerorg', to_field='name'),
),
]
17 changes: 17 additions & 0 deletions rh/migrations/0006_remove_rentalhistoryrequest_referral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.13 on 2024-01-19 23:38

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('rh', '0005_alter_rentalhistoryrequest_referral'),
]

operations = [
migrations.RemoveField(
model_name='rentalhistoryrequest',
name='referral',
),
]
20 changes: 20 additions & 0 deletions rh/migrations/0007_rentalhistoryrequest_referral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.13 on 2024-01-19 23:47

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('partnerships', '0001_initial'),
('rh', '0006_remove_rentalhistoryrequest_referral'),
]

operations = [
migrations.AddField(
model_name='rentalhistoryrequest',
name='referral',
field=models.ForeignKey(blank=True, db_column='referral', null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='partnerships.partnerorg', to_field='name'),
),
]
10 changes: 9 additions & 1 deletion rh/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models

from users.models import JustfixUser
from partnerships.models import PartnerOrg
from project.util import phone_number as pn
from project.util.address_form_fields import ADDRESS_FIELD_KWARGS, BOROUGH_FIELD_KWARGS

Expand All @@ -15,7 +16,14 @@ class RentalHistoryRequest(models.Model):
address_verified = models.BooleanField()
borough = models.CharField(**BOROUGH_FIELD_KWARGS)
zipcode = models.CharField(max_length=5, blank=True)
referral = models.CharField(max_length=30, blank=True, null=True)
referral = models.ForeignKey(
PartnerOrg,
db_column="referral",
to_field="name",
on_delete=models.DO_NOTHING,
blank=True,
null=True,
)
user = models.ForeignKey(
JustfixUser,
on_delete=models.SET_NULL,
Expand Down
6 changes: 5 additions & 1 deletion rh/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ def get_slack_notify_text(rhr: models.RentalHistoryRequest) -> str:
user_text = slack.hyperlink(text=rhr.user.best_first_name, href=rhr.user.admin_url)
else:
user_text = slack.escape(rhr.first_name)

if rhr.referral:
return f"{user_text} has requested {rh_link} via {rhr.referral}!"

return f"{user_text} has requested {rh_link}!"


def run_rent_stab_sql_query(bbl: str) -> Optional[Dict[str, Any]]:
sql_query = """
select uc2007, uc2008, uc2009, uc2010, uc2011, uc2012, uc2013,
uc2014, uc2015, uc2016, uc2017, uc2018, uc2019, uc2020
uc2014, uc2015, uc2016, uc2017, uc2018, uc2019, uc2020, uc2021, uc2022
from rentstab
full join rentstab_v2 using(ucbbl)
where ucbbl = %(bbl)s
Expand Down
1 change: 1 addition & 0 deletions rh/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ class Meta:
address = "123 Funky Way"
borough = "MANHATTAN"
address_verified = False
referral = None
10 changes: 10 additions & 0 deletions rh/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rh.schema import get_slack_notify_text
from rh.models import RentalHistoryRequest
from rh.tests.test_utils import EXAMPLE_RENT_STAB_DATA
from partnerships.tests.factories import PartnerOrgFactory

VALID_RH_DATA = {
"firstName": "Boop",
Expand Down Expand Up @@ -167,3 +168,12 @@ def test_it_works_for_anonymous_users(self, db):
f"Glorp &amp; Blorp has requested "
f"<https://example.com/admin/rh/rentalhistoryrequest/{rhr.pk}/change/|rent history>!"
)

def test_it_works_for_referrals(self, db):
referral = PartnerOrgFactory(name="Met Council on Housing", slug="mch")
rhr = RentalHistoryRequestFactory(user=None, first_name="Peep", referral=referral)
assert get_slack_notify_text(rhr) == (
f"Peep has requested "
f"<https://example.com/admin/rh/rentalhistoryrequest/{rhr.pk}/change/|rent history> "
f"via Met Council on Housing (mch)!"
)

0 comments on commit 4251fee

Please sign in to comment.