diff --git a/.github/workflows/repo-checks.yml b/.github/workflows/repo-checks.yml index 99606a1a9..fbf0d9b96 100644 --- a/.github/workflows/repo-checks.yml +++ b/.github/workflows/repo-checks.yml @@ -96,6 +96,7 @@ jobs: tests/integration/ballotQuestions.test.ts tests/integration/backfillTestimonyScope.test.ts tests/integration/conferences.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/components/conference/ConferenceDetails.switching.test.tsx b/components/conference/ConferenceDetails.switching.test.tsx index d4bdf848f..330fb915b 100644 --- a/components/conference/ConferenceDetails.switching.test.tsx +++ b/components/conference/ConferenceDetails.switching.test.tsx @@ -49,6 +49,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 39dda51c9..ca05e8b6a 100644 --- a/components/conference/ConferenceDetails.tsx +++ b/components/conference/ConferenceDetails.tsx @@ -33,6 +33,7 @@ import { useAppDispatch } from "../hooks" import { maple, siteUrl } from "../links" import { useConferenceTop, usePinnedNavbar } from "./hooks" import { InputContext } from "./input/context" +import { initialsOf } from "./input/accounts" import { CosignForm, CosignStep } from "./input/CosignForm" import { CosignLetter } from "./input/CosignLetter" import { InputForm, Step } from "./input/InputForm" @@ -44,7 +45,12 @@ import { Close } from "./input/Close" import { X } from "./input/icons" import { jumpToSection } from "./navbar" 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)` @@ -527,6 +533,7 @@ function Details({ void }) { const { t } = useTranslation("conferences") + const initials = useMemo( + () => conferees.map(c => initialsOf(c.name)), + [conferees] + ) const edit = useEditConferenceTestimony( uid, conference.court, @@ -600,7 +613,7 @@ function InputPanel({ const title = cosigning && edit.publication ? "input.cosign_edit_title" - : !cosigning && step === "review" + : (cosigning ? cosignStep : step) === "review" ? "input.review_title" : "input.panel_title" return ( @@ -621,6 +634,9 @@ function InputPanel({ key={cosigning.id} edit={edit} parent={cosigning} + court={conference.court} + subject={subject} + conferees={initials} step={cosignStep} onStep={onCosignStep} onCancel={onClose} @@ -628,7 +644,8 @@ function InputPanel({ /> ) : ( ({ })) 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 }) })) +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", authorUid: "org", @@ -58,6 +71,9 @@ const mount = ( { 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("keeps another reply-to address on the draft, for this letter alone", async () => { + jest.useFakeTimers() + const edit = editing({ draft } as Partial) + mount(edit, "review") + press("review.use_other") + fireEvent.change(screen.getByLabelText("review.reply_to_label"), { + target: { value: "gina.work@example.org" } + }) + press("review.done") + await act(async () => { + jest.advanceTimersByTime(2000) + }) + expect(edit.saveDraft.execute).toHaveBeenCalledWith( + expect.objectContaining({ + cosignOfId: "p1", + replyTo: "gina.work@example.org" + }) + ) + jest.useRealTimers() + }) + + it("refuses an address that isn't one", () => { + const edit = editing({ draft } as Partial) + mount(edit, "review") + press("review.use_other") + fireEvent.change(screen.getByLabelText("review.reply_to_label"), { + target: { value: "not an address" } + }) + press("review.done") + expect(screen.getByRole("alert")).toHaveTextContent( + "review.reply_to_invalid" + ) + }) }) it.each([ diff --git a/components/conference/input/CosignForm.tsx b/components/conference/input/CosignForm.tsx index 09c98c5a8..1b1b2bfc4 100644 --- a/components/conference/input/CosignForm.tsx +++ b/components/conference/input/CosignForm.tsx @@ -37,10 +37,12 @@ import { } from "./InputForm" import { useInputContext } from "./context" import { UsersRound } from "./icons" +import { LetterReview, NoLine } from "./Review" +import { hasLegislators } from "functions/src/conferenceLetters/letter" import { brand, caution, ink, line, red, wash } from "./tokens" import { useAutosave } from "./useAutosave" -export type CosignStep = "confirm" | "write" +export type CosignStep = "confirm" | "write" | "review" /** Below lg, where the letter sits above it, the agreeing step takes the room * it needs and leaves the letter the rest. */ @@ -256,11 +258,19 @@ const refusalOf = (error?: Error) => export function CosignForm({ edit, parent, + court, + subject, + conferees, step, onStep, onCancel, onPosted }: { + court: number + /** 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. */ @@ -281,6 +291,8 @@ export function CosignForm({ const [note, setNote] = useState("") const [noWords, setNoWords] = useState(false) const [reason, setReason] = useState("") + // The address replies to this letter go to, when not the reader's own. + const [chosen, setChosen] = useState("") const field = useRef(null) const ask = useAsk(field) @@ -298,25 +310,28 @@ export function CosignForm({ // Words drafted since are kept over the posted cosign's having none. if (edit.publication && !edit.publication.content && !seed) setNoWords(true) if (edit.draft?.editReason) setReason(edit.draft.editReason) + if (edit.draft?.replyTo) setChosen(edit.draft.replyTo) }, [edit.loading, edit.draft, edit.publication, parent.id]) const content = noWords ? "" : note const { save, dirty, posting, post, flush, reset } = useAutosave({ edit, - fields: { content, reason }, + fields: { content, reason, chosen }, matches: (draft, f) => draft?.cosignOfId === parent.id && draft?.content === f.content && - (draft?.editReason ?? "") === f.reason, + (draft?.editReason ?? "") === f.reason && + (draft?.replyTo ?? "") === f.chosen, write: f => saveDraft.execute({ position: parent.position, content: f.content, cosignOfId: parent.id, cosignOfAuthorUid: parent.authorUid, - editReason: f.reason || undefined + editReason: f.reason || undefined, + replyTo: f.chosen || undefined }), - active: step === "write", + active: step !== "confirm", onPosted }) @@ -389,6 +404,47 @@ export function CosignForm({ const name = profile?.fullName || t("input.anonymous") const type = accountTypeOf(claims?.role ?? "user") + if (step === "review") { + const sends = hasLegislators(profile ?? {}) + return ( + + + {!content.trim() && onStep("write")} />} + + {problem} + + + onStep("write")} + style={{ marginRight: "auto" }} + > + {t("review.back")} + + + {t("input.cancel")} + + + {t(sends ? "review.cosign_and_send" : "input.cosign")} + + + + ) + } return ( @@ -466,9 +522,13 @@ export function CosignForm({ posting || publishTestimony.loading } - onClick={() => (ready ? post() : ask.ask())} + onClick={() => { + if (!ready) ask.ask() + else if (editing) post() + else onStep("review") + }} > - {t(editing ? "input.cosign_save" : "input.cosign")} + {t(editing ? "input.cosign_save" : "input.review")} diff --git a/components/conference/input/InputForm.test.tsx b/components/conference/input/InputForm.test.tsx index 72c5157de..b1c269d0c 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,50 @@ 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 }) })) +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,7 +70,8 @@ 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("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("keeps another reply-to address on the draft, for this letter alone", async () => { + jest.useFakeTimers() + const edit = editing() + review(edit) + fireEvent.click(screen.getByRole("button", { name: "review.use_other" })) + fireEvent.change(screen.getByLabelText("review.reply_to_label"), { + target: { value: "gina.work@example.org" } + }) + fireEvent.click(screen.getByRole("button", { name: "review.done" })) + await act(async () => { + jest.advanceTimersByTime(2000) + }) + expect(edit.saveDraft.execute).toHaveBeenCalledWith( + expect.objectContaining({ replyTo: "gina.work@example.org" }) + ) + jest.useRealTimers() + }) + }) }) diff --git a/components/conference/input/InputForm.tsx b/components/conference/input/InputForm.tsx index 132b29248..030355059 100644 --- a/components/conference/input/InputForm.tsx +++ b/components/conference/input/InputForm.tsx @@ -1,11 +1,8 @@ import { RefObject, useCallback, useEffect, useRef, useState } from "react" -import { Timestamp } from "firebase/firestore" import { Trans, useTranslation } from "next-i18next" import styled, { keyframes } from "styled-components" -import { useAuth } from "components/auth" import { ConferencePosition, - ConferenceTestimony, hasDraftChanged, Testimony, UseEditConferenceTestimony, @@ -13,7 +10,8 @@ import { WorkingDraft } from "components/db" import { Internal } from "components/links" -import { Entry } from "./Entry" +import { hasLegislators } from "functions/src/conferenceLetters/letter" +import { LetterReview } from "./Review" import { conferencePositions, positionColours } from "./positions" import { brand, ink, line, red, wash } from "./tokens" import { useAutosave } from "./useAutosave" @@ -154,12 +152,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; @@ -259,7 +251,8 @@ export function ConductRule() { * from the review step. */ export function InputForm({ - uid, + court, + conferees, edit, subject, step, @@ -267,7 +260,9 @@ export function InputForm({ onCancel, onPosted }: { - uid: string + court: number + /** 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 +273,6 @@ export function InputForm({ onPosted: () => void }) { const { t } = useTranslation("conferences") - const { claims } = useAuth() const { profile } = useProfile() const { saveDraft, publishTestimony, discardDraft } = edit @@ -287,6 +281,8 @@ export function InputForm({ ) const [content, setContent] = useState("") const [reason, setReason] = useState("") + // The address replies to this letter go to, when not the reader's own. + const [chosen, setChosen] = useState("") const field = useRef(null) const ask = useAsk(field) @@ -302,23 +298,26 @@ export function InputForm({ if (source?.position) setPosition(source.position) if (source?.content) setContent(source.content) if (edit.draft?.editReason) setReason(edit.draft.editReason) + if (edit.draft?.replyTo) setChosen(edit.draft.replyTo) }, [edit.loading, edit.draft, edit.publication]) // Keep the draft as they type, once there are words to keep. const { save, dirty, posting, post, reset } = useAutosave({ edit, - fields: { position, content, reason }, + fields: { position, content, reason, chosen }, // A cosign's draft is not this form's, whatever its words. matches: (draft, f) => !draft?.cosignOfId && draft?.content === f.content && draft?.position === f.position && - (draft?.editReason ?? "") === f.reason, + (draft?.editReason ?? "") === f.reason && + (draft?.replyTo ?? "") === f.chosen, write: f => saveDraft.execute({ position: f.position, content: f.content, - editReason: f.reason || undefined + editReason: f.reason || undefined, + replyTo: f.chosen || undefined }), idle: f => !f.content.trim(), onPosted @@ -355,16 +354,6 @@ export function InputForm({ 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") return ( @@ -420,22 +409,24 @@ export function InputForm({ ) + // Whether the letter goes anywhere, from the profile alone. + const sends = hasLegislators(profile ?? {}) return ( - - - - +
{t("input.rules_label")} -
  • {t("input.rule_public")}
  • {t("input.rule_revise")}
  • @@ -467,7 +458,7 @@ export function InputForm({ } onClick={post} > - {t("input.post")} + {t(sends ? "review.post_and_send" : "input.post")}
    diff --git a/components/conference/input/Review.tsx b/components/conference/input/Review.tsx new file mode 100644 index 000000000..cd99954fd --- /dev/null +++ b/components/conference/input/Review.tsx @@ -0,0 +1,518 @@ +import { useState } from "react" +import { useTranslation } from "next-i18next" +import styled from "styled-components" +import { useAuth } from "components/auth" +import { ProfileMember, useMember, useProfile } from "components/db" +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` + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 8px; + margin-top: 6px; + font-size: 0.875rem; + color: ${ink.strong}; +` + +const Link = styled.button` + padding: 6px 2px; + border: 0; + background: none; + font-size: 0.8125rem; + font-weight: 600; + color: ${brand.ink}; + text-decoration: underline; +` + +const Edit = styled.div` + display: flex; + flex-direction: column; + gap: 6px; + margin-top: 8px; + label { + font-size: 0.8125rem; + font-weight: 600; + color: ${ink.mid}; + } + div { + display: flex; + gap: 10px; + } + input { + flex: 1; + min-width: 0; + height: 40px; + padding: 0 12px; + border: 1px solid ${brand.fill}; + border-radius: 8px; + font-size: 0.875rem; + color: ${ink.strong}; + } + button { + height: 40px; + padding: 0 16px; + border: 1px solid ${brand.fill}; + border-radius: 8px; + background: var(--maple-surface-base); + font-size: 0.8125rem; + font-weight: 600; + color: ${brand.fill}; + } +` + +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) + } +} + +/** Where a reply goes: the reader's own address, or the one they gave for + * this letter. Their login address is not changed. */ +function useReplyTo(chosen?: string | null) { + const { user } = useAuth() + const own = user?.email ?? "" + return { own, replyTo: chosen?.trim() || own } +} + +/** + * 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} + ))} + + +
    +
    +
    + + ) +} + +const looksLikeEmail = (s: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s) + +/** The reply-to address and the way to give another for this letter only. */ +export function ReplyTo({ + own, + value, + onChange +}: { + /** The reader's login address. */ + own: string + /** The address chosen for this letter; empty to use `own`. */ + value: string + onChange: (replyTo: string) => void +}) { + const { t } = useTranslation("conferences") + const [editing, setEditing] = useState(false) + const [text, setText] = useState("") + const [invalid, setInvalid] = useState(false) + const open = () => { + setText(value || own) + setEditing(true) + } + const done = () => { + const next = text.trim() + if (next && !looksLikeEmail(next)) return setInvalid(true) + onChange(next === own ? "" : next) + setEditing(false) + } + return ( + <> + {t("review.email_label")} + + {t("review.email_note")} + {editing ? ( + + +
    + { + setText(e.target.value) + setInvalid(false) + }} + onKeyDown={e => e.key === "Enter" && done()} + /> + +
    + + {t(invalid ? "review.reply_to_invalid" : "review.reply_to_note")} + +
    + ) : ( + + {value || own} + + {t("review.use_other")} + + + )} +
    + + ) +} + +/** 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, + conferees, + author, + conference, + position, + content, + cosign, + chosen, + onChoose +}: { + court: number + /** Initials of the conferees, for the weekly update's row. */ + conferees: string[] + author: string + conference: 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 } + /** The reply-to the reader chose for this letter; empty for their own. */ + chosen: string + onChoose: (replyTo: string) => void +}) { + const { representative, senator } = useLegislators(court) + const { own, replyTo } = useReplyTo(chosen) + return ( + <> + + {hasLegislators({ representative, senator }) && ( + <> + + + + )} + + ) +} diff --git a/components/conference/input/icons.tsx b/components/conference/input/icons.tsx index a70800f24..4040e5500 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/testimony/types.ts b/components/db/testimony/types.ts index fca2826fe..9f83195e9 100644 --- a/components/db/testimony/types.ts +++ b/components/db/testimony/types.ts @@ -96,7 +96,10 @@ export type DraftTestimony = Static const DraftTestimonyFields = BaseTestimony.extend({ publishedVersion: Optional(Number), /** If present, array of legislator member codes */ - recipientMemberCodes: Maybe(Array(String)) + recipientMemberCodes: Maybe(Array(String)), + /** Where replies to this conference input's letter go, in place of the + * author's own email. Drafts are private, publications are not. */ + replyTo: Maybe(String) }) export const DraftTestimony = DraftTestimonyFields.withConstraint(oneTarget).withConstraint(cosignShape) diff --git a/components/db/testimony/useEditTestimony.ts b/components/db/testimony/useEditTestimony.ts index 65ee9c365..8ced82d16 100644 --- a/components/db/testimony/useEditTestimony.ts +++ b/components/db/testimony/useEditTestimony.ts @@ -283,6 +283,7 @@ type SaveDraftRequest = Pick< | "editReason" | "cosignOfId" | "cosignOfAuthorUid" + | "replyTo" > & { ballotQuestionId?: string | null } type SaveConferenceDraftRequest = Omit & { position: ConferencePosition @@ -311,6 +312,7 @@ function useSaveDraft( editReason, cosignOfId, cosignOfAuthorUid, + replyTo, ballotQuestionId: requestBallotQuestionId }: SaveDraftRequest) => { if (draftLoading) { @@ -328,7 +330,8 @@ function useSaveDraft( requestBallotQuestionId ?? ballotQuestionId ?? null, conferenceId: conferenceId ?? null, cosignOfId: cosignOfId ?? null, - cosignOfAuthorUid: cosignOfAuthorUid ?? null + cosignOfAuthorUid: cosignOfAuthorUid ?? null, + replyTo: replyTo ?? null } const result = await addDoc( collection(firestore, `/users/${uid}/draftTestimony`), @@ -351,6 +354,7 @@ function useSaveDraft( requestBallotQuestionId ?? ballotQuestionId ?? null, cosignOfId: cosignOfId ?? null, cosignOfAuthorUid: cosignOfAuthorUid ?? null, + replyTo: replyTo ?? null, publishedVersion: hasChanges ? deleteField() : undefined }) } 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..55f7b792f 100644 --- a/functions/.env.digital-testimony-dev +++ b/functions/.env.digital-testimony-dev @@ -1,2 +1,6 @@ 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 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/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..87d3dedcc --- /dev/null +++ b/functions/src/conferenceLetters/letter.test.ts @@ -0,0 +1,82 @@ +import { hasLegislators, LetterInput, renderLetter } from "./letter" + +const base: LetterInput = { + author: "Ada Example", + replyTo: "ada@example.org", + 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 post once it has one", () => { + expect(renderLetter(base).footer.endsWith("publicly.")).toBe(true) + expect( + renderLetter({ ...base, postUrl: "https://example.org/p" }).footer + ).toContain("publicly: https://example.org/p") + }) +}) + +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..766891c29 --- /dev/null +++ b/functions/src/conferenceLetters/letter.ts @@ -0,0 +1,98 @@ +/** 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 type { ConferencePosition } from "../testimony/types" +export type { ConferencePosition } + +const positionLabels: Record = { + passSomething: "Please pass something", + passSenateVersion: "Pass the Senate version", + passHouseVersion: "Pass the House version", + passNothing: "Don't pass anything" +} + +export type LetterMember = { name: string; district: string } + +export type LetterInput = { + /** The reader's name. */ + author: string + /** Where replies go, and who is copied: the reader's own address, or the + * one they chose for this letter. */ + replyTo: string + conference: string + 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 + /** The posted input's page, known once it is posted. */ + postUrl?: string +} + +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 = positionLabels[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.replyTo + ] + const where = i.cosign ? "this cosign" : "this input" + return { + from: `${i.author} via MAPLE`, + replyTo: i.replyTo, + cc: i.replyTo, + to: [rep && `Rep. ${rep.name}`, sen && `Sen. ${sen.name}`] + .filter(Boolean) + .join(", "), + subject: i.cosign + ? `${i.conference}: Cosigned input (${label})` + : `${i.conference}: ${label}`, + 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` + + (i.postUrl ? `: ${i.postUrl}` : ".") + } +} diff --git a/functions/src/conferenceLetters/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts new file mode 100644 index 000000000..f9bf73870 --- /dev/null +++ b/functions/src/conferenceLetters/letters.test.ts @@ -0,0 +1,120 @@ +import { renderLetter } from "./letter" +import { + buildLetterEmail, + dayWindow, + letterFrom, + renderLetterHtml, + renderLetterText +} from "./letters" + +const letter = renderLetter({ + author: 'Ada "Ace" ', + replyTo: "ada@example.org", + 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("is yesterday in Eastern time", () => { + const { start, end } = dayWindow(new Date("2026-11-11T14:00:00Z")) + expect(start.toISO()).toBe("2026-11-10T00:00:00.000-05:00") + expect(end.toISO()).toBe("2026-11-11T00:00:00.000-05:00") + }) + + it("is a 25 hour day when the clocks go back", () => { + const { start, end } = dayWindow(new Date("2026-11-02T14:00:00Z")) + expect(end.diff(start, "hours").hours).toBe(25) + }) + + it("is a 23 hour day when the clocks go forward", () => { + const { start, end } = dayWindow(new Date("2027-03-15T14:00:00Z")) + 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", + replyTo: "ada@example.org", + 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", + to: ["rep@malegislature.gov"], + subjectPrefix: "[dev] ", + createdAt: null + }) + + 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.to).toEqual(["rep@malegislature.gov"]) + expect(email.message.subject).toBe( + "[dev] 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..4f13f72e8 --- /dev/null +++ b/functions/src/conferenceLetters/letters.ts @@ -0,0 +1,113 @@ +import { DateTime } from "luxon" +import { timeZone } from "../malegislature" +import { Letter } from "./letter" + +/** Yesterday, Eastern: the day whose new input the daily job mails. */ +export function dayWindow(now: Date) { + const end = DateTime.fromJSDate(now, { zone: timeZone }).startOf("day") + 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" + | "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 `emails` document 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, + to, + subjectPrefix, + createdAt +}: { + letter: Letter + author: string + fromAddress: string + to: string[] + subjectPrefix: string + createdAt: unknown +}) => { + const html = renderLetterHtml(letter) + return { + to, + 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: subjectPrefix + letter.subject, + text: renderLetterText(letter), + html + }, + createdAt + } +} diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts new file mode 100644 index 000000000..e18560a99 --- /dev/null +++ b/functions/src/conferenceLetters/sendLetters.ts @@ -0,0 +1,207 @@ +import { onSchedule } from "firebase-functions/v2/scheduler" +import * as logger from "firebase-functions/logger" +import { Conference } from "../conferences/types" +import { resolveRecipients } from "../email/recipients" +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 { hasLegislators, renderLetter } from "./letter" +import { buildLetterEmail, dayWindow, SkipReason } from "./letters" + +const ALREADY_EXISTS = 6 +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 + } +} + +const create = async ( + ref: FirebaseFirestore.DocumentReference, + data: object +) => { + try { + await ref.create(data) + return true + } catch (e) { + if ((e as { code?: number }).code !== ALREADY_EXISTS) throw e + return false + } +} + +const readerAuthor = (t: Testimony) => t.fullName ?? t.authorDisplayName + +/** The letter for one input, or why it is not sent. */ +async function prepare( + e: ConferenceInputEvent, + eventId: string, + lastFor: Map, + memberContent: (key: string) => Promise, + conferenceOf: (key: string) => Promise +): Promise<{ skip: SkipReason } | { email: object }> { + // An input withdrawn and posted again in the window has two "added" events; + // only the last is mailed, with what is posted now. + if (lastFor.get(e.testimonyId) !== eventId) return { skip: "superseded" } + + const [pubSnap, readerEmail, draft, conference] = await Promise.all([ + db.doc(`/users/${e.authorUid}/publishedTestimony/${e.testimonyId}`).get(), + verifiedEmail(e.authorUid), + db + .collection(`/users/${e.authorUid}/draftTestimony`) + .where("conferenceId", "==", e.conferenceId) + .limit(1) + .get(), + conferenceOf(`${e.court}/${e.conferenceId}`) + ]) + const pub = pubSnap.data() as Testimony | undefined + const state = pub && stateOf(pub) + if (!pub || !state) return { skip: "withdrawn" } + if (!readerEmail) return { skip: "noVerifiedEmail" } + if (!pub.representativeId && !pub.senatorId) return { skip: "noLegislator" } + if (!conference) return { skip: "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 content?.Name && email + ? { + 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 { skip: "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 { skip: "withdrawn" } + cosign = { author: readerAuthor(parent), content: parent.content } + } + + const author = readerAuthor(pub) + const letter = renderLetter({ + author, + replyTo: (draft.docs[0]?.data().replyTo as string)?.trim() || readerEmail, + conference: conference.name ?? conference.title, + position: state.position, + content: pub.content, + cosign, + representative, + senator + }) + const { to, subjectPrefix } = resolveRecipients( + reachable.flatMap(m => m?.email ?? []) + ) + if (!to.length) return { skip: "noRecipients" } + return { + email: buildLetterEmail({ + letter, + author, + fromAddress: process.env.LETTER_FROM ?? DEFAULT_FROM, + to, + subjectPrefix, + createdAt: Timestamp.now() + }) + } +} + +/** Mails each conference input added in the 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}, created, so a second run + * sends nothing again. An input that is not mailed is recorded, with why, at + * /conferenceLetterSkips/{input event id}. */ +export async function sendConferenceLetters(now = new Date()) { + const { start, end } = dayWindow(now) + const events = await db + .collection("/conferenceInputEvents") + .where("at", ">=", Timestamp.fromDate(start.toJSDate())) + .where("at", "<", Timestamp.fromDate(end.toJSDate())) + .get() + + const added = events.docs + .map(doc => ({ id: doc.id, e: doc.data() as ConferenceInputEvent })) + .filter(({ e }) => e.change === "added") + .sort((a, b) => a.e.at.toMillis() - b.e.at.toMillis()) + const lastFor = new Map(added.map(({ id, e }) => [e.testimonyId, id])) + 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 + for (const { id, e } of added) { + try { + const result = await prepare(e, id, lastFor, memberContent, conferenceOf) + if ("skip" in result) { + await create(db.doc(`/conferenceLetterSkips/${id}`), { + testimonyId: e.testimonyId, + authorUid: e.authorUid, + conferenceId: e.conferenceId, + reason: result.skip, + createdAt: Timestamp.now() + }) + } else if (await create(db.doc(`/emails/letter_${id}`), result.email)) { + sent++ + } + } catch (err) { + logger.error(`Letter for input event ${id} failed`, err) + } + } + logger.info(`Mailed ${sent} conference letters for ${start.toISODate()}`) + return sent +} + +export const mailConferenceLetters = onSchedule( + { schedule: "0 9 * * *", timeZone, timeoutSeconds: 300 }, + () => sendConferenceLetters().then(() => undefined) +) diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts new file mode 100644 index 000000000..bec39a2cd --- /dev/null +++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts @@ -0,0 +1,163 @@ +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 { resolveRecipients } from "../email/recipients" +import { splitMemberName } from "../shared/names" +import { takesTestimony } from "../shared/conferences" +import { Testimony } from "../testimony/types" +import { stateOf } from "./inputEvents" +import { ConferenceInputEvent, ConferenceUpdate } from "./types" +import { + buildConferenceUpdateEmail, + conferenceUpdateSubject, + renderConferenceUpdate, + renderConferenceUpdateText, + summarize, + tally, + weekWindow +} from "./weeklyUpdate" + +const ALREADY_EXISTS = 6 +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 in the week before `now`, and stores it as + * /conferenceUpdates/{court}_{conferenceId}_{week start}. Creating it + * rather than setting it means a second run in the same week leaves the + * first update as it was. */ +export async function compileWeeklyUpdates(now = new Date()) { + const { start, end } = weekWindow(now) + const events = await db + .collection("/conferenceInputEvents") + .where("at", ">=", Timestamp.fromDate(start.toJSDate())) + .where("at", "<", 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 stored = 0 + 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, + start, + now + }) + const html = renderConferenceUpdate(data) + const reachable = conferees.filter(c => c.email) + const update: ConferenceUpdate = { + court, + conferenceId: id, + weekStart: Timestamp.fromDate(start.toJSDate()), + standing, + changes, + html, + delivery: { + sentTo: reachable.map(c => c.memberCode), + skipped: conferees.filter(c => !c.email).map(c => c.memberCode) + }, + createdAt: Timestamp.now() + } + + // One batch, so the update and its email exist together or not at all: + // a second run finds the update and sends nothing. + const updateRef = db + .collection("/conferenceUpdates") + .doc(`${court}_${id}_${start.toISODate()}`) + const batch = db.batch() + batch.create(updateRef, update) + const { to, subjectPrefix } = resolveRecipients( + reachable.map(c => c.email!) + ) + if (to.length) { + batch.create(db.collection("emails").doc(updateRef.id), { + to, + 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: subjectPrefix + conferenceUpdateSubject(data), + text: renderConferenceUpdateText(data), + html + }, + createdAt: Timestamp.now() + }) + } else { + logger.warn(`No recipients for the update to conference ${key}`) + } + try { + await batch.commit() + stored++ + } catch (e) { + if ((e as { code?: number }).code !== ALREADY_EXISTS) throw e + } + } catch (e) { + logger.error(`Weekly update for conference ${key} failed`, e) + } + } + logger.info(`Stored ${stored} weekly conference updates`) +} + +export const compileConferenceUpdates = onSchedule( + { + schedule: "0 8 * * 1", // Monday 8:00 AM + timeZone, + timeoutSeconds: 300 + }, + () => compileWeeklyUpdates() +) 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..4e13d6e83 --- /dev/null +++ b/functions/src/conferenceUpdates/inputEvents.ts @@ -0,0 +1,68 @@ +import { onDocumentWritten } from "firebase-functions/v2/firestore" +import { db, 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 weekly update to + * read. The event's id is the trigger event's, so a retry writes the same + * document. */ +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 db + .collection("/conferenceInputEvents") + .doc(event.id) + .set({ ...body, at: Timestamp.fromDate(new Date(event.time)) }) + } +) diff --git a/functions/src/conferenceUpdates/types.ts b/functions/src/conferenceUpdates/types.ts new file mode 100644 index 000000000..a47433450 --- /dev/null +++ b/functions/src/conferenceUpdates/types.ts @@ -0,0 +1,107 @@ +import { + InstanceOf, + Literal as L, + Array, + 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`; the weekly update reads a week of them. + * 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) +}) + +/** 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 +}) + +/** Who an update was sent to, by member code. Skipped conferees have no + * email address on their member document. */ +export type Delivery = Static +export const Delivery = Record({ + sentTo: Array(String), + skipped: Array(String) +}) + +/** One conference's update for one week, stored so it can be sent. + * Firestore: /conferenceUpdates/{court}_{conferenceId}_{weekStart date} */ +export type ConferenceUpdate = Static +export const ConferenceUpdate = Record({ + court: Number, + conferenceId: String, + /** Monday 00:00, Eastern. The week runs to the next Monday. */ + weekStart: InstanceOf(Timestamp), + /** The input on the conference when the update was compiled. */ + standing: Counts, + changes: Changes, + html: String, + delivery: Delivery, + createdAt: InstanceOf(Timestamp) +}) diff --git a/functions/src/conferenceUpdates/weeklyUpdate.test.ts b/functions/src/conferenceUpdates/weeklyUpdate.test.ts new file mode 100644 index 000000000..62f8a66ff --- /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("is the Monday-to-Sunday week before the Monday on or before now", () => { + // Monday Sept 28, 2026, 8am Eastern + const { start, end } = weekWindow(new Date("2026-09-28T12:00:00Z")) + expect(start.toISODate()).toBe("2026-09-21") + expect(end.toISODate()).toBe("2026-09-28") + expect(end.toISO()).toContain("T00:00:00.000-04:00") + }) + + it("still ends on the Monday midday through the next Sunday", () => { + const { end } = weekWindow(new Date("2026-10-04T23:00:00Z")) + expect(end.toISODate()).toBe("2026-09-28") + }) + + it("keeps Monday midnight across the autumn clock change", () => { + const { start, end } = weekWindow(new Date("2026-11-02T15:00:00Z")) + expect(start.toISO()).toBe("2026-10-26T00:00:00.000-04:00") + expect(end.toISO()).toBe("2026-11-02T00:00:00.000-05: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. Crighton", chair: true }, + { name: "Rep. Peisch", chair: false } + ], + standing, + changes, + start: weekWindow(new Date("2026-09-28T12:00:00Z")).start, + 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. Crighton (chair), Rep. Peisch") + 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. Crighton (chair), Rep. Peisch\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..b75854d34 --- /dev/null +++ b/functions/src/conferenceUpdates/weeklyUpdate.ts @@ -0,0 +1,215 @@ +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 } 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 Monday-to-Sunday week, Eastern, that ended at the Monday on or before + * `now`. `end` is exclusive. */ +export function weekWindow(now: Date) { + const end = DateTime.fromJSDate(now, { zone: timeZone }).startOf("week") + return { 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 + * week, which is the standing now less the week's net change. */ +export function buildConferenceUpdateEmail({ + conference, + conferees, + standing, + changes, + start, + now +}: { + conference: Pick< + Conference, + "court" | "id" | "title" | "name" | "senateBillId" | "houseBillId" + > + conferees: { name: string; chair: boolean }[] + standing: Counts + changes: Changes + start: 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: conference.name ?? conference.title, + conferenceUrl: `https://mapletestimony.org/conferences/${conference.court}/${conference.id}`, + weekLabel: formatWeek(start), + 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..d63777b08 --- /dev/null +++ b/functions/src/email/recipients.test.ts @@ -0,0 +1,31 @@ +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([]) + }) +}) diff --git a/functions/src/email/recipients.ts b/functions/src/email/recipients.ts new file mode 100644 index 000000000..f326362ef --- /dev/null +++ b/functions/src/email/recipients.ts @@ -0,0 +1,19 @@ +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, nothing is + * sent. */ +export const resolveRecipients = ( + real: string[] +): { to: string[]; subjectPrefix: string } => { + 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/notifications/deliverNotifications.test.ts b/functions/src/notifications/deliverNotifications.test.ts index 9213de33c..a9e4ec51a 100644 --- a/functions/src/notifications/deliverNotifications.test.ts +++ b/functions/src/notifications/deliverNotifications.test.ts @@ -1,4 +1,5 @@ -import { buildDigestData, CONFERENCE_STANCES } from "./deliverNotifications" +import { buildDigestData } from "./deliverNotifications" +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 f7c159c0b..2becd52f4 100644 --- a/functions/src/notifications/deliverNotifications.ts +++ b/functions/src/notifications/deliverNotifications.ts @@ -20,6 +20,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 @@ -28,14 +29,6 @@ const NUM_TESTIMONIES_TO_DISPLAY = 6 const NUM_BALLOT_QUESTIONS_TO_DISPLAY = 4 const EMAIL_TEMPLATE_PATH = "../email/digestEmail.handlebars" -// 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" -} - const path = require("path") const getVerifiedUserEmail = async (uid: string) => { diff --git a/functions/src/notifications/emailTypes.ts b/functions/src/notifications/emailTypes.ts index ae6fe0bd8..6b72815fc 100644 --- a/functions/src/notifications/emailTypes.ts +++ b/functions/src/notifications/emailTypes.ts @@ -42,3 +42,26 @@ export type NotificationEmailDigest = { ballotQuestions: BallotQuestionDigest[] // cap of 4 numBallotQuestionsWithNewTestimony: 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 3ce9fd47c..d76b80935 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. */ @@ -10,3 +11,11 @@ export const hasReport = (c: Pick) => * existed counts as covered. */ export const takesTestimony = (c: Pick) => c.status === "appointed" && c.covered !== false + +// 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/functions/src/testimony/types.ts b/functions/src/testimony/types.ts index fd0845433..ba2f1cc9c 100644 --- a/functions/src/testimony/types.ts +++ b/functions/src/testimony/types.ts @@ -122,7 +122,10 @@ export const oneTarget = ({ export type DraftTestimony = Static export const DraftTestimony = BaseTestimony.extend({ - publishedVersion: Optional(Number) + publishedVersion: Optional(Number), + /** Where replies to a conference input's letter go, in place of the + * author's own email. Never copied to the publication. */ + replyTo: Maybe(RtString) }) .withConstraint(positionMatchesTarget) .withConstraint(oneTarget) diff --git a/infra/gcp/dns/README.md b/infra/gcp/dns/README.md index bd3dd83d6..5994d7775 100644 --- a/infra/gcp/dns/README.md +++ b/infra/gcp/dns/README.md @@ -68,5 +68,5 @@ the name servers. One public managed zone, **$0.20/month**, in prod only — too few rows to be worth a table. Queries add $0.40 per million; DNSSEC signing is free and only makes responses larger; records -inside a zone cost nothing, so the 15 here are free. The PDS zones belong to +inside a zone cost nothing, so the ones here are free. The PDS zones belong to [infra/gcp](../README.md). List prices, Cloud Billing Catalog API, 2026-09-21. diff --git a/infra/gcp/dns/records.tf b/infra/gcp/dns/records.tf index 29c403a35..09ca4f4a7 100644 --- a/infra/gcp/dns/records.tf +++ b/infra/gcp/dns/records.tf @@ -41,6 +41,21 @@ locals { "s1._domainkey CNAME" = { ttl = 14400, rrdatas = ["s1.domainkey.u32856346.wl097.sendgrid.net."] } "s2._domainkey CNAME" = { ttl = 14400, rrdatas = ["s2.domainkey.u32856346.wl097.sendgrid.net."] } + # SendGrid domain authentication for the conference streams: letters from + # readers to their legislators, and MAPLE's weekly update to conferees. + # Each sends from its own subdomain so a reputation problem in one cannot + # reach the other or the main domain. No MX on purpose: replies go to + # Reply-To, and mail to a sending address bounces. The _dmarc records + # repeat what the apex already says so either policy can diverge later. + "em759.letters CNAME" = { ttl = 14400, rrdatas = ["u32856346.wl097.sendgrid.net."] } + "s1._domainkey.letters CNAME" = { ttl = 14400, rrdatas = ["s1.domainkey.u32856346.wl097.sendgrid.net."] } + "s2._domainkey.letters CNAME" = { ttl = 14400, rrdatas = ["s2.domainkey.u32856346.wl097.sendgrid.net."] } + "_dmarc.letters TXT" = { ttl = 14400, rrdatas = ["\"v=DMARC1; p=none;\""] } + "em9068.updates CNAME" = { ttl = 14400, rrdatas = ["u32856346.wl097.sendgrid.net."] } + "s1._domainkey.updates CNAME" = { ttl = 14400, rrdatas = ["s1.domainkey.u32856346.wl097.sendgrid.net."] } + "s2._domainkey.updates CNAME" = { ttl = 14400, rrdatas = ["s2.domainkey.u32856346.wl097.sendgrid.net."] } + "_dmarc.updates TXT" = { ttl = 14400, rrdatas = ["\"v=DMARC1; p=none;\""] } + # Firebase's custom mail domain, mail.mapletestimony.org: its own SPF, the # project verification token, and the two DKIM CNAMEs beneath it. This is # the envelope domain for password resets and digest mail. diff --git a/public/locales/en/conferences.json b/public/locales/en/conferences.json index eb4e69817..ca9d26ce0 100644 --- a/public/locales/en/conferences.json +++ b/public/locales/en/conferences.json @@ -209,7 +209,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.", @@ -284,5 +283,40 @@ "cosign_closed": "This conference committee is no longer taking input, so your cosign could not be filed.", "fill_window": "Fill the window", "leave_full": "Leave full screen" + }, + "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.", + "use_other": "Use a different email", + "reply_to_label": "Reply-to email", + "reply_to_note": "Used for this message only. Your MAPLE login email stays the same.", + "done": "Done", + "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", + "back": "Back to Editing", + "reply_to_invalid": "That doesn’t look like an email address.", + "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..d4ee24707 --- /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 it is the job's next scheduled run, so letters + * cover today's input and updates 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 +} + +const nextRun = (job: "letters" | "updates") => { + const today = DateTime.now().setZone(timeZone).startOf("day") + return job === "letters" + ? today.plus({ days: 1 }).set({ hour: 9 }) + : today.startOf("week").plus({ weeks: 1 }).set({ hour: 8 }) +} + +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 }) + : nextRun(job) + 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) and /conferenceLetterSkips in the emulator UI" + ) +} diff --git a/tests/integration/conferenceLetters.test.ts b/tests/integration/conferenceLetters.test.ts new file mode 100644 index 000000000..65335c15a --- /dev/null +++ b/tests/integration/conferenceLetters.test.ts @@ -0,0 +1,178 @@ +import { nanoid } from "nanoid" +import { sendConferenceLetters } from "../../functions/src/conferenceLetters/sendLetters" +import { Timestamp } from "../../functions/src/firebase" +import { testAuth, testDb } from "../testUtils" + +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 + replyTo?: string +} + +/** 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() + const eventId = 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 } : {}) + }) + } + await testDb.doc(`users/${uid}/draftTestimony/${id()}`).set({ + conferenceId, + billId: "H9901", + court: COURT, + ...(s.replyTo ? { replyTo: s.replyTo } : {}) + }) + await testDb.doc(`conferenceInputEvents/${eventId}`).set({ + court: COURT, + conferenceId, + testimonyId, + authorUid: uid, + change: "added", + after: { position: "passSomething", group: "individual", version: 1 }, + at: Timestamp.fromDate(ADDED_AT) + }) + return { uid, eventId, testimonyId } +} + +const email = async (eventId: string) => + (await testDb.doc(`emails/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 } = await seed(conferenceId, { + rep, + sen, + replyTo: "chosen@example.com" + }) + await sendConferenceLetters(NOW) + const sent = await email(eventId) + expect(sent).toMatchObject({ + to: [TEST_RECIPIENT], + cc: ["chosen@example.com"], + replyTo: "chosen@example.com", + 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(JSON.parse(sent!.headers["X-SMTPAPI"]).category).toEqual([ + "constituent-letters" + ]) + + const second = await sendConferenceLetters(NOW) + expect(second).toBe(0) + expect((await email(eventId))!.createdAt).toEqual(sent!.createdAt) + expect(uid).toBeTruthy() + }) + + it("replies to the reader's own address without an override", async () => { + const { eventId, uid } = await seed(conferenceId, { rep }) + await sendConferenceLetters(NOW) + const sent = await email(eventId) + // The auth emulator lowercases addresses. + const own = `${uid}@example.com`.toLowerCase() + expect(sent!.replyTo).toBe(own) + expect(sent!.cc).toEqual([own]) + }) + + 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") + }) + + 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 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 = id() + await testDb.doc(`conferenceInputEvents/${secondId}`).set({ + court: COURT, + conferenceId, + testimonyId: first.testimonyId, + authorUid: first.uid, + change: "added", + after: { position: "passSomething", group: "individual", version: 2 }, + at: Timestamp.fromDate(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() + }) +}) diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts index 2287727b5..e7cb117b9 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, @@ -574,6 +576,25 @@ describe("publishTestimony", () => { expect(bill.latestTestimonyId).toBeUndefined() }) + it("Keeps a reply-to address on the private draft, never on the publication", async () => { + const { draftId: conferenceDraftId } = await createConferenceDraft( + uid, + billId, + conferenceId, + "passSenateVersion" + ) + await updateDoc(refs.draftTestimony(uid, conferenceDraftId), { + replyTo: "reader.work@example.org" + }) + const res = await publishTestimony({ draftId: conferenceDraftId }) + + const draft = await getDoc(refs.draftTestimony(uid, conferenceDraftId)) + expect(draft.data()?.replyTo).toBe("reader.work@example.org") + const publication = await getPublication(uid, res.data.publicationId) + expect(publication).not.toHaveProperty("replyTo") + expect(JSON.stringify(publication)).not.toContain("reader.work") + }) + it("Keeps bill and conference testimony on the same bill apart", async () => { const resA = await publishTestimony({ draftId }) const { draftId: conferenceDraftId } = await createConferenceDraft( @@ -1401,6 +1422,221 @@ describe("publishTestimony", () => { }) }) +describe("conference weekly update", () => { + 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 } + } + + it("compiles one update from a week of changes", async () => { + const conferenceId = await createConference(billId) + 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()).end.plus({ weeks: 1, hours: 1 }) + const weekStart = weekWindow(now.toJSDate()).start.toISODate() + const path = `/conferenceUpdates/${currentGeneralCourt}_${conferenceId}_${weekStart}` + await compileWeeklyUpdates(now.toJSDate()) + + const update = (await testDb.doc(path).get()).data() as any + 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 leaves the update as it was. + await compileWeeklyUpdates(now.toJSDate()) + expect((await testDb.doc(path).get()).data()!.createdAt).toEqual( + update.createdAt + ) + }, 60000) + + describe("sending", () => { + const env = { ...process.env } + afterEach(() => { + process.env = { ...env } + }) + + /** 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()) { + const memberCode = `T${nanoid(6)}` + await testDb + .doc(`/generalCourts/${currentGeneralCourt}/members/${memberCode}`) + .set({ + id: memberCode, + content: { Name: `Test Member${i}`, EmailAddress: address } + }) + conferees.push({ + memberCode, + branch: i % 2 ? ("House" as const) : ("Senate" as const), + chair: i < 2 + }) + } + const conferenceId = await createConference( + billId, + "appointed", + conferees + ) + await fileInput(signInUser1, billId, conferenceId, "passNothing") + await waitFor( + async () => expect((await eventsOf(conferenceId)).size).toBe(1), + { timeout: 20000 } + ) + const now = weekWindow(new Date()).end.plus({ weeks: 1, hours: 1 }) + const weekStart = weekWindow(now.toJSDate()).start.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.") + + const update = (await testDb.doc(`/conferenceUpdates/${id}`).get()).data() + expect(update!.delivery).toEqual({ + sentTo: [conferees[0].memberCode, conferees[2].memberCode], + skipped: [conferees[1].memberCode] + }) + + 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("stores the update without an email when no conferee has an address", async () => { + process.env.GCLOUD_PROJECT = "digital-testimony-prod" + const { conferees, now, id } = await conferenceWith([null]) + await compileWeeklyUpdates(now) + + expect(await emailOf(id)).toBeUndefined() + expect( + (await testDb.doc(`/conferenceUpdates/${id}`).get()).data()!.delivery + ).toEqual({ sentTo: [], skipped: [conferees[0].memberCode] }) + }, 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()).end.plus({ weeks: 2, hours: 1 }) + const weekStart = weekWindow(now.toJSDate()).start.toISODate() + await compileWeeklyUpdates(now.toJSDate()) + + expect( + ( + await testDb + .doc( + `/conferenceUpdates/${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("at", ">=", start.toJSDate()) + .where("at", "<", end.toJSDate()) + .get() + expect(inWeek.size).toBe(0) + }) +}) + describe("deleteTestimony", () => { async function getSignedInAdmin() { const adminUser = await signInTestAdmin() @@ -1621,7 +1857,15 @@ async function createDraftAttachment(uid: string, id: string, content: string) { /** A conference whose canonical bill is `billId`, with its counts zeroed. It * takes testimony only while `status` is appointed. */ -async function createConference(billId: string, status = "appointed") { +async function createConference( + billId: string, + status = "appointed", + conferees: { + memberCode: string + branch: "House" | "Senate" + chair?: boolean + }[] = [] +) { const id = `conference-${nanoid()}`.toLowerCase() await testDb .doc(`/generalCourts/${currentGeneralCourt}/conferences/${id}`) @@ -1632,7 +1876,7 @@ async function createConference(billId: string, status = "appointed") { senateBillId: billId, houseBillId: "H1", canonicalBillId: billId, - conferees: [], + conferees, status, hearingIds: [], differences: [],