diff --git a/.github/workflows/repo-checks.yml b/.github/workflows/repo-checks.yml index b183e37e1..0d4a75c39 100644 --- a/.github/workflows/repo-checks.yml +++ b/.github/workflows/repo-checks.yml @@ -97,6 +97,7 @@ jobs: tests/integration/backfillTestimonyScope.test.ts tests/integration/conferences.test.ts tests/integration/conferenceFollowing.test.ts + tests/integration/conferenceLetters.test.ts components/auth/hooks.test.ts components/db/useUpcoming.test.ts components/db/testimony diff --git a/.gitignore b/.gitignore index b835a9df9..a076545f9 100644 --- a/.gitignore +++ b/.gitignore @@ -71,6 +71,8 @@ prod-build.env !*.secret infra/dev/typesense-secrets.yml infra/prod/typesense-secrets.yml +# Firebase extension secrets for the emulator +extensions/*.secret.local .python-version __pycache__/ diff --git a/AGENTS.md b/AGENTS.md index 26de4101a..d652c885b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -171,15 +171,15 @@ your branch ──PR──▶ main ──(automatic)──▶ dev site + dev bac [Repo Checks](.github/workflows/repo-checks.yml) runs on every push and PR. Run the same commands before you push: -| Check | Command | -| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------- | -| Lint | `yarn lint` | -| Formatting (all files, including Markdown) | `yarn check-formatting`; fix with `yarn fix` | -| Types | `yarn check-types` | -| Cloud Functions tests | `yarn test` | -| Website tests | `yarn test:website` | -| Integration tests | `yarn test:integration-ci ` (needs Java for the emulators) | -| Python in `llm/` | `ruff check llm/`, `ruff format --check llm/`, `pytest llm/` (after `pip install -r llm/requirements.txt`) | +| Check | Command | +| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- | +| Lint | `yarn lint` | +| Formatting (all files, including Markdown) | `yarn check-formatting`; fix with `yarn fix` | +| Types | `yarn check-types` | +| Cloud Functions tests | `yarn test` | +| Website tests | `yarn test:website` | +| Integration tests | `yarn test:integration-ci ` (needs Java for the emulators, and Typesense: `docker compose -f infra/docker-compose.yml up -d search`) | +| Python in `llm/` | `ruff check llm/`, `ruff format --check llm/`, `pytest llm/` (after `pip install -r llm/requirements.txt`) | CI runs only the website tests named in the `test:website` script and the integration test files listed in [repo-checks.yml](.github/workflows/repo-checks.yml). Add a new test to one of those lists, or it can fail on `main` unnoticed. diff --git a/components/conference/ConferenceDetails.switching.test.tsx b/components/conference/ConferenceDetails.switching.test.tsx index 057697e70..d0faac6fc 100644 --- a/components/conference/ConferenceDetails.switching.test.tsx +++ b/components/conference/ConferenceDetails.switching.test.tsx @@ -59,6 +59,7 @@ jest.mock("../auth", () => ({ const mounted: string[] = [] jest.mock("../db", () => ({ useProfile: () => ({ profile: {} }), + useMember: () => ({ member: undefined }), hasDraftChanged: () => false, useConferenceTestimony: () => ({ items: [], loading: false }), useEditConferenceTestimony: ( diff --git a/components/conference/ConferenceDetails.tsx b/components/conference/ConferenceDetails.tsx index 80e116735..a41ecd49f 100644 --- a/components/conference/ConferenceDetails.tsx +++ b/components/conference/ConferenceDetails.tsx @@ -37,7 +37,8 @@ import { useAppDispatch } from "../hooks" import { maple, siteUrl } from "../links" import { usePinnedNavbar } from "./hooks" import { InputContext } from "./input/context" -import { CosignForm, CosignStep } from "./input/CosignForm" +import { initialsOf } from "./input/accounts" +import { CosignForm } from "./input/CosignForm" import { CosignLetter } from "./input/CosignLetter" import { InputForm, Step } from "./input/InputForm" import { Panel, usePanelWidth } from "./input/Panel" @@ -49,7 +50,12 @@ import { X } from "./input/icons" import { jumpToSection } from "./navbar" import { OverviewLink } from "./OverviewLink" import { Notice, PageColumn, Title } from "./styles" -import { conferenceName, ConferencePageProps, ConferenceSummary } from "./types" +import { + conferenceName, + ConferencePageProps, + ConferenceSummary, + ConfereeView +} from "./types" /** The mockup's column, widened to 1320 once the rail sits beside it. */ const Page = styled(PageColumn)` @@ -244,11 +250,10 @@ function Details({ ) const byId = useMemo(() => new Map(items.map(x => [x.id, x])), [items]) const [open, setOpen] = useState(false) - const [step, setStep] = useState("compose") + const [step, setStep] = useState("write") // The panel files one of two things: the reader's own input, or a cosign of // `cosigning`. Both are the reader's one draft on the conference. const [cosigning, setCosigning] = useState() - const [cosignStep, setCosignStep] = useState("confirm") const [full, setFull] = useState(true) // What the reader's draft holds, as far as the page cares: what it // cosigns, if anything, and whether it has words. @@ -294,12 +299,12 @@ function Details({ const close = useCallback(() => setOpen(false), []) const compose = useCallback(() => { setCosigning(undefined) - setStep("compose") + setStep("write") setOpen(true) }, []) const resumeCosign = useCallback((parent: ConferenceTestimony) => { setCosigning(parent) - setCosignStep("write") + setStep("write") setOpen(true) }, []) // A posted cosign is edited as one, its note alone. @@ -329,7 +334,7 @@ function Details({ if (!signIn()) return if (draftWords && draftOf === parent.id) return resumeCosign(parent) setCosigning(parent) - setCosignStep("confirm") + setStep("confirm") setFull(true) setOpen(true) }, @@ -474,7 +479,7 @@ function Details({ ) // Signing fills the window with what is signed beside the form; once agreed, // the reader may fold it back to the drawer. - const filled = !!cosigning && (cosignStep === "confirm" || full) + const filled = !!cosigning && (step === "confirm" || full) return ( void cosigning?: ConferenceTestimony - cosignStep: CosignStep - onCosignStep: (s: CosignStep) => void full: boolean onFull: (full: boolean) => void onDraft: (draft?: ConferenceWorkingDraft, read?: boolean) => void onPosted: () => void }) { const { t } = useTranslation("conferences") + const initials = useMemo( + () => conferees.map(c => initialsOf(c.name)), + [conferees] + ) const edit = useEditConferenceTestimony( uid, conference.court, @@ -659,10 +665,10 @@ function InputPanel({ }, [edit.draft, edit.loading, onDraft]) const title = - cosigning && edit.publication - ? "input.cosign_edit_title" - : !cosigning && step === "review" + step === "review" ? "input.review_title" + : cosigning && edit.publication + ? "input.cosign_edit_title" : "input.panel_title" return ( } full={full} - onFull={cosigning && cosignStep === "write" ? onFull : undefined} + onFull={cosigning && step === "write" ? onFull : undefined} > {cosigning ? ( // Keyed so another parent starts from its own draft. @@ -682,14 +688,20 @@ function InputPanel({ key={cosigning.id} edit={edit} parent={cosigning} - step={cosignStep} - onStep={onCosignStep} + court={conference.court} + conferenceId={conference.id} + subject={subject} + conferees={initials} + step={step} + onStep={onStep} onCancel={onClose} onPosted={onPosted} /> ) : ( ({ useTranslation: () => ({ @@ -18,11 +19,37 @@ jest.mock("next-i18next", () => ({ })) jest.mock("components/auth", () => ({ - useAuth: () => ({ claims: { role: "user" } }) + useAuth: () => ({ + claims: { role: "user" }, + user: { email: "gina@example.org" } + }) })) +const mockProfile: { current: Record } = { current: {} } jest.mock("components/db", () => ({ - useProfile: () => ({ profile: { fullName: "Gina K." } }) + useProfile: () => ({ profile: mockProfile.current }), + useMember: () => ({ member: undefined }), + // As components/db compares a draft with what was posted. + hasDraftChanged: ( + draft?: Record, + published?: Record + ) => + !!draft && + !!published && + (["position", "content"] as const).some( + k => (draft[k] || undefined) !== (published[k] || undefined) + ) })) +jest.mock("components/db/legislatorMail", () => ({ + useLetterOnRecord: () => false +})) + +const legislators = { + representative: { id: "r1", name: "Rae Rep", district: "3rd Middlesex" }, + senator: { id: "s1", name: "Sam Sen", district: "Middlesex and Suffolk" } +} +beforeEach(() => { + mockProfile.current = { fullName: "Gina K.", ...legislators } +}) const parent = { id: "p1", @@ -51,13 +78,17 @@ const editing = ( const mount = ( edit: UseEditConferenceTestimony, - step: CosignStep, + step: Step, more: { onStep?: jest.Mock; onPosted?: jest.Mock } = {} ) => render( { const draft = { cosignOfId: "p1", content: "" } const edit = editing({ draft } as Partial) const onPosted = jest.fn() - mount(edit, "write", { onPosted }) - press("input.cosign") - expect(edit.publishTestimony.execute).not.toHaveBeenCalled() + const onStep = jest.fn() + mount(edit, "write", { onPosted, onStep }) + press("input.review") + expect(onStep).not.toHaveBeenCalled() fireEvent.click( screen.getByRole("checkbox", { name: "input.cosign_no_words_label" }) ) expect(screen.getByRole("textbox")).toBeDisabled() - await act(async () => press("input.cosign")) - expect(edit.publishTestimony.execute).toHaveBeenCalled() - expect(onPosted).toHaveBeenCalled() + press("input.review") + expect(onStep).toHaveBeenCalledWith("review") + expect(edit.publishTestimony.execute).not.toHaveBeenCalled() + }) + + describe("review", () => { + const draft = { cosignOfId: "p1", content: "My note." } + + it("shows where the cosign goes and the letter as sent, and posts it", async () => { + const edit = editing({ draft } as Partial) + const onPosted = jest.fn() + mount(edit, "review", { onPosted }) + expect(screen.getByText("review.goes_cosign")).toBeInTheDocument() + expect( + screen.getByText("Dear Representative Rae Rep and Senator Sam Sen,") + ).toBeInTheDocument() + expect( + screen.getByText("I cosigned this input by Example Coalition on MAPLE:") + ).toBeInTheDocument() + expect(screen.getByText("The letter.")).toBeInTheDocument() + // The reader is copied, and replies go to them. + expect(screen.getAllByText("gina@example.org").length).toBeGreaterThan(1) + await act(async () => press("review.cosign_and_send")) + expect(edit.publishTestimony.execute).toHaveBeenCalled() + expect(onPosted).toHaveBeenCalled() + }) + + it("tells a cosign without words of its own that it still counts", () => { + const onStep = jest.fn() + mount( + editing({ + draft: { cosignOfId: "p1", content: "" } + } as Partial), + "review", + { onStep } + ) + expect(screen.getByText(/review\.no_line/)).toBeInTheDocument() + press("review.add_line") + expect(onStep).toHaveBeenCalledWith("write") + }) + + it("says no banner over a cosign with words", () => { + mount(editing({ draft } as Partial), "review") + expect(screen.queryByText(/review\.no_line/)).not.toBeInTheDocument() + }) + + it("says so, and sends no letter, when MAPLE has no one to notify", async () => { + mockProfile.current = { fullName: "Gina K." } + const edit = editing({ draft } as Partial) + mount(edit, "review") + expect(screen.getByText("review.emailed_none")).toBeInTheDocument() + expect(screen.queryByText("review.preview_label")).not.toBeInTheDocument() + await act(async () => press("input.cosign")) + expect(edit.publishTestimony.execute).toHaveBeenCalled() + }) }) it.each([ @@ -145,29 +229,82 @@ describe("CosignForm", () => { expect(screen.getByRole("alert")).toHaveTextContent(message) }) - it("edits a posted cosign's words alone, with a reason", () => { - const publication = { cosignOfId: "p1", content: "Before." } - mount( - editing({ publication } as Partial), - "write" - ) - expect(screen.getByRole("textbox")).toHaveValue("Before.") - const save = screen.getByRole("button", { name: "input.cosign_save" }) - expect(save).toBeDisabled() - expect(screen.queryByText("publish.reasonLabel")).not.toBeInTheDocument() - // No way to pick another position, or to leave the parent. - expect( - screen.queryByRole("button", { name: /input\.position\./ }) - ).not.toBeInTheDocument() - - fireEvent.change(screen.getByRole("textbox"), { - target: { value: "After." } + describe("editing a posted cosign", () => { + const publication = { + id: "c1", + cosignOfId: "p1", + position: "passSenateVersion", + content: "Before." + } + + it("changes its words alone, and goes to review to post them", () => { + const onStep = jest.fn() + mount( + editing({ publication } as Partial), + "write", + { onStep } + ) + expect(screen.getByRole("textbox")).toHaveValue("Before.") + // No way to pick another position, to leave the parent, or to post + // without seeing the letter. + expect( + screen.queryByRole("button", { name: /input\.position\./ }) + ).not.toBeInTheDocument() + expect(screen.queryByText("publish.reasonLabel")).not.toBeInTheDocument() + fireEvent.change(screen.getByRole("textbox"), { + target: { value: "After." } + }) + press("input.review") + expect(onStep).toHaveBeenCalledWith("review") + }) + + it("asks for a reason for the change at review", () => { + mount( + editing({ + publication, + draft: { cosignOfId: "p1", content: "After." } + } as Partial), + "review" + ) + const send = screen.getByRole("button", { + name: "review.cosign_and_send" + }) + expect(send).toBeDisabled() + fireEvent.change( + screen.getByPlaceholderText("publish.reasonPlaceholder"), + { target: { value: "Clearer" } } + ) + expect(send).toBeEnabled() }) - expect(screen.getByText("publish.reasonLabel")).toBeInTheDocument() - expect(save).toBeDisabled() - fireEvent.change(screen.getAllByRole("textbox")[1], { - target: { value: "Clearer" } + + it("posts the change with its reason", async () => { + const edit = editing({ + publication, + draft: { cosignOfId: "p1", content: "After.", editReason: "Clearer" } + } as Partial) + const onPosted = jest.fn() + mount(edit, "review", { onPosted }) + await act(async () => press("review.cosign_and_send")) + expect(edit.publishTestimony.execute).toHaveBeenCalled() + expect(onPosted).toHaveBeenCalled() + // The form stays mounted, so the next edit starts without this reason. + expect( + screen.getByPlaceholderText("publish.reasonPlaceholder") + ).toHaveValue("") + }) + + it("cannot post it unchanged", () => { + mount( + editing({ + publication, + draft: { cosignOfId: "p1", content: "Before." } + } as Partial), + "review" + ) + expect( + screen.getByRole("button", { name: "review.cosign_and_send" }) + ).toBeDisabled() + expect(screen.queryByText("publish.reasonLabel")).not.toBeInTheDocument() }) - expect(save).toBeEnabled() }) }) diff --git a/components/conference/input/CosignForm.tsx b/components/conference/input/CosignForm.tsx index 09c98c5a8..6cabf2fad 100644 --- a/components/conference/input/CosignForm.tsx +++ b/components/conference/input/CosignForm.tsx @@ -29,10 +29,11 @@ import { Primary, Problem, Quiet, - ReasonField, + ReviewStep, Rules, Scroll, Status, + Step, useAsk } from "./InputForm" import { useInputContext } from "./context" @@ -40,8 +41,6 @@ import { UsersRound } from "./icons" import { brand, caution, ink, line, red, wash } from "./tokens" import { useAutosave } from "./useAutosave" -export type CosignStep = "confirm" | "write" - /** Below lg, where the letter sits above it, the agreeing step takes the room * it needs and leaves the letter the rest. */ const Column = styled(Body)<{ $natural?: boolean }>` @@ -248,25 +247,35 @@ const refusalOf = (error?: Error) => ?.refusal /** - * Cosigning someone else's input, in the panel's two steps: agreeing to what - * is signed, then an optional note of one's own. Editing a cosign is the - * second step alone, and changes the note only. The cosign is the user's one - * draft on the conference, kept as they type. + * Cosigning someone else's input, in the panel's three steps: agreeing to + * what is signed, an optional note of one's own, and the review every input + * goes through. Editing a cosign starts at the note, and changes it only. The + * cosign is the user's one draft on the conference, kept as they type. */ export function CosignForm({ edit, parent, + court, + conferenceId, + subject, + conferees, step, onStep, onCancel, onPosted }: { + court: number + conferenceId: string + /** What the conference is called, for the letter's subject. */ + subject: string + /** Initials of the conferees, for the weekly update's row. */ + conferees: string[] /** The user's draft and publication on the conference. */ edit: UseEditConferenceTestimony /** What is being cosigned. */ parent: ConferenceTestimony - step: CosignStep - onStep: (s: CosignStep) => void + step: Step + onStep: (s: Step) => void onCancel: () => void /** The cosign is on the record. */ onPosted: () => void @@ -276,7 +285,6 @@ export function CosignForm({ const { byId } = useInputContext() const { profile } = useProfile() const { saveDraft, publishTestimony, discardDraft } = edit - const editing = !!edit.publication const [note, setNote] = useState("") const [noWords, setNoWords] = useState(false) @@ -316,8 +324,12 @@ export function CosignForm({ cosignOfAuthorUid: parent.authorUid, editReason: f.reason || undefined }), - active: step === "write", - onPosted + active: step !== "confirm", + // As InputForm's: the form outlives the post, so its reason goes too. + onPosted: () => { + setReason("") + onPosted() + } }) // Ready to write on arrival, without scrolling the preview out of view. @@ -326,8 +338,7 @@ export function CosignForm({ }, [step]) const ready = !!note.trim() || noWords - const changed = !editing || content !== edit.publication?.content - const needsReason = editing && changed + const editing = !!edit.publication // Delete Draft is offered once there are words kept to delete. const kept = edit.draft?.cosignOfId === parent.id && !!edit.draft.content const showDelete = !editing && kept && save === "none" && !dirty @@ -389,6 +400,28 @@ export function CosignForm({ const name = profile?.fullName || t("input.anonymous") const type = accountTypeOf(claims?.role ?? "user") + if (step === "review") + return ( + onStep("write")} + onCancel={onCancel} + onPost={post} + /> + ) return ( @@ -442,7 +475,6 @@ export function CosignForm({ /> {t("input.cosign_no_words_label")} - {needsReason && } {problem} @@ -461,14 +493,9 @@ export function CosignForm({ (ready ? post() : ask.ask())} + onClick={() => (ready ? onStep("review") : ask.ask())} > - {t(editing ? "input.cosign_save" : "input.cosign")} + {t("input.review")} diff --git a/components/conference/input/InputForm.test.tsx b/components/conference/input/InputForm.test.tsx index 72c5157de..e8e8aee7e 100644 --- a/components/conference/input/InputForm.test.tsx +++ b/components/conference/input/InputForm.test.tsx @@ -1,5 +1,5 @@ import "@testing-library/jest-dom" -import { act, render } from "@testing-library/react" +import { act, fireEvent, render, screen } from "@testing-library/react" import type { UseEditConferenceTestimony } from "components/db" import { InputForm } from "./InputForm" @@ -9,12 +9,58 @@ jest.mock("next-i18next", () => ({ })) jest.mock("components/auth", () => ({ - useAuth: () => ({ claims: { role: "user" } }) + useAuth: () => ({ + claims: { role: "user" }, + user: { email: "gina@example.org" } + }) })) +const mockProfile: { current: Record } = { current: {} } jest.mock("components/db", () => ({ - useProfile: () => ({ profile: { fullName: "Gina K." } }) + useProfile: () => ({ profile: mockProfile.current }), + useMember: () => ({ member: undefined }), + hasDraftChanged: () => true +})) +// The testimony ids with a letter on record. +const mockOnRecord = new Set() +jest.mock("components/db/legislatorMail", () => ({ + useLetterOnRecord: (testimonyId?: string) => + !!testimonyId && mockOnRecord.has(testimonyId) })) +beforeEach(() => { + mockProfile.current = { + fullName: "Gina K.", + representative: { id: "r1", name: "Rae Rep", district: "3rd Middlesex" }, + senator: { id: "s1", name: "Sam Sen", district: "Middlesex and Suffolk" } + } +}) + +const editing = (overrides: Record = {}) => + ({ + saveDraft: { execute: jest.fn().mockResolvedValue(undefined) }, + publishTestimony: { execute: jest.fn().mockResolvedValue(undefined) }, + discardDraft: { execute: jest.fn() }, + loading: false, + draftLoading: false, + draft: { position: "passSomething", content: "Pass it, please." }, + ...overrides + } as unknown as UseEditConferenceTestimony) + +const review = (edit: UseEditConferenceTestimony) => + render( + + ) + describe("InputForm", () => { it("leaves a cosign's draft alone while it sits behind a closed panel", () => { jest.useFakeTimers() @@ -32,10 +78,12 @@ describe("InputForm", () => { } as unknown as UseEditConferenceTestimony render( { expect(edit.saveDraft.execute).not.toHaveBeenCalled() jest.useRealTimers() }) + + describe("review", () => { + it("shows where the input goes and the letter as it will be sent", () => { + review(editing()) + expect(screen.getByText("review.goes_input")).toBeInTheDocument() + expect( + screen.getByText("Dear Representative Rae Rep and Senator Sam Sen,") + ).toBeInTheDocument() + expect(screen.getByText("Pass it, please.")).toBeInTheDocument() + expect( + screen.getByRole("button", { name: "review.post_and_send" }) + ).toBeInTheDocument() + }) + + it("marks the letter updated only when an earlier one is on record", () => { + const publication = { id: "t1", position: "passSomething", content: "" } + const edit = editing({ + publication + } as Partial) + const { unmount } = review(edit) + expect(screen.getByText(/^Phone-Free Schools: /)).not.toHaveTextContent( + "(updated)" + ) + unmount() + + mockOnRecord.add("t1") + review(edit) + expect(screen.getByText(/^Phone-Free Schools: /)).toHaveTextContent( + /\(updated\)$/ + ) + mockOnRecord.clear() + }) + + it("says no one will be emailed when the profile has no legislators", () => { + mockProfile.current = { fullName: "Gina K." } + review(editing()) + expect(screen.getByText("review.emailed_none")).toBeInTheDocument() + expect(screen.queryByText("review.preview_label")).not.toBeInTheDocument() + expect( + screen.getByRole("button", { name: "input.post" }) + ).toBeInTheDocument() + }) + + it("starts the next edit without the last one's reason", async () => { + const edit = editing({ + publication: { id: "t1", position: "passSomething", content: "Pass." }, + draft: { + position: "passSomething", + content: "Pass it, please.", + editReason: "Clearer" + } + }) + review(edit) + const reason = screen.getByPlaceholderText("publish.reasonPlaceholder") + expect(reason).toHaveValue("Clearer") + await act(async () => + fireEvent.click( + screen.getByRole("button", { name: "review.post_and_send" }) + ) + ) + expect(edit.publishTestimony.execute).toHaveBeenCalled() + expect(reason).toHaveValue("") + }) + }) }) diff --git a/components/conference/input/InputForm.tsx b/components/conference/input/InputForm.tsx index 132b29248..7d2e1459c 100644 --- a/components/conference/input/InputForm.tsx +++ b/components/conference/input/InputForm.tsx @@ -1,11 +1,16 @@ -import { RefObject, useCallback, useEffect, useRef, useState } from "react" -import { Timestamp } from "firebase/firestore" +import { + ComponentProps, + ReactNode, + RefObject, + useCallback, + useEffect, + useRef, + useState +} from "react" import { Trans, useTranslation } from "next-i18next" import styled, { keyframes } from "styled-components" -import { useAuth } from "components/auth" import { ConferencePosition, - ConferenceTestimony, hasDraftChanged, Testimony, UseEditConferenceTestimony, @@ -13,12 +18,14 @@ import { WorkingDraft } from "components/db" import { Internal } from "components/links" -import { Entry } from "./Entry" +import { hasLegislators } from "functions/src/conferenceLetters/letter" +import { LetterReview, NoLine } from "./Review" import { conferencePositions, positionColours } from "./positions" import { brand, ink, line, red, wash } from "./tokens" import { useAutosave } from "./useAutosave" -export type Step = "compose" | "review" +/** The panel's steps. Only a cosign is agreed to first. */ +export type Step = "confirm" | "write" | "review" export const Body = styled.div` display: flex; @@ -154,12 +161,6 @@ export const Primary = styled.button` } ` -const PreviewBox = styled.div` - background: var(--maple-surface-base); - border: 1px solid ${line.default}; - border-radius: 16px; -` - export const Rules = styled.ul` margin: 0; padding-left: 16px; @@ -216,7 +217,7 @@ export function useAsk(field: RefObject) { } /** The edit reason a republished filing needs, as it does everywhere else. */ -export function ReasonField({ +function ReasonField({ value, onChange }: { @@ -253,13 +254,108 @@ export function ConductRule() { ) } +/** The step before posting, for input and cosigns alike: where it goes, the + * letter as it will be sent, the reason when it changes what was posted, and + * the way to post it. */ +export function ReviewStep({ + edit, + ready, + posting, + problem, + reason, + onReason, + onBack, + onCancel, + onPost, + ...letter +}: Omit, "testimonyId"> & { + /** The user's draft and publication on the conference. */ + edit: UseEditConferenceTestimony + /** There is something to post: input's words, or a cosign with or without. */ + ready: boolean + posting: boolean + problem?: ReactNode + reason: string + onReason: (reason: string) => void + onBack: () => void + onCancel: () => void + onPost: () => void +}) { + const { t } = useTranslation("conferences") + const { profile } = useProfile() + // Whether the letter goes anywhere, from the profile alone. + const sends = hasLegislators(profile ?? {}) + // Republishing needs a reason for the change, as it does everywhere else. + const editing = !!edit.publication + const changed = + !editing || + hasDraftChanged( + { + position: letter.position, + content: letter.content + } as unknown as WorkingDraft, + edit.publication as unknown as Testimony + ) + const needsReason = editing && changed + const canPost = + ready && + changed && + (!needsReason || !!reason.trim()) && + !posting && + !edit.publishTestimony.loading + const cosign = !!letter.cosign + return ( + + + {cosign && !letter.content.trim() && } + + {!cosign && ( +
+ {t("input.rules_label")} + +
  • {t("input.rule_revise")}
  • + +
    +
    + )} + {needsReason && } + {problem} +
    +
    + {!ready && {t("input.empty_hint")}} + + + {t("input.back")} + + + {t("input.cancel")} + + + {t( + cosign + ? sends + ? "review.cosign_and_send" + : "input.cosign" + : sends + ? "review.post_and_send" + : "input.post" + )} + + +
    + + ) +} + /** * Writing, reviewing and posting testimony on a conference, in the panel's - * three steps. The draft is the user's own, kept as they type and published + * two steps. The draft is the user's own, kept as they type and published * from the review step. */ export function InputForm({ - uid, + court, + conferenceId, + conferees, edit, subject, step, @@ -267,7 +363,10 @@ export function InputForm({ onCancel, onPosted }: { - uid: string + court: number + conferenceId: string + /** Initials of the conferees, for the weekly update's row. */ + conferees: string[] /** The user's draft and publication on the conference. */ edit: UseEditConferenceTestimony subject: string @@ -278,7 +377,6 @@ export function InputForm({ onPosted: () => void }) { const { t } = useTranslation("conferences") - const { claims } = useAuth() const { profile } = useProfile() const { saveDraft, publishTestimony, discardDraft } = edit @@ -321,7 +419,12 @@ export function InputForm({ editReason: f.reason || undefined }), idle: f => !f.content.trim(), - onPosted + // Publishing clears the draft's reason; the form outlives the post, behind + // the closed panel, so it clears its own for the next edit too. + onPosted: () => { + setReason("") + onPosted() + } }) const written = content.trim().length > 0 @@ -343,30 +446,11 @@ export function InputForm({ saveDraft.error || publishTestimony.error || discardDraft.error - // Republishing needs a reason for the change, as it does everywhere else. - const editing = !!edit.publication - const changed = - !editing || - hasDraftChanged( - { position, content } as unknown as WorkingDraft, - edit.publication as unknown as Testimony - ) - const needsReason = editing && changed const showDelete = !!edit.draft && save === "none" && !dirty const name = profile?.fullName || t("input.anonymous") - const entry = { - authorUid: uid, - id: "preview", - position, - content, - authorRole: claims?.role ?? "user", - authorDisplayName: name, - fullName: name, - publishedAt: Timestamp.now() - } as unknown as ConferenceTestimony - if (step === "compose") + if (step !== "review") return ( @@ -421,56 +505,23 @@ export function InputForm({ ) return ( - - - - - - -
    - {t("input.rules_label")} - -
  • {t("input.rule_public")}
  • -
  • {t("input.rule_revise")}
  • - -
    -
    - {needsReason && } - {failed && {t("input.error")}} -
    -
    - {!written && {t("input.empty_hint")}} - - onStep("compose")} - style={{ marginRight: "auto" }} - > - {t("input.back")} - - - {t("input.cancel")} - - - {t("input.post")} - - -
    - + {t("input.error")}} + reason={reason} + onReason={setReason} + onBack={() => onStep("write")} + onCancel={onCancel} + onPost={post} + /> ) } diff --git a/components/conference/input/Review.tsx b/components/conference/input/Review.tsx new file mode 100644 index 000000000..a198898f0 --- /dev/null +++ b/components/conference/input/Review.tsx @@ -0,0 +1,412 @@ +import { useTranslation } from "next-i18next" +import styled from "styled-components" +import { useAuth } from "components/auth" +import { ProfileMember, useMember, useProfile } from "components/db" +import { useLetterOnRecord } from "components/db/legislatorMail" +import { + ConferencePosition, + hasLegislators, + Letter, + LetterMember, + renderLetter +} from "functions/src/conferenceLetters/letter" +import { Disc as Avatar } from "./Entry" +import { CalendarDays, House, Mail, SquarePen } from "./icons" +import { brand, ink, line, wash } from "./tokens" + +const Heading = styled.p` + margin: 0 0 10px; + font-size: 0.6875rem; + font-weight: 600; + letter-spacing: 0.08em; + text-transform: uppercase; + color: ${ink.mid}; +` + +const Goes = styled.ul` + list-style: none; + margin: 0 0 22px; + padding: 0; + display: flex; + flex-direction: column; + gap: 14px; +` + +const Row = styled.li` + display: flex; + gap: 12px; + align-items: flex-start; + font-size: 0.875rem; + line-height: 1.5; + color: ${ink.midheavy}; + svg { + flex: none; + margin-top: 2px; + color: ${brand.ink}; + } + strong { + color: ${ink.strong}; + } +` + +const Aside = styled.span` + display: block; + margin-top: 2px; + font-size: 0.8125rem; + color: ${ink.mid}; +` + +const Conferees = styled.div` + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + margin-top: 10px; + font-size: 0.8125rem; + color: ${ink.mid}; +` + +const Discs = styled.div` + display: flex; + padding-left: 8px; +` + +const Disc = styled(Avatar)` + width: 30px; + height: 30px; + margin-left: -8px; + border: 2px solid var(--maple-surface-base); + font-size: 0.6875rem; + font-weight: 700; +` + +const Box = styled.div` + margin-bottom: 22px; + padding: 14px 16px; + border: 1px solid ${line.default}; + border-radius: 8px; + font-size: 0.8125rem; + line-height: 1.5; + color: ${ink.midheavy}; +` + +const Current = styled.div` + margin-top: 6px; + font-size: 0.875rem; + color: ${ink.strong}; +` + +const Message = styled.div` + padding: 14px 16px; + background: ${wash.default}; + border: 1px solid ${line.default}; + border-radius: 8px; + font-size: 0.8125rem; + line-height: 1.55; + color: ${ink.midheavy}; + p { + margin: 0 0 10px; + } +` + +const Headers = styled.dl` + display: grid; + grid-template-columns: 70px 1fr; + gap: 4px 12px; + margin: 0 0 12px; + padding-bottom: 12px; + border-bottom: 1px solid ${line.default}; + dt { + font-weight: 400; + color: ${ink.mid}; + } + dd { + margin: 0; + color: ${ink.strong}; + overflow-wrap: anywhere; + } +` + +const Quote = styled.blockquote` + margin: 0 0 12px; + padding: 2px 0 2px 14px; + border-left: 3px solid ${brand.edge}; + p { + margin-bottom: 6px; + } +` + +const Foot = styled.p` + && { + margin: 0; + font-size: 0.75rem; + color: ${ink.mid}; + } +` + +const Banner = styled.p` + display: flex; + gap: 10px; + align-items: flex-start; + margin: 0 0 20px; + padding: 12px 14px; + background: ${brand.soft}; + border-radius: 8px; + font-size: 0.8125rem; + line-height: 1.5; + color: ${brand.fill}; + svg { + flex: none; + margin-top: 2px; + } + button { + padding: 0; + border: 0; + background: none; + font: inherit; + font-weight: 700; + color: inherit; + text-decoration: underline; + } +` + +/** What a cosign without words of its own says to its reader: it still + * counts, and a line would carry more weight. */ +export function NoLine({ onAdd }: { onAdd: () => void }) { + const { t } = useTranslation("conferences") + return ( + + + + {t("review.no_line")}{" "} + + + + ) +} + +/** The reader's rep and senator as the letter names them: the member + * documents' own name and district, so a renamed district isn't stale, and + * what the profile holds until they load. */ +export function useLegislators(court: number) { + const { profile } = useProfile() + const rep = useMember(court, profile?.representative?.id).member + const sen = useMember(court, profile?.senator?.id).member + const named = ( + held: ProfileMember | undefined, + doc: typeof rep + ): LetterMember | undefined => + held && { + name: doc?.Name ?? held.name, + district: doc?.District ?? held.district + } + return { + representative: named(profile?.representative, rep), + senator: named(profile?.senator, sen) + } +} + +/** The reader's own address, which replies go to and which is copied. */ +function YourEmail({ email }: { email: string }) { + const { t } = useTranslation("conferences") + return ( + <> + {t("review.email_label")} + + {t("review.email_note")} + + {email} + + + + ) +} + +/** + * Where conference input goes: public on MAPLE, to the reader's rep and + * senator, and into the conferees' weekly update. A reader whose profile has + * neither is told whom MAPLE can't notify, and that only the update counts + * them. + */ +export function Delivery({ + cosign, + conferees, + representative, + senator +}: { + /** The name of whoever's input this cosigns. */ + cosign?: string + /** Initials of the conferees. */ + conferees: string[] + representative?: LetterMember + senator?: LetterMember +}) { + const { t } = useTranslation("conferences") + const known = hasLegislators({ representative, senator }) + const names = [ + representative && t("review.rep", { name: representative.name }), + senator && t("review.sen", { name: senator.name }) + ] + .filter(Boolean) + .join(t("review.and")) + return ( + <> + + {t(cosign ? "review.goes_cosign" : "review.goes_input")} + + + + +
    + {cosign + ? t("review.public_cosign", { name: cosign }) + : t("review.public_input")} + {!cosign && } +
    +
    + + +
    + {known ? ( + <> + {t("review.emailed")} {names}. + {cosign && " " + t("review.emailed_cosign", { name: cosign })} + + ) : ( + t("review.emailed_none") + )} +
    +
    + + +
    + {t(cosign ? "review.weekly_cosign" : "review.weekly_input")} + + {t("review.conferees")} + + {conferees.map((c, i) => ( + {c} + ))} + + +
    +
    +
    + + ) +} + +/** The letter as it will be sent. */ +export function LetterPreview({ letter }: { letter: Letter }) { + const { t } = useTranslation("conferences") + return ( + <> + {t("review.preview_label")} + + +
    {t("review.from")}
    +
    {letter.from}
    +
    {t("review.reply_to")}
    +
    {letter.replyTo}
    +
    {t("review.cc")}
    +
    {letter.cc}
    +
    {t("review.to")}
    +
    {letter.to}
    +
    {t("review.subject")}
    +
    {letter.subject}
    +
    +

    {letter.salutation}

    + {letter.paragraphs.map((p, i) => ( +

    {p}

    + ))} + {letter.cosigned && ( + <> +

    + {letter.cosigned.lead} +

    + + {letter.cosigned.quote.map((p, i) => ( +

    {p}

    + ))} +
    + + )} +

    + {letter.signature.map((s, i) => ( + + {i > 0 &&
    } + {s} +
    + ))} +

    + {letter.footer} +
    + + ) +} + +/** The body of a review step: where the input goes and, when MAPLE knows whom + * to write to, the reply-to address and the letter as it will be sent. Mounted + * on the review step alone, so the legislators are read only there. */ +export function LetterReview({ + court, + conferenceId, + conferees, + author, + conference, + testimonyId, + position, + content, + cosign +}: { + court: number + conferenceId: string + /** Initials of the conferees, for the weekly update's row. */ + conferees: string[] + author: string + conference: string + /** The input the reader posted before, when editing it. The letter is an + * update when one for it is on record. */ + testimonyId?: string + position: ConferencePosition + /** The reader's own words, empty on a cosign without a note. */ + content: string + /** Whose input this cosigns. */ + cosign?: { author: string; content: string } +}) { + const { representative, senator } = useLegislators(court) + const email = useAuth().user?.email ?? "" + const edited = useLetterOnRecord(testimonyId) + return ( + <> + + {hasLegislators({ representative, senator }) && ( + <> + + + + )} + + ) +} diff --git a/components/conference/input/icons.tsx b/components/conference/input/icons.tsx index e4bf3defe..8075157ed 100644 --- a/components/conference/input/icons.tsx +++ b/components/conference/input/icons.tsx @@ -27,6 +27,26 @@ function Icon({ type Props = { size?: number; strokeWidth?: number; fill?: string } +export const House = (p: Props) => ( + + + + +) +export const Mail = (p: Props) => ( + + + + +) +export const CalendarDays = (p: Props) => ( + + + + + + +) export const Megaphone = (p: Props) => ( diff --git a/components/db/legislatorMail.ts b/components/db/legislatorMail.ts new file mode 100644 index 000000000..9f148b90d --- /dev/null +++ b/components/db/legislatorMail.ts @@ -0,0 +1,29 @@ +import { collection, getDocs, limit, query, where } from "firebase/firestore" +import { useAsync } from "react-async-hook" +import { useAuth } from "../auth" +import { firestore } from "../firebase" + +/** Whether a letter for the reader's input is on record, so that one written + * from it now goes out as an update. A reader can read only their own + * records, so the query names them. */ +export async function hasLetterOnRecord(uid: string, testimonyId: string) { + const snap = await getDocs( + query( + collection(firestore, "legislatorMail"), + where("uid", "==", uid), + where("testimonyId", "==", testimonyId), + limit(1) + ) + ) + return !snap.empty +} + +/** Whether the signed-in reader's input has a letter on record. */ +export function useLetterOnRecord(testimonyId?: string) { + const uid = useAuth().user?.uid + const { result } = useAsync( + async () => !!uid && !!testimonyId && hasLetterOnRecord(uid, testimonyId), + [uid, testimonyId] + ) + return result ?? false +} diff --git a/firestore.indexes.json b/firestore.indexes.json index bef3e384e..7ad937f48 100644 --- a/firestore.indexes.json +++ b/firestore.indexes.json @@ -1137,6 +1137,20 @@ "order": "DESCENDING" } ] + }, + { + "collectionGroup": "legislatorMail", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "testimonyId", + "order": "ASCENDING" + }, + { + "fieldPath": "createdAt", + "order": "DESCENDING" + } + ] } ], "fieldOverrides": [ diff --git a/firestore.rules b/firestore.rules index ce51353d5..8c97b4837 100644 --- a/firestore.rules +++ b/firestore.rules @@ -79,6 +79,11 @@ service cloud.firestore { allow read: if true; allow write: if request.auth.token.get("role", "user") == "admin"; } + // Emails sent to legislators, written by functions only. A reader can + // read the records of their own letters. + match /legislatorMail/{id} { + allow read: if request.auth != null && resource.data.uid == request.auth.uid; + } match /users/{uid} { allow read, write: if request.auth.token.get("role", "user") == "admin" match /draftTestimony/{id} { diff --git a/functions/.env.demo-dtp b/functions/.env.demo-dtp index a2cfe3433..9ad31d75c 100644 --- a/functions/.env.demo-dtp +++ b/functions/.env.demo-dtp @@ -1 +1,4 @@ TYPESENSE_API_URL=http://localhost:8108 + +# Outside production every email to legislators goes here instead. +LETTER_TEST_RECIPIENT=test@example.invalid diff --git a/functions/.env.digital-testimony-dev b/functions/.env.digital-testimony-dev index 43f445568..848b93511 100644 --- a/functions/.env.digital-testimony-dev +++ b/functions/.env.digital-testimony-dev @@ -1,2 +1,9 @@ MCP_SERVER_URL=https://maple-mcp-server-ke6znoupgq-uc.a.run.app -TRANSCRIPTION_WEBHOOK_URL=https://transcriptionv2-ke6znoupgq-uc.a.run.app \ No newline at end of file +TRANSCRIPTION_WEBHOOK_URL=https://transcriptionv2-ke6znoupgq-uc.a.run.app + +# Each conference stream sends from its own authenticated subdomain. +LETTER_FROM=noreply@letters.mapletestimony.org +CONFEREE_UPDATE_FROM=noreply@updates.mapletestimony.org + +# Matt gets test emails in dev +LETTER_TEST_RECIPIENT=msvictor51@gmail.com diff --git a/functions/.env.digital-testimony-prod b/functions/.env.digital-testimony-prod index a4277475f..56f20ccb3 100644 --- a/functions/.env.digital-testimony-prod +++ b/functions/.env.digital-testimony-prod @@ -1,4 +1,8 @@ TYPESENSE_API_URL=https://yyd73lsw3h.execute-api.us-east-1.amazonaws.com/search FUNCTIONS_API_BASE=https://us-central1-digital-testimony-prod.cloudfunctions.net MCP_SERVER_URL=https://maple-mcp-server-652493556525.us-central1.run.app -TRANSCRIPTION_WEBHOOK_URL=https://transcriptionv2-rwjc76gq5q-uc.a.run.app \ No newline at end of file +TRANSCRIPTION_WEBHOOK_URL=https://transcriptionv2-rwjc76gq5q-uc.a.run.app + +# Each conference stream sends from its own authenticated subdomain. +LETTER_FROM=noreply@letters.mapletestimony.org +CONFEREE_UPDATE_FROM=noreply@updates.mapletestimony.org diff --git a/functions/src/common.ts b/functions/src/common.ts index 6bedad54b..3ee5548d2 100644 --- a/functions/src/common.ts +++ b/functions/src/common.ts @@ -91,6 +91,20 @@ export const logFetchError = (label: string, id?: string) => (e: any) => { } } +const ALREADY_EXISTS = 6 + +/** Awaits a Firestore create, or a batch of creates: false when a document + * already exists, so a rerun writes nothing. */ +export async function createdUnlessExists(write: Promise) { + try { + await write + return true + } catch (e) { + if ((e as { code?: number }).code !== ALREADY_EXISTS) throw e + return false + } +} + // In particular, reject "/" in ID strings const simpleId = /^[A-Za-z0-9-_ ]+$/ /** Validates firestore-compatible ID's */ diff --git a/functions/src/conferenceLetters/index.ts b/functions/src/conferenceLetters/index.ts new file mode 100644 index 000000000..06bcc4531 --- /dev/null +++ b/functions/src/conferenceLetters/index.ts @@ -0,0 +1 @@ +export { mailConferenceLetters } from "./sendLetters" diff --git a/functions/src/conferenceLetters/letter.test.ts b/functions/src/conferenceLetters/letter.test.ts new file mode 100644 index 000000000..124f6bfc6 --- /dev/null +++ b/functions/src/conferenceLetters/letter.test.ts @@ -0,0 +1,98 @@ +import { hasLegislators, LetterInput, renderLetter } from "./letter" + +const base: LetterInput = { + author: "Ada Example", + email: "ada@example.org", + court: 194, + conferenceId: "H4321-S2100", + conference: "Data privacy conference committee", + position: "passSenateVersion", + content: "First thought.\n\nSecond thought.", + representative: { name: "Rae Rep", district: "3rd Middlesex" }, + senator: { name: "Sam Sen", district: "Middlesex and Suffolk" } +} + +describe("renderLetter", () => { + it("addresses both legislators and copies the reader", () => { + const l = renderLetter(base) + expect(l.salutation).toBe( + "Dear Representative Rae Rep and Senator Sam Sen," + ) + expect(l.from).toBe("Ada Example via MAPLE") + expect(l.to).toBe("Rep. Rae Rep, Sen. Sam Sen") + expect(l.replyTo).toBe("ada@example.org") + expect(l.cc).toBe("ada@example.org") + expect(l.subject).toBe( + "Data privacy conference committee: Pass the Senate version" + ) + expect(l.paragraphs).toEqual(["First thought.", "Second thought."]) + expect(l.signature).toEqual([ + "Ada Example", + "House district: 3rd Middlesex", + "Senate district: Middlesex and Suffolk", + "ada@example.org" + ]) + }) + + it("writes to whichever legislator the reader has", () => { + const l = renderLetter({ ...base, senator: undefined }) + expect(l.salutation).toBe("Dear Representative Rae Rep,") + expect(l.to).toBe("Rep. Rae Rep") + expect(l.signature).not.toContain("Senate district: Middlesex and Suffolk") + }) + + it("quotes what a cosign cosigns, with the reader's own line above it", () => { + const l = renderLetter({ + ...base, + cosign: { author: "Grace", content: "Pass it." } + }) + expect(l.subject).toBe( + "Data privacy conference committee: Cosigned input (Pass the Senate version)" + ) + expect(l.paragraphs).toEqual(["First thought.", "Second thought."]) + expect(l.cosigned).toEqual({ + lead: "I cosigned this input by Grace on MAPLE:", + quote: ["Pass it."] + }) + expect(l.footer).toContain("this cosign is posted publicly") + }) + + it("has no own paragraphs on a cosign without a note", () => { + const l = renderLetter({ + ...base, + content: "", + cosign: { author: "Grace", content: "Pass it." } + }) + expect(l.paragraphs).toEqual([]) + expect(l.cosigned?.quote).toEqual(["Pass it."]) + }) + + it("links the conference page, where the input is posted", () => { + expect(renderLetter(base).footer).toContain( + "publicly: https://mapletestimony.org/conferences/194/H4321-S2100" + ) + }) + + it("marks an edit as an update", () => { + expect(renderLetter({ ...base, edited: true }).subject).toBe( + "Data privacy conference committee: Pass the Senate version (updated)" + ) + expect( + renderLetter({ + ...base, + edited: true, + cosign: { author: "Grace", content: "Pass it." } + }).subject + ).toBe( + "Data privacy conference committee: Cosigned input (Pass the Senate version) (updated)" + ) + }) +}) + +describe("hasLegislators", () => { + it("needs a rep or a senator", () => { + expect(hasLegislators(base)).toBe(true) + expect(hasLegislators({ senator: base.senator })).toBe(true) + expect(hasLegislators({})).toBe(false) + }) +}) diff --git a/functions/src/conferenceLetters/letter.ts b/functions/src/conferenceLetters/letter.ts new file mode 100644 index 000000000..c3a91a624 --- /dev/null +++ b/functions/src/conferenceLetters/letter.ts @@ -0,0 +1,94 @@ +/** A reader's conference input as the letter their rep and senator receive. + * One renderer: the review step previews it and the daily sender mails it, so + * what the reader approves is what is sent. Pure, so the website can import it. */ + +import { CONFERENCE_STANCES } from "../shared/conferences" +import type { ConferencePosition } from "../testimony/types" +export type { ConferencePosition } + +export type LetterMember = { name: string; district: string } + +export type LetterInput = { + /** The reader's name. */ + author: string + /** The reader's own address: where replies go, and who is copied. */ + email: string + court: number + conferenceId: string + /** The conference's name. */ + conference: string + /** Set when the input was posted before and this is its edit. */ + edited?: boolean + position: ConferencePosition + /** The reader's own words, empty on a cosign without a note. */ + content: string + /** Set when the input cosigns another's. */ + cosign?: { author: string; content: string } + representative?: LetterMember + senator?: LetterMember +} + +export type Letter = { + from: string + replyTo: string + cc: string + to: string + subject: string + salutation: string + /** The reader's own words, a paragraph each. */ + paragraphs: string[] + /** The cosigned input, quoted under a line saying so. */ + cosigned?: { lead: string; quote: string[] } + signature: string[] + footer: string +} + +/** A letter has someone to go to once the reader has a rep or a senator. */ +export const hasLegislators = ( + i: Pick +) => !!(i.representative || i.senator) + +const paragraphsOf = (text: string) => + text + .split(/\n\s*\n/) + .map(p => p.trim()) + .filter(Boolean) + +export function renderLetter(i: LetterInput): Letter { + const label = CONFERENCE_STANCES[i.position] + const { representative: rep, senator: sen } = i + const salutation = `Dear ${[ + rep && `Representative ${rep.name}`, + sen && `Senator ${sen.name}` + ] + .filter(Boolean) + .join(" and ")},` + const signature = [ + i.author, + ...(rep ? [`House district: ${rep.district}`] : []), + ...(sen ? [`Senate district: ${sen.district}`] : []), + i.email + ] + const where = i.cosign ? "this cosign" : "this input" + return { + from: `${i.author} via MAPLE`, + replyTo: i.email, + cc: i.email, + to: [rep && `Rep. ${rep.name}`, sen && `Sen. ${sen.name}`] + .filter(Boolean) + .join(", "), + subject: `${i.conference}: ${ + i.cosign ? `Cosigned input (${label})` : label + }${i.edited ? " (updated)" : ""}`, + salutation, + paragraphs: paragraphsOf(i.content), + cosigned: i.cosign && { + lead: `I cosigned this input by ${i.cosign.author} on MAPLE:`, + quote: paragraphsOf(i.cosign.content) + }, + signature, + footer: + `Position: ${label}. Sent by a constituent through MAPLE, where ${where} is posted publicly: ` + + `https://mapletestimony.org/conferences/${i.court}/${i.conferenceId}` + } +} diff --git a/functions/src/conferenceLetters/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts new file mode 100644 index 000000000..2ab3991f6 --- /dev/null +++ b/functions/src/conferenceLetters/letters.test.ts @@ -0,0 +1,131 @@ +import { DateTime } from "luxon" +import { renderLetter } from "./letter" +import { + buildLetterEmail, + dayWindow, + letterFrom, + renderLetterHtml, + renderLetterText +} from "./letters" + +const letter = renderLetter({ + author: 'Ada "Ace" ', + email: "ada@example.org", + court: 194, + conferenceId: "H1-S1", + conference: "Phone-Free Schools", + position: "passSomething", + content: "First.\n\nSecond & third.", + representative: { name: "Pat Rep", district: "3rd Essex" }, + senator: { name: "Sam Sen", district: "1st Suffolk" } +}) + +describe("dayWindow", () => { + it("runs from 9:00 the day before to the 9:00 run", () => { + const { start, end } = dayWindow(new Date("2026-11-11T14:00:00Z")) + expect(start.toISO()).toBe("2026-11-10T09:00:00.000-05:00") + expect(end.toISO()).toBe("2026-11-11T09:00:00.000-05:00") + }) + + it("mails input posted at 00:30 at 9:00 the same morning", () => { + const { start, end } = dayWindow(new Date("2026-11-11T14:00:00Z")) + const posted = DateTime.fromISO("2026-11-11T00:30:00-05:00") + expect(posted >= start && posted < end).toBe(true) + }) + + it("starts at 9:00 and runs 25 hours when the clocks go back", () => { + // Sunday Nov 1, 2026, 9am EST + const { start, end } = dayWindow(new Date("2026-11-01T14:00:00Z")) + expect(start.toISO()).toBe("2026-10-31T09:00:00.000-04:00") + expect(end.diff(start, "hours").hours).toBe(25) + }) + + it("starts at 9:00 and runs 23 hours when the clocks go forward", () => { + // Sunday Mar 14, 2027, 9am EDT + const { start, end } = dayWindow(new Date("2027-03-14T13:00:00Z")) + expect(start.toISO()).toBe("2027-03-13T09:00:00.000-05:00") + expect(end.diff(start, "hours").hours).toBe(23) + }) +}) + +describe("letterFrom", () => { + it("escapes quotes and backslashes and drops line breaks", () => { + expect(letterFrom('A"B\\C\r\nD', "x@y.org")).toBe( + '"A\\"B\\\\C D via MAPLE" ' + ) + }) + + it("names an author without a name", () => { + expect(letterFrom("", "x@y.org")).toBe( + '"A MAPLE reader via MAPLE" ' + ) + }) +}) + +describe("renderLetterHtml", () => { + it("escapes the reader's words", () => { + const html = renderLetterHtml(letter) + expect(html).toContain("Second & third.") + expect(html).not.toContain("") + }) +}) + +describe("renderLetterText", () => { + it("keeps the paragraphs and signature lines apart", () => { + expect(renderLetterText(letter)).toBe( + [ + "Dear Representative Pat Rep and Senator Sam Sen,", + "First.", + "Second & third.", + 'Ada "Ace" \nHouse district: 3rd Essex\nSenate district: 1st Suffolk\nada@example.org', + letter.footer + ].join("\n\n") + ) + }) + + it("quotes the cosigned input", () => { + const cosigned = renderLetter({ + author: "Ada", + email: "ada@example.org", + court: 194, + conferenceId: "H1-S1", + conference: "Phone-Free Schools", + position: "passSomething", + content: "Agreed.", + cosign: { author: "Bo", content: "One.\n\nTwo." }, + representative: { name: "Pat Rep", district: "3rd Essex" } + }) + expect(renderLetterText(cosigned)).toContain( + "> I cosigned this input by Bo on MAPLE:\n>\n> One.\n>\n> Two." + ) + }) +}) + +describe("buildLetterEmail", () => { + const email = buildLetterEmail({ + letter, + author: "Ada", + fromAddress: "notifications@mapletestimony.org" + }) + + it("copies the reader and sends replies to them", () => { + expect(email.cc).toEqual(["ada@example.org"]) + expect(email.replyTo).toBe("ada@example.org") + expect(email.from).toBe( + '"Ada via MAPLE" ' + ) + expect(email.message.subject).toBe( + "Phone-Free Schools: Please pass something" + ) + }) + + it("turns tracking off per message", () => { + expect(JSON.parse(email.headers["X-SMTPAPI"])).toEqual({ + category: ["constituent-letters"], + filters: { + opentrack: { settings: { enable: 0 } }, + clicktrack: { settings: { enable: 0 } } + } + }) + }) +}) diff --git a/functions/src/conferenceLetters/letters.ts b/functions/src/conferenceLetters/letters.ts new file mode 100644 index 000000000..ce26f9f02 --- /dev/null +++ b/functions/src/conferenceLetters/letters.ts @@ -0,0 +1,106 @@ +import { DateTime } from "luxon" +import { timeZone } from "../malegislature" +import type { LegislatorEmail } from "../legislatorMail/types" +import { Letter } from "./letter" + +/** The input a daily run at `now` mails: from the same time the day before, + * Eastern, to the run, so the daily runs follow on from each other and each + * input goes out at the first run after it. `end` is exclusive. */ +export function dayWindow(now: Date) { + const end = DateTime.fromJSDate(now, { zone: timeZone }) + return { start: end.minus({ days: 1 }), end } +} + +/** Why a letter was not sent, kept so a skipped input is accounted for. */ +export type SkipReason = + | "withdrawn" + | "superseded" + | "alreadySent" + | "noVerifiedEmail" + | "noLegislator" + | "noLegislatorEmail" + | "noConference" + | "noRecipients" + +/** A display name made safe for a quoted From header: no line breaks, and + * `"` and `\` escaped. */ +export const quotedName = (name: string) => + name + .replace(/[\r\n]+/g, " ") + .replace(/\\/g, "\\\\") + .replace(/"/g, '\\"') + .trim() + +export const letterFrom = (author: string, address: string) => + `"${quotedName(author) || "A MAPLE reader"} via MAPLE" <${address}>` + +const escapeHtml = (s: string) => + s + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + +/** The letter as HTML, in the order the review step previews it. */ +export const renderLetterHtml = (letter: Letter) => { + const paragraphs = (ps: string[]) => + ps.map(p => `

    ${escapeHtml(p).replace(/\n/g, "
    ")}

    `).join("") + return [ + `

    ${escapeHtml(letter.salutation)}

    `, + paragraphs(letter.paragraphs), + letter.cosigned && + `

    ${escapeHtml(letter.cosigned.lead)}

    ` + + `
    ${paragraphs(letter.cosigned.quote)}
    `, + `

    ${letter.signature.map(escapeHtml).join("
    ")}

    `, + `

    ${escapeHtml(letter.footer)}

    ` + ] + .filter(Boolean) + .join("\n") +} + +/** The same letter as plain text, for clients that show the text part. */ +export const renderLetterText = (letter: Letter) => + [ + letter.salutation, + ...letter.paragraphs, + letter.cosigned && + [letter.cosigned.lead, ...letter.cosigned.quote] + .join("\n\n") + .replace(/^(.*)$/gm, line => (line ? `> ${line}` : ">")), + letter.signature.join("\n"), + letter.footer + ] + .filter(Boolean) + .join("\n\n") + +/** The email for a letter, for the send-email extension. Click and open + * tracking are off per message: they would rewrite the links in a letter to a + * legislator. */ +export const buildLetterEmail = ({ + letter, + author, + fromAddress +}: { + letter: Letter + author: string + fromAddress: string +}): LegislatorEmail => ({ + cc: [letter.cc], + from: letterFrom(author, fromAddress), + replyTo: letter.replyTo, + // The extension reads headers from the document, not from `message`. + headers: { + "X-SMTPAPI": JSON.stringify({ + category: ["constituent-letters"], + filters: { + opentrack: { settings: { enable: 0 } }, + clicktrack: { settings: { enable: 0 } } + } + }) + }, + message: { + subject: letter.subject, + text: renderLetterText(letter), + html: renderLetterHtml(letter) + } +}) diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts new file mode 100644 index 000000000..96255cfa3 --- /dev/null +++ b/functions/src/conferenceLetters/sendLetters.ts @@ -0,0 +1,240 @@ +import { onSchedule } from "firebase-functions/v2/scheduler" +import * as logger from "firebase-functions/logger" +import { createdUnlessExists } from "../common" +import { Conference } from "../conferences/types" +import { auth, db, Timestamp } from "../firebase" +import { timeZone } from "../malegislature" +import { stateOf } from "../conferenceUpdates/inputEvents" +import { ConferenceInputEvent } from "../conferenceUpdates/types" +import { Testimony } from "../testimony/types" +import { queueLegislatorEmail } from "../legislatorMail/queue" +import { lastLetterFor } from "../legislatorMail/records" +import { conferenceName } from "../shared/conferences" +import { hasLegislators, renderLetter } from "./letter" +import { buildLetterEmail, dayWindow, SkipReason } from "./letters" + +const DEFAULT_FROM = "notifications@mapletestimony.org" + +type Content = Record | undefined + +/** Reads shared by many inputs, fetched once per run. */ +const cached = (read: (key: string) => Promise) => { + const seen = new Map>() + return (key: string) => { + if (!seen.has(key)) seen.set(key, read(key)) + return seen.get(key)! + } +} + +const verifiedEmail = async (uid: string) => { + try { + const user = await auth.getUser(uid) + return user.emailVerified && user.email ? user.email : undefined + } catch (e) { + logger.warn(`No auth user ${uid}`, e) + return undefined + } +} + +// As the site names an author (`nameOf`): an empty full name falls back too. +const readerAuthor = (t: Testimony) => t.fullName || t.authorDisplayName + +/** Mails the letter for one input, or says why it is not sent. */ +async function mailLetter( + e: ConferenceInputEvent, + eventId: string, + lastFor: Map, + memberContent: (key: string) => Promise, + conferenceOf: (key: string) => Promise +): Promise { + // An input posted or edited more than once in the window is mailed once, + // with what is posted now. + if (lastFor.get(e.testimonyId) !== eventId) return "superseded" + + const letterId = `letter_${eventId}` + const [last, pubSnap, readerEmail, conference] = await Promise.all([ + lastLetterFor(e.testimonyId), + db.doc(`/users/${e.authorUid}/publishedTestimony/${e.testimonyId}`).get(), + verifiedEmail(e.authorUid), + conferenceOf(`${e.court}/${e.conferenceId}`) + ]) + const pub = pubSnap.data() as Testimony | undefined + const state = pub && stateOf(pub) + if (!pub || !state) return "withdrawn" + // A version goes out once, and a letter is an update only to one that went + // out before. + if (last?.version === pub.version) return "alreadySent" + const updated = !!last + if (!readerEmail) return "noVerifiedEmail" + if (!pub.representativeId && !pub.senatorId) return "noLegislator" + if (!conference) return "noConference" + + // Only those who can receive it are written to, so the salutation names + // exactly who gets the letter. The member document's own name and district + // are used, as in the preview, falling back to the district recorded when + // the input was posted. + const reachable = await Promise.all( + [ + [pub.representativeId, pub.representativeDistrict], + [pub.senatorId, pub.senatorDistrict] + ].map(async ([id, recorded]) => { + const content = id ? await memberContent(`${e.court}/${id}`) : undefined + const email: string | undefined = content?.EmailAddress?.trim() + return id && content?.Name && email + ? { + memberCode: id, + email, + member: { + name: content.Name as string, + district: (content.District ?? recorded ?? "") as string + } + } + : undefined + }) + ) + const [representative, senator] = reachable.map(m => m?.member) + if (!hasLegislators({ representative, senator })) { + return "noLegislatorEmail" + } + + let cosign: { author: string; content: string } | undefined + if (pub.cosignOfId && pub.cosignOfAuthorUid) { + const parent = ( + await db + .doc( + `/users/${pub.cosignOfAuthorUid}/publishedTestimony/${pub.cosignOfId}` + ) + .get() + ).data() as Testimony | undefined + if (!parent) return "withdrawn" + cosign = { author: readerAuthor(parent), content: parent.content } + } + + const author = readerAuthor(pub) + const letter = renderLetter({ + author, + email: readerEmail, + court: e.court, + conferenceId: e.conferenceId, + conference: conferenceName(conference), + edited: updated, + position: state.position, + content: pub.content, + cosign, + representative, + senator + }) + return queueLegislatorEmail( + letterId, + reachable.flatMap(m => m?.email ?? []), + buildLetterEmail({ + letter, + author, + fromAddress: process.env.LETTER_FROM ?? DEFAULT_FROM + }), + { + kind: "letter", + court: e.court, + memberCodes: reachable.flatMap(m => m?.memberCode ?? []), + uid: e.authorUid, + testimonyId: e.testimonyId, + version: pub.version, + conferenceId: e.conferenceId, + updated + } + ) +} + +/** Mails each conference input event recorded since the run a day before `now` to + * the reader's rep and senator, as the letter the reader previewed. A letter is the + * `emails` document /emails/letter_{input event id} and its record at + * /legislatorMail/letter_{input event id}, created, so a second run sends + * nothing again. An input that is not mailed is recorded, with why, at + * /conferenceLetterSkips/{input event id}. A letter that fails fails the run, + * after the others, so the scheduler runs it again. */ +export async function sendConferenceLetters(now: Date) { + const { start, end } = dayWindow(now) + const events = await db + .collection("/conferenceInputEvents") + .where("recordedAt", ">=", Timestamp.fromDate(start.toJSDate())) + .where("recordedAt", "<", Timestamp.fromDate(end.toJSDate())) + .get() + + const written = events.docs + .map(doc => ({ id: doc.id, e: doc.data() as ConferenceInputEvent })) + .filter(({ e }) => e.change !== "withdrawn") + .sort((a, b) => a.e.at.toMillis() - b.e.at.toMillis()) + const lastFor = new Map(written.map(({ id, e }) => [e.testimonyId, id])) + // An event mailed or skipped on an earlier run is decided, and a rerun + // leaves it: a skip is final. + const decided = written.length + ? await db.getAll( + ...written.flatMap(({ id }) => [ + db.doc(`/legislatorMail/letter_${id}`), + db.doc(`/conferenceLetterSkips/${id}`) + ]) + ) + : [] + const done = new Set( + decided.filter(d => d.exists).map(d => d.id.replace(/^letter_/, "")) + ) + const memberContent = cached( + async key => + ( + await db.doc(`/generalCourts/${key.replace("/", "/members/")}`).get() + ).data()?.content as Content + ) + const conferenceOf = cached( + async key => + ( + await db + .doc(`/generalCourts/${key.replace("/", "/conferences/")}`) + .get() + ).data() as Conference | undefined + ) + + let sent = 0 + const failed: string[] = [] + for (const { id, e } of written) { + if (done.has(id)) continue + try { + const outcome = await mailLetter( + e, + id, + lastFor, + memberContent, + conferenceOf + ) + if (outcome === "queued") sent++ + else if (outcome !== "exists") { + await createdUnlessExists( + db.doc(`/conferenceLetterSkips/${id}`).create({ + testimonyId: e.testimonyId, + authorUid: e.authorUid, + conferenceId: e.conferenceId, + reason: outcome, + createdAt: Timestamp.now() + }) + ) + } + } catch (err) { + logger.error(`Letter for input event ${id} failed`, err) + failed.push(id) + } + } + logger.info( + `Mailed ${sent} conference letters from ${start.toISO()} to ${end.toISO()}` + ) + if (failed.length) { + throw new Error(`Letters for input events ${failed.join(", ")} failed`) + } + return sent +} + +export const mailConferenceLetters = onSchedule( + { schedule: "0 9 * * *", timeZone, timeoutSeconds: 300, retryCount: 3 }, + // The time the run was due, not when it started, so each day's window + // begins exactly where the last one ended, and a retry reads the same one. + event => + sendConferenceLetters(new Date(event.scheduleTime)).then(() => undefined) +) diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts new file mode 100644 index 000000000..258f38c6b --- /dev/null +++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts @@ -0,0 +1,162 @@ +import { onSchedule } from "firebase-functions/v2/scheduler" +import * as logger from "firebase-functions/logger" +import { Conference } from "../conferences/types" +import { db, Timestamp } from "../firebase" +import { timeZone } from "../malegislature" +import { splitMemberName } from "../shared/names" +import { takesTestimony } from "../shared/conferences" +import { Testimony } from "../testimony/types" +import { stateOf } from "./inputEvents" +import { queueLegislatorEmail } from "../legislatorMail/queue" +import { ConferenceInputEvent } from "./types" +import { + buildConferenceUpdateEmail, + conferenceUpdateSubject, + renderConferenceUpdate, + renderConferenceUpdateText, + summarize, + tally, + weekWindow +} from "./weeklyUpdate" + +const REPLY_TO = "info@mapletestimony.org" +const DEFAULT_FROM = "notifications@mapletestimony.org" + +/** "Sen. Crighton" or "Rep. Peisch", for a header's To line, and the address + * the update goes to, if the member document has one. */ +const conferee = async (court: number, c: Conference["conferees"][number]) => { + const snap = await db + .doc(`/generalCourts/${court}/members/${c.memberCode}`) + .get() + const name: string | undefined = snap.data()?.content?.Name + const email: string | undefined = snap.data()?.content?.EmailAddress?.trim() + return { + memberCode: c.memberCode, + email: email || undefined, + name: `${c.branch === "Senate" ? "Sen." : "Rep."} ${ + name ? splitMemberName(name).surname : c.memberCode + }`, + chair: c.chair === true + } +} + +/** Compiles the update for each appointed conference that had a + * change recorded in the week up to `now`, and mails it to the conferees as + * /emails/{court}_{conferenceId}_{week}, recorded at /legislatorMail under the + * same id. Both are created, so a second run in the same week sends nothing + * again. An update that fails fails the run, after the others, so the + * scheduler runs it again. */ +export async function compileWeeklyUpdates(now: Date) { + const { week, start, end } = weekWindow(now) + const events = await db + .collection("/conferenceInputEvents") + .where("recordedAt", ">=", Timestamp.fromDate(start.toJSDate())) + .where("recordedAt", "<", Timestamp.fromDate(end.toJSDate())) + .get() + + const eventsOf = new Map() + for (const doc of events.docs) { + const e = doc.data() as ConferenceInputEvent + const key = `${e.court}/${e.conferenceId}` + eventsOf.set(key, [...(eventsOf.get(key) ?? []), e]) + } + + let sent = 0 + const failed: string[] = [] + for (const [key, conferenceEvents] of eventsOf) { + const { court, conferenceId: id } = conferenceEvents[0] + try { + const snap = await db + .doc(`/generalCourts/${court}/conferences/${id}`) + .get() + const conference = snap.data() as Conference | undefined + if (!conference || !takesTestimony(conference)) continue + + const published = await db + .collectionGroup("publishedTestimony") + .where("conferenceId", "==", id) + .select("court", "authorRole", "position", "version") + .get() + const standing = tally( + published.docs + .map(d => d.data() as Testimony) + .filter(t => t.court === court) + .flatMap(t => stateOf(t) ?? []) + ) + + const changes = summarize(conferenceEvents) + const conferees = await Promise.all( + conference.conferees.map(c => conferee(court, c)) + ) + const data = buildConferenceUpdateEmail({ + conference, + conferees, + standing, + changes, + week, + now + }) + const skipped = conferees.filter(c => !c.email) + if (skipped.length) { + logger.warn( + `No address for conferees ${skipped + .map(c => c.memberCode) + .join(", ")} of conference ${key}` + ) + } + + const reachable = conferees.filter(c => c.email) + const outcome = await queueLegislatorEmail( + `${court}_${id}_${week.toISODate()}`, + reachable.map(c => c.email!), + { + from: `"MAPLE" <${process.env.CONFEREE_UPDATE_FROM ?? DEFAULT_FROM}>`, + replyTo: REPLY_TO, + // The extension reads headers from the document, not from `message`. + headers: { + "X-SMTPAPI": JSON.stringify({ category: ["conferee-update"] }) + }, + message: { + subject: conferenceUpdateSubject(data), + text: renderConferenceUpdateText(data), + html: renderConferenceUpdate(data) + } + }, + { + kind: "conferenceUpdate", + court, + memberCodes: reachable.map(c => c.memberCode), + conferenceId: id, + weekStart: Timestamp.fromDate(week.toJSDate()), + standing, + changes + } + ) + if (outcome === "queued") sent++ + if (outcome === "noRecipients") { + logger.warn(`No recipients for the update to conference ${key}`) + } + } catch (e) { + logger.error(`Weekly update for conference ${key} failed`, e) + failed.push(key) + } + } + logger.info(`Mailed ${sent} weekly conference updates`) + if (failed.length) { + throw new Error( + `Weekly updates for conferences ${failed.join(", ")} failed` + ) + } +} + +export const compileConferenceUpdates = onSchedule( + { + schedule: "0 8 * * 1", // Monday 8:00 AM + timeZone, + timeoutSeconds: 300, + retryCount: 3 + }, + // The time the run was due, not when it started, so each week's window + // begins exactly where the last one ended, and a retry reads the same one. + event => compileWeeklyUpdates(new Date(event.scheduleTime)) +) diff --git a/functions/src/conferenceUpdates/index.ts b/functions/src/conferenceUpdates/index.ts new file mode 100644 index 000000000..767075aa3 --- /dev/null +++ b/functions/src/conferenceUpdates/index.ts @@ -0,0 +1,2 @@ +export { compileConferenceUpdates } from "./compileWeeklyUpdates" +export { recordConferenceInput } from "./inputEvents" diff --git a/functions/src/conferenceUpdates/inputEvents.test.ts b/functions/src/conferenceUpdates/inputEvents.test.ts new file mode 100644 index 000000000..21641a79c --- /dev/null +++ b/functions/src/conferenceUpdates/inputEvents.test.ts @@ -0,0 +1,73 @@ +import { conferenceInputEvent } from "./inputEvents" + +const pub = (over: Record = {}) => + ({ + conferenceId: "c1", + court: 194, + authorUid: "u1", + authorRole: "user", + position: "passSenateVersion", + version: 1, + ...over + } as Parameters[0]) + +const state = (position = "passSenateVersion", version = 1) => ({ + position, + group: "individual", + version +}) + +describe("conferenceInputEvent", () => { + it("records conference input being added", () => { + expect(conferenceInputEvent(undefined, pub(), "t1")).toEqual({ + court: 194, + conferenceId: "c1", + testimonyId: "t1", + authorUid: "u1", + change: "added", + after: state() + }) + }) + + it("records an edit with both sides", () => { + expect( + conferenceInputEvent( + pub(), + pub({ version: 2, position: "passNothing" }), + "t1" + ) + ).toMatchObject({ + change: "edited", + before: state(), + after: state("passNothing", 2) + }) + }) + + it("records a deletion as a withdrawal", () => { + expect(conferenceInputEvent(pub(), undefined, "t1")).toMatchObject({ + change: "withdrawn", + before: state() + }) + }) + + it("ignores a write that keeps the version, such as a cosign tally", () => { + expect( + conferenceInputEvent(pub(), pub({ cosignCount: 3 }), "t1") + ).toBeUndefined() + }) + + it("ignores testimony that is not conference input", () => { + const bill = pub({ conferenceId: undefined, position: "endorse" }) + expect(conferenceInputEvent(undefined, bill, "t1")).toBeUndefined() + expect(conferenceInputEvent(bill, undefined, "t1")).toBeUndefined() + }) + + it("groups authors as the site does", () => { + const group = (authorRole: string) => + conferenceInputEvent(undefined, pub({ authorRole }), "t1")?.after?.group + expect(group("organization")).toBe("organization") + expect(group("legislator")).toBe("government") + expect(group("user")).toBe("individual") + expect(group("admin")).toBe("individual") + }) +}) diff --git a/functions/src/conferenceUpdates/inputEvents.ts b/functions/src/conferenceUpdates/inputEvents.ts new file mode 100644 index 000000000..76b3cc97a --- /dev/null +++ b/functions/src/conferenceUpdates/inputEvents.ts @@ -0,0 +1,75 @@ +import { onDocumentWritten } from "firebase-functions/v2/firestore" +import { createdUnlessExists } from "../common" +import { db, FieldValue, Timestamp } from "../firebase" +import { ConferencePosition, Testimony } from "../testimony/types" +import { authorGroupOf, ConferenceInputEvent, InputState } from "./types" + +type Publication = Pick< + Testimony, + "conferenceId" | "court" | "authorUid" | "authorRole" | "position" | "version" +> + +export const stateOf = (t: Publication): InputState | undefined => + ConferencePosition.guard(t.position) + ? { + position: t.position, + group: authorGroupOf(t.authorRole), + version: t.version + } + : undefined + +/** What a write to a publication means for the weekly update, or nothing. + * Only conference input counts, so a write to anything else has no event. A write + * that keeps the version, such as the cosign tally's, is not an edit. */ +export function conferenceInputEvent( + before: Publication | undefined, + after: Publication | undefined, + testimonyId: string +): Omit | undefined { + const current = after ?? before + if (!current?.conferenceId) return undefined + const b = before && stateOf(before) + const a = after && stateOf(after) + if (!b && !a) return undefined + if (b && a && b.version === a.version) return undefined + return { + court: current.court, + conferenceId: current.conferenceId, + testimonyId, + authorUid: current.authorUid, + change: !b ? "added" : !a ? "withdrawn" : "edited", + ...(b && { before: b }), + ...(a && { after: a }) + } +} + +/** Records each change to conference input, for the letters and the weekly + * update to read. The event's id is the trigger event's and the document is + * created, so a retry or a second delivery keeps the first `recordedAt`. */ +export const recordConferenceInput = onDocumentWritten( + { + document: "/users/{userId}/publishedTestimony/{testimonyId}", + timeoutSeconds: 30, + retry: true + }, + async event => { + const change = event.data + if (!change) return + const body = conferenceInputEvent( + change.before.data() as Publication | undefined, + change.after.data() as Publication | undefined, + event.params.testimonyId + ) + if (!body) return + await createdUnlessExists( + db + .collection("/conferenceInputEvents") + .doc(event.id) + .create({ + ...body, + at: Timestamp.fromDate(new Date(event.time)), + recordedAt: FieldValue.serverTimestamp() + }) + ) + } +) diff --git a/functions/src/conferenceUpdates/types.ts b/functions/src/conferenceUpdates/types.ts new file mode 100644 index 000000000..87051914f --- /dev/null +++ b/functions/src/conferenceUpdates/types.ts @@ -0,0 +1,86 @@ +import { + InstanceOf, + Literal as L, + Number, + Optional, + Record, + Static, + String, + Union +} from "runtypes" +import { Role } from "../auth/types" +import { Timestamp } from "../firebase" +import { ConferencePosition } from "../testimony/types" + +/** The site's three kinds of author, as Public Input groups them. Mirrors + * `accountTypeOf` in components/conference/input/accounts.ts. */ +export type AuthorGroup = Static +export const AuthorGroup = Union( + L("individual"), + L("organization"), + L("government") +) +export const authorGroups = AuthorGroup.alternatives.map(a => a.value) + +export const authorGroupOf = (role: Role): AuthorGroup => + role === "organization" + ? "organization" + : role === "legislator" + ? "government" + : "individual" + +/** How many inputs, in all and by author group and position. In a change it + * is a signed difference. */ +export type Counts = Static +export const Counts = Record({ + total: Number, + byGroup: Record({ + individual: Number, + organization: Number, + government: Number + }), + byPosition: Record({ + passSomething: Number, + passSenateVersion: Number, + passHouseVersion: Number, + passNothing: Number + }) +}) + +/** What a publication contributes to the update: the figures it is counted + * under. */ +export type InputState = Static +export const InputState = Record({ + position: ConferencePosition, + group: AuthorGroup, + version: Number +}) + +/** One conference input appearing, changing or going. Written by + * `recordConferenceInput`. `at` is when the input was written, `recordedAt` + * when this event was, which can be later; the letters and the weekly update + * read the events recorded in their window, so a late event falls in the next + * one rather than in none. + * Firestore: /conferenceInputEvents/{eventId} */ +export type ConferenceInputEvent = Static +export const ConferenceInputEvent = Record({ + court: Number, + conferenceId: String, + testimonyId: String, + authorUid: String, + change: Union(L("added"), L("edited"), L("withdrawn")), + before: Optional(InputState), + after: Optional(InputState), + at: InstanceOf(Timestamp), + recordedAt: InstanceOf(Timestamp) +}) + +/** What changed in a week, as the update says it. `net` is the resulting + * difference in the counts. */ +export type Changes = Static +export const Changes = Record({ + added: Number, + edited: Number, + withdrawn: Number, + net: Counts +}) diff --git a/functions/src/conferenceUpdates/weeklyUpdate.test.ts b/functions/src/conferenceUpdates/weeklyUpdate.test.ts new file mode 100644 index 000000000..ea6d1f284 --- /dev/null +++ b/functions/src/conferenceUpdates/weeklyUpdate.test.ts @@ -0,0 +1,192 @@ +import { + buildConferenceUpdateEmail, + conferenceUpdateSubject, + renderConferenceUpdate, + renderConferenceUpdateText, + summarize, + tally, + weekWindow +} from "./weeklyUpdate" +import { InputState } from "./types" + +const state = (over: Partial = {}): InputState => ({ + position: "passSenateVersion", + group: "individual", + version: 1, + ...over +}) + +describe("weekWindow", () => { + it("runs from a week before the run to the run", () => { + // Monday Sept 28, 2026, 8am Eastern + const { week, start, end } = weekWindow(new Date("2026-09-28T12:00:00Z")) + expect(week.toISO()).toBe("2026-09-21T00:00:00.000-04:00") + expect(start.toISO()).toBe("2026-09-21T08:00:00.000-04:00") + expect(end.toISO()).toBe("2026-09-28T08:00:00.000-04:00") + }) + + it("names the week before through the next Sunday", () => { + const { week } = weekWindow(new Date("2026-10-04T23:00:00Z")) + expect(week.toISODate()).toBe("2026-09-21") + }) + + it("keeps Monday midnight across the autumn clock change", () => { + const { week, start } = weekWindow(new Date("2026-11-02T13:00:00Z")) + expect(week.toISO()).toBe("2026-10-26T00:00:00.000-04:00") + expect(start.toISO()).toBe("2026-10-26T08:00:00.000-04:00") + }) +}) + +describe("tally", () => { + it("counts input by group and position", () => { + const counts = tally([ + state(), + state({ group: "organization", position: "passNothing" }) + ]) + expect(counts).toEqual({ + total: 2, + byGroup: { individual: 1, organization: 1, government: 0 }, + byPosition: { + passSomething: 0, + passSenateVersion: 1, + passHouseVersion: 0, + passNothing: 1 + } + }) + }) +}) + +describe("summarize", () => { + it("adds new input", () => { + const c = summarize([ + { after: state() }, + { after: state({ group: "government" }) } + ]) + expect(c).toMatchObject({ added: 2, edited: 0, withdrawn: 0 }) + expect(c.net.total).toBe(2) + expect(c.net.byGroup).toEqual({ + individual: 1, + organization: 0, + government: 1 + }) + }) + + it("moves an edit between positions without changing the total", () => { + const c = summarize([ + { + before: state(), + after: state({ position: "passNothing", version: 2 }) + } + ]) + expect(c).toMatchObject({ added: 0, edited: 1 }) + expect(c.net.total).toBe(0) + expect(c.net.byPosition).toMatchObject({ + passSenateVersion: -1, + passNothing: 1 + }) + }) + + it("lowers the counts for a withdrawal", () => { + const c = summarize([{ before: state({ group: "organization" }) }]) + expect(c).toMatchObject({ withdrawn: 1, edited: 0, added: 0 }) + expect(c.net.total).toBe(-1) + expect(c.net.byGroup).toMatchObject({ organization: -1 }) + }) + + it("is empty for no events", () => { + expect(summarize([])).toMatchObject({ + added: 0, + edited: 0, + withdrawn: 0, + net: { total: 0 } + }) + }) +}) + +describe("the update email", () => { + const conference = { + court: 194, + id: "phone-free-schools", + title: "An Act relative to phones in schools", + name: "Phone-Free Schools", + senateBillId: "S2581", + houseBillId: "H5366" + } + const standing = tally([ + state(), + state(), + state({ position: "passNothing", group: "organization" }) + ]) + const changes = summarize([ + { after: state() }, + { after: state({ position: "passNothing", group: "organization" }) } + ]) + const data = buildConferenceUpdateEmail({ + conference, + conferees: [ + { name: "Sen. Sam Sen", chair: true }, + { name: "Rep. Pat Rep", chair: false } + ], + standing, + changes, + week: weekWindow(new Date("2026-09-28T12:00:00Z")).week, + now: new Date("2026-09-28T12:00:00Z") + }) + + it("names the conference and the week in its subject", () => { + expect(conferenceUpdateSubject(data)).toBe( + `Public input on Phone-Free Schools: ${data.weekLabel}` + ) + }) + + it("states the week, the bills and the figures", () => { + expect(data).toMatchObject({ + bills: "S.2581 / H.5366", + weekLabel: "September 21–27, 2026", + sentLabel: "Monday, September 28, 2026", + total: 3, + totalDelta: "▲ 2" + }) + expect(data.groups.map(g => [g.label, g.count, g.delta])).toEqual([ + ["Individuals", 2, "▲ 1"], + ["Organizations", 1, "▲ 1"], + ["Government offices", 0, "—"] + ]) + expect(data.changes).toContain("2 inputs added.") + expect(data.changes).toContain("Counted input went from 1 to 3.") + expect(data.changes).toContain("Don't pass anything: 0 → 1.") + }) + + it("renders the conferees, figures and a link to the page", () => { + const html = renderConferenceUpdate(data) + expect(html).toContain("Sen. Sam Sen (chair), Rep. Pat Rep") + expect(html).toContain("S.2581 / H.5366 — Phone-Free Schools") + expect(html).toContain( + 'href="https://mapletestimony.org/conferences/194/phone-free-schools"' + ) + expect(html).toContain("Pass the Senate version") + expect(html).toContain("info@mapletestimony.org") + }) + + it("leaves out unsubscribing and the open questions", () => { + const html = renderConferenceUpdate(data).toLowerCase() + expect(html).not.toContain("unsubscribe") + expect(html).not.toContain("open questions") + }) + + it("says the same in its text part, a line per figure", () => { + const text = renderConferenceUpdateText(data) + expect(text).toContain("To: Sen. Sam Sen (chair), Rep. Pat Rep\n") + expect(text).toContain( + "Total testimonies: 3 (▲ 2 this week)\nIndividuals: 2 (▲ 1)\n" + ) + expect(text).toContain("- 2 inputs added.\n") + expect(text).toContain( + "https://mapletestimony.org/conferences/194/phone-free-schools" + ) + }) + + it("links the page without saying how many it holds", () => { + expect(renderConferenceUpdate(data)).not.toMatch(/Read all \d+/) + }) +}) diff --git a/functions/src/conferenceUpdates/weeklyUpdate.ts b/functions/src/conferenceUpdates/weeklyUpdate.ts new file mode 100644 index 000000000..0f0fef0b4 --- /dev/null +++ b/functions/src/conferenceUpdates/weeklyUpdate.ts @@ -0,0 +1,218 @@ +import * as fs from "fs" +import * as handlebars from "handlebars" +import { DateTime } from "luxon" +import * as path from "path" +import { Conference } from "../conferences/types" +import { timeZone } from "../malegislature" +import { ConferenceUpdateEmail } from "../notifications/emailTypes" +import { CONFERENCE_STANCES, conferenceName } from "../shared/conferences" +import { ConferencePosition } from "../testimony/types" +import { + AuthorGroup, + authorGroups, + Changes, + ConferenceInputEvent, + Counts, + InputState +} from "./types" + +const EMAIL_TEMPLATE_PATH = "../email/conferenceUpdateEmail.handlebars" + +/** The order the mockup lists them in. */ +const POSITION_ORDER: ConferencePosition[] = [ + "passSenateVersion", + "passHouseVersion", + "passSomething", + "passNothing" +] +const POSITION_COLORS: Record = { + passSenateVersion: "#1A3185", + passHouseVersion: "#5B6FB8", + passSomething: "#1F7A4D", + passNothing: "#C71E32" +} +const GROUPS: Record = { + individual: "Individuals", + organization: "Organizations", + government: "Government offices" +} + +/** The week an update run at `now` reports. `week` is the Monday, Eastern, + * of the Monday-to-Sunday week it is named for. Its changes run from `start`, a + * week before the run, to `end`, the run, so they end where the standing is + * counted; the weekly runs follow on from each other. `end` is exclusive. */ +export function weekWindow(now: Date) { + const end = DateTime.fromJSDate(now, { zone: timeZone }) + const week = end.startOf("week").minus({ weeks: 1 }) + return { week, start: end.minus({ weeks: 1 }), end } +} + +const emptyCounts = (): Counts => ({ + total: 0, + byGroup: { individual: 0, organization: 0, government: 0 }, + byPosition: { + passSomething: 0, + passSenateVersion: 0, + passHouseVersion: 0, + passNothing: 0 + } +}) + +const add = (counts: Counts, state: InputState | undefined, sign: 1 | -1) => { + if (!state) return + counts.total += sign + counts.byGroup[state.group] += sign + counts.byPosition[state.position] += sign +} + +/** The counts for a conference's input. */ +export function tally(states: InputState[]): Counts { + const counts = emptyCounts() + states.forEach(s => add(counts, s, 1)) + return counts +} + +/** What a week's events add up to. Each event is input appearing + * (`added`), edited, or deleted (`withdrawn`). */ +export function summarize( + events: Pick[] +): Changes { + const changes: Changes = { + added: 0, + edited: 0, + withdrawn: 0, + net: emptyCounts() + } + for (const { before, after } of events) { + add(changes.net, before, -1) + add(changes.net, after, 1) + if (!before) changes.added++ + else if (after) changes.edited++ + else changes.withdrawn++ + } + return changes +} + +const delta = (n: number) => (n > 0 ? `▲ ${n}` : n < 0 ? `▼ ${-n}` : "—") +const plural = (n: number, one: string, many = `${one}s`) => + `${n} ${n === 1 ? one : many}` + +const billLabel = (id: string) => id.replace(/^([A-Za-z]+)(\d+)$/, "$1.$2") + +const formatWeek = (start: DateTime) => { + const last = start.plus({ days: 6 }) + return start.month === last.month + ? `${start.toFormat("LLLL d")}–${last.toFormat("d, yyyy")}` + : `${start.toFormat("LLLL d")} – ${last.toFormat("LLLL d, yyyy")}` +} + +/** What the template shows. `before` is the standing at the start of the + * changes, which is the standing now less their net change. */ +export function buildConferenceUpdateEmail({ + conference, + conferees, + standing, + changes, + week, + now +}: { + conference: Pick< + Conference, + "court" | "id" | "title" | "name" | "senateBillId" | "houseBillId" + > + conferees: { name: string; chair: boolean }[] + standing: Counts + changes: Changes + week: DateTime + now: Date +}): ConferenceUpdateEmail { + const { net } = changes + const total = standing.total + const bar = POSITION_ORDER.filter(p => standing.byPosition[p] > 0) + + const lines: string[] = [] + if (changes.added) lines.push(`${plural(changes.added, "input")} added.`) + if (changes.edited) lines.push(`${plural(changes.edited, "input")} edited.`) + if (changes.withdrawn) + lines.push(`${plural(changes.withdrawn, "input")} withdrawn.`) + if (net.total) + lines.push(`Counted input went from ${total - net.total} to ${total}.`) + for (const p of POSITION_ORDER) + if (net.byPosition[p]) + lines.push( + `${CONFERENCE_STANCES[p]}: ${ + standing.byPosition[p] - net.byPosition[p] + } → ${standing.byPosition[p]}.` + ) + + return { + bills: `${billLabel(conference.senateBillId)} / ${billLabel( + conference.houseBillId + )}`, + title: conferenceName(conference), + conferenceUrl: `https://mapletestimony.org/conferences/${conference.court}/${conference.id}`, + weekLabel: formatWeek(week), + sentLabel: DateTime.fromJSDate(now, { zone: timeZone }).toFormat( + "cccc, LLLL d, yyyy" + ), + conferees, + total, + totalDelta: delta(net.total), + groups: authorGroups.map(g => ({ + label: GROUPS[g], + count: standing.byGroup[g], + delta: delta(net.byGroup[g]) + })), + positions: bar.map(p => ({ + label: CONFERENCE_STANCES[p], + count: standing.byPosition[p], + delta: delta(net.byPosition[p]), + width: Math.max(1, Math.round((standing.byPosition[p] / total) * 100)), + color: POSITION_COLORS[p] + })), + changes: lines + } +} + +export const conferenceUpdateSubject = ({ + title, + weekLabel +}: Pick) => + `Public input on ${title}: ${weekLabel}` + +let template: HandlebarsTemplateDelegate | undefined + +export const renderConferenceUpdate = (data: ConferenceUpdateEmail) => + (template ??= handlebars.compile( + fs.readFileSync(path.join(__dirname, EMAIL_TEMPLATE_PATH), "utf8") + ))(data) + +/** The same update as plain text, section by section as the template. */ +export const renderConferenceUpdateText = (d: ConferenceUpdateEmail) => + [ + [ + `To: ${d.conferees + .map(c => (c.chair ? `${c.name} (chair)` : c.name)) + .join(", ")}`, + `Re: ${d.bills} — ${d.title} — conference committee`, + `Week of: ${d.weekLabel} · Sent: ${d.sentLabel}` + ].join("\n"), + `Public input on ${d.title}: where it stands, and what changed this week`, + "One summary a week from MAPLE, in place of individual emails. Everything here is drawn from testimony submitted publicly on MAPLE whose authors chose to share it with the committee.", + [ + "1 · Where input stands", + `Total testimonies: ${d.total} (${d.totalDelta} this week)`, + ...d.groups.map(g => `${g.label}: ${g.count} (${g.delta})`), + "", + "Position on the bills", + ...d.positions.map(p => `${p.label}: ${p.count} (${p.delta})`) + ].join("\n"), + ["2 · What changed this week", ...d.changes.map(c => `- ${c}`)].join("\n"), + [ + "3 · Read the testimony", + "Every submission is public on MAPLE with the author's stated position. Nothing in this summary is paraphrased from private input.", + d.conferenceUrl + ].join("\n"), + "How this is compiled. Counts and positions are from testimony submitted on MAPLE. Positions are the authors' own, not MAPLE's view.", + "MAPLE is nonpartisan infrastructure and takes no position on this or any bill. Questions, or a reply to this email: info@mapletestimony.org." + ].join("\n\n") diff --git a/functions/src/email/conferenceUpdateEmail.handlebars b/functions/src/email/conferenceUpdateEmail.handlebars new file mode 100644 index 000000000..ce9580c62 --- /dev/null +++ b/functions/src/email/conferenceUpdateEmail.handlebars @@ -0,0 +1,77 @@ + + + + Weekly update to the conferees + + + + + + + + + + + + + + + + +
    + MAPLE + Weekly update to the conferees +
    +
    To: {{#each conferees}}{{name}}{{#if chair}} (chair){{/if}}{{#unless @last}}, {{/unless}}{{/each}}
    +
    Re: {{bills}} — {{title}} — conference committee
    +
    Week of: {{weekLabel}}  ·  Sent: {{sentLabel}}
    +
    +

    Public input on {{title}}: where it stands, and what changed this week

    +

    One summary a week from MAPLE, in place of individual emails. Everything here is drawn from testimony submitted publicly on MAPLE whose authors chose to share it with the committee.

    + +

    1 · Where input stands

    + + + + {{#each groups}} + + {{/each}} + +
    +
    Total testimonies
    +
    {{total}}
    +
    {{totalDelta}} this week
    +
    +
    {{label}}
    +
    {{count}}
    +
    {{delta}}
    +
    + +

    Position on the bills

    + + + {{#each positions}} + + {{/each}} + +
    {{count}}
    +

    + {{#each positions}}■ {{label}} {{count}} ({{delta}}){{#unless @last}}  ·  {{/unless}}{{/each}} +

    + +

    2 · What changed this week

    +
      + {{#each changes}} +
    • {{this}}
    • + {{/each}} +
    + +

    3 · Read the testimony

    +

    Every submission is public on MAPLE with the author's stated position. Nothing in this summary is paraphrased from private input.

    + Read the testimony → +
    +

    How this is compiled. Counts and positions are from testimony submitted on MAPLE. Positions are the authors' own, not MAPLE's view.

    +

    MAPLE is nonpartisan infrastructure and takes no position on this or any bill. Questions, or a reply to this email: info@mapletestimony.org.

    +
    + + diff --git a/functions/src/email/recipients.test.ts b/functions/src/email/recipients.test.ts new file mode 100644 index 000000000..711a0518a --- /dev/null +++ b/functions/src/email/recipients.test.ts @@ -0,0 +1,37 @@ +import { resolveRecipients } from "./recipients" + +describe("resolveRecipients", () => { + const env = { ...process.env } + afterEach(() => { + process.env = { ...env } + }) + + it("sends to the real addresses in production", () => { + process.env.GCLOUD_PROJECT = "digital-testimony-prod" + expect(resolveRecipients(["a@x.org", "b@x.org"])).toEqual({ + to: ["a@x.org", "b@x.org"], + subjectPrefix: "" + }) + }) + + it("swaps in the test address elsewhere and names who it would have reached", () => { + process.env.GCLOUD_PROJECT = "digital-testimony-dev" + process.env.LETTER_TEST_RECIPIENT = "test@x.org" + expect(resolveRecipients(["a@x.org", "b@x.org"])).toEqual({ + to: ["test@x.org"], + subjectPrefix: "[dev] would have gone to: a@x.org, b@x.org — " + }) + }) + + it("sends nothing elsewhere when no test address is set", () => { + process.env.GCLOUD_PROJECT = "demo-dtp" + delete process.env.LETTER_TEST_RECIPIENT + expect(resolveRecipients(["a@x.org"]).to).toEqual([]) + }) + + it("sends nothing elsewhere when there is no one to stand in for", () => { + process.env.GCLOUD_PROJECT = "digital-testimony-dev" + process.env.LETTER_TEST_RECIPIENT = "test@x.org" + expect(resolveRecipients([]).to).toEqual([]) + }) +}) diff --git a/functions/src/email/recipients.ts b/functions/src/email/recipients.ts new file mode 100644 index 000000000..8617ebb8c --- /dev/null +++ b/functions/src/email/recipients.ts @@ -0,0 +1,20 @@ +const PRODUCTION_PROJECT = "digital-testimony-prod" + +const isProduction = () => process.env.GCLOUD_PROJECT === PRODUCTION_PROJECT + +/** Who an email to real people actually goes to. Outside production no email + * reaches them: the To line becomes the one configured test address + * (`LETTER_TEST_RECIPIENT`), and the subject says who it would have gone to, + * so a test also checks routing. With no test address configured, or no one + * to stand in for, nothing is sent. */ +export const resolveRecipients = ( + real: string[] +): { to: string[]; subjectPrefix: string } => { + if (!real.length) return { to: [], subjectPrefix: "" } + if (isProduction()) return { to: real, subjectPrefix: "" } + const test = process.env.LETTER_TEST_RECIPIENT + return { + to: test ? [test] : [], + subjectPrefix: `[dev] would have gone to: ${real.join(", ")} — ` + } +} diff --git a/functions/src/index.ts b/functions/src/index.ts index 9d41674df..e04dda3cf 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -63,6 +63,11 @@ export { unfollowUserV2, getFollowersV2 } from "./subscriptions" +export { + compileConferenceUpdates, + recordConferenceInput +} from "./conferenceUpdates" +export { mailConferenceLetters } from "./conferenceLetters" export { scrapeElections } from "./legislators" export { transcriptionV2 } from "./webhooks" diff --git a/functions/src/legislatorMail/queue.ts b/functions/src/legislatorMail/queue.ts new file mode 100644 index 000000000..6a0ef6f81 --- /dev/null +++ b/functions/src/legislatorMail/queue.ts @@ -0,0 +1,35 @@ +import { createdUnlessExists } from "../common" +import { resolveRecipients } from "../email/recipients" +import { db, Timestamp } from "../firebase" +import { LegislatorEmail, LegislatorMail } from "./types" + +/** What a record takes from its email. */ +type FromEmail = "subject" | "html" | "text" | "createdAt" +type RecordOf = T extends unknown ? Omit : never + +/** Queues `email` to the legislators at `addresses` for the send-email + * extension at /emails/{id}, and records it as sent, unprefixed, at + * /legislatorMail/{id}. One batch, so neither exists without the other; both + * are created, so a second call with the same id writes nothing. */ +export async function queueLegislatorEmail( + id: string, + addresses: string[], + email: LegislatorEmail, + record: RecordOf +): Promise<"queued" | "exists" | "noRecipients"> { + const { to, subjectPrefix } = resolveRecipients(addresses) + if (!to.length) return "noRecipients" + + const createdAt = Timestamp.now() + const { subject, text, html } = email.message + const sent: LegislatorMail = { ...record, subject, html, text, createdAt } + const batch = db.batch() + batch.create(db.doc(`/emails/${id}`), { + ...email, + to, + message: { ...email.message, subject: subjectPrefix + subject }, + createdAt + }) + batch.create(db.doc(`/legislatorMail/${id}`), sent) + return (await createdUnlessExists(batch.commit())) ? "queued" : "exists" +} diff --git a/functions/src/legislatorMail/records.ts b/functions/src/legislatorMail/records.ts new file mode 100644 index 000000000..87d00567f --- /dev/null +++ b/functions/src/legislatorMail/records.ts @@ -0,0 +1,14 @@ +import { db } from "../firebase" +import { LetterRecord } from "./types" + +/** The last letter on record for a reader's input, if one went out. Only + * letters carry a `testimonyId`. */ +export async function lastLetterFor(testimonyId: string) { + const snap = await db + .collection("/legislatorMail") + .where("testimonyId", "==", testimonyId) + .orderBy("createdAt", "desc") + .limit(1) + .get() + return snap.docs[0]?.data() as LetterRecord | undefined +} diff --git a/functions/src/legislatorMail/types.ts b/functions/src/legislatorMail/types.ts new file mode 100644 index 000000000..e9488716a --- /dev/null +++ b/functions/src/legislatorMail/types.ts @@ -0,0 +1,70 @@ +import { + Array, + Boolean, + InstanceOf, + Literal as L, + Number, + Record, + Static, + String, + Union +} from "runtypes" +import { Timestamp } from "../firebase" +import { Changes, Counts } from "../conferenceUpdates/types" + +/** What every email to legislators records. `memberCodes` are the + * legislators it went to; outside production, those it would have gone to + * instead of the test address. `subject` is without the test prefix. */ +const Sent = { + court: Number, + memberCodes: Array(String), + subject: String, + html: String, + text: String, + /** When the email was queued: the `emails` document's `createdAt`. */ + createdAt: InstanceOf(Timestamp) +} + +/** A reader's conference input, mailed as a letter to their rep and senator. */ +export type LetterRecord = Static +export const LetterRecord = Record({ + kind: L("letter"), + ...Sent, + uid: String, + testimonyId: String, + /** The version of the published testimony the letter was written from. */ + version: Number, + conferenceId: String, + /** Sent as an update to an earlier letter, "(updated)" in its subject. */ + updated: Boolean +}) + +/** One conference's weekly update to its conferees. */ +export type ConferenceUpdateRecord = Static +export const ConferenceUpdateRecord = Record({ + kind: L("conferenceUpdate"), + ...Sent, + conferenceId: String, + /** Monday 00:00, Eastern, of the week the update is named for. */ + weekStart: InstanceOf(Timestamp), + /** The input on the conference when the update was compiled, where its + * changes end. */ + standing: Counts, + changes: Changes +}) + +/** An email MAPLE sent to legislators, as sent. Written with the `emails` + * document that sends it, under the same id. + * Firestore: /legislatorMail/{emails document id} */ +export type LegislatorMail = Static +export const LegislatorMail = Union(LetterRecord, ConferenceUpdateRecord) + +/** An email to legislators as the send-email extension takes it, before its + * recipients are resolved: the subject is the one they see. */ +export type LegislatorEmail = { + from: string + replyTo: string + cc?: string[] + headers: { [name: string]: string } + message: { subject: string; text: string; html: string } +} diff --git a/functions/src/notifications/deliverNotifications.test.ts b/functions/src/notifications/deliverNotifications.test.ts index 1a8d7ef83..68c5cb988 100644 --- a/functions/src/notifications/deliverNotifications.test.ts +++ b/functions/src/notifications/deliverNotifications.test.ts @@ -1,5 +1,5 @@ import { buildDigestData } from "./deliverNotifications" -import { CONFERENCE_STANCES } from "./emailTypes" +import { CONFERENCE_STANCES } from "../shared/conferences" import { Timestamp } from "../firebase" // Mock firebase module diff --git a/functions/src/notifications/deliverNotifications.ts b/functions/src/notifications/deliverNotifications.ts index 239d731ac..9a5e26467 100644 --- a/functions/src/notifications/deliverNotifications.ts +++ b/functions/src/notifications/deliverNotifications.ts @@ -13,7 +13,6 @@ import { BallotQuestionDigest, BillDigest, BillResult, - CONFERENCE_STANCES, ConferenceDigest, NotificationEmailDigest, Position, @@ -22,6 +21,7 @@ import { import { prepareHandlebars } from "../email/handlebarsHelpers" import { Frequency } from "../auth/types" import { ConferencePosition } from "../testimony/types" +import { CONFERENCE_STANCES } from "../shared/conferences" const PROFILE_BATCH_SIZE = 50 const NUM_BILLS_TO_DISPLAY = 4 diff --git a/functions/src/notifications/emailTypes.ts b/functions/src/notifications/emailTypes.ts index 1d0622232..7a3d3837d 100644 --- a/functions/src/notifications/emailTypes.ts +++ b/functions/src/notifications/emailTypes.ts @@ -1,13 +1,4 @@ import { Frequency } from "../auth/types" -import type { ConferencePosition } from "../testimony/types" - -// The site's wording, conferences input.position.*.long; a test holds them equal. -export const CONFERENCE_STANCES: Record = { - passSomething: "Please pass something", - passSenateVersion: "Pass the Senate version", - passHouseVersion: "Pass the House version", - passNothing: "Don't pass anything" -} export type BillDigest = { billId: string @@ -62,3 +53,26 @@ export type NotificationEmailDigest = { conferences: ConferenceDigest[] // cap of 4 numConferencesWithNewTestimony: number } + +type ConferenceUpdateRow = { + label: string + count: number + /** "▲ 3", "▼ 1" or "—". */ + delta: string +} +export type ConferenceUpdateEmail = { + /** "S.2581 / H.5366" */ + bills: string + title: string + conferenceUrl: string + /** "September 21–27, 2026" */ + weekLabel: string + sentLabel: string + conferees: { name: string; chair: boolean }[] + total: number + totalDelta: string + groups: ConferenceUpdateRow[] + /** Each with its share of the bar, in whole percent, and its colour. */ + positions: (ConferenceUpdateRow & { width: number; color: string })[] + changes: string[] +} diff --git a/functions/src/shared/conferences.ts b/functions/src/shared/conferences.ts index 97954b8d1..8428bbca1 100644 --- a/functions/src/shared/conferences.ts +++ b/functions/src/shared/conferences.ts @@ -1,4 +1,5 @@ import type { Conference } from "../conferences/types" +import type { ConferencePosition } from "../testimony/types" /** Once the report is filed, the committee has decided: each question says * how the law resolved it. */ @@ -21,3 +22,10 @@ type Named = { name?: string | null; title?: string | null } /** The short name where there is one, else the act title. */ export const conferenceName = (c: Named) => c.name?.trim() || c.title?.trim() || "" +// The site's wording, conferences input.position.*.long; a test holds them equal. +export const CONFERENCE_STANCES: Record = { + passSomething: "Please pass something", + passSenateVersion: "Pass the Senate version", + passHouseVersion: "Pass the House version", + passNothing: "Don't pass anything" +} diff --git a/public/locales/en/conferences.json b/public/locales/en/conferences.json index af4cd1111..9d27f49e7 100644 --- a/public/locales/en/conferences.json +++ b/public/locales/en/conferences.json @@ -211,7 +211,6 @@ "review": "Review and Post", "review_title": "Review and Post", "preview_label": "Preview", - "preview_on": "on {{subject}}", "rules_label": "Before You Post", "rule_public": "Posting is public and stays attached to your account.", "rule_revise": "You can revise it later; earlier versions stay on the record.", @@ -280,7 +279,6 @@ "cosign_optional": "Optional", "cosign_prompt": "Why does this matter to you? One or two sentences in your own words will have more impact with lawmakers.", "cosign_no_words_label": "I do not want to add input of my own", - "cosign_save": "Save Cosign", "cosign_edit_title": "Edit Your Cosign", "cosign_parent_gone": "The input you are cosigning has been removed, so your cosign could not be filed.", "cosign_closed": "This conference committee is no longer taking input, so your cosign could not be filed.", @@ -291,5 +289,34 @@ "follow": "Follow", "following": "Following", "unfollow": "Unfollow" + }, + "review": { + "goes_input": "Where your input goes", + "goes_cosign": "Where your cosign goes", + "public_input": "Posted publicly on MAPLE with your name.", + "public_input_aside": "Others can cosign your input. When they do, it’s sent to their legislators with your name attached.", + "public_cosign": "Posted publicly on MAPLE as your cosign of {{name}}’s input. It counts as your position on this bill.", + "emailed": "Emailed to your state representative and state senator:", + "emailed_cosign": "The email says clearly that you cosigned {{name}}’s input.", + "emailed_none": "MAPLE doesn’t know who your state representative and state senator are, so it can’t email them. Your input is only counted for the committee.", + "rep": "Rep. {{name}}", + "sen": "Sen. {{name}}", + "and": " and ", + "weekly_input": "Included in MAPLE’s weekly update to the conferees.", + "weekly_cosign": "Counted in MAPLE’s weekly update to the conferees.", + "conferees": "This committee’s conferees", + "conferees_label": "The conferees", + "email_label": "Your email", + "email_note": "Your email is included in the message so your legislators can reply to you directly. Replies go to you, not to MAPLE.", + "preview_label": "Preview of the email", + "from": "From", + "reply_to": "Reply-To", + "cc": "Cc", + "to": "To", + "subject": "Subject", + "post_and_send": "Post and Send", + "cosign_and_send": "Cosign and Send", + "no_line": "You didn’t add a line of your own. That’s fine: your cosign still counts. A sentence about why it matters to you tends to carry more weight with legislators.", + "add_line": "Add a line" } } diff --git a/scripts/README.md b/scripts/README.md index 21bf92b16..543b5f59d 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -273,6 +273,17 @@ Updates the conferences collection to match `conferences/`, deleting any documen +#### `runConferenceMail` + +Local only. Runs the conference letters or weekly updates job once, since the emulators never fire schedules. The emails go to `LETTER_TEST_RECIPIENT` (set it in `functions/.env.local`), and the send-email extension delivers them once `extensions/firestore-send-email.secret.local` holds `SMTP_PASSWORD`. `yarn emulators:start` leaves the extension out, so start the emulators with it, and with a quota project for the extension download: + +```sh +GOOGLE_CLOUD_QUOTA_PROJECT=digital-testimony-dev npx firebase --project demo-dtp emulators:start \ + --only auth,functions,pubsub,firestore,storage,extensions --import tests/integration/exportedTestData +yarn firebase-admin run-script runConferenceMail --env local --job letters +yarn firebase-admin run-script runConferenceMail --env local --job updates --now 2026-11-16T08:00 +``` + #### `runScrapers` diff --git a/scripts/firebase-admin/runConferenceMail.ts b/scripts/firebase-admin/runConferenceMail.ts new file mode 100644 index 000000000..45bb5f877 --- /dev/null +++ b/scripts/firebase-admin/runConferenceMail.ts @@ -0,0 +1,78 @@ +import * as fs from "fs" +import { DateTime } from "luxon" +import { z } from "zod" +import { sendConferenceLetters } from "../../functions/src/conferenceLetters/sendLetters" +import { compileWeeklyUpdates } from "../../functions/src/conferenceUpdates/compileWeeklyUpdates" +import { timeZone } from "../../functions/src/malegislature" +import { ArgsError } from "./args" +import { Script } from "./types" + +/** + * Runs a conference mail job once against the local emulators, as its schedule + * would: `letters` (mailConferenceLetters, daily at 9:00) or `updates` + * (compileConferenceUpdates, Mondays at 8:00). The emulators never fire + * schedules. With no --now, letters run as of now, covering the last day's + * input, and updates as of next Monday, covering this week's. The emails land + * in /emails for the send-email extension, addressed to LETTER_TEST_RECIPIENT, + * which is read from the functions env files as the functions emulator reads + * it. Local only. + * + * yarn firebase-admin run-script runConferenceMail --env local --job letters + * yarn firebase-admin run-script runConferenceMail --env local --job updates --now 2026-11-16T08:00 + */ +const Args = z.object({ + job: z.enum(["letters", "updates"]), + now: z.string().optional() +}) + +const ENV_FILES = [ + "functions/.env", + "functions/.env.demo-dtp", + "functions/.env.local" +] +const ENV_KEYS = [ + "LETTER_TEST_RECIPIENT", + "LETTER_FROM", + "CONFEREE_UPDATE_FROM" +] + +/** The jobs read these in this process, not the emulator's, so load them the + * same way: later files win, and the shell wins over all. */ +const loadFunctionsEnv = () => { + const values: Record = {} + for (const file of ENV_FILES.filter(f => fs.existsSync(f))) + for (const line of fs.readFileSync(file, "utf8").split("\n")) { + const m = line.match(/^\s*([A-Z_]+)\s*=\s*(.*?)\s*$/) + if (m && ENV_KEYS.includes(m[1])) values[m[1]] = m[2] + } + for (const [k, v] of Object.entries(values)) process.env[k] ??= v +} + +export const script: Script = async ({ env, args }) => { + if (env !== "local") throw Error("only local supported") + const parsed = Args.safeParse(args) + if (!parsed.success) throw new ArgsError(parsed.error.issues) + const { job, now: nowArg } = parsed.data + + const now = nowArg + ? DateTime.fromISO(nowArg, { zone: timeZone }) + : job === "letters" + ? DateTime.now().setZone(timeZone) + : DateTime.now() + .setZone(timeZone) + .startOf("week") + .plus({ weeks: 1 }) + .set({ hour: 8 }) + if (!now.isValid) throw Error(`--now ${nowArg}: ${now.invalidExplanation}`) + + loadFunctionsEnv() + const to = process.env.LETTER_TEST_RECIPIENT + if (!to) throw Error("LETTER_TEST_RECIPIENT is unset, so nothing would send") + + console.log(`Running ${job} as of ${now.toISO()}, mailing ${to}`) + if (job === "letters") await sendConferenceLetters(now.toJSDate()) + else await compileWeeklyUpdates(now.toJSDate()) + console.log( + "Check /emails (delivery), /legislatorMail and /conferenceLetterSkips in the emulator UI" + ) +} diff --git a/scripts/test-integration-ci.js b/scripts/test-integration-ci.js index 3ddbf9184..b6eab22c8 100755 --- a/scripts/test-integration-ci.js +++ b/scripts/test-integration-ci.js @@ -1,17 +1,38 @@ +const { readFileSync } = require("fs") const { demoProjectId, env, runOrExit } = require("./common") -runOrExit("yarn", ["--cwd", "functions", "build"], { stdio: "inherit" }) -runOrExit( - "./node_modules/.bin/firebase", - [ - "--project", - demoProjectId, - "emulators:exec", - "--only", - "auth,functions,pubsub,firestore,storage", - "--import", - "tests/integration/exportedTestData", - `yarn test:integration --forceExit ${process.argv.slice(2).join(" ")}` - ], - { stdio: "inherit", env } -) +// The functions sync every testimony and bill write to Typesense, as CI's +// typesense service provides. Without it each sync throws, the emulator kills +// that function's worker, and the next event waits on a fresh one: the backlog +// starves triggers the tests wait on, such as the cosign tally. +async function requireSearch() { + const url = readFileSync(`functions/.env.${demoProjectId}`, "utf8").match( + /^TYPESENSE_API_URL=(.*)$/m + )[1] + try { + if ((await fetch(`${url}/health`)).ok) return + } catch {} + console.error( + `Integration tests need Typesense at ${url}, as CI runs it.\n` + + "Start it with: docker compose -f infra/docker-compose.yml up -d search" + ) + process.exit(1) +} + +requireSearch().then(() => { + runOrExit("yarn", ["--cwd", "functions", "build"], { stdio: "inherit" }) + runOrExit( + "./node_modules/.bin/firebase", + [ + "--project", + demoProjectId, + "emulators:exec", + "--only", + "auth,functions,pubsub,firestore,storage", + "--import", + "tests/integration/exportedTestData", + `yarn test:integration --forceExit ${process.argv.slice(2).join(" ")}` + ], + { stdio: "inherit", env } + ) +}) diff --git a/stories/emailTemplates/NotificationDigestEmail.stories.tsx b/stories/emailTemplates/NotificationDigestEmail.stories.tsx index 96355cd3d..01454d787 100644 --- a/stories/emailTemplates/NotificationDigestEmail.stories.tsx +++ b/stories/emailTemplates/NotificationDigestEmail.stories.tsx @@ -4,11 +4,11 @@ import { EmailTemplateRenderer } from "./EmailTemplateRenderer" import { BillDigest, BillResult, - CONFERENCE_STANCES, ConferenceDigest, NotificationEmailDigest, UserDigest } from "functions/src/notifications/emailTypes" +import { CONFERENCE_STANCES } from "functions/src/shared/conferences" import { Frequency } from "components/auth" const meta: Meta = { diff --git a/tests/integration/conferenceLetters.test.ts b/tests/integration/conferenceLetters.test.ts new file mode 100644 index 000000000..4059a2e28 --- /dev/null +++ b/tests/integration/conferenceLetters.test.ts @@ -0,0 +1,374 @@ +import { + collection, + doc, + getDoc, + getDocs, + query, + where +} from "firebase/firestore" +import { nanoid } from "nanoid" +import { hasLetterOnRecord } from "../../components/db/legislatorMail" +import { firestore } from "../../components/firebase" +import { sendConferenceLetters } from "../../functions/src/conferenceLetters/sendLetters" +import { Timestamp } from "../../functions/src/firebase" +import { testAuth, testDb } from "../testUtils" +import { expectPermissionDenied, signInUser1, signInUser2 } from "./common" + +const COURT = 194 +const TEST_RECIPIENT = "test-inbox@example.com" +// Events at noon Eastern on the 12th; the job runs the morning after. +const ADDED_AT = new Date("2031-05-12T16:00:00Z") +const NOW = new Date("2031-05-13T13:00:00Z") + +const id = () => nanoid().replace(/[^a-zA-Z0-9]/g, "x") + +const member = (code: string, email?: string) => + testDb.doc(`generalCourts/${COURT}/members/${code}`).set({ + id: code, + content: { + Name: `Member ${code}`, + District: `District ${code}`, + ...(email ? { EmailAddress: email } : {}) + } + }) + +type Seed = { + verified?: boolean + rep?: string + sen?: string + withdrawn?: boolean +} + +type Input = { uid: string; testimonyId: string; conferenceId: string } + +/** An event saying the reader posted or edited their input, to `version`, at + * `at`, recorded then or at `recordedAt`. */ +const event = async ( + input: Input, + change: "added" | "edited", + version: number, + at: Date, + recordedAt = at +) => { + const eventId = id() + const state = (version: number) => ({ + position: "passSomething", + group: "individual", + version + }) + await testDb.doc(`conferenceInputEvents/${eventId}`).set({ + court: COURT, + conferenceId: input.conferenceId, + testimonyId: input.testimonyId, + authorUid: input.uid, + change, + ...(change === "edited" && { before: state(version - 1) }), + after: state(version), + at: Timestamp.fromDate(at), + recordedAt: Timestamp.fromDate(recordedAt) + }) + return eventId +} + +/** A reader who posted conference input yesterday, and the event that says so. */ +const seed = async (conferenceId: string, s: Seed = {}) => { + const uid = id() + const testimonyId = id() + await testAuth.createUser({ + uid, + email: `${uid}@example.com`, + emailVerified: s.verified ?? true + }) + if (!s.withdrawn) { + await testDb.doc(`users/${uid}/publishedTestimony/${testimonyId}`).set({ + conferenceId, + court: COURT, + billId: "H9901", + position: "passSomething", + content: "Please pass something.", + authorUid: uid, + authorDisplayName: "ada", + fullName: 'Ada "Ace" Lovelace', + authorRole: "user", + version: 1, + ...(s.rep ? { representativeId: s.rep } : {}), + ...(s.sen ? { senatorId: s.sen } : {}) + }) + } + const input = { uid, testimonyId, conferenceId } + const eventId = await event(input, "added", 1, ADDED_AT) + return { ...input, eventId } +} + +const DAY = 24 * 60 * 60 * 1000 +/** The run `days` after the first, and its window's noon. */ +const run = (days: number) => new Date(NOW.getTime() + days * DAY) +const noon = (days: number) => new Date(ADDED_AT.getTime() + days * DAY) + +/** The reader's input as published now, at `version`. */ +const publishVersion = (input: Input, version: number) => + testDb + .doc(`users/${input.uid}/publishedTestimony/${input.testimonyId}`) + .update({ version }) + +const email = async (eventId: string) => + (await testDb.doc(`emails/letter_${eventId}`).get()).data() +const record = async (eventId: string) => + (await testDb.doc(`legislatorMail/letter_${eventId}`).get()).data() +const skip = async (eventId: string) => + (await testDb.doc(`conferenceLetterSkips/${eventId}`).get()).data() + +let conferenceId: string +const rep = `R${id()}` +const sen = `S${id()}` +const mute = `M${id()}` + +beforeAll(async () => { + process.env.LETTER_TEST_RECIPIENT = TEST_RECIPIENT + conferenceId = id() + await testDb.doc(`generalCourts/${COURT}/conferences/${conferenceId}`).set({ + id: conferenceId, + court: COURT, + title: "An Act relative to phone-free schools", + name: "Phone-Free Schools" + }) + await Promise.all([ + member(rep, "rep@malegislature.gov"), + member(sen, "sen@malegislature.gov"), + member(mute) + ]) +}) + +describe("sendConferenceLetters", () => { + it("mails a letter once, to the test address, copying the reader", async () => { + const { eventId, uid, testimonyId } = await seed(conferenceId, { + rep, + sen + }) + await sendConferenceLetters(NOW) + const sent = await email(eventId) + // The auth emulator lowercases addresses. + const own = `${uid}@example.com`.toLowerCase() + expect(sent).toMatchObject({ + to: [TEST_RECIPIENT], + cc: [own], + replyTo: own, + from: '"Ada \\"Ace\\" Lovelace via MAPLE" ' + }) + expect(sent!.message.subject).toBe( + `[dev] would have gone to: rep@malegislature.gov, sen@malegislature.gov — Phone-Free Schools: Please pass something` + ) + expect(sent!.message.html).toContain(`Dear Representative Member ${rep}`) + expect(sent!.message.html).toContain( + `https://mapletestimony.org/conferences/${COURT}/${conferenceId}` + ) + expect(JSON.parse(sent!.headers["X-SMTPAPI"]).category).toEqual([ + "constituent-letters" + ]) + + // The record names the legislators, not the test address, and the + // subject without the test prefix. + const sentRecord = await record(eventId) + expect(sentRecord).toEqual({ + kind: "letter", + court: COURT, + memberCodes: [rep, sen], + subject: "Phone-Free Schools: Please pass something", + html: sent!.message.html, + text: sent!.message.text, + createdAt: sent!.createdAt, + uid, + testimonyId, + version: 1, + conferenceId, + updated: false + }) + + const second = await sendConferenceLetters(NOW) + expect(second).toBe(0) + expect((await email(eventId))!.createdAt).toEqual(sent!.createdAt) + }) + + it("writes to the legislator who has an address, and only names them", async () => { + const { eventId } = await seed(conferenceId, { rep, sen: mute }) + await sendConferenceLetters(NOW) + const sent = await email(eventId) + expect(sent!.message.subject).toContain("rep@malegislature.gov —") + expect(sent!.message.html).not.toContain("Senator") + expect((await record(eventId))!.memberCodes).toEqual([rep]) + }) + + it.each<[string, Seed, string]>([ + ["no verified email", { rep, verified: false }, "noVerifiedEmail"], + ["no rep or senator", {}, "noLegislator"], + ["no legislator address", { sen: mute }, "noLegislatorEmail"], + ["withdrawn before the job", { rep, withdrawn: true }, "withdrawn"] + ])("records %s as skipped, once", async (_, s, reason) => { + const { eventId } = await seed(conferenceId, s) + await sendConferenceLetters(NOW) + expect(await email(eventId)).toBeUndefined() + expect(await record(eventId)).toBeUndefined() + expect((await skip(eventId))!.reason).toBe(reason) + }) + + it("mails only the last of two posts of the same input", async () => { + const first = await seed(conferenceId, { rep }) + const secondId = await event( + first, + "added", + 2, + new Date(ADDED_AT.getTime() + 60_000) + ) + await sendConferenceLetters(NOW) + expect(await email(first.eventId)).toBeUndefined() + expect((await skip(first.eventId))!.reason).toBe("superseded") + expect(await email(secondId)).toBeDefined() + }) + + it("mails an edit as an update to the letter on record", async () => { + const posted = await seed(conferenceId, { rep }) + await sendConferenceLetters(run(0)) + expect(await record(posted.eventId)).toMatchObject({ updated: false }) + + await publishVersion(posted, 2) + const editId = await event(posted, "edited", 2, noon(1)) + await sendConferenceLetters(run(1)) + expect((await email(editId))!.message.subject).toMatch( + /Phone-Free Schools: Please pass something \(updated\)$/ + ) + expect(await record(editId)).toMatchObject({ + subject: "Phone-Free Schools: Please pass something (updated)", + version: 2, + updated: true + }) + }) + + it("mails each version once", async () => { + // Posted the evening before the run, edited the morning after it, before + // the run read what was posted: the run mails the edit with the post. + const posted = await seed(conferenceId, { rep }) + await publishVersion(posted, 2) + await sendConferenceLetters(run(0)) + expect(await record(posted.eventId)).toMatchObject({ + version: 2, + updated: false + }) + + const editId = await event(posted, "edited", 2, noon(1)) + await sendConferenceLetters(run(1)) + expect(await email(editId)).toBeUndefined() + expect((await skip(editId))!.reason).toBe("alreadySent") + + await publishVersion(posted, 3) + const againId = await event(posted, "edited", 3, noon(2)) + await sendConferenceLetters(run(2)) + expect(await record(againId)).toMatchObject({ version: 3, updated: true }) + }) + + it("mails an edit to input whose letter was skipped as new", async () => { + const posted = await seed(conferenceId, { rep, verified: false }) + await sendConferenceLetters(run(0)) + expect((await skip(posted.eventId))!.reason).toBe("noVerifiedEmail") + + await testAuth.updateUser(posted.uid, { emailVerified: true }) + await publishVersion(posted, 2) + const editId = await event(posted, "edited", 2, noon(1)) + await sendConferenceLetters(run(1)) + expect((await email(editId))!.message.subject).toMatch( + /Please pass something$/ + ) + expect(await record(editId)).toMatchObject({ updated: false }) + }) + + it("mails an event recorded after the run on the next run", async () => { + const posted = await seed(conferenceId, { rep }) + await sendConferenceLetters(run(0)) + + // Edited just before the next run, recorded by the trigger just after it. + await publishVersion(posted, 2) + const lateId = await event( + posted, + "edited", + 2, + new Date(run(1).getTime() - 2_000), + new Date(run(1).getTime() + 6_000) + ) + await sendConferenceLetters(run(1)) + expect(await email(lateId)).toBeUndefined() + expect(await skip(lateId)).toBeUndefined() + + await sendConferenceLetters(run(2)) + expect(await record(lateId)).toMatchObject({ version: 2, updated: true }) + }) + + it("leaves a mailed event alone when a rerun finds a later one", async () => { + const posted = await seed(conferenceId, { rep }) + await sendConferenceLetters(NOW) + expect(await email(posted.eventId)).toBeDefined() + + await publishVersion(posted, 2) + const editId = await event( + posted, + "edited", + 2, + new Date(ADDED_AT.getTime() + 60_000) + ) + await sendConferenceLetters(NOW) + expect(await skip(posted.eventId)).toBeUndefined() + expect(await record(editId)).toMatchObject({ version: 2, updated: true }) + }) + + it("mails an input posted and edited the same day once, as new", async () => { + const first = await seed(conferenceId, { rep }) + const editId = await event( + first, + "edited", + 2, + new Date(ADDED_AT.getTime() + 60_000) + ) + await sendConferenceLetters(NOW) + expect((await skip(first.eventId))!.reason).toBe("superseded") + expect((await email(editId))!.message.subject).toMatch( + /Please pass something$/ + ) + }) +}) + +describe("legislatorMail", () => { + it("lets a reader read only the records of their own letters", async () => { + const reader = await signInUser1() + const recordId = `letter_${id()}` + await testDb.doc(`legislatorMail/${recordId}`).set({ + kind: "letter", + uid: reader.uid + }) + const ref = doc(firestore, `legislatorMail/${recordId}`) + expect((await getDoc(ref)).exists()).toBe(true) + + await signInUser2() + await expectPermissionDenied(getDoc(ref)) + }) + + it("tells the reader's preview whether a letter for their input is on record", async () => { + const reader = await signInUser1() + const testimonyId = id() + await testDb.doc(`legislatorMail/letter_${id()}`).set({ + kind: "letter", + uid: reader.uid, + testimonyId + }) + expect(await hasLetterOnRecord(reader.uid, testimonyId)).toBe(true) + expect(await hasLetterOnRecord(reader.uid, id())).toBe(false) + + const other = await signInUser2() + expect(await hasLetterOnRecord(other.uid, testimonyId)).toBe(false) + await expectPermissionDenied( + getDocs( + query( + collection(firestore, "legislatorMail"), + where("testimonyId", "==", testimonyId) + ) + ) + ) + }) +}) diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts index 2a4609ec0..c8db11a0a 100644 --- a/tests/integration/testimony.test.ts +++ b/tests/integration/testimony.test.ts @@ -10,6 +10,8 @@ import { auth, firestore, functions, storage } from "../../components/firebase" import { terminateFirebase, testDb, testStorage } from "../testUtils" import { script as backfillTestimonyScope } from "../../scripts/firebase-admin/backfillTestimonyScope" import { resolveReport } from "components/db" +import { compileWeeklyUpdates } from "functions/src/conferenceUpdates/compileWeeklyUpdates" +import { weekWindow } from "functions/src/conferenceUpdates/weeklyUpdate" import { createFakeBill, createNewReport, @@ -1402,6 +1404,232 @@ describe("publishTestimony", () => { }) }) +describe("conference weekly update", () => { + const env = { ...process.env } + afterEach(() => { + process.env = { ...env } + }) + + const eventsOf = (conferenceId: string) => + testDb + .collection("/conferenceInputEvents") + .where("conferenceId", "==", conferenceId) + .get() + + /** The conference's input from `author`, in `position`. */ + const fileInput = async ( + author: () => Promise, + billId: string, + conferenceId: string, + position: BaseTestimony["position"] + ) => { + const { uid } = await author() + const { draftId } = await createConferenceDraft( + uid, + billId, + conferenceId, + position + ) + const res = await publishTestimony({ draftId }) + return { uid, draftId, publicationId: res.data.publicationId } + } + + /** The `i`th conferee, whose member document has `address`; null stands + * for a member document without one. */ + const conferee = async (address: string | null, i = 0) => { + const memberCode = `T${nanoid(6)}` + await testDb + .doc(`/generalCourts/${currentGeneralCourt}/members/${memberCode}`) + .set({ + id: memberCode, + content: { Name: `Test Member${i}`, EmailAddress: address } + }) + return { + memberCode, + branch: i % 2 ? ("House" as const) : ("Senate" as const), + chair: i < 2 + } + } + + it("compiles one update from a week of changes", async () => { + process.env.GCLOUD_PROJECT = "digital-testimony-prod" + const conferenceId = await createConference(billId, { + conferees: [await conferee("one@example.org")] + }) + const first = await fileInput( + signInUser1, + billId, + conferenceId, + "passSenateVersion" + ) + const second = await fileInput( + signInUser2, + billId, + conferenceId, + "passHouseVersion" + ) + await signInUser1() + await updateDoc(refs.draftTestimony(first.uid, first.draftId), { + position: "passNothing", + editReason: "Changed my mind" + }) + await publishTestimony({ draftId: first.draftId }) + await signInTestAdmin() + await deleteTestimony({ + uid: second.uid, + publicationId: second.publicationId + }) + + await waitFor( + async () => expect((await eventsOf(conferenceId)).size).toBe(4), + { timeout: 20000 } + ) + + // The week these events fall in is the one before next Monday. + const now = weekWindow(new Date()).week.plus({ weeks: 2, hours: 1 }) + const weekStart = weekWindow(now.toJSDate()).week.toISODate() + const path = `/legislatorMail/${currentGeneralCourt}_${conferenceId}_${weekStart}` + await compileWeeklyUpdates(now.toJSDate()) + + const update = (await testDb.doc(path).get()).data() as any + expect(update.kind).toBe("conferenceUpdate") + expect(update.standing.total).toBe(1) + expect(update.standing.byPosition.passNothing).toBe(1) + expect(update.changes).toMatchObject({ + added: 2, + edited: 1, + withdrawn: 1 + }) + expect(update.changes.net.total).toBe(1) + expect(update.html).toContain( + `https://mapletestimony.org/conferences/${currentGeneralCourt}/${conferenceId}` + ) + + // A second run in the same week sends and records nothing more. + await compileWeeklyUpdates(now.toJSDate()) + expect((await testDb.doc(path).get()).data()!.createdAt).toEqual( + update.createdAt + ) + }, 60000) + + describe("sending", () => { + /** An appointed conference whose conferees are `members`, a null address + * standing for a member document without one. */ + const conferenceWith = async (members: (string | null)[]) => { + const conferees = [] + for (const [i, address] of members.entries()) { + conferees.push(await conferee(address, i)) + } + const conferenceId = await createConference(billId, { conferees }) + await fileInput(signInUser1, billId, conferenceId, "passNothing") + await waitFor( + async () => expect((await eventsOf(conferenceId)).size).toBe(1), + { timeout: 20000 } + ) + const now = weekWindow(new Date()).week.plus({ weeks: 2, hours: 1 }) + const weekStart = weekWindow(now.toJSDate()).week.toISODate() + const id = `${currentGeneralCourt}_${conferenceId}_${weekStart}` + return { conferees, now: now.toJSDate(), id } + } + const emailOf = async (id: string) => + (await testDb.doc(`/emails/${id}`).get()).data() as any + + it("sends the update to the test address outside production, once", async () => { + process.env.GCLOUD_PROJECT = "digital-testimony-dev" + process.env.LETTER_TEST_RECIPIENT = "test@example.org" + const { conferees, now, id } = await conferenceWith([ + "one@example.org", + null, + "three@example.org" + ]) + await compileWeeklyUpdates(now) + + const email = await emailOf(id) + expect(email.to).toEqual(["test@example.org"]) + expect(email.replyTo).toBe("info@mapletestimony.org") + expect(email.from).toMatch(/^"MAPLE" <.+@mapletestimony\.org>$/) + expect(email.message.subject).toContain( + "[dev] would have gone to: one@example.org, three@example.org" + ) + expect(email.headers["X-SMTPAPI"]).toBe( + '{"category":["conferee-update"]}' + ) + expect(email.message.html).toContain("Sen.") + + // The record names the conferees, not the test address. + const update = (await testDb.doc(`/legislatorMail/${id}`).get()).data() + expect(update!.memberCodes).toEqual([ + conferees[0].memberCode, + conferees[2].memberCode + ]) + expect(email.message.subject).toContain(update!.subject) + expect(update!.subject).not.toContain("[dev]") + expect(update!.createdAt).toEqual(email.createdAt) + + await compileWeeklyUpdates(now) + expect((await emailOf(id)).createdAt).toEqual(email.createdAt) + }, 60000) + + it("sends to the conferees' own addresses in production", async () => { + process.env.GCLOUD_PROJECT = "digital-testimony-prod" + const { now, id } = await conferenceWith([ + "one@example.org", + "two@example.org" + ]) + await compileWeeklyUpdates(now) + + const email = await emailOf(id) + expect(email.to).toEqual(["one@example.org", "two@example.org"]) + expect(email.message.subject).not.toContain("[dev]") + }, 60000) + + it("sends and records nothing when no conferee has an address", async () => { + process.env.GCLOUD_PROJECT = "digital-testimony-dev" + process.env.LETTER_TEST_RECIPIENT = "test@example.org" + const { now, id } = await conferenceWith([null]) + await compileWeeklyUpdates(now) + + expect(await emailOf(id)).toBeUndefined() + expect((await testDb.doc(`/legislatorMail/${id}`).get()).exists).toBe( + false + ) + }, 60000) + }) + + it("compiles nothing for a week without a change", async () => { + const conferenceId = await createConference(billId) + await fileInput(signInUser1, billId, conferenceId, "passNothing") + await waitFor( + async () => expect((await eventsOf(conferenceId)).size).toBe(1), + { timeout: 20000 } + ) + + // Two weeks on, the week before it is empty. + const now = weekWindow(new Date()).week.plus({ weeks: 3, hours: 1 }) + const weekStart = weekWindow(now.toJSDate()).week.toISODate() + await compileWeeklyUpdates(now.toJSDate()) + + expect( + ( + await testDb + .doc( + `/legislatorMail/${currentGeneralCourt}_${conferenceId}_${weekStart}` + ) + .get() + ).exists + ).toBe(false) + // The input is on record, but in an earlier week. + const { start, end } = weekWindow(now.toJSDate()) + const inWeek = await testDb + .collection("/conferenceInputEvents") + .where("conferenceId", "==", conferenceId) + .where("recordedAt", ">=", start.toJSDate()) + .where("recordedAt", "<", end.toJSDate()) + .get() + expect(inWeek.size).toBe(0) + }) +}) + describe("deleteTestimony", () => { async function getSignedInAdmin() { const adminUser = await signInTestAdmin()