From c1e96464ab6a843ac1d5a716efd5947f1f79a521 Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 12:49:52 -0500
Subject: [PATCH 01/17] Send conference letters and updates from their own
subdomains
Letters to legislators now come from noreply@letters.mapletestimony.org
and the weekly update to conferees from noreply@updates.mapletestimony.org,
on dev and prod, instead of notifications@. Each stream keeps its own
sender reputation, apart from the other and from the digests on the main
domain. Merge only after SendGrid has verified both subdomains, since it
rejects mail from a sender domain it has not authenticated.
Not verified: a send from either address; the subdomains' DNS was not
live, so SendGrid has not verified either yet.
Co-Authored-By: Claude Opus 5.5
---
functions/.env.digital-testimony-dev | 6 +++++-
functions/.env.digital-testimony-prod | 6 +++++-
2 files changed, 10 insertions(+), 2 deletions(-)
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
From 76426b691a3aabb48b8db0234c5631caddcf020e Mon Sep 17 00:00:00 2001
From: violet
Date: Wed, 11 Nov 2026 12:56:15 -0500
Subject: [PATCH 02/17] Compile each conference's weekly update from its input
A trigger on published testimony records each add, edit and withdrawal of
conference input in conferenceInputEvents, kept apart from
notificationEvents so the digest never sees them. On Mondays at 8:00
Eastern a job reads the previous Monday-to-Sunday window of events for the
net change, reads the conference's live input for where it stands, renders
the email HTML, and creates conferenceUpdates/{court}_{id}_{week}. A week
with no change stores nothing, and a second run leaves the first update as
it was. All conference input counts; an admin deletion counts as a
withdrawal. The update has no number and no unsubscribe line, and follows
the designer's mockup without its open-questions section and form.
The conference stance wording moves to shared/conferences so the digest
and the update use one copy.
Closes hyphacoop/maple#137
Verified: functions jest and the conference component tests (119) pass;
the integration cases "conference weekly update" pass on a throwaway
emulator with the opt-in removed: the trigger, a callable edit, an admin
deletion, the job, an idempotent rerun and an empty week; functions and
website type checks pass apart from untracked scratch scripts under
scripts/firebase-admin.
Not verified: the HTML was laid beside the mockup before two lines of its
wording changed (the footer, and the note under "Read the testimony"); how
it renders in real mail clients; a full run of the integration file; the
job on a deployed schedule. "Read the testimony" links the conference page
without a total because Public Input has no date filter.
Co-Authored-By: Claude Sonnet 5.5
---
.../conferenceUpdates/compileWeeklyUpdates.ts | 122 ++++++++++++
functions/src/conferenceUpdates/index.ts | 2 +
.../src/conferenceUpdates/inputEvents.test.ts | 73 +++++++
.../src/conferenceUpdates/inputEvents.ts | 68 +++++++
functions/src/conferenceUpdates/types.ts | 97 ++++++++++
.../conferenceUpdates/weeklyUpdate.test.ts | 172 +++++++++++++++++
.../src/conferenceUpdates/weeklyUpdate.ts | 179 ++++++++++++++++++
.../email/conferenceUpdateEmail.handlebars | 77 ++++++++
functions/src/index.ts | 4 +
.../deliverNotifications.test.ts | 2 +-
.../src/notifications/deliverNotifications.ts | 2 +-
functions/src/notifications/emailTypes.ts | 32 +++-
functions/src/shared/conferences.ts | 8 +
.../NotificationDigestEmail.stories.tsx | 2 +-
tests/integration/testimony.test.ts | 118 ++++++++++++
15 files changed, 946 insertions(+), 12 deletions(-)
create mode 100644 functions/src/conferenceUpdates/compileWeeklyUpdates.ts
create mode 100644 functions/src/conferenceUpdates/index.ts
create mode 100644 functions/src/conferenceUpdates/inputEvents.test.ts
create mode 100644 functions/src/conferenceUpdates/inputEvents.ts
create mode 100644 functions/src/conferenceUpdates/types.ts
create mode 100644 functions/src/conferenceUpdates/weeklyUpdate.test.ts
create mode 100644 functions/src/conferenceUpdates/weeklyUpdate.ts
create mode 100644 functions/src/email/conferenceUpdateEmail.handlebars
diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
new file mode 100644
index 000000000..77d3fff60
--- /dev/null
+++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
@@ -0,0 +1,122 @@
+import { onSchedule } from "firebase-functions/v2/scheduler"
+import * as logger from "firebase-functions/logger"
+import { Conference } from "../conferences/types"
+import { db, Timestamp } from "../firebase"
+import { timeZone } from "../malegislature"
+import { splitMemberName } from "../shared/names"
+import { takesTestimony } from "../shared/conferences"
+import { Testimony } from "../testimony/types"
+import { stateOf } from "./inputEvents"
+import { ConferenceInputEvent, ConferenceUpdate } from "./types"
+import {
+ buildConferenceUpdateEmail,
+ renderConferenceUpdate,
+ summarize,
+ tally,
+ weekWindow
+} from "./weeklyUpdate"
+
+const ALREADY_EXISTS = 6
+
+/** "Sen. Crighton" or "Rep. Peisch", for a header's To line. */
+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
+ return {
+ 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 html = renderConferenceUpdate(
+ buildConferenceUpdateEmail({
+ conference,
+ conferees: await Promise.all(
+ conference.conferees.map(c => conferee(court, c))
+ ),
+ standing,
+ changes,
+ start,
+ now
+ })
+ )
+ const update: ConferenceUpdate = {
+ court,
+ conferenceId: id,
+ weekStart: Timestamp.fromDate(start.toJSDate()),
+ standing,
+ changes,
+ html,
+ createdAt: Timestamp.now()
+ }
+ try {
+ await db
+ .collection("/conferenceUpdates")
+ .doc(`${court}_${id}_${start.toISODate()}`)
+ .create(update)
+ 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..25b3b0361
--- /dev/null
+++ b/functions/src/conferenceUpdates/types.ts
@@ -0,0 +1,97 @@
+import {
+ InstanceOf,
+ Literal as L,
+ Number,
+ Optional,
+ Record,
+ Static,
+ String,
+ Union
+} from "runtypes"
+import { Role } from "../auth/types"
+import { Timestamp } from "../firebase"
+import { ConferencePosition } from "../testimony/types"
+
+/** The site's three kinds of author, as Public Input groups them. Mirrors
+ * `accountTypeOf` in components/conference/input/accounts.ts. */
+export type AuthorGroup = Static
+export const AuthorGroup = Union(
+ L("individual"),
+ L("organization"),
+ L("government")
+)
+export const authorGroups = AuthorGroup.alternatives.map(a => a.value)
+
+export const authorGroupOf = (role: Role): AuthorGroup =>
+ role === "organization"
+ ? "organization"
+ : role === "legislator"
+ ? "government"
+ : "individual"
+
+/** How many inputs, in all and by author group and position. In a change it
+ * is a signed difference. */
+export type Counts = Static
+export const Counts = Record({
+ total: Number,
+ byGroup: Record({
+ individual: Number,
+ organization: Number,
+ government: Number
+ }),
+ byPosition: Record({
+ passSomething: Number,
+ passSenateVersion: Number,
+ passHouseVersion: Number,
+ passNothing: Number
+ })
+})
+
+/** What a publication contributes to the update: the figures it is counted
+ * under. */
+export type InputState = Static
+export const InputState = Record({
+ position: ConferencePosition,
+ group: AuthorGroup,
+ version: Number
+})
+
+/** One conference input appearing, changing or going. Written by
+ * `recordConferenceInput`; 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
+})
+
+/** 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,
+ 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..a4affbbf0
--- /dev/null
+++ b/functions/src/conferenceUpdates/weeklyUpdate.test.ts
@@ -0,0 +1,172 @@
+import {
+ buildConferenceUpdateEmail,
+ renderConferenceUpdate,
+ 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. Sam Sen", chair: true },
+ { name: "Rep. Pat Rep", chair: false }
+ ],
+ standing,
+ changes,
+ start: weekWindow(new Date("2026-09-28T12:00:00Z")).start,
+ now: new Date("2026-09-28T12:00:00Z")
+ })
+
+ it("states the week, the bills and the figures", () => {
+ expect(data).toMatchObject({
+ bills: "S.2581 / H.5366",
+ weekLabel: "September 21–27, 2026",
+ sentLabel: "Monday, September 28, 2026",
+ total: 3,
+ totalDelta: "▲ 2"
+ })
+ expect(data.groups.map(g => [g.label, g.count, g.delta])).toEqual([
+ ["Individuals", 2, "▲ 1"],
+ ["Organizations", 1, "▲ 1"],
+ ["Government offices", 0, "—"]
+ ])
+ expect(data.changes).toContain("2 inputs added.")
+ expect(data.changes).toContain("Counted input went from 1 to 3.")
+ expect(data.changes).toContain("Don't pass anything: 0 → 1.")
+ })
+
+ it("renders the conferees, figures and a link to the page", () => {
+ const html = renderConferenceUpdate(data)
+ expect(html).toContain("Sen. Sam Sen (chair), Rep. Pat Rep")
+ expect(html).toContain("S.2581 / H.5366 — Phone-Free Schools")
+ expect(html).toContain(
+ 'href="https://mapletestimony.org/conferences/194/phone-free-schools"'
+ )
+ expect(html).toContain("Pass the Senate version")
+ expect(html).toContain("info@mapletestimony.org")
+ })
+
+ it("leaves out unsubscribing and the open questions", () => {
+ const html = renderConferenceUpdate(data).toLowerCase()
+ expect(html).not.toContain("unsubscribe")
+ expect(html).not.toContain("open questions")
+ })
+
+ it("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..bc849422d
--- /dev/null
+++ b/functions/src/conferenceUpdates/weeklyUpdate.ts
@@ -0,0 +1,179 @@
+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
+ }
+}
+
+let template: HandlebarsTemplateDelegate | undefined
+
+export const renderConferenceUpdate = (data: ConferenceUpdateEmail) =>
+ (template ??= handlebars.compile(
+ fs.readFileSync(path.join(__dirname, EMAIL_TEMPLATE_PATH), "utf8")
+ ))(data)
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
+
+
+
+
+
+
+
+ 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
+
+
+
+ Total testimonies
+ {{total}}
+ {{totalDelta}} this week
+
+ {{#each groups}}
+
+ {{label}}
+ {{count}}
+ {{delta}}
+
+ {{/each}}
+
+
+
+ Position on the bills
+
+
+ {{#each positions}}
+ {{count}}
+ {{/each}}
+
+
+
+ {{#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/index.ts b/functions/src/index.ts
index 9d41674df..76783508c 100644
--- a/functions/src/index.ts
+++ b/functions/src/index.ts
@@ -63,6 +63,10 @@ export {
unfollowUserV2,
getFollowersV2
} from "./subscriptions"
+export {
+ compileConferenceUpdates,
+ recordConferenceInput
+} from "./conferenceUpdates"
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 1a8d7ef83..68c5cb988 100644
--- a/functions/src/notifications/deliverNotifications.test.ts
+++ b/functions/src/notifications/deliverNotifications.test.ts
@@ -1,5 +1,5 @@
import { buildDigestData } from "./deliverNotifications"
-import { CONFERENCE_STANCES } from "./emailTypes"
+import { CONFERENCE_STANCES } from "../shared/conferences"
import { Timestamp } from "../firebase"
// Mock firebase module
diff --git a/functions/src/notifications/deliverNotifications.ts b/functions/src/notifications/deliverNotifications.ts
index 239d731ac..9a5e26467 100644
--- a/functions/src/notifications/deliverNotifications.ts
+++ b/functions/src/notifications/deliverNotifications.ts
@@ -13,7 +13,6 @@ import {
BallotQuestionDigest,
BillDigest,
BillResult,
- CONFERENCE_STANCES,
ConferenceDigest,
NotificationEmailDigest,
Position,
@@ -22,6 +21,7 @@ import {
import { prepareHandlebars } from "../email/handlebarsHelpers"
import { Frequency } from "../auth/types"
import { ConferencePosition } from "../testimony/types"
+import { CONFERENCE_STANCES } from "../shared/conferences"
const PROFILE_BATCH_SIZE = 50
const NUM_BILLS_TO_DISPLAY = 4
diff --git a/functions/src/notifications/emailTypes.ts b/functions/src/notifications/emailTypes.ts
index 1d0622232..7a3d3837d 100644
--- a/functions/src/notifications/emailTypes.ts
+++ b/functions/src/notifications/emailTypes.ts
@@ -1,13 +1,4 @@
import { Frequency } from "../auth/types"
-import type { ConferencePosition } from "../testimony/types"
-
-// The site's wording, conferences input.position.*.long; a test holds them equal.
-export const CONFERENCE_STANCES: Record = {
- passSomething: "Please pass something",
- passSenateVersion: "Pass the Senate version",
- passHouseVersion: "Pass the House version",
- passNothing: "Don't pass anything"
-}
export type BillDigest = {
billId: string
@@ -62,3 +53,26 @@ export type NotificationEmailDigest = {
conferences: ConferenceDigest[] // cap of 4
numConferencesWithNewTestimony: number
}
+
+type ConferenceUpdateRow = {
+ label: string
+ count: number
+ /** "▲ 3", "▼ 1" or "—". */
+ delta: string
+}
+export type ConferenceUpdateEmail = {
+ /** "S.2581 / H.5366" */
+ bills: string
+ title: string
+ conferenceUrl: string
+ /** "September 21–27, 2026" */
+ weekLabel: string
+ sentLabel: string
+ conferees: { name: string; chair: boolean }[]
+ total: number
+ totalDelta: string
+ groups: ConferenceUpdateRow[]
+ /** Each with its share of the bar, in whole percent, and its colour. */
+ positions: (ConferenceUpdateRow & { width: number; color: string })[]
+ changes: string[]
+}
diff --git a/functions/src/shared/conferences.ts b/functions/src/shared/conferences.ts
index 97954b8d1..8428bbca1 100644
--- a/functions/src/shared/conferences.ts
+++ b/functions/src/shared/conferences.ts
@@ -1,4 +1,5 @@
import type { Conference } from "../conferences/types"
+import type { ConferencePosition } from "../testimony/types"
/** Once the report is filed, the committee has decided: each question says
* how the law resolved it. */
@@ -21,3 +22,10 @@ type Named = { name?: string | null; title?: string | null }
/** The short name where there is one, else the act title. */
export const conferenceName = (c: Named) =>
c.name?.trim() || c.title?.trim() || ""
+// The site's wording, conferences input.position.*.long; a test holds them equal.
+export const CONFERENCE_STANCES: Record = {
+ passSomething: "Please pass something",
+ passSenateVersion: "Pass the Senate version",
+ passHouseVersion: "Pass the House version",
+ passNothing: "Don't pass anything"
+}
diff --git a/stories/emailTemplates/NotificationDigestEmail.stories.tsx b/stories/emailTemplates/NotificationDigestEmail.stories.tsx
index 96355cd3d..01454d787 100644
--- a/stories/emailTemplates/NotificationDigestEmail.stories.tsx
+++ b/stories/emailTemplates/NotificationDigestEmail.stories.tsx
@@ -4,11 +4,11 @@ import { EmailTemplateRenderer } from "./EmailTemplateRenderer"
import {
BillDigest,
BillResult,
- CONFERENCE_STANCES,
ConferenceDigest,
NotificationEmailDigest,
UserDigest
} from "functions/src/notifications/emailTypes"
+import { CONFERENCE_STANCES } from "functions/src/shared/conferences"
import { Frequency } from "components/auth"
const meta: Meta = {
diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts
index 2a4609ec0..490fd2ba0 100644
--- a/tests/integration/testimony.test.ts
+++ b/tests/integration/testimony.test.ts
@@ -10,6 +10,8 @@ import { auth, firestore, functions, storage } from "../../components/firebase"
import { terminateFirebase, testDb, testStorage } from "../testUtils"
import { script as backfillTestimonyScope } from "../../scripts/firebase-admin/backfillTestimonyScope"
import { resolveReport } from "components/db"
+import { compileWeeklyUpdates } from "functions/src/conferenceUpdates/compileWeeklyUpdates"
+import { weekWindow } from "functions/src/conferenceUpdates/weeklyUpdate"
import {
createFakeBill,
createNewReport,
@@ -1402,6 +1404,122 @@ 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)
+
+ 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()
From 105b5129c781d3e01b77f0b28911778863015c91 Mon Sep 17 00:00:00 2001
From: violet
Date: Wed, 11 Nov 2026 13:37:05 -0500
Subject: [PATCH 03/17] Email each conference's weekly update to its conferees
The job that compiles a conference's update now also queues its email, in
the same batch that stores the update, so a second run in the week finds the
update and sends nothing. The email goes out from MAPLE with replies to
info@ and a SendGrid category for the update, set in the document's
top-level headers: the send-email extension ignores message.headers. The
update records which conferees it went to and which were skipped for having
no email address.
Outside production no email reaches a legislator: the To line becomes
LETTER_TEST_RECIPIENT and the subject names who it would have gone to. With
no test address set, nothing is sent. The sending address comes from
CONFEREE_UPDATE_FROM and defaults to notifications@ until the updates
subdomain is authenticated.
Closes hyphacoop/maple#138
Verified: functions jest (176); integration "conference weekly update" on a
throwaway emulator: swapped recipient and subject prefix, headers, replyTo,
no second email on rerun, real addresses in production mode, skipped
conferee without an address, update stored with no email when none has one;
that extension 0.1.36 reads headers from the top level of the document, from
its source; a run on the local emulator with the send-email extension sent
an update through SendGrid that reached the test inbox.
Not verified: the SendGrid category arriving; the sending address on the
updates subdomain, which waits on hyphacoop/maple#140.
Co-Authored-By: Claude Sonnet 5.5
---
functions/.env.demo-dtp | 3 +
.../conferenceUpdates/compileWeeklyUpdates.ts | 73 ++++++++++----
functions/src/conferenceUpdates/types.ts | 10 ++
.../conferenceUpdates/weeklyUpdate.test.ts | 7 ++
.../src/conferenceUpdates/weeklyUpdate.ts | 6 ++
functions/src/email/recipients.test.ts | 37 ++++++++
functions/src/email/recipients.ts | 20 ++++
tests/integration/testimony.test.ts | 95 +++++++++++++++++++
8 files changed, 235 insertions(+), 16 deletions(-)
create mode 100644 functions/src/email/recipients.test.ts
create mode 100644 functions/src/email/recipients.ts
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/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
index 77d3fff60..19a161522 100644
--- a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
+++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
@@ -3,6 +3,8 @@ 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 { convertHtmlToText } from "../notifications/helpers"
import { splitMemberName } from "../shared/names"
import { takesTestimony } from "../shared/conferences"
import { Testimony } from "../testimony/types"
@@ -10,6 +12,7 @@ import { stateOf } from "./inputEvents"
import { ConferenceInputEvent, ConferenceUpdate } from "./types"
import {
buildConferenceUpdateEmail,
+ conferenceUpdateSubject,
renderConferenceUpdate,
summarize,
tally,
@@ -17,14 +20,20 @@ import {
} 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. */
+/** "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
}`,
@@ -75,18 +84,19 @@ export async function compileWeeklyUpdates(now = new Date()) {
)
const changes = summarize(conferenceEvents)
- const html = renderConferenceUpdate(
- buildConferenceUpdateEmail({
- conference,
- conferees: await Promise.all(
- conference.conferees.map(c => conferee(court, c))
- ),
- standing,
- changes,
- start,
- now
- })
+ 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,
@@ -94,13 +104,44 @@ export async function compileWeeklyUpdates(now = new Date()) {
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: convertHtmlToText(html),
+ html
+ },
+ createdAt: Timestamp.now()
+ })
+ } else {
+ logger.warn(`No recipients for the update to conference ${key}`)
+ }
try {
- await db
- .collection("/conferenceUpdates")
- .doc(`${court}_${id}_${start.toISODate()}`)
- .create(update)
+ await batch.commit()
stored++
} catch (e) {
if ((e as { code?: number }).code !== ALREADY_EXISTS) throw e
diff --git a/functions/src/conferenceUpdates/types.ts b/functions/src/conferenceUpdates/types.ts
index 25b3b0361..a47433450 100644
--- a/functions/src/conferenceUpdates/types.ts
+++ b/functions/src/conferenceUpdates/types.ts
@@ -1,6 +1,7 @@
import {
InstanceOf,
Literal as L,
+ Array,
Number,
Optional,
Record,
@@ -81,6 +82,14 @@ export const Changes = Record({
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
@@ -93,5 +102,6 @@ export const ConferenceUpdate = Record({
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
index a4affbbf0..1bec35b6e 100644
--- a/functions/src/conferenceUpdates/weeklyUpdate.test.ts
+++ b/functions/src/conferenceUpdates/weeklyUpdate.test.ts
@@ -1,5 +1,6 @@
import {
buildConferenceUpdateEmail,
+ conferenceUpdateSubject,
renderConferenceUpdate,
summarize,
tally,
@@ -131,6 +132,12 @@ describe("the update email", () => {
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",
diff --git a/functions/src/conferenceUpdates/weeklyUpdate.ts b/functions/src/conferenceUpdates/weeklyUpdate.ts
index bc849422d..1117a4a91 100644
--- a/functions/src/conferenceUpdates/weeklyUpdate.ts
+++ b/functions/src/conferenceUpdates/weeklyUpdate.ts
@@ -171,6 +171,12 @@ export function buildConferenceUpdateEmail({
}
}
+export const conferenceUpdateSubject = ({
+ title,
+ weekLabel
+}: Pick) =>
+ `Public input on ${title}: ${weekLabel}`
+
let template: HandlebarsTemplateDelegate | undefined
export const renderConferenceUpdate = (data: ConferenceUpdateEmail) =>
diff --git a/functions/src/email/recipients.test.ts b/functions/src/email/recipients.test.ts
new file mode 100644
index 000000000..711a0518a
--- /dev/null
+++ b/functions/src/email/recipients.test.ts
@@ -0,0 +1,37 @@
+import { resolveRecipients } from "./recipients"
+
+describe("resolveRecipients", () => {
+ const env = { ...process.env }
+ afterEach(() => {
+ process.env = { ...env }
+ })
+
+ it("sends to the real addresses in production", () => {
+ process.env.GCLOUD_PROJECT = "digital-testimony-prod"
+ expect(resolveRecipients(["a@x.org", "b@x.org"])).toEqual({
+ to: ["a@x.org", "b@x.org"],
+ subjectPrefix: ""
+ })
+ })
+
+ it("swaps in the test address elsewhere and names who it would have reached", () => {
+ process.env.GCLOUD_PROJECT = "digital-testimony-dev"
+ process.env.LETTER_TEST_RECIPIENT = "test@x.org"
+ expect(resolveRecipients(["a@x.org", "b@x.org"])).toEqual({
+ to: ["test@x.org"],
+ subjectPrefix: "[dev] would have gone to: a@x.org, b@x.org — "
+ })
+ })
+
+ it("sends nothing elsewhere when no test address is set", () => {
+ process.env.GCLOUD_PROJECT = "demo-dtp"
+ delete process.env.LETTER_TEST_RECIPIENT
+ expect(resolveRecipients(["a@x.org"]).to).toEqual([])
+ })
+
+ it("sends nothing elsewhere when there is no one to stand in for", () => {
+ process.env.GCLOUD_PROJECT = "digital-testimony-dev"
+ process.env.LETTER_TEST_RECIPIENT = "test@x.org"
+ expect(resolveRecipients([]).to).toEqual([])
+ })
+})
diff --git a/functions/src/email/recipients.ts b/functions/src/email/recipients.ts
new file mode 100644
index 000000000..8617ebb8c
--- /dev/null
+++ b/functions/src/email/recipients.ts
@@ -0,0 +1,20 @@
+const PRODUCTION_PROJECT = "digital-testimony-prod"
+
+const isProduction = () => process.env.GCLOUD_PROJECT === PRODUCTION_PROJECT
+
+/** Who an email to real people actually goes to. Outside production no email
+ * reaches them: the To line becomes the one configured test address
+ * (`LETTER_TEST_RECIPIENT`), and the subject says who it would have gone to,
+ * so a test also checks routing. With no test address configured, or no one
+ * to stand in for, nothing is sent. */
+export const resolveRecipients = (
+ real: string[]
+): { to: string[]; subjectPrefix: string } => {
+ if (!real.length) return { to: [], subjectPrefix: "" }
+ if (isProduction()) return { to: real, subjectPrefix: "" }
+ const test = process.env.LETTER_TEST_RECIPIENT
+ return {
+ to: test ? [test] : [],
+ subjectPrefix: `[dev] would have gone to: ${real.join(", ")} — `
+ }
+}
diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts
index 490fd2ba0..f081dbade 100644
--- a/tests/integration/testimony.test.ts
+++ b/tests/integration/testimony.test.ts
@@ -1486,6 +1486,101 @@ describe("conference weekly update", () => {
)
}, 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, { 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")
From 9b6a3971f52fd50f8d3d3d4f9426bd6cc855b357 Mon Sep 17 00:00:00 2001
From: violet
Date: Wed, 11 Nov 2026 14:42:31 -0500
Subject: [PATCH 04/17] Show readers where their conference input goes, and
preview their letter
The review step before posting new conference input or a cosign now says
where it goes (public on MAPLE, emailed to the reader's own rep and
senator, counted in the weekly update), shows the letter as it will be
sent with a Cc row, and lets the reader give a different reply-to for that
letter alone. A cosign gains a review step after writing; a cosign without
a note gets the mockup's "add a line" banner. A reader whose profile has no
rep or senator is told MAPLE has no one to notify, and sees no letter.
The reply-to is kept on the reader's private draft and never copied to the
publication. The letter text comes from one pure renderer under
functions/src, so the daily sender can mail exactly what was previewed.
Closes hyphacoop/maple#142
Verified: yarn test:website, yarn test, check-types, lint on the changed
files and yarn build:dev pass. A throwaway-emulator integration test shows the reply-to stays
on the draft and is absent from the publication. The review blocks were laid
beside the mockup's Review frame at panel width.
Not verified: phone width, and the review flow in the real panel with a
signed-in reader on the emulator. The cosign screens were not laid beside
their mockup frames.
Co-Authored-By: Claude Sonnet 5.5
---
.../ConferenceDetails.switching.test.tsx | 1 +
components/conference/ConferenceDetails.tsx | 23 +-
.../conference/input/CosignForm.test.tsx | 119 +++-
components/conference/input/CosignForm.tsx | 74 ++-
.../conference/input/InputForm.test.tsx | 89 ++-
components/conference/input/InputForm.tsx | 65 +--
components/conference/input/Review.tsx | 518 ++++++++++++++++++
components/conference/input/icons.tsx | 20 +
components/db/testimony/types.ts | 5 +-
components/db/testimony/useEditTestimony.ts | 6 +-
.../src/conferenceLetters/letter.test.ts | 82 +++
functions/src/conferenceLetters/letter.ts | 98 ++++
functions/src/testimony/types.ts | 5 +-
public/locales/en/conferences.json | 36 +-
tests/integration/testimony.test.ts | 19 +
15 files changed, 1097 insertions(+), 63 deletions(-)
create mode 100644 components/conference/input/Review.tsx
create mode 100644 functions/src/conferenceLetters/letter.test.ts
create mode 100644 functions/src/conferenceLetters/letter.ts
diff --git a/components/conference/ConferenceDetails.switching.test.tsx b/components/conference/ConferenceDetails.switching.test.tsx
index 057697e70..d0faac6fc 100644
--- a/components/conference/ConferenceDetails.switching.test.tsx
+++ b/components/conference/ConferenceDetails.switching.test.tsx
@@ -59,6 +59,7 @@ jest.mock("../auth", () => ({
const mounted: string[] = []
jest.mock("../db", () => ({
useProfile: () => ({ profile: {} }),
+ useMember: () => ({ member: undefined }),
hasDraftChanged: () => false,
useConferenceTestimony: () => ({ items: [], loading: false }),
useEditConferenceTestimony: (
diff --git a/components/conference/ConferenceDetails.tsx b/components/conference/ConferenceDetails.tsx
index 80e116735..79bf8a510 100644
--- a/components/conference/ConferenceDetails.tsx
+++ b/components/conference/ConferenceDetails.tsx
@@ -37,6 +37,7 @@ import { useAppDispatch } from "../hooks"
import { maple, siteUrl } from "../links"
import { 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"
@@ -49,7 +50,12 @@ import { X } from "./input/icons"
import { jumpToSection } from "./navbar"
import { OverviewLink } from "./OverviewLink"
import { Notice, PageColumn, Title } from "./styles"
-import { conferenceName, ConferencePageProps, ConferenceSummary } from "./types"
+import {
+ conferenceName,
+ ConferencePageProps,
+ ConferenceSummary,
+ ConfereeView
+} from "./types"
/** The mockup's column, widened to 1320 once the rail sits beside it. */
const Page = styled(PageColumn)`
@@ -588,6 +594,7 @@ function Details({
void
}) {
const { t } = useTranslation("conferences")
+ const initials = useMemo(
+ () => conferees.map(c => initialsOf(c.name)),
+ [conferees]
+ )
const edit = useEditConferenceTestimony(
uid,
conference.court,
@@ -661,7 +674,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 (
@@ -682,6 +695,9 @@ function InputPanel({
key={cosigning.id}
edit={edit}
parent={cosigning}
+ court={conference.court}
+ subject={subject}
+ conferees={initials}
step={cosignStep}
onStep={onCosignStep}
onCancel={onClose}
@@ -689,7 +705,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({
-
- {t("input.preview_label")}{" "}
-
- {t("input.preview_on", { subject })}
-
-
-
-
-
+
{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")}{" "}
+
+ {t("review.add_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 &&
{t("review.public_input_aside")} }
+
+
+
+
+
+ {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 ? (
+
+ {t("review.reply_to_label")}
+
+ {
+ setText(e.target.value)
+ setInvalid(false)
+ }}
+ onKeyDown={e => e.key === "Enter" && done()}
+ />
+
+ {t("review.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 e4bf3defe..8075157ed 100644
--- a/components/conference/input/icons.tsx
+++ b/components/conference/input/icons.tsx
@@ -27,6 +27,26 @@ function Icon({
type Props = { size?: number; strokeWidth?: number; fill?: string }
+export const House = (p: Props) => (
+
+
+
+
+)
+export const Mail = (p: Props) => (
+
+
+
+
+)
+export const CalendarDays = (p: Props) => (
+
+
+
+
+
+
+)
export const Megaphone = (p: Props) => (
diff --git a/components/db/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/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/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/public/locales/en/conferences.json b/public/locales/en/conferences.json
index af4cd1111..2620be262 100644
--- a/public/locales/en/conferences.json
+++ b/public/locales/en/conferences.json
@@ -211,7 +211,6 @@
"review": "Review and Post",
"review_title": "Review and Post",
"preview_label": "Preview",
- "preview_on": "on {{subject}}",
"rules_label": "Before You Post",
"rule_public": "Posting is public and stays attached to your account.",
"rule_revise": "You can revise it later; earlier versions stay on the record.",
@@ -291,5 +290,40 @@
"follow": "Follow",
"following": "Following",
"unfollow": "Unfollow"
+ },
+ "review": {
+ "goes_input": "Where your input goes",
+ "goes_cosign": "Where your cosign goes",
+ "public_input": "Posted publicly on MAPLE with your name.",
+ "public_input_aside": "Others can cosign your input. When they do, it’s sent to their legislators with your name attached.",
+ "public_cosign": "Posted publicly on MAPLE as your cosign of {{name}}’s input. It counts as your position on this bill.",
+ "emailed": "Emailed to your state representative and state senator:",
+ "emailed_cosign": "The email says clearly that you cosigned {{name}}’s input.",
+ "emailed_none": "MAPLE doesn’t know who your state representative and state senator are, so it can’t email them. Your input is only counted for the committee.",
+ "rep": "Rep. {{name}}",
+ "sen": "Sen. {{name}}",
+ "and": " and ",
+ "weekly_input": "Included in MAPLE’s weekly update to the conferees.",
+ "weekly_cosign": "Counted in MAPLE’s weekly update to the conferees.",
+ "conferees": "This committee’s conferees",
+ "conferees_label": "The conferees",
+ "email_label": "Your email",
+ "email_note": "Your email is included in the message so your legislators can reply to you directly. Replies go to you, not to MAPLE.",
+ "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/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts
index f081dbade..a3b4bd234 100644
--- a/tests/integration/testimony.test.ts
+++ b/tests/integration/testimony.test.ts
@@ -577,6 +577,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(
From 2f87b6fff2c00c047a337d994f5252492419beb8 Mon Sep 17 00:00:00 2001
From: violet
Date: Wed, 11 Nov 2026 16:14:37 -0500
Subject: [PATCH 05/17] Mail each new conference input to the reader's rep and
senator
A daily job reads the previous Eastern day's newly added conference input
and writes one email per letter, using the same renderer as the review
step's preview, to whichever of the reader's rep and senator has an
address. The reader is copied and replies go to the address they chose for
that letter, else their verified one. The email is created under the input
event's id, so a rerun sends nothing twice, and an input posted twice in a
day sends only its last. Inputs that are not mailed are recorded with the
reason. Outside production recipients are swapped for the test address.
Tracking headers go in the email document's top-level headers: the
send-email extension ignores message.headers.
Closes hyphacoop/maple#143
Verified: unit tests for the day window, From header and email document;
an integration test on a throwaway emulator covering a sent letter, rerun,
reply-to override, a legislator without an address, each skip reason, and
a repost; input posted through the review step on the local emulator
was mailed through the send-email extension and SendGrid and reached the
test inbox, copying the reader.
Not verified: the letter's look beside the mockup, and DKIM and DMARC from
the letters subdomain.
Co-Authored-By: Claude Sonnet 5.5
---
.github/workflows/repo-checks.yml | 1 +
functions/src/conferenceLetters/index.ts | 1 +
.../src/conferenceLetters/letters.test.ts | 90 ++++++++
functions/src/conferenceLetters/letters.ts | 99 +++++++++
.../src/conferenceLetters/sendLetters.ts | 208 ++++++++++++++++++
functions/src/index.ts | 1 +
tests/integration/conferenceLetters.test.ts | 178 +++++++++++++++
7 files changed, 578 insertions(+)
create mode 100644 functions/src/conferenceLetters/index.ts
create mode 100644 functions/src/conferenceLetters/letters.test.ts
create mode 100644 functions/src/conferenceLetters/letters.ts
create mode 100644 functions/src/conferenceLetters/sendLetters.ts
create mode 100644 tests/integration/conferenceLetters.test.ts
diff --git a/.github/workflows/repo-checks.yml b/.github/workflows/repo-checks.yml
index b183e37e1..0d4a75c39 100644
--- a/.github/workflows/repo-checks.yml
+++ b/.github/workflows/repo-checks.yml
@@ -97,6 +97,7 @@ jobs:
tests/integration/backfillTestimonyScope.test.ts
tests/integration/conferences.test.ts
tests/integration/conferenceFollowing.test.ts
+ tests/integration/conferenceLetters.test.ts
components/auth/hooks.test.ts
components/db/useUpcoming.test.ts
components/db/testimony
diff --git a/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/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts
new file mode 100644
index 000000000..ebf507a61
--- /dev/null
+++ b/functions/src/conferenceLetters/letters.test.ts
@@ -0,0 +1,90 @@
+import { renderLetter } from "./letter"
+import {
+ buildLetterEmail,
+ dayWindow,
+ letterFrom,
+ renderLetterHtml
+} 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("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..1d35ca12c
--- /dev/null
+++ b/functions/src/conferenceLetters/letters.ts
@@ -0,0 +1,99 @@
+import { DateTime } from "luxon"
+import { timeZone } from "../malegislature"
+import { convertHtmlToText } from "../notifications/helpers"
+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 `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: convertHtmlToText(html),
+ html
+ },
+ createdAt
+ }
+}
diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts
new file mode 100644
index 000000000..9ec96d98f
--- /dev/null
+++ b/functions/src/conferenceLetters/sendLetters.ts
@@ -0,0 +1,208 @@
+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
+ }
+}
+
+// As the site names an author (`nameOf`): an empty full name falls back too.
+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/index.ts b/functions/src/index.ts
index 76783508c..e04dda3cf 100644
--- a/functions/src/index.ts
+++ b/functions/src/index.ts
@@ -67,6 +67,7 @@ export {
compileConferenceUpdates,
recordConferenceInput
} from "./conferenceUpdates"
+export { mailConferenceLetters } from "./conferenceLetters"
export { scrapeElections } from "./legislators"
export { transcriptionV2 } from "./webhooks"
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()
+ })
+})
From 0f4e31890dead8c0c87c10a6928976327579673e Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 12:54:25 -0500
Subject: [PATCH 06/17] Write the letters' and weekly updates' plain text from
their content
The text part of each email was the HTML's textContent, which drops
line breaks: a letter's signature ran together ("Fake VioletHouse
district: 12th Suffolk...") and the update's figures ran into their
labels. Each is now rendered from the same data as its HTML, with a
line per signature line or figure and the cosigned input quoted.
Verified: letter and update unit tests (36) and functions tsc pass; a
letter and two weekly updates sent through the local emulator's
send-email extension and SendGrid arrived with readable text parts.
Co-Authored-By: Claude Opus 5.5
---
.../src/conferenceLetters/letters.test.ts | 32 ++++++++++++++++++-
functions/src/conferenceLetters/letters.ts | 18 +++++++++--
.../conferenceUpdates/compileWeeklyUpdates.ts | 4 +--
.../conferenceUpdates/weeklyUpdate.test.ts | 13 ++++++++
.../src/conferenceUpdates/weeklyUpdate.ts | 30 +++++++++++++++++
5 files changed, 92 insertions(+), 5 deletions(-)
diff --git a/functions/src/conferenceLetters/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts
index ebf507a61..f9bf73870 100644
--- a/functions/src/conferenceLetters/letters.test.ts
+++ b/functions/src/conferenceLetters/letters.test.ts
@@ -3,7 +3,8 @@ import {
buildLetterEmail,
dayWindow,
letterFrom,
- renderLetterHtml
+ renderLetterHtml,
+ renderLetterText
} from "./letters"
const letter = renderLetter({
@@ -56,6 +57,35 @@ describe("renderLetterHtml", () => {
})
})
+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,
diff --git a/functions/src/conferenceLetters/letters.ts b/functions/src/conferenceLetters/letters.ts
index 1d35ca12c..4f13f72e8 100644
--- a/functions/src/conferenceLetters/letters.ts
+++ b/functions/src/conferenceLetters/letters.ts
@@ -1,6 +1,5 @@
import { DateTime } from "luxon"
import { timeZone } from "../malegislature"
-import { convertHtmlToText } from "../notifications/helpers"
import { Letter } from "./letter"
/** Yesterday, Eastern: the day whose new input the daily job mails. */
@@ -55,6 +54,21 @@ export const renderLetterHtml = (letter: Letter) => {
.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. */
@@ -91,7 +105,7 @@ export const buildLetterEmail = ({
},
message: {
subject: subjectPrefix + letter.subject,
- text: convertHtmlToText(html),
+ text: renderLetterText(letter),
html
},
createdAt
diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
index 19a161522..bec39a2cd 100644
--- a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
+++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
@@ -4,7 +4,6 @@ import { Conference } from "../conferences/types"
import { db, Timestamp } from "../firebase"
import { timeZone } from "../malegislature"
import { resolveRecipients } from "../email/recipients"
-import { convertHtmlToText } from "../notifications/helpers"
import { splitMemberName } from "../shared/names"
import { takesTestimony } from "../shared/conferences"
import { Testimony } from "../testimony/types"
@@ -14,6 +13,7 @@ import {
buildConferenceUpdateEmail,
conferenceUpdateSubject,
renderConferenceUpdate,
+ renderConferenceUpdateText,
summarize,
tally,
weekWindow
@@ -132,7 +132,7 @@ export async function compileWeeklyUpdates(now = new Date()) {
},
message: {
subject: subjectPrefix + conferenceUpdateSubject(data),
- text: convertHtmlToText(html),
+ text: renderConferenceUpdateText(data),
html
},
createdAt: Timestamp.now()
diff --git a/functions/src/conferenceUpdates/weeklyUpdate.test.ts b/functions/src/conferenceUpdates/weeklyUpdate.test.ts
index 1bec35b6e..098f7afbc 100644
--- a/functions/src/conferenceUpdates/weeklyUpdate.test.ts
+++ b/functions/src/conferenceUpdates/weeklyUpdate.test.ts
@@ -2,6 +2,7 @@ import {
buildConferenceUpdateEmail,
conferenceUpdateSubject,
renderConferenceUpdate,
+ renderConferenceUpdateText,
summarize,
tally,
weekWindow
@@ -173,6 +174,18 @@ describe("the update email", () => {
expect(html).not.toContain("open questions")
})
+ it("says the same in its text part, a line per figure", () => {
+ const text = renderConferenceUpdateText(data)
+ expect(text).toContain("To: Sen. Sam Sen (chair), Rep. Pat Rep\n")
+ expect(text).toContain(
+ "Total testimonies: 3 (▲ 2 this week)\nIndividuals: 2 (▲ 1)\n"
+ )
+ expect(text).toContain("- 2 inputs added.\n")
+ expect(text).toContain(
+ "https://mapletestimony.org/conferences/194/phone-free-schools"
+ )
+ })
+
it("links the page without saying how many it holds", () => {
expect(renderConferenceUpdate(data)).not.toMatch(/Read all \d+/)
})
diff --git a/functions/src/conferenceUpdates/weeklyUpdate.ts b/functions/src/conferenceUpdates/weeklyUpdate.ts
index 1117a4a91..b75854d34 100644
--- a/functions/src/conferenceUpdates/weeklyUpdate.ts
+++ b/functions/src/conferenceUpdates/weeklyUpdate.ts
@@ -183,3 +183,33 @@ 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")
From 37ddefb1a2a0714034f182f0f770156318db23e8 Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 12:54:25 -0500
Subject: [PATCH 07/17] Run the conference mail jobs once against the local
emulators
The emulators never fire schedules, so a new admin script runs the
letters or weekly-update job as its next scheduled run would, or at a
given --now. It is local only, and mails LETTER_TEST_RECIPIENT as read
from the functions env files. The README says how to start the
emulators with the send-email extension, and the extension's local
secret file is ignored.
Verified: with the extension emulator and a SendGrid key, letters and
updates runs reached the test inbox; the only recipients were the test
address and the reader's reply-to, with legislators named only in the
subject; a bad --job is refused with the usual arguments error.
Co-Authored-By: Claude Opus 5.5
---
.gitignore | 2 +
scripts/README.md | 11 +++
scripts/firebase-admin/runConferenceMail.ts | 78 +++++++++++++++++++++
3 files changed, 91 insertions(+)
create mode 100644 scripts/firebase-admin/runConferenceMail.ts
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/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"
+ )
+}
From a07d1761ff01b42aaaece57b32f10a8d77fdb2a8 Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 15:03:16 -0500
Subject: [PATCH 08/17] Mail edits to conference input as updated letters
An edit to posted conference input now mails a new letter, its subject
marked "(updated)" unless the input was also first posted in the same
day's run; the preview shows the same subject while editing. The letter's
footer links the conference's page rather than nothing. The draft holding
the reply-to is found by court as well as conference, and a reply-to that
is not a single address is refused on the draft and ignored by the sender,
which falls back to the reader's verified email. The conference's name
comes from one helper in functions/src/shared.
Verified: functions jest (letter, letters, testimony); yarn test:website
conference tests (128); check-types; lint; integration conferenceLetters
and testimony on a throwaway emulator (edit mailed as update, post and
edit the same day mailed once as new, conference link).
Not verified: yarn build:dev. An edit whose original letter was skipped is
still marked "(updated)"; a same-day post and edit previews "(updated)" but
sends without it.
Co-Authored-By: Claude Opus 5.5
---
components/conference/ConferenceDetails.tsx | 2 +
.../conference/input/CosignForm.test.tsx | 1 +
components/conference/input/CosignForm.tsx | 4 ++
.../conference/input/InputForm.test.tsx | 2 +
components/conference/input/InputForm.tsx | 4 ++
components/conference/input/Review.tsx | 11 ++++-
.../src/conferenceLetters/letter.test.ts | 24 ++++++++--
functions/src/conferenceLetters/letter.ts | 30 +++++++------
.../src/conferenceLetters/letters.test.ts | 4 ++
.../src/conferenceLetters/sendLetters.ts | 45 ++++++++++++++-----
functions/src/testimony/types.ts | 5 ++-
tests/integration/conferenceLetters.test.ts | 37 ++++++++++++++-
12 files changed, 135 insertions(+), 34 deletions(-)
diff --git a/components/conference/ConferenceDetails.tsx b/components/conference/ConferenceDetails.tsx
index 79bf8a510..c38473691 100644
--- a/components/conference/ConferenceDetails.tsx
+++ b/components/conference/ConferenceDetails.tsx
@@ -696,6 +696,7 @@ function InputPanel({
edit={edit}
parent={cosigning}
court={conference.court}
+ conferenceId={conference.id}
subject={subject}
conferees={initials}
step={cosignStep}
@@ -706,6 +707,7 @@ function InputPanel({
) : (
onStep("write")} />}
render(
{
render(
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s)
-
/** The reply-to address and the way to give another for this letter only. */
export function ReplyTo({
own,
@@ -463,9 +462,11 @@ export function LetterPreview({ letter }: { letter: Letter }) {
* on the review step alone, so the legislators are read only there. */
export function LetterReview({
court,
+ conferenceId,
conferees,
author,
conference,
+ edited,
position,
content,
cosign,
@@ -473,10 +474,13 @@ export function LetterReview({
onChoose
}: {
court: number
+ conferenceId: string
/** Initials of the conferees, for the weekly update's row. */
conferees: string[]
author: string
conference: string
+ /** The reader is editing input they posted before. */
+ edited: boolean
position: ConferencePosition
/** The reader's own words, empty on a cosign without a note. */
content: string
@@ -503,7 +507,10 @@ export function LetterReview({
letter={renderLetter({
author,
replyTo,
+ court,
+ conferenceId,
conference,
+ edited,
position,
content,
cosign,
diff --git a/functions/src/conferenceLetters/letter.test.ts b/functions/src/conferenceLetters/letter.test.ts
index 87d3dedcc..03af9a3bc 100644
--- a/functions/src/conferenceLetters/letter.test.ts
+++ b/functions/src/conferenceLetters/letter.test.ts
@@ -3,6 +3,8 @@ import { hasLegislators, LetterInput, renderLetter } from "./letter"
const base: LetterInput = {
author: "Ada Example",
replyTo: "ada@example.org",
+ court: 194,
+ conferenceId: "H4321-S2100",
conference: "Data privacy conference committee",
position: "passSenateVersion",
content: "First thought.\n\nSecond thought.",
@@ -65,11 +67,25 @@ describe("renderLetter", () => {
expect(l.cosigned?.quote).toEqual(["Pass it."])
})
- it("links the post once it has one", () => {
- expect(renderLetter(base).footer.endsWith("publicly.")).toBe(true)
+ it("links the conference page, where the input is posted", () => {
+ expect(renderLetter(base).footer).toContain(
+ "publicly: https://mapletestimony.org/conferences/194/H4321-S2100"
+ )
+ })
+
+ it("marks an edit as an update", () => {
+ expect(renderLetter({ ...base, edited: true }).subject).toBe(
+ "Data privacy conference committee: Pass the Senate version (updated)"
+ )
expect(
- renderLetter({ ...base, postUrl: "https://example.org/p" }).footer
- ).toContain("publicly: https://example.org/p")
+ renderLetter({
+ ...base,
+ edited: true,
+ cosign: { author: "Grace", content: "Pass it." }
+ }).subject
+ ).toBe(
+ "Data privacy conference committee: Cosigned input (Pass the Senate version) (updated)"
+ )
})
})
diff --git a/functions/src/conferenceLetters/letter.ts b/functions/src/conferenceLetters/letter.ts
index 766891c29..64eee58f9 100644
--- a/functions/src/conferenceLetters/letter.ts
+++ b/functions/src/conferenceLetters/letter.ts
@@ -2,15 +2,14 @@
* One renderer: the review step previews it and the daily sender mails it, so
* what the reader approves is what is sent. Pure, so the website can import it. */
+import { CONFERENCE_STANCES } from "../shared/conferences"
import type { ConferencePosition } from "../testimony/types"
export type { ConferencePosition }
-const positionLabels: Record = {
- passSomething: "Please pass something",
- passSenateVersion: "Pass the Senate version",
- passHouseVersion: "Pass the House version",
- passNothing: "Don't pass anything"
-}
+/** A single address, as a reply-to must be: the draft is the reader's to
+ * write, so the sender checks it again, and a list could copy anyone. */
+export const looksLikeEmail = (s: string) =>
+ /^[^\s@,;]+@[^\s@,;]+\.[^\s@,;]+$/.test(s)
export type LetterMember = { name: string; district: string }
@@ -20,7 +19,12 @@ export type LetterInput = {
/** Where replies go, and who is copied: the reader's own address, or the
* one they chose for this letter. */
replyTo: string
+ court: number
+ conferenceId: string
+ /** The conference's name. */
conference: string
+ /** Set when the input was posted before and this is its edit. */
+ edited?: boolean
position: ConferencePosition
/** The reader's own words, empty on a cosign without a note. */
content: string
@@ -28,8 +32,6 @@ export type LetterInput = {
cosign?: { author: string; content: string }
representative?: LetterMember
senator?: LetterMember
- /** The posted input's page, known once it is posted. */
- postUrl?: string
}
export type Letter = {
@@ -59,7 +61,7 @@ const paragraphsOf = (text: string) =>
.filter(Boolean)
export function renderLetter(i: LetterInput): Letter {
- const label = positionLabels[i.position]
+ const label = CONFERENCE_STANCES[i.position]
const { representative: rep, senator: sen } = i
const salutation = `Dear ${[
rep && `Representative ${rep.name}`,
@@ -81,9 +83,9 @@ export function renderLetter(i: LetterInput): Letter {
to: [rep && `Rep. ${rep.name}`, sen && `Sen. ${sen.name}`]
.filter(Boolean)
.join(", "),
- subject: i.cosign
- ? `${i.conference}: Cosigned input (${label})`
- : `${i.conference}: ${label}`,
+ subject: `${i.conference}: ${
+ i.cosign ? `Cosigned input (${label})` : label
+ }${i.edited ? " (updated)" : ""}`,
salutation,
paragraphs: paragraphsOf(i.content),
cosigned: i.cosign && {
@@ -92,7 +94,7 @@ export function renderLetter(i: LetterInput): Letter {
},
signature,
footer:
- `Position: ${label}. Sent by a constituent through MAPLE, where ${where} is posted publicly` +
- (i.postUrl ? `: ${i.postUrl}` : ".")
+ `Position: ${label}. Sent by a constituent through MAPLE, where ${where} is posted publicly: ` +
+ `https://mapletestimony.org/conferences/${i.court}/${i.conferenceId}`
}
}
diff --git a/functions/src/conferenceLetters/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts
index f9bf73870..b26e04d29 100644
--- a/functions/src/conferenceLetters/letters.test.ts
+++ b/functions/src/conferenceLetters/letters.test.ts
@@ -10,6 +10,8 @@ import {
const letter = renderLetter({
author: 'Ada "Ace" ',
replyTo: "ada@example.org",
+ court: 194,
+ conferenceId: "H1-S1",
conference: "Phone-Free Schools",
position: "passSomething",
content: "First.\n\nSecond & third.",
@@ -74,6 +76,8 @@ describe("renderLetterText", () => {
const cosigned = renderLetter({
author: "Ada",
replyTo: "ada@example.org",
+ court: 194,
+ conferenceId: "H1-S1",
conference: "Phone-Free Schools",
position: "passSomething",
content: "Agreed.",
diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts
index 9ec96d98f..cc3ea9c1a 100644
--- a/functions/src/conferenceLetters/sendLetters.ts
+++ b/functions/src/conferenceLetters/sendLetters.ts
@@ -7,7 +7,8 @@ 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 { conferenceName } from "../shared/conferences"
+import { hasLegislators, looksLikeEmail, renderLetter } from "./letter"
import { buildLetterEmail, dayWindow, SkipReason } from "./letters"
const ALREADY_EXISTS = 6
@@ -55,11 +56,12 @@ async function prepare(
e: ConferenceInputEvent,
eventId: string,
lastFor: Map,
+ edited: boolean,
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.
+ // An input posted or edited more than once in the window is mailed once,
+ // with what is posted now.
if (lastFor.get(e.testimonyId) !== eventId) return { skip: "superseded" }
const [pubSnap, readerEmail, draft, conference] = await Promise.all([
@@ -68,6 +70,7 @@ async function prepare(
db
.collection(`/users/${e.authorUid}/draftTestimony`)
.where("conferenceId", "==", e.conferenceId)
+ .where("court", "==", e.court)
.limit(1)
.get(),
conferenceOf(`${e.court}/${e.conferenceId}`)
@@ -120,10 +123,14 @@ async function prepare(
}
const author = readerAuthor(pub)
+ const chosen = (draft.docs[0]?.data().replyTo as string | null)?.trim()
const letter = renderLetter({
author,
- replyTo: (draft.docs[0]?.data().replyTo as string)?.trim() || readerEmail,
- conference: conference.name ?? conference.title,
+ replyTo: chosen && looksLikeEmail(chosen) ? chosen : readerEmail,
+ court: e.court,
+ conferenceId: e.conferenceId,
+ conference: conferenceName(conference),
+ edited,
position: state.position,
content: pub.content,
cosign,
@@ -146,8 +153,8 @@ async function prepare(
}
}
-/** 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
+/** Mails each conference input posted or edited 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}. */
@@ -159,11 +166,17 @@ export async function sendConferenceLetters(now = new Date()) {
.where("at", "<", Timestamp.fromDate(end.toJSDate()))
.get()
- const added = events.docs
+ const written = events.docs
.map(doc => ({ id: doc.id, e: doc.data() as ConferenceInputEvent }))
- .filter(({ e }) => e.change === "added")
+ .filter(({ e }) => e.change !== "withdrawn")
.sort((a, b) => a.e.at.toMillis() - b.e.at.toMillis())
- const lastFor = new Map(added.map(({ id, e }) => [e.testimonyId, id]))
+ const lastFor = new Map(written.map(({ id, e }) => [e.testimonyId, id]))
+ // An edit is an update unless the input was also posted in the window.
+ const postedIn = new Set(
+ written
+ .filter(({ e }) => e.change === "added")
+ .map(({ e }) => e.testimonyId)
+ )
const memberContent = cached(
async key =>
(
@@ -180,9 +193,17 @@ export async function sendConferenceLetters(now = new Date()) {
)
let sent = 0
- for (const { id, e } of added) {
+ for (const { id, e } of written) {
try {
- const result = await prepare(e, id, lastFor, memberContent, conferenceOf)
+ const edited = !postedIn.has(e.testimonyId)
+ const result = await prepare(
+ e,
+ id,
+ lastFor,
+ edited,
+ memberContent,
+ conferenceOf
+ )
if ("skip" in result) {
await create(db.doc(`/conferenceLetterSkips/${id}`), {
testimonyId: e.testimonyId,
diff --git a/functions/src/testimony/types.ts b/functions/src/testimony/types.ts
index ba2f1cc9c..853a3728a 100644
--- a/functions/src/testimony/types.ts
+++ b/functions/src/testimony/types.ts
@@ -13,6 +13,7 @@ import {
import { Role } from "../auth/types"
import { Id, Maybe, withDefaults } from "../common"
import { Timestamp } from "../firebase"
+import { looksLikeEmail } from "../conferenceLetters/letter"
import { cosignShape } from "./cosign"
const maxTestimonyLength = 10_000
@@ -125,7 +126,9 @@ export const DraftTestimony = BaseTestimony.extend({
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)
+ replyTo: Maybe(
+ RtString.withConstraint(s => looksLikeEmail(s) || "Not a single address")
+ )
})
.withConstraint(positionMatchesTarget)
.withConstraint(oneTarget)
diff --git a/tests/integration/conferenceLetters.test.ts b/tests/integration/conferenceLetters.test.ts
index 65335c15a..cb47dab23 100644
--- a/tests/integration/conferenceLetters.test.ts
+++ b/tests/integration/conferenceLetters.test.ts
@@ -27,6 +27,7 @@ type Seed = {
sen?: string
withdrawn?: boolean
replyTo?: string
+ change?: "added" | "edited"
}
/** A reader who posted conference input yesterday, and the event that says so. */
@@ -66,7 +67,10 @@ const seed = async (conferenceId: string, s: Seed = {}) => {
conferenceId,
testimonyId,
authorUid: uid,
- change: "added",
+ change: s.change ?? "added",
+ ...(s.change === "edited" && {
+ before: { position: "passNothing", group: "individual", version: 0 }
+ }),
after: { position: "passSomething", group: "individual", version: 1 },
at: Timestamp.fromDate(ADDED_AT)
})
@@ -118,6 +122,9 @@ describe("sendConferenceLetters", () => {
`[dev] would have gone to: rep@malegislature.gov, sen@malegislature.gov — Phone-Free Schools: Please pass something`
)
expect(sent!.message.html).toContain(`Dear Representative Member ${rep}`)
+ expect(sent!.message.html).toContain(
+ `https://mapletestimony.org/conferences/${COURT}/${conferenceId}`
+ )
expect(JSON.parse(sent!.headers["X-SMTPAPI"]).category).toEqual([
"constituent-letters"
])
@@ -175,4 +182,32 @@ describe("sendConferenceLetters", () => {
expect((await skip(first.eventId))!.reason).toBe("superseded")
expect(await email(secondId)).toBeDefined()
})
+
+ it("mails an edit as an update", async () => {
+ const { eventId } = await seed(conferenceId, { rep, change: "edited" })
+ await sendConferenceLetters(NOW)
+ expect((await email(eventId))!.message.subject).toMatch(
+ /Phone-Free Schools: Please pass something \(updated\)$/
+ )
+ })
+
+ it("mails an input posted and edited the same day once, as new", async () => {
+ const first = await seed(conferenceId, { rep })
+ const editId = id()
+ await testDb.doc(`conferenceInputEvents/${editId}`).set({
+ court: COURT,
+ conferenceId,
+ testimonyId: first.testimonyId,
+ authorUid: first.uid,
+ change: "edited",
+ before: { position: "passSomething", group: "individual", version: 1 },
+ after: { position: "passSomething", group: "individual", version: 2 },
+ at: Timestamp.fromDate(new Date(ADDED_AT.getTime() + 60_000))
+ })
+ await sendConferenceLetters(NOW)
+ expect((await skip(first.eventId))!.reason).toBe("superseded")
+ expect((await email(editId))!.message.subject).toMatch(
+ /Please pass something$/
+ )
+ })
})
From 5f8c1d246bb62b9f44294efda325d2a70c09fc43 Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 15:03:16 -0500
Subject: [PATCH 09/17] Count a weekly update's changes up to when it is
compiled
The update's changes ran Monday 00:00 to Monday 00:00 but the job runs at
8:00, so input in those eight hours waited a week. They now run from a week
before the compile to the compile. The update keeps the Monday it is named
for in its id, weekStart and heading. A Monday run that fails and is rerun
later in the week misses or repeats about a day of changes.
Verified: functions jest (weekWindow across the week and daylight saving);
integration testimony.test.ts on a throwaway emulator.
Co-Authored-By: Claude Opus 5.5
---
.../conferenceUpdates/compileWeeklyUpdates.ts | 16 ++++++-------
functions/src/conferenceUpdates/types.ts | 5 ++--
.../conferenceUpdates/weeklyUpdate.test.ts | 24 +++++++++----------
.../src/conferenceUpdates/weeklyUpdate.ts | 23 ++++++++++--------
tests/integration/testimony.test.ts | 12 +++++-----
5 files changed, 42 insertions(+), 38 deletions(-)
diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
index bec39a2cd..b18d7eb3c 100644
--- a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
+++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
@@ -42,12 +42,12 @@ const conferee = async (court: number, c: Conference["conferees"][number]) => {
}
/** 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. */
+ * change in the week up to `now`, and stores it as
+ * /conferenceUpdates/{court}_{conferenceId}_{week}. 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 { week, start, end } = weekWindow(now)
const events = await db
.collection("/conferenceInputEvents")
.where("at", ">=", Timestamp.fromDate(start.toJSDate()))
@@ -92,7 +92,7 @@ export async function compileWeeklyUpdates(now = new Date()) {
conferees,
standing,
changes,
- start,
+ week,
now
})
const html = renderConferenceUpdate(data)
@@ -100,7 +100,7 @@ export async function compileWeeklyUpdates(now = new Date()) {
const update: ConferenceUpdate = {
court,
conferenceId: id,
- weekStart: Timestamp.fromDate(start.toJSDate()),
+ weekStart: Timestamp.fromDate(week.toJSDate()),
standing,
changes,
html,
@@ -115,7 +115,7 @@ export async function compileWeeklyUpdates(now = new Date()) {
// a second run finds the update and sends nothing.
const updateRef = db
.collection("/conferenceUpdates")
- .doc(`${court}_${id}_${start.toISODate()}`)
+ .doc(`${court}_${id}_${week.toISODate()}`)
const batch = db.batch()
batch.create(updateRef, update)
const { to, subjectPrefix } = resolveRecipients(
diff --git a/functions/src/conferenceUpdates/types.ts b/functions/src/conferenceUpdates/types.ts
index a47433450..72cb5b9f7 100644
--- a/functions/src/conferenceUpdates/types.ts
+++ b/functions/src/conferenceUpdates/types.ts
@@ -96,9 +96,10 @@ export type ConferenceUpdate = Static
export const ConferenceUpdate = Record({
court: Number,
conferenceId: String,
- /** Monday 00:00, Eastern. The week runs to the next Monday. */
+ /** Monday 00:00, Eastern, of the week the update is named for. */
weekStart: InstanceOf(Timestamp),
- /** The input on the conference when the update was compiled. */
+ /** The input on the conference when the update was compiled, where its
+ * changes end. */
standing: Counts,
changes: Changes,
html: String,
diff --git a/functions/src/conferenceUpdates/weeklyUpdate.test.ts b/functions/src/conferenceUpdates/weeklyUpdate.test.ts
index 098f7afbc..ea6d1f284 100644
--- a/functions/src/conferenceUpdates/weeklyUpdate.test.ts
+++ b/functions/src/conferenceUpdates/weeklyUpdate.test.ts
@@ -17,23 +17,23 @@ const state = (over: Partial = {}): InputState => ({
})
describe("weekWindow", () => {
- it("is the Monday-to-Sunday week before the Monday on or before now", () => {
+ it("runs from a week before the run to the run", () => {
// 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")
+ const { week, start, end } = weekWindow(new Date("2026-09-28T12:00:00Z"))
+ expect(week.toISO()).toBe("2026-09-21T00:00:00.000-04:00")
+ expect(start.toISO()).toBe("2026-09-21T08:00:00.000-04:00")
+ expect(end.toISO()).toBe("2026-09-28T08:00:00.000-04:00")
})
- it("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("names the week before through the next Sunday", () => {
+ const { week } = weekWindow(new Date("2026-10-04T23:00:00Z"))
+ expect(week.toISODate()).toBe("2026-09-21")
})
it("keeps Monday midnight across the autumn clock change", () => {
- const { 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")
+ const { week, start } = weekWindow(new Date("2026-11-02T13:00:00Z"))
+ expect(week.toISO()).toBe("2026-10-26T00:00:00.000-04:00")
+ expect(start.toISO()).toBe("2026-10-26T08:00:00.000-04:00")
})
})
@@ -129,7 +129,7 @@ describe("the update email", () => {
],
standing,
changes,
- start: weekWindow(new Date("2026-09-28T12:00:00Z")).start,
+ week: weekWindow(new Date("2026-09-28T12:00:00Z")).week,
now: new Date("2026-09-28T12:00:00Z")
})
diff --git a/functions/src/conferenceUpdates/weeklyUpdate.ts b/functions/src/conferenceUpdates/weeklyUpdate.ts
index b75854d34..0f0fef0b4 100644
--- a/functions/src/conferenceUpdates/weeklyUpdate.ts
+++ b/functions/src/conferenceUpdates/weeklyUpdate.ts
@@ -5,7 +5,7 @@ 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 { CONFERENCE_STANCES, conferenceName } from "../shared/conferences"
import { ConferencePosition } from "../testimony/types"
import {
AuthorGroup,
@@ -37,11 +37,14 @@ const GROUPS: Record = {
government: "Government offices"
}
-/** The Monday-to-Sunday week, Eastern, that ended at the Monday on or before
- * `now`. `end` is exclusive. */
+/** The week an update run at `now` reports. `week` is the Monday, Eastern,
+ * of the Monday-to-Sunday week it is named for. Its changes run from `start`, a
+ * week before the run, to `end`, the run, so they end where the standing is
+ * counted; the weekly runs follow on from each other. `end` is exclusive. */
export function weekWindow(now: Date) {
- const end = DateTime.fromJSDate(now, { zone: timeZone }).startOf("week")
- return { start: end.minus({ weeks: 1 }), end }
+ const end = DateTime.fromJSDate(now, { zone: timeZone })
+ const week = end.startOf("week").minus({ weeks: 1 })
+ return { week, start: end.minus({ weeks: 1 }), end }
}
const emptyCounts = (): Counts => ({
@@ -104,13 +107,13 @@ const formatWeek = (start: DateTime) => {
}
/** 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. */
+ * changes, which is the standing now less their net change. */
export function buildConferenceUpdateEmail({
conference,
conferees,
standing,
changes,
- start,
+ week,
now
}: {
conference: Pick<
@@ -120,7 +123,7 @@ export function buildConferenceUpdateEmail({
conferees: { name: string; chair: boolean }[]
standing: Counts
changes: Changes
- start: DateTime
+ week: DateTime
now: Date
}): ConferenceUpdateEmail {
const { net } = changes
@@ -146,9 +149,9 @@ export function buildConferenceUpdateEmail({
bills: `${billLabel(conference.senateBillId)} / ${billLabel(
conference.houseBillId
)}`,
- title: conference.name ?? conference.title,
+ title: conferenceName(conference),
conferenceUrl: `https://mapletestimony.org/conferences/${conference.court}/${conference.id}`,
- weekLabel: formatWeek(start),
+ weekLabel: formatWeek(week),
sentLabel: DateTime.fromJSDate(now, { zone: timeZone }).toFormat(
"cccc, LLLL d, yyyy"
),
diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts
index a3b4bd234..567dd2b5e 100644
--- a/tests/integration/testimony.test.ts
+++ b/tests/integration/testimony.test.ts
@@ -1480,8 +1480,8 @@ describe("conference weekly update", () => {
)
// 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 now = weekWindow(new Date()).week.plus({ weeks: 2, hours: 1 })
+ const weekStart = weekWindow(now.toJSDate()).week.toISODate()
const path = `/conferenceUpdates/${currentGeneralCourt}_${conferenceId}_${weekStart}`
await compileWeeklyUpdates(now.toJSDate())
@@ -1535,8 +1535,8 @@ describe("conference weekly update", () => {
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 now = weekWindow(new Date()).week.plus({ weeks: 2, hours: 1 })
+ const weekStart = weekWindow(now.toJSDate()).week.toISODate()
const id = `${currentGeneralCourt}_${conferenceId}_${weekStart}`
return { conferees, now: now.toJSDate(), id }
}
@@ -1609,8 +1609,8 @@ describe("conference weekly update", () => {
)
// 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()
+ const now = weekWindow(new Date()).week.plus({ weeks: 3, hours: 1 })
+ const weekStart = weekWindow(now.toJSDate()).week.toISODate()
await compileWeeklyUpdates(now.toJSDate())
expect(
From 8380b4b2ad670266f2c7b4e460dae874039a8ae8 Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 15:17:45 -0500
Subject: [PATCH 10/17] Mail each conference input at the first 9:00 after it's
posted
The daily letter job ran at 9:00 Eastern but mailed the previous calendar
day, so input posted before 9:00 waited a day, and a post at 23:00 edited
at 8:00 was mailed twice. The day's window now runs from the run a day
before to this one, as the weekly update's does, stepping back a calendar
day so the windows meet at 9:00 across clock changes.
Both scheduled jobs now take the time the run was due from the scheduler,
not the time it started, so each window begins exactly where the last one
ended. The jobs take that time as a required argument, and the local
run-once script runs the letters as of now.
Closes hyphacoop/maple#145
Verified: dayWindow tests for the 9:00-to-9:00 window, input posted at
00:30 mailed that morning, and both clock changes; functions jest for
conferenceLetters and conferenceUpdates (38); check-types, functions tsc,
lint and prettier; the conferenceLetters integration test (10/10) on a
throwaway emulator with a Typesense stand-in, before now became required.
Not verified: a real scheduled run passing scheduleTime; the testimony
integration test's weekly update cases; yarn build:dev.
Co-Authored-By: Claude Opus 5.5
---
.../src/conferenceLetters/letters.test.ts | 25 +++++++++++++------
functions/src/conferenceLetters/letters.ts | 6 +++--
.../src/conferenceLetters/sendLetters.ts | 13 +++++++---
.../conferenceUpdates/compileWeeklyUpdates.ts | 6 +++--
scripts/firebase-admin/runConferenceMail.ts | 24 +++++++++---------
5 files changed, 47 insertions(+), 27 deletions(-)
diff --git a/functions/src/conferenceLetters/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts
index b26e04d29..06a8f531b 100644
--- a/functions/src/conferenceLetters/letters.test.ts
+++ b/functions/src/conferenceLetters/letters.test.ts
@@ -1,3 +1,4 @@
+import { DateTime } from "luxon"
import { renderLetter } from "./letter"
import {
buildLetterEmail,
@@ -20,19 +21,29 @@ const letter = renderLetter({
})
describe("dayWindow", () => {
- it("is yesterday in Eastern time", () => {
+ it("runs from 9:00 the day before to the 9:00 run", () => {
const { start, end } = dayWindow(new Date("2026-11-11T14:00:00Z"))
- expect(start.toISO()).toBe("2026-11-10T00:00:00.000-05:00")
- expect(end.toISO()).toBe("2026-11-11T00:00:00.000-05:00")
+ expect(start.toISO()).toBe("2026-11-10T09:00:00.000-05:00")
+ expect(end.toISO()).toBe("2026-11-11T09:00:00.000-05:00")
})
- it("is a 25 hour day when the clocks go back", () => {
- const { start, end } = dayWindow(new Date("2026-11-02T14:00:00Z"))
+ it("mails input posted at 00:30 at 9:00 the same morning", () => {
+ const { start, end } = dayWindow(new Date("2026-11-11T14:00:00Z"))
+ const posted = DateTime.fromISO("2026-11-11T00:30:00-05:00")
+ expect(posted >= start && posted < end).toBe(true)
+ })
+
+ it("starts at 9:00 and runs 25 hours when the clocks go back", () => {
+ // Sunday Nov 1, 2026, 9am EST
+ const { start, end } = dayWindow(new Date("2026-11-01T14:00:00Z"))
+ expect(start.toISO()).toBe("2026-10-31T09:00:00.000-04:00")
expect(end.diff(start, "hours").hours).toBe(25)
})
- it("is a 23 hour day when the clocks go forward", () => {
- const { start, end } = dayWindow(new Date("2027-03-15T14:00:00Z"))
+ it("starts at 9:00 and runs 23 hours when the clocks go forward", () => {
+ // Sunday Mar 14, 2027, 9am EDT
+ const { start, end } = dayWindow(new Date("2027-03-14T13:00:00Z"))
+ expect(start.toISO()).toBe("2027-03-13T09:00:00.000-05:00")
expect(end.diff(start, "hours").hours).toBe(23)
})
})
diff --git a/functions/src/conferenceLetters/letters.ts b/functions/src/conferenceLetters/letters.ts
index 4f13f72e8..065734577 100644
--- a/functions/src/conferenceLetters/letters.ts
+++ b/functions/src/conferenceLetters/letters.ts
@@ -2,9 +2,11 @@ import { DateTime } from "luxon"
import { timeZone } from "../malegislature"
import { Letter } from "./letter"
-/** Yesterday, Eastern: the day whose new input the daily job mails. */
+/** The input a daily run at `now` mails: from the same time the day before,
+ * Eastern, to the run, so the daily runs follow on from each other and each
+ * input goes out at the first run after it. `end` is exclusive. */
export function dayWindow(now: Date) {
- const end = DateTime.fromJSDate(now, { zone: timeZone }).startOf("day")
+ const end = DateTime.fromJSDate(now, { zone: timeZone })
return { start: end.minus({ days: 1 }), end }
}
diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts
index cc3ea9c1a..07f6eaa96 100644
--- a/functions/src/conferenceLetters/sendLetters.ts
+++ b/functions/src/conferenceLetters/sendLetters.ts
@@ -153,12 +153,12 @@ async function prepare(
}
}
-/** Mails each conference input posted or edited in the day before `now` to
+/** Mails each conference input posted or edited since the run a day before `now` to
* the reader's rep and senator, as the letter the reader previewed. A letter is the
* `emails` document /emails/letter_{input event id}, 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()) {
+export async function sendConferenceLetters(now: Date) {
const { start, end } = dayWindow(now)
const events = await db
.collection("/conferenceInputEvents")
@@ -219,11 +219,16 @@ export async function sendConferenceLetters(now = new Date()) {
logger.error(`Letter for input event ${id} failed`, err)
}
}
- logger.info(`Mailed ${sent} conference letters for ${start.toISODate()}`)
+ logger.info(
+ `Mailed ${sent} conference letters from ${start.toISO()} to ${end.toISO()}`
+ )
return sent
}
export const mailConferenceLetters = onSchedule(
{ schedule: "0 9 * * *", timeZone, timeoutSeconds: 300 },
- () => sendConferenceLetters().then(() => undefined)
+ // The time the run was due, not when it started, so each day's window
+ // begins exactly where the last one ended.
+ event =>
+ sendConferenceLetters(new Date(event.scheduleTime)).then(() => undefined)
)
diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
index b18d7eb3c..4fe268e39 100644
--- a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
+++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
@@ -46,7 +46,7 @@ const conferee = async (court: number, c: Conference["conferees"][number]) => {
* /conferenceUpdates/{court}_{conferenceId}_{week}. 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()) {
+export async function compileWeeklyUpdates(now: Date) {
const { week, start, end } = weekWindow(now)
const events = await db
.collection("/conferenceInputEvents")
@@ -159,5 +159,7 @@ export const compileConferenceUpdates = onSchedule(
timeZone,
timeoutSeconds: 300
},
- () => compileWeeklyUpdates()
+ // The time the run was due, not when it started, so each week's window
+ // begins exactly where the last one ended.
+ event => compileWeeklyUpdates(new Date(event.scheduleTime))
)
diff --git a/scripts/firebase-admin/runConferenceMail.ts b/scripts/firebase-admin/runConferenceMail.ts
index d4ee24707..4da75639c 100644
--- a/scripts/firebase-admin/runConferenceMail.ts
+++ b/scripts/firebase-admin/runConferenceMail.ts
@@ -11,10 +11,11 @@ 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.
+ * schedules. With no --now, letters run as of now, covering the last day's
+ * input, and updates as of next Monday, covering this week's. The emails land
+ * in /emails for the send-email extension, addressed to LETTER_TEST_RECIPIENT,
+ * which is read from the functions env files as the functions emulator reads
+ * it. Local only.
*
* yarn firebase-admin run-script runConferenceMail --env local --job letters
* yarn firebase-admin run-script runConferenceMail --env local --job updates --now 2026-11-16T08:00
@@ -47,13 +48,6 @@ const loadFunctionsEnv = () => {
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)
@@ -62,7 +56,13 @@ export const script: Script = async ({ env, args }) => {
const now = nowArg
? DateTime.fromISO(nowArg, { zone: timeZone })
- : nextRun(job)
+ : job === "letters"
+ ? DateTime.now().setZone(timeZone)
+ : DateTime.now()
+ .setZone(timeZone)
+ .startOf("week")
+ .plus({ weeks: 1 })
+ .set({ hour: 8 })
if (!now.isValid) throw Error(`--now ${nowArg}: ${now.invalidExplanation}`)
loadFunctionsEnv()
From f8eca0a14a8cda164a6e90b833d81c794a13487e Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 15:35:41 -0500
Subject: [PATCH 11/17] Record every email MAPLE sends to a legislator
Each letter and weekly conference update is now recorded at
/legislatorMail under its /emails document's id, created in the same
batch, so neither exists without the other and a rerun adds nothing.
The record keeps the kind, court, the legislators' member codes (the real
ones outside production), the subject as they see it and the body; a
letter adds the reader, testimony, version, conference and whether it
went out as an update, and an update adds its week, standing and changes.
It replaces /conferenceUpdates, so an update no conferee can receive is
no longer stored. Readers can read the records of their own letters.
Closes hyphacoop/maple#146
Verified: conferenceLetters and testimony integration suites (89) on a
throwaway emulator with a Typesense stub, including records, reruns and
the read rule; functions jest for conference letters, updates and email;
check-types, functions tsc, lint, build:dev.
Not verified: the composite index deploys; a real scheduled run.
Co-Authored-By: Claude Opus 5.5
---
firestore.indexes.json | 14 +++
firestore.rules | 5 +
functions/src/common.ts | 14 +++
.../src/conferenceLetters/letters.test.ts | 8 +-
functions/src/conferenceLetters/letters.ts | 58 +++++------
.../src/conferenceLetters/sendLetters.ts | 97 +++++++++----------
.../conferenceUpdates/compileWeeklyUpdates.ts | 84 +++++++---------
functions/src/conferenceUpdates/types.ts | 26 -----
functions/src/legislatorMail/queue.ts | 35 +++++++
functions/src/legislatorMail/types.ts | 70 +++++++++++++
scripts/firebase-admin/runConferenceMail.ts | 2 +-
tests/integration/conferenceLetters.test.ts | 48 ++++++++-
tests/integration/testimony.test.ts | 79 +++++++++------
13 files changed, 340 insertions(+), 200 deletions(-)
create mode 100644 functions/src/legislatorMail/queue.ts
create mode 100644 functions/src/legislatorMail/types.ts
diff --git a/firestore.indexes.json b/firestore.indexes.json
index bef3e384e..7ad937f48 100644
--- a/firestore.indexes.json
+++ b/firestore.indexes.json
@@ -1137,6 +1137,20 @@
"order": "DESCENDING"
}
]
+ },
+ {
+ "collectionGroup": "legislatorMail",
+ "queryScope": "COLLECTION",
+ "fields": [
+ {
+ "fieldPath": "testimonyId",
+ "order": "ASCENDING"
+ },
+ {
+ "fieldPath": "createdAt",
+ "order": "DESCENDING"
+ }
+ ]
}
],
"fieldOverrides": [
diff --git a/firestore.rules b/firestore.rules
index ce51353d5..8c97b4837 100644
--- a/firestore.rules
+++ b/firestore.rules
@@ -79,6 +79,11 @@ service cloud.firestore {
allow read: if true;
allow write: if request.auth.token.get("role", "user") == "admin";
}
+ // Emails sent to legislators, written by functions only. A reader can
+ // read the records of their own letters.
+ match /legislatorMail/{id} {
+ allow read: if request.auth != null && resource.data.uid == request.auth.uid;
+ }
match /users/{uid} {
allow read, write: if request.auth.token.get("role", "user") == "admin"
match /draftTestimony/{id} {
diff --git a/functions/src/common.ts b/functions/src/common.ts
index 6bedad54b..3ee5548d2 100644
--- a/functions/src/common.ts
+++ b/functions/src/common.ts
@@ -91,6 +91,20 @@ export const logFetchError = (label: string, id?: string) => (e: any) => {
}
}
+const ALREADY_EXISTS = 6
+
+/** Awaits a Firestore create, or a batch of creates: false when a document
+ * already exists, so a rerun writes nothing. */
+export async function createdUnlessExists(write: Promise) {
+ try {
+ await write
+ return true
+ } catch (e) {
+ if ((e as { code?: number }).code !== ALREADY_EXISTS) throw e
+ return false
+ }
+}
+
// In particular, reject "/" in ID strings
const simpleId = /^[A-Za-z0-9-_ ]+$/
/** Validates firestore-compatible ID's */
diff --git a/functions/src/conferenceLetters/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts
index 06a8f531b..9812724a5 100644
--- a/functions/src/conferenceLetters/letters.test.ts
+++ b/functions/src/conferenceLetters/letters.test.ts
@@ -105,10 +105,7 @@ describe("buildLetterEmail", () => {
const email = buildLetterEmail({
letter,
author: "Ada",
- fromAddress: "notifications@mapletestimony.org",
- to: ["rep@malegislature.gov"],
- subjectPrefix: "[dev] ",
- createdAt: null
+ fromAddress: "notifications@mapletestimony.org"
})
it("copies the reader and sends replies to them", () => {
@@ -117,9 +114,8 @@ describe("buildLetterEmail", () => {
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"
+ "Phone-Free Schools: Please pass something"
)
})
diff --git a/functions/src/conferenceLetters/letters.ts b/functions/src/conferenceLetters/letters.ts
index 065734577..112b6972a 100644
--- a/functions/src/conferenceLetters/letters.ts
+++ b/functions/src/conferenceLetters/letters.ts
@@ -1,5 +1,6 @@
import { DateTime } from "luxon"
import { timeZone } from "../malegislature"
+import type { LegislatorEmail } from "../legislatorMail/types"
import { Letter } from "./letter"
/** The input a daily run at `now` mails: from the same time the day before,
@@ -71,45 +72,34 @@ export const renderLetterText = (letter: Letter) =>
.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. */
+/** The email for a letter, for the send-email extension. Click and open
+ * tracking are off per message: they would rewrite the links in a letter to a
+ * legislator. */
export const buildLetterEmail = ({
letter,
author,
- fromAddress,
- to,
- subjectPrefix,
- createdAt
+ fromAddress
}: {
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
+}): LegislatorEmail => ({
+ cc: [letter.cc],
+ from: letterFrom(author, fromAddress),
+ replyTo: letter.replyTo,
+ // The extension reads headers from the document, not from `message`.
+ headers: {
+ "X-SMTPAPI": JSON.stringify({
+ category: ["constituent-letters"],
+ filters: {
+ opentrack: { settings: { enable: 0 } },
+ clicktrack: { settings: { enable: 0 } }
+ }
+ })
+ },
+ message: {
+ subject: letter.subject,
+ text: renderLetterText(letter),
+ html: renderLetterHtml(letter)
}
-}
+})
diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts
index 07f6eaa96..15aa86524 100644
--- a/functions/src/conferenceLetters/sendLetters.ts
+++ b/functions/src/conferenceLetters/sendLetters.ts
@@ -1,17 +1,17 @@
import { onSchedule } from "firebase-functions/v2/scheduler"
import * as logger from "firebase-functions/logger"
+import { createdUnlessExists } from "../common"
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 { queueLegislatorEmail } from "../legislatorMail/queue"
import { conferenceName } from "../shared/conferences"
import { hasLegislators, looksLikeEmail, renderLetter } from "./letter"
import { buildLetterEmail, dayWindow, SkipReason } from "./letters"
-const ALREADY_EXISTS = 6
const DEFAULT_FROM = "notifications@mapletestimony.org"
type Content = Record | undefined
@@ -35,34 +35,21 @@ const verifiedEmail = async (uid: string) => {
}
}
-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
- }
-}
-
// As the site names an author (`nameOf`): an empty full name falls back too.
const readerAuthor = (t: Testimony) => t.fullName || t.authorDisplayName
-/** The letter for one input, or why it is not sent. */
-async function prepare(
+/** Mails the letter for one input, or says why it is not sent. */
+async function mailLetter(
e: ConferenceInputEvent,
eventId: string,
lastFor: Map,
edited: boolean,
memberContent: (key: string) => Promise,
conferenceOf: (key: string) => Promise
-): Promise<{ skip: SkipReason } | { email: object }> {
+): Promise {
// An input posted or edited more than once in the window is mailed once,
// with what is posted now.
- if (lastFor.get(e.testimonyId) !== eventId) return { skip: "superseded" }
+ if (lastFor.get(e.testimonyId) !== eventId) return "superseded"
const [pubSnap, readerEmail, draft, conference] = await Promise.all([
db.doc(`/users/${e.authorUid}/publishedTestimony/${e.testimonyId}`).get(),
@@ -77,10 +64,10 @@ async function prepare(
])
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" }
+ if (!pub || !state) return "withdrawn"
+ if (!readerEmail) return "noVerifiedEmail"
+ if (!pub.representativeId && !pub.senatorId) return "noLegislator"
+ if (!conference) return "noConference"
// Only those who can receive it are written to, so the salutation names
// exactly who gets the letter. The member document's own name and district
@@ -93,8 +80,9 @@ async function prepare(
].map(async ([id, recorded]) => {
const content = id ? await memberContent(`${e.court}/${id}`) : undefined
const email: string | undefined = content?.EmailAddress?.trim()
- return content?.Name && email
+ return id && content?.Name && email
? {
+ memberCode: id,
email,
member: {
name: content.Name as string,
@@ -106,7 +94,7 @@ async function prepare(
)
const [representative, senator] = reachable.map(m => m?.member)
if (!hasLegislators({ representative, senator })) {
- return { skip: "noLegislatorEmail" }
+ return "noLegislatorEmail"
}
let cosign: { author: string; content: string } | undefined
@@ -118,7 +106,7 @@ async function prepare(
)
.get()
).data() as Testimony | undefined
- if (!parent) return { skip: "withdrawn" }
+ if (!parent) return "withdrawn"
cosign = { author: readerAuthor(parent), content: parent.content }
}
@@ -137,26 +125,32 @@ async function prepare(
representative,
senator
})
- const { to, subjectPrefix } = resolveRecipients(
- reachable.flatMap(m => m?.email ?? [])
- )
- if (!to.length) return { skip: "noRecipients" }
- return {
- email: buildLetterEmail({
+ return queueLegislatorEmail(
+ `letter_${eventId}`,
+ reachable.flatMap(m => m?.email ?? []),
+ buildLetterEmail({
letter,
author,
- fromAddress: process.env.LETTER_FROM ?? DEFAULT_FROM,
- to,
- subjectPrefix,
- createdAt: Timestamp.now()
- })
- }
+ fromAddress: process.env.LETTER_FROM ?? DEFAULT_FROM
+ }),
+ {
+ kind: "letter",
+ court: e.court,
+ memberCodes: reachable.flatMap(m => m?.memberCode ?? []),
+ uid: e.authorUid,
+ testimonyId: e.testimonyId,
+ version: pub.version,
+ conferenceId: e.conferenceId,
+ updated: edited
+ }
+ )
}
/** Mails each conference input posted or edited since the run a day before `now` to
* the reader's rep and senator, as the letter the reader previewed. A letter is the
- * `emails` document /emails/letter_{input event id}, created, so a second run
- * sends nothing again. An input that is not mailed is recorded, with why, at
+ * `emails` document /emails/letter_{input event id} and its record at
+ * /legislatorMail/letter_{input event id}, created, so a second run sends
+ * nothing again. An input that is not mailed is recorded, with why, at
* /conferenceLetterSkips/{input event id}. */
export async function sendConferenceLetters(now: Date) {
const { start, end } = dayWindow(now)
@@ -196,7 +190,7 @@ export async function sendConferenceLetters(now: Date) {
for (const { id, e } of written) {
try {
const edited = !postedIn.has(e.testimonyId)
- const result = await prepare(
+ const outcome = await mailLetter(
e,
id,
lastFor,
@@ -204,16 +198,17 @@ export async function sendConferenceLetters(now: Date) {
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++
+ if (outcome === "queued") sent++
+ else if (outcome !== "exists") {
+ await createdUnlessExists(
+ db.doc(`/conferenceLetterSkips/${id}`).create({
+ testimonyId: e.testimonyId,
+ authorUid: e.authorUid,
+ conferenceId: e.conferenceId,
+ reason: outcome,
+ createdAt: Timestamp.now()
+ })
+ )
}
} catch (err) {
logger.error(`Letter for input event ${id} failed`, err)
diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
index 4fe268e39..fea1cbde9 100644
--- a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
+++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
@@ -3,12 +3,12 @@ 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 { queueLegislatorEmail } from "../legislatorMail/queue"
+import { ConferenceInputEvent } from "./types"
import {
buildConferenceUpdateEmail,
conferenceUpdateSubject,
@@ -19,7 +19,6 @@ import {
weekWindow
} from "./weeklyUpdate"
-const ALREADY_EXISTS = 6
const REPLY_TO = "info@mapletestimony.org"
const DEFAULT_FROM = "notifications@mapletestimony.org"
@@ -42,10 +41,10 @@ const conferee = async (court: number, c: Conference["conferees"][number]) => {
}
/** Compiles the update for each appointed conference that had a
- * change in the week up to `now`, and stores it as
- * /conferenceUpdates/{court}_{conferenceId}_{week}. Creating it rather than
- * setting it means a second run in the same week leaves the first update as
- * it was. */
+ * change in the week up to `now`, and mails it to the conferees as
+ * /emails/{court}_{conferenceId}_{week}, recorded at /legislatorMail under the
+ * same id. Both are created, so a second run in the same week sends nothing
+ * again. */
export async function compileWeeklyUpdates(now: Date) {
const { week, start, end } = weekWindow(now)
const events = await db
@@ -61,7 +60,7 @@ export async function compileWeeklyUpdates(now: Date) {
eventsOf.set(key, [...(eventsOf.get(key) ?? []), e])
}
- let stored = 0
+ let sent = 0
for (const [key, conferenceEvents] of eventsOf) {
const { court, conferenceId: id } = conferenceEvents[0]
try {
@@ -95,35 +94,20 @@ export async function compileWeeklyUpdates(now: Date) {
week,
now
})
- const html = renderConferenceUpdate(data)
- const reachable = conferees.filter(c => c.email)
- const update: ConferenceUpdate = {
- court,
- conferenceId: id,
- weekStart: Timestamp.fromDate(week.toJSDate()),
- standing,
- changes,
- html,
- delivery: {
- sentTo: reachable.map(c => c.memberCode),
- skipped: conferees.filter(c => !c.email).map(c => c.memberCode)
- },
- createdAt: Timestamp.now()
+ const skipped = conferees.filter(c => !c.email)
+ if (skipped.length) {
+ logger.warn(
+ `No address for conferees ${skipped
+ .map(c => c.memberCode)
+ .join(", ")} of conference ${key}`
+ )
}
- // 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}_${week.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,
+ const reachable = conferees.filter(c => c.email)
+ const outcome = await queueLegislatorEmail(
+ `${court}_${id}_${week.toISODate()}`,
+ reachable.map(c => c.email!),
+ {
from: `"MAPLE" <${process.env.CONFEREE_UPDATE_FROM ?? DEFAULT_FROM}>`,
replyTo: REPLY_TO,
// The extension reads headers from the document, not from `message`.
@@ -131,26 +115,30 @@ export async function compileWeeklyUpdates(now: Date) {
"X-SMTPAPI": JSON.stringify({ category: ["conferee-update"] })
},
message: {
- subject: subjectPrefix + conferenceUpdateSubject(data),
+ subject: conferenceUpdateSubject(data),
text: renderConferenceUpdateText(data),
- html
- },
- createdAt: Timestamp.now()
- })
- } else {
+ html: renderConferenceUpdate(data)
+ }
+ },
+ {
+ kind: "conferenceUpdate",
+ court,
+ memberCodes: reachable.map(c => c.memberCode),
+ conferenceId: id,
+ weekStart: Timestamp.fromDate(week.toJSDate()),
+ standing,
+ changes
+ }
+ )
+ if (outcome === "queued") sent++
+ if (outcome === "noRecipients") {
logger.warn(`No recipients for the update to conference ${key}`)
}
- 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`)
+ logger.info(`Mailed ${sent} weekly conference updates`)
}
export const compileConferenceUpdates = onSchedule(
diff --git a/functions/src/conferenceUpdates/types.ts b/functions/src/conferenceUpdates/types.ts
index 72cb5b9f7..4428bcf63 100644
--- a/functions/src/conferenceUpdates/types.ts
+++ b/functions/src/conferenceUpdates/types.ts
@@ -1,7 +1,6 @@
import {
InstanceOf,
Literal as L,
- Array,
Number,
Optional,
Record,
@@ -81,28 +80,3 @@ export const Changes = Record({
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, of the week the update is named for. */
- weekStart: InstanceOf(Timestamp),
- /** The input on the conference when the update was compiled, where its
- * changes end. */
- standing: Counts,
- changes: Changes,
- html: String,
- delivery: Delivery,
- createdAt: InstanceOf(Timestamp)
-})
diff --git a/functions/src/legislatorMail/queue.ts b/functions/src/legislatorMail/queue.ts
new file mode 100644
index 000000000..6a0ef6f81
--- /dev/null
+++ b/functions/src/legislatorMail/queue.ts
@@ -0,0 +1,35 @@
+import { createdUnlessExists } from "../common"
+import { resolveRecipients } from "../email/recipients"
+import { db, Timestamp } from "../firebase"
+import { LegislatorEmail, LegislatorMail } from "./types"
+
+/** What a record takes from its email. */
+type FromEmail = "subject" | "html" | "text" | "createdAt"
+type RecordOf = T extends unknown ? Omit : never
+
+/** Queues `email` to the legislators at `addresses` for the send-email
+ * extension at /emails/{id}, and records it as sent, unprefixed, at
+ * /legislatorMail/{id}. One batch, so neither exists without the other; both
+ * are created, so a second call with the same id writes nothing. */
+export async function queueLegislatorEmail(
+ id: string,
+ addresses: string[],
+ email: LegislatorEmail,
+ record: RecordOf
+): Promise<"queued" | "exists" | "noRecipients"> {
+ const { to, subjectPrefix } = resolveRecipients(addresses)
+ if (!to.length) return "noRecipients"
+
+ const createdAt = Timestamp.now()
+ const { subject, text, html } = email.message
+ const sent: LegislatorMail = { ...record, subject, html, text, createdAt }
+ const batch = db.batch()
+ batch.create(db.doc(`/emails/${id}`), {
+ ...email,
+ to,
+ message: { ...email.message, subject: subjectPrefix + subject },
+ createdAt
+ })
+ batch.create(db.doc(`/legislatorMail/${id}`), sent)
+ return (await createdUnlessExists(batch.commit())) ? "queued" : "exists"
+}
diff --git a/functions/src/legislatorMail/types.ts b/functions/src/legislatorMail/types.ts
new file mode 100644
index 000000000..e9488716a
--- /dev/null
+++ b/functions/src/legislatorMail/types.ts
@@ -0,0 +1,70 @@
+import {
+ Array,
+ Boolean,
+ InstanceOf,
+ Literal as L,
+ Number,
+ Record,
+ Static,
+ String,
+ Union
+} from "runtypes"
+import { Timestamp } from "../firebase"
+import { Changes, Counts } from "../conferenceUpdates/types"
+
+/** What every email to legislators records. `memberCodes` are the
+ * legislators it went to; outside production, those it would have gone to
+ * instead of the test address. `subject` is without the test prefix. */
+const Sent = {
+ court: Number,
+ memberCodes: Array(String),
+ subject: String,
+ html: String,
+ text: String,
+ /** When the email was queued: the `emails` document's `createdAt`. */
+ createdAt: InstanceOf(Timestamp)
+}
+
+/** A reader's conference input, mailed as a letter to their rep and senator. */
+export type LetterRecord = Static
+export const LetterRecord = Record({
+ kind: L("letter"),
+ ...Sent,
+ uid: String,
+ testimonyId: String,
+ /** The version of the published testimony the letter was written from. */
+ version: Number,
+ conferenceId: String,
+ /** Sent as an update to an earlier letter, "(updated)" in its subject. */
+ updated: Boolean
+})
+
+/** One conference's weekly update to its conferees. */
+export type ConferenceUpdateRecord = Static
+export const ConferenceUpdateRecord = Record({
+ kind: L("conferenceUpdate"),
+ ...Sent,
+ conferenceId: String,
+ /** Monday 00:00, Eastern, of the week the update is named for. */
+ weekStart: InstanceOf(Timestamp),
+ /** The input on the conference when the update was compiled, where its
+ * changes end. */
+ standing: Counts,
+ changes: Changes
+})
+
+/** An email MAPLE sent to legislators, as sent. Written with the `emails`
+ * document that sends it, under the same id.
+ * Firestore: /legislatorMail/{emails document id} */
+export type LegislatorMail = Static
+export const LegislatorMail = Union(LetterRecord, ConferenceUpdateRecord)
+
+/** An email to legislators as the send-email extension takes it, before its
+ * recipients are resolved: the subject is the one they see. */
+export type LegislatorEmail = {
+ from: string
+ replyTo: string
+ cc?: string[]
+ headers: { [name: string]: string }
+ message: { subject: string; text: string; html: string }
+}
diff --git a/scripts/firebase-admin/runConferenceMail.ts b/scripts/firebase-admin/runConferenceMail.ts
index 4da75639c..45bb5f877 100644
--- a/scripts/firebase-admin/runConferenceMail.ts
+++ b/scripts/firebase-admin/runConferenceMail.ts
@@ -73,6 +73,6 @@ export const script: Script = async ({ env, args }) => {
if (job === "letters") await sendConferenceLetters(now.toJSDate())
else await compileWeeklyUpdates(now.toJSDate())
console.log(
- "Check /emails (delivery) and /conferenceLetterSkips in the emulator UI"
+ "Check /emails (delivery), /legislatorMail and /conferenceLetterSkips in the emulator UI"
)
}
diff --git a/tests/integration/conferenceLetters.test.ts b/tests/integration/conferenceLetters.test.ts
index cb47dab23..e8a27a3ca 100644
--- a/tests/integration/conferenceLetters.test.ts
+++ b/tests/integration/conferenceLetters.test.ts
@@ -1,7 +1,10 @@
+import { doc, getDoc } from "firebase/firestore"
import { nanoid } from "nanoid"
+import { firestore } from "../../components/firebase"
import { sendConferenceLetters } from "../../functions/src/conferenceLetters/sendLetters"
import { Timestamp } from "../../functions/src/firebase"
import { testAuth, testDb } from "../testUtils"
+import { expectPermissionDenied, signInUser1, signInUser2 } from "./common"
const COURT = 194
const TEST_RECIPIENT = "test-inbox@example.com"
@@ -79,6 +82,8 @@ const seed = async (conferenceId: string, s: Seed = {}) => {
const email = async (eventId: string) =>
(await testDb.doc(`emails/letter_${eventId}`).get()).data()
+const record = async (eventId: string) =>
+ (await testDb.doc(`legislatorMail/letter_${eventId}`).get()).data()
const skip = async (eventId: string) =>
(await testDb.doc(`conferenceLetterSkips/${eventId}`).get()).data()
@@ -105,7 +110,7 @@ beforeAll(async () => {
describe("sendConferenceLetters", () => {
it("mails a letter once, to the test address, copying the reader", async () => {
- const { eventId, uid } = await seed(conferenceId, {
+ const { eventId, uid, testimonyId } = await seed(conferenceId, {
rep,
sen,
replyTo: "chosen@example.com"
@@ -129,10 +134,27 @@ describe("sendConferenceLetters", () => {
"constituent-letters"
])
+ // The record names the legislators, not the test address, and the
+ // subject without the test prefix.
+ const sentRecord = await record(eventId)
+ expect(sentRecord).toEqual({
+ kind: "letter",
+ court: COURT,
+ memberCodes: [rep, sen],
+ subject: "Phone-Free Schools: Please pass something",
+ html: sent!.message.html,
+ text: sent!.message.text,
+ createdAt: sent!.createdAt,
+ uid,
+ testimonyId,
+ version: 1,
+ conferenceId,
+ updated: false
+ })
+
const second = await sendConferenceLetters(NOW)
expect(second).toBe(0)
expect((await email(eventId))!.createdAt).toEqual(sent!.createdAt)
- expect(uid).toBeTruthy()
})
it("replies to the reader's own address without an override", async () => {
@@ -151,6 +173,7 @@ describe("sendConferenceLetters", () => {
const sent = await email(eventId)
expect(sent!.message.subject).toContain("rep@malegislature.gov —")
expect(sent!.message.html).not.toContain("Senator")
+ expect((await record(eventId))!.memberCodes).toEqual([rep])
})
it.each<[string, Seed, string]>([
@@ -162,6 +185,7 @@ describe("sendConferenceLetters", () => {
const { eventId } = await seed(conferenceId, s)
await sendConferenceLetters(NOW)
expect(await email(eventId)).toBeUndefined()
+ expect(await record(eventId)).toBeUndefined()
expect((await skip(eventId))!.reason).toBe(reason)
})
@@ -189,6 +213,10 @@ describe("sendConferenceLetters", () => {
expect((await email(eventId))!.message.subject).toMatch(
/Phone-Free Schools: Please pass something \(updated\)$/
)
+ expect(await record(eventId)).toMatchObject({
+ subject: "Phone-Free Schools: Please pass something (updated)",
+ updated: true
+ })
})
it("mails an input posted and edited the same day once, as new", async () => {
@@ -211,3 +239,19 @@ describe("sendConferenceLetters", () => {
)
})
})
+
+describe("legislatorMail", () => {
+ it("lets a reader read only the records of their own letters", async () => {
+ const reader = await signInUser1()
+ const recordId = `letter_${id()}`
+ await testDb.doc(`legislatorMail/${recordId}`).set({
+ kind: "letter",
+ uid: reader.uid
+ })
+ const ref = doc(firestore, `legislatorMail/${recordId}`)
+ expect((await getDoc(ref)).exists()).toBe(true)
+
+ await signInUser2()
+ await expectPermissionDenied(getDoc(ref))
+ })
+})
diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts
index 567dd2b5e..b18558af4 100644
--- a/tests/integration/testimony.test.ts
+++ b/tests/integration/testimony.test.ts
@@ -1424,6 +1424,11 @@ describe("publishTestimony", () => {
})
describe("conference weekly update", () => {
+ const env = { ...process.env }
+ afterEach(() => {
+ process.env = { ...env }
+ })
+
const eventsOf = (conferenceId: string) =>
testDb
.collection("/conferenceInputEvents")
@@ -1448,8 +1453,28 @@ describe("conference weekly update", () => {
return { uid, draftId, publicationId: res.data.publicationId }
}
+ /** The `i`th conferee, whose member document has `address`; null stands
+ * for a member document without one. */
+ const conferee = async (address: string | null, i = 0) => {
+ const memberCode = `T${nanoid(6)}`
+ await testDb
+ .doc(`/generalCourts/${currentGeneralCourt}/members/${memberCode}`)
+ .set({
+ id: memberCode,
+ content: { Name: `Test Member${i}`, EmailAddress: address }
+ })
+ return {
+ memberCode,
+ branch: i % 2 ? ("House" as const) : ("Senate" as const),
+ chair: i < 2
+ }
+ }
+
it("compiles one update from a week of changes", async () => {
- const conferenceId = await createConference(billId)
+ process.env.GCLOUD_PROJECT = "digital-testimony-prod"
+ const conferenceId = await createConference(billId, {
+ conferees: [await conferee("one@example.org")]
+ })
const first = await fileInput(
signInUser1,
billId,
@@ -1482,10 +1507,11 @@ describe("conference weekly update", () => {
// The week these events fall in is the one before next Monday.
const now = weekWindow(new Date()).week.plus({ weeks: 2, hours: 1 })
const weekStart = weekWindow(now.toJSDate()).week.toISODate()
- const path = `/conferenceUpdates/${currentGeneralCourt}_${conferenceId}_${weekStart}`
+ const path = `/legislatorMail/${currentGeneralCourt}_${conferenceId}_${weekStart}`
await compileWeeklyUpdates(now.toJSDate())
const update = (await testDb.doc(path).get()).data() as any
+ expect(update.kind).toBe("conferenceUpdate")
expect(update.standing.total).toBe(1)
expect(update.standing.byPosition.passNothing).toBe(1)
expect(update.changes).toMatchObject({
@@ -1498,7 +1524,7 @@ describe("conference weekly update", () => {
`https://mapletestimony.org/conferences/${currentGeneralCourt}/${conferenceId}`
)
- // A second run in the same week leaves the update as it was.
+ // A second run in the same week sends and records nothing more.
await compileWeeklyUpdates(now.toJSDate())
expect((await testDb.doc(path).get()).data()!.createdAt).toEqual(
update.createdAt
@@ -1506,28 +1532,12 @@ describe("conference weekly update", () => {
}, 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
- })
+ conferees.push(await conferee(address, i))
}
const conferenceId = await createConference(billId, { conferees })
await fileInput(signInUser1, billId, conferenceId, "passNothing")
@@ -1565,11 +1575,15 @@ describe("conference weekly 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]
- })
+ // The record names the conferees, not the test address.
+ const update = (await testDb.doc(`/legislatorMail/${id}`).get()).data()
+ expect(update!.memberCodes).toEqual([
+ conferees[0].memberCode,
+ conferees[2].memberCode
+ ])
+ expect(email.message.subject).toContain(update!.subject)
+ expect(update!.subject).not.toContain("[dev]")
+ expect(update!.createdAt).toEqual(email.createdAt)
await compileWeeklyUpdates(now)
expect((await emailOf(id)).createdAt).toEqual(email.createdAt)
@@ -1588,15 +1602,16 @@ describe("conference weekly update", () => {
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])
+ it("sends and records nothing when no conferee has an address", async () => {
+ process.env.GCLOUD_PROJECT = "digital-testimony-dev"
+ process.env.LETTER_TEST_RECIPIENT = "test@example.org"
+ const { now, id } = await conferenceWith([null])
await compileWeeklyUpdates(now)
expect(await emailOf(id)).toBeUndefined()
- expect(
- (await testDb.doc(`/conferenceUpdates/${id}`).get()).data()!.delivery
- ).toEqual({ sentTo: [], skipped: [conferees[0].memberCode] })
+ expect((await testDb.doc(`/legislatorMail/${id}`).get()).exists).toBe(
+ false
+ )
}, 60000)
})
@@ -1617,7 +1632,7 @@ describe("conference weekly update", () => {
(
await testDb
.doc(
- `/conferenceUpdates/${currentGeneralCourt}_${conferenceId}_${weekStart}`
+ `/legislatorMail/${currentGeneralCourt}_${conferenceId}_${weekStart}`
)
.get()
).exists
From 4144842decf592276b061250ab9ddb1010c84b23 Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 15:50:47 -0500
Subject: [PATCH 12/17] Decide repeat and updated letters from the record of
what was sent
The letter job decided whether an edit was an update from the events in
its own run, so a version already mailed could go out again and an edit
could be marked "(updated)" after an original that was never sent. It now
reads /legislatorMail: an event whose own letter is on record is done, an
input whose current version already has a letter is skipped with the new
reason alreadySent, and a letter is an update only when an earlier one
for that input is on record. The reader's preview asks the same question
of their own records, so its subject matches the letter that is sent.
Closes hyphacoop/maple#147
Verified: conferenceLetters and testimony integration suites (92) on a
throwaway emulator with a Typesense stub, including each version mailed
once, an edit after a skipped original going out unmarked, an edit after
a sent letter marked updated, and the preview's query under the read rule;
InputForm and CosignForm tests for the preview's subject; functions jest
for conference letters, updates and email; test:website, check-types,
lint, build:dev.
Not verified: the preview in a running site; the preview's two-field
query against real Firestore indexes; a real scheduled run.
Co-Authored-By: Claude Opus 5.5
---
.../conference/input/CosignForm.test.tsx | 3 +
components/conference/input/CosignForm.tsx | 2 +-
.../conference/input/InputForm.test.tsx | 28 ++-
components/conference/input/InputForm.tsx | 2 +-
components/conference/input/Review.tsx | 9 +-
components/db/legislatorMail.ts | 29 +++
functions/src/conferenceLetters/letters.ts | 1 +
.../src/conferenceLetters/sendLetters.ts | 48 ++---
functions/src/legislatorMail/records.ts | 14 ++
tests/integration/conferenceLetters.test.ts | 177 +++++++++++++-----
10 files changed, 242 insertions(+), 71 deletions(-)
create mode 100644 components/db/legislatorMail.ts
create mode 100644 functions/src/legislatorMail/records.ts
diff --git a/components/conference/input/CosignForm.test.tsx b/components/conference/input/CosignForm.test.tsx
index 9c993c07e..3e0f384c0 100644
--- a/components/conference/input/CosignForm.test.tsx
+++ b/components/conference/input/CosignForm.test.tsx
@@ -28,6 +28,9 @@ jest.mock("components/db", () => ({
useProfile: () => ({ profile: mockProfile.current }),
useMember: () => ({ member: undefined })
}))
+jest.mock("components/db/legislatorMail", () => ({
+ useLetterOnRecord: () => false
+}))
const legislators = {
representative: { id: "r1", name: "Rae Rep", district: "3rd Middlesex" },
diff --git a/components/conference/input/CosignForm.tsx b/components/conference/input/CosignForm.tsx
index e2af112e5..ab87a7a82 100644
--- a/components/conference/input/CosignForm.tsx
+++ b/components/conference/input/CosignForm.tsx
@@ -418,7 +418,7 @@ export function CosignForm({
conferees={conferees}
author={name}
conference={subject}
- edited={editing}
+ testimonyId={edit.publication?.id}
position={parent.position}
content={content}
cosign={{ author: nameOf(parent), content: parent.content }}
diff --git a/components/conference/input/InputForm.test.tsx b/components/conference/input/InputForm.test.tsx
index c08322129..42f7e2664 100644
--- a/components/conference/input/InputForm.test.tsx
+++ b/components/conference/input/InputForm.test.tsx
@@ -17,7 +17,14 @@ jest.mock("components/auth", () => ({
const mockProfile: { current: Record } = { current: {} }
jest.mock("components/db", () => ({
useProfile: () => ({ profile: mockProfile.current }),
- useMember: () => ({ member: undefined })
+ useMember: () => ({ member: undefined }),
+ hasDraftChanged: () => true
+}))
+// The testimony ids with a letter on record.
+const mockOnRecord = new Set()
+jest.mock("components/db/legislatorMail", () => ({
+ useLetterOnRecord: (testimonyId?: string) =>
+ !!testimonyId && mockOnRecord.has(testimonyId)
}))
beforeEach(() => {
@@ -102,6 +109,25 @@ describe("InputForm", () => {
).toBeInTheDocument()
})
+ it("marks the letter updated only when an earlier one is on record", () => {
+ const publication = { id: "t1", position: "passSomething", content: "" }
+ const edit = editing({
+ publication
+ } as Partial)
+ const { unmount } = review(edit)
+ expect(screen.getByText(/^Phone-Free Schools: /)).not.toHaveTextContent(
+ "(updated)"
+ )
+ unmount()
+
+ mockOnRecord.add("t1")
+ review(edit)
+ expect(screen.getByText(/^Phone-Free Schools: /)).toHaveTextContent(
+ /\(updated\)$/
+ )
+ mockOnRecord.clear()
+ })
+
it("says no one will be emailed when the profile has no legislators", () => {
mockProfile.current = { fullName: "Gina K." }
review(editing())
diff --git a/components/conference/input/InputForm.tsx b/components/conference/input/InputForm.tsx
index 73853f5af..695e83e4e 100644
--- a/components/conference/input/InputForm.tsx
+++ b/components/conference/input/InputForm.tsx
@@ -422,7 +422,7 @@ export function InputForm({
conferees={conferees}
author={name}
conference={subject}
- edited={editing}
+ testimonyId={edit.publication?.id}
position={position}
content={content}
chosen={chosen}
diff --git a/components/conference/input/Review.tsx b/components/conference/input/Review.tsx
index 83faa558f..b5ec46b27 100644
--- a/components/conference/input/Review.tsx
+++ b/components/conference/input/Review.tsx
@@ -3,6 +3,7 @@ import { useTranslation } from "next-i18next"
import styled from "styled-components"
import { useAuth } from "components/auth"
import { ProfileMember, useMember, useProfile } from "components/db"
+import { useLetterOnRecord } from "components/db/legislatorMail"
import {
ConferencePosition,
hasLegislators,
@@ -466,7 +467,7 @@ export function LetterReview({
conferees,
author,
conference,
- edited,
+ testimonyId,
position,
content,
cosign,
@@ -479,8 +480,9 @@ export function LetterReview({
conferees: string[]
author: string
conference: string
- /** The reader is editing input they posted before. */
- edited: boolean
+ /** The input the reader posted before, when editing it. The letter is an
+ * update when one for it is on record. */
+ testimonyId?: string
position: ConferencePosition
/** The reader's own words, empty on a cosign without a note. */
content: string
@@ -492,6 +494,7 @@ export function LetterReview({
}) {
const { representative, senator } = useLegislators(court)
const { own, replyTo } = useReplyTo(chosen)
+ const edited = useLetterOnRecord(testimonyId)
return (
<>
!!uid && !!testimonyId && hasLetterOnRecord(uid, testimonyId),
+ [uid, testimonyId]
+ )
+ return result ?? false
+}
diff --git a/functions/src/conferenceLetters/letters.ts b/functions/src/conferenceLetters/letters.ts
index 112b6972a..ce26f9f02 100644
--- a/functions/src/conferenceLetters/letters.ts
+++ b/functions/src/conferenceLetters/letters.ts
@@ -15,6 +15,7 @@ export function dayWindow(now: Date) {
export type SkipReason =
| "withdrawn"
| "superseded"
+ | "alreadySent"
| "noVerifiedEmail"
| "noLegislator"
| "noLegislatorEmail"
diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts
index 15aa86524..171e49ce9 100644
--- a/functions/src/conferenceLetters/sendLetters.ts
+++ b/functions/src/conferenceLetters/sendLetters.ts
@@ -8,6 +8,7 @@ import { stateOf } from "../conferenceUpdates/inputEvents"
import { ConferenceInputEvent } from "../conferenceUpdates/types"
import { Testimony } from "../testimony/types"
import { queueLegislatorEmail } from "../legislatorMail/queue"
+import { lastLetterFor } from "../legislatorMail/records"
import { conferenceName } from "../shared/conferences"
import { hasLegislators, looksLikeEmail, renderLetter } from "./letter"
import { buildLetterEmail, dayWindow, SkipReason } from "./letters"
@@ -43,7 +44,6 @@ async function mailLetter(
e: ConferenceInputEvent,
eventId: string,
lastFor: Map,
- edited: boolean,
memberContent: (key: string) => Promise,
conferenceOf: (key: string) => Promise
): Promise {
@@ -51,20 +51,30 @@ async function mailLetter(
// with what is posted now.
if (lastFor.get(e.testimonyId) !== eventId) return "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)
- .where("court", "==", e.court)
- .limit(1)
- .get(),
- conferenceOf(`${e.court}/${e.conferenceId}`)
- ])
+ const letterId = `letter_${eventId}`
+ const [own, last, pubSnap, readerEmail, draft, conference] =
+ await Promise.all([
+ db.doc(`/legislatorMail/${letterId}`).get(),
+ lastLetterFor(e.testimonyId),
+ db.doc(`/users/${e.authorUid}/publishedTestimony/${e.testimonyId}`).get(),
+ verifiedEmail(e.authorUid),
+ db
+ .collection(`/users/${e.authorUid}/draftTestimony`)
+ .where("conferenceId", "==", e.conferenceId)
+ .where("court", "==", e.court)
+ .limit(1)
+ .get(),
+ conferenceOf(`${e.court}/${e.conferenceId}`)
+ ])
+ // This event's letter went out on an earlier run.
+ if (own.exists) return "exists"
const pub = pubSnap.data() as Testimony | undefined
const state = pub && stateOf(pub)
if (!pub || !state) return "withdrawn"
+ // A version goes out once, and a letter is an update only to one that went
+ // out before.
+ if (last?.version === pub.version) return "alreadySent"
+ const updated = !!last
if (!readerEmail) return "noVerifiedEmail"
if (!pub.representativeId && !pub.senatorId) return "noLegislator"
if (!conference) return "noConference"
@@ -118,7 +128,7 @@ async function mailLetter(
court: e.court,
conferenceId: e.conferenceId,
conference: conferenceName(conference),
- edited,
+ edited: updated,
position: state.position,
content: pub.content,
cosign,
@@ -126,7 +136,7 @@ async function mailLetter(
senator
})
return queueLegislatorEmail(
- `letter_${eventId}`,
+ letterId,
reachable.flatMap(m => m?.email ?? []),
buildLetterEmail({
letter,
@@ -141,7 +151,7 @@ async function mailLetter(
testimonyId: e.testimonyId,
version: pub.version,
conferenceId: e.conferenceId,
- updated: edited
+ updated
}
)
}
@@ -165,12 +175,6 @@ export async function sendConferenceLetters(now: Date) {
.filter(({ e }) => e.change !== "withdrawn")
.sort((a, b) => a.e.at.toMillis() - b.e.at.toMillis())
const lastFor = new Map(written.map(({ id, e }) => [e.testimonyId, id]))
- // An edit is an update unless the input was also posted in the window.
- const postedIn = new Set(
- written
- .filter(({ e }) => e.change === "added")
- .map(({ e }) => e.testimonyId)
- )
const memberContent = cached(
async key =>
(
@@ -189,12 +193,10 @@ export async function sendConferenceLetters(now: Date) {
let sent = 0
for (const { id, e } of written) {
try {
- const edited = !postedIn.has(e.testimonyId)
const outcome = await mailLetter(
e,
id,
lastFor,
- edited,
memberContent,
conferenceOf
)
diff --git a/functions/src/legislatorMail/records.ts b/functions/src/legislatorMail/records.ts
new file mode 100644
index 000000000..87d00567f
--- /dev/null
+++ b/functions/src/legislatorMail/records.ts
@@ -0,0 +1,14 @@
+import { db } from "../firebase"
+import { LetterRecord } from "./types"
+
+/** The last letter on record for a reader's input, if one went out. Only
+ * letters carry a `testimonyId`. */
+export async function lastLetterFor(testimonyId: string) {
+ const snap = await db
+ .collection("/legislatorMail")
+ .where("testimonyId", "==", testimonyId)
+ .orderBy("createdAt", "desc")
+ .limit(1)
+ .get()
+ return snap.docs[0]?.data() as LetterRecord | undefined
+}
diff --git a/tests/integration/conferenceLetters.test.ts b/tests/integration/conferenceLetters.test.ts
index e8a27a3ca..9de703f17 100644
--- a/tests/integration/conferenceLetters.test.ts
+++ b/tests/integration/conferenceLetters.test.ts
@@ -1,5 +1,13 @@
-import { doc, getDoc } from "firebase/firestore"
+import {
+ collection,
+ doc,
+ getDoc,
+ getDocs,
+ query,
+ where
+} from "firebase/firestore"
import { nanoid } from "nanoid"
+import { hasLetterOnRecord } from "../../components/db/legislatorMail"
import { firestore } from "../../components/firebase"
import { sendConferenceLetters } from "../../functions/src/conferenceLetters/sendLetters"
import { Timestamp } from "../../functions/src/firebase"
@@ -30,14 +38,41 @@ type Seed = {
sen?: string
withdrawn?: boolean
replyTo?: string
- change?: "added" | "edited"
+}
+
+type Input = { uid: string; testimonyId: string; conferenceId: string }
+
+/** An event saying the reader posted or edited their input, to `version`, at
+ * `at`. */
+const event = async (
+ input: Input,
+ change: "added" | "edited",
+ version: number,
+ at: Date
+) => {
+ const eventId = id()
+ const state = (version: number) => ({
+ position: "passSomething",
+ group: "individual",
+ version
+ })
+ await testDb.doc(`conferenceInputEvents/${eventId}`).set({
+ court: COURT,
+ conferenceId: input.conferenceId,
+ testimonyId: input.testimonyId,
+ authorUid: input.uid,
+ change,
+ ...(change === "edited" && { before: state(version - 1) }),
+ after: state(version),
+ at: Timestamp.fromDate(at)
+ })
+ return eventId
}
/** A reader who posted conference input yesterday, and the event that says so. */
const seed = async (conferenceId: string, s: Seed = {}) => {
const uid = id()
const testimonyId = id()
- const eventId = id()
await testAuth.createUser({
uid,
email: `${uid}@example.com`,
@@ -65,21 +100,22 @@ const seed = async (conferenceId: string, s: Seed = {}) => {
court: COURT,
...(s.replyTo ? { replyTo: s.replyTo } : {})
})
- await testDb.doc(`conferenceInputEvents/${eventId}`).set({
- court: COURT,
- conferenceId,
- testimonyId,
- authorUid: uid,
- change: s.change ?? "added",
- ...(s.change === "edited" && {
- before: { position: "passNothing", group: "individual", version: 0 }
- }),
- after: { position: "passSomething", group: "individual", version: 1 },
- at: Timestamp.fromDate(ADDED_AT)
- })
- return { uid, eventId, testimonyId }
+ const input = { uid, testimonyId, conferenceId }
+ const eventId = await event(input, "added", 1, ADDED_AT)
+ return { ...input, eventId }
}
+const DAY = 24 * 60 * 60 * 1000
+/** The run `days` after the first, and its window's noon. */
+const run = (days: number) => new Date(NOW.getTime() + days * DAY)
+const noon = (days: number) => new Date(ADDED_AT.getTime() + days * DAY)
+
+/** The reader's input as published now, at `version`. */
+const publishVersion = (input: Input, version: number) =>
+ testDb
+ .doc(`users/${input.uid}/publishedTestimony/${input.testimonyId}`)
+ .update({ version })
+
const email = async (eventId: string) =>
(await testDb.doc(`emails/letter_${eventId}`).get()).data()
const record = async (eventId: string) =>
@@ -191,47 +227,81 @@ describe("sendConferenceLetters", () => {
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))
- })
+ const secondId = await event(
+ first,
+ "added",
+ 2,
+ new Date(ADDED_AT.getTime() + 60_000)
+ )
await sendConferenceLetters(NOW)
expect(await email(first.eventId)).toBeUndefined()
expect((await skip(first.eventId))!.reason).toBe("superseded")
expect(await email(secondId)).toBeDefined()
})
- it("mails an edit as an update", async () => {
- const { eventId } = await seed(conferenceId, { rep, change: "edited" })
- await sendConferenceLetters(NOW)
- expect((await email(eventId))!.message.subject).toMatch(
+ it("mails an edit as an update to the letter on record", async () => {
+ const posted = await seed(conferenceId, { rep })
+ await sendConferenceLetters(run(0))
+ expect(await record(posted.eventId)).toMatchObject({ updated: false })
+
+ await publishVersion(posted, 2)
+ const editId = await event(posted, "edited", 2, noon(1))
+ await sendConferenceLetters(run(1))
+ expect((await email(editId))!.message.subject).toMatch(
/Phone-Free Schools: Please pass something \(updated\)$/
)
- expect(await record(eventId)).toMatchObject({
+ expect(await record(editId)).toMatchObject({
subject: "Phone-Free Schools: Please pass something (updated)",
+ version: 2,
updated: true
})
})
+ it("mails each version once", async () => {
+ // Posted the evening before the run, edited the morning after it, before
+ // the run read what was posted: the run mails the edit with the post.
+ const posted = await seed(conferenceId, { rep })
+ await publishVersion(posted, 2)
+ await sendConferenceLetters(run(0))
+ expect(await record(posted.eventId)).toMatchObject({
+ version: 2,
+ updated: false
+ })
+
+ const editId = await event(posted, "edited", 2, noon(1))
+ await sendConferenceLetters(run(1))
+ expect(await email(editId)).toBeUndefined()
+ expect((await skip(editId))!.reason).toBe("alreadySent")
+
+ await publishVersion(posted, 3)
+ const againId = await event(posted, "edited", 3, noon(2))
+ await sendConferenceLetters(run(2))
+ expect(await record(againId)).toMatchObject({ version: 3, updated: true })
+ })
+
+ it("mails an edit to input whose letter was skipped as new", async () => {
+ const posted = await seed(conferenceId, { rep, verified: false })
+ await sendConferenceLetters(run(0))
+ expect((await skip(posted.eventId))!.reason).toBe("noVerifiedEmail")
+
+ await testAuth.updateUser(posted.uid, { emailVerified: true })
+ await publishVersion(posted, 2)
+ const editId = await event(posted, "edited", 2, noon(1))
+ await sendConferenceLetters(run(1))
+ expect((await email(editId))!.message.subject).toMatch(
+ /Please pass something$/
+ )
+ expect(await record(editId)).toMatchObject({ updated: false })
+ })
+
it("mails an input posted and edited the same day once, as new", async () => {
const first = await seed(conferenceId, { rep })
- const editId = id()
- await testDb.doc(`conferenceInputEvents/${editId}`).set({
- court: COURT,
- conferenceId,
- testimonyId: first.testimonyId,
- authorUid: first.uid,
- change: "edited",
- before: { position: "passSomething", group: "individual", version: 1 },
- after: { position: "passSomething", group: "individual", version: 2 },
- at: Timestamp.fromDate(new Date(ADDED_AT.getTime() + 60_000))
- })
+ const editId = await event(
+ first,
+ "edited",
+ 2,
+ new Date(ADDED_AT.getTime() + 60_000)
+ )
await sendConferenceLetters(NOW)
expect((await skip(first.eventId))!.reason).toBe("superseded")
expect((await email(editId))!.message.subject).toMatch(
@@ -254,4 +324,27 @@ describe("legislatorMail", () => {
await signInUser2()
await expectPermissionDenied(getDoc(ref))
})
+
+ it("tells the reader's preview whether a letter for their input is on record", async () => {
+ const reader = await signInUser1()
+ const testimonyId = id()
+ await testDb.doc(`legislatorMail/letter_${id()}`).set({
+ kind: "letter",
+ uid: reader.uid,
+ testimonyId
+ })
+ expect(await hasLetterOnRecord(reader.uid, testimonyId)).toBe(true)
+ expect(await hasLetterOnRecord(reader.uid, id())).toBe(false)
+
+ const other = await signInUser2()
+ expect(await hasLetterOnRecord(other.uid, testimonyId)).toBe(false)
+ await expectPermissionDenied(
+ getDocs(
+ query(
+ collection(firestore, "legislatorMail"),
+ where("testimonyId", "==", testimonyId)
+ )
+ )
+ )
+ })
})
From 483d2de2117003e34fbc46573437534d80a1939b Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 16:43:24 -0500
Subject: [PATCH 13/17] Mail conference input by when its event was recorded
The letter and weekly update jobs read the events whose publication was
written in their window, but the trigger records an event after that
write, sometimes after the run has read the window. Such an event was in
no window: no letter, no skip, missing from the update. Events now carry
recordedAt, the time the event itself was written, and both jobs read by
it, so a late event goes out with the next run. The trigger creates the
event, so a second delivery keeps the first time.
A letter or update that fails now fails the run after the others, and
the schedules retry it; what was sent or skipped is created by id, so a
retry repeats nothing. An event already mailed or skipped is left alone
on any later run, read once for the window, so a rerun no longer records
a mailed event as superseded and a skip is final.
Verified: conferenceLetters and testimony integration (94) on a
throwaway emulator with a Typesense stub, including an edit recorded
just after a run going out on the next, and a rerun after a later edit
leaving the first letter alone; functions jest for conference letters,
updates and email; functions tsc, check-types, lint, formatting.
Not verified: a run failing and being retried; that a retried run gets
the original scheduleTime from Cloud Scheduler; a real scheduled run.
Co-Authored-By: Claude Opus 5.5
---
.../src/conferenceLetters/sendLetters.ts | 60 ++++++++++++-------
.../conferenceUpdates/compileWeeklyUpdates.ts | 21 +++++--
.../src/conferenceUpdates/inputEvents.ts | 25 +++++---
functions/src/conferenceUpdates/types.ts | 8 ++-
tests/integration/conferenceLetters.test.ts | 46 +++++++++++++-
tests/integration/testimony.test.ts | 4 +-
6 files changed, 120 insertions(+), 44 deletions(-)
diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts
index 171e49ce9..119e08d2a 100644
--- a/functions/src/conferenceLetters/sendLetters.ts
+++ b/functions/src/conferenceLetters/sendLetters.ts
@@ -52,22 +52,18 @@ async function mailLetter(
if (lastFor.get(e.testimonyId) !== eventId) return "superseded"
const letterId = `letter_${eventId}`
- const [own, last, pubSnap, readerEmail, draft, conference] =
- await Promise.all([
- db.doc(`/legislatorMail/${letterId}`).get(),
- lastLetterFor(e.testimonyId),
- db.doc(`/users/${e.authorUid}/publishedTestimony/${e.testimonyId}`).get(),
- verifiedEmail(e.authorUid),
- db
- .collection(`/users/${e.authorUid}/draftTestimony`)
- .where("conferenceId", "==", e.conferenceId)
- .where("court", "==", e.court)
- .limit(1)
- .get(),
- conferenceOf(`${e.court}/${e.conferenceId}`)
- ])
- // This event's letter went out on an earlier run.
- if (own.exists) return "exists"
+ const [last, pubSnap, readerEmail, draft, conference] = await Promise.all([
+ lastLetterFor(e.testimonyId),
+ db.doc(`/users/${e.authorUid}/publishedTestimony/${e.testimonyId}`).get(),
+ verifiedEmail(e.authorUid),
+ db
+ .collection(`/users/${e.authorUid}/draftTestimony`)
+ .where("conferenceId", "==", e.conferenceId)
+ .where("court", "==", e.court)
+ .limit(1)
+ .get(),
+ conferenceOf(`${e.court}/${e.conferenceId}`)
+ ])
const pub = pubSnap.data() as Testimony | undefined
const state = pub && stateOf(pub)
if (!pub || !state) return "withdrawn"
@@ -156,18 +152,19 @@ async function mailLetter(
)
}
-/** Mails each conference input posted or edited since the run a day before `now` to
+/** Mails each conference input event recorded since the run a day before `now` to
* the reader's rep and senator, as the letter the reader previewed. A letter is the
* `emails` document /emails/letter_{input event id} and its record at
* /legislatorMail/letter_{input event id}, created, so a second run sends
* nothing again. An input that is not mailed is recorded, with why, at
- * /conferenceLetterSkips/{input event id}. */
+ * /conferenceLetterSkips/{input event id}. A letter that fails fails the run,
+ * after the others, so the scheduler runs it again. */
export async function sendConferenceLetters(now: Date) {
const { start, end } = dayWindow(now)
const events = await db
.collection("/conferenceInputEvents")
- .where("at", ">=", Timestamp.fromDate(start.toJSDate()))
- .where("at", "<", Timestamp.fromDate(end.toJSDate()))
+ .where("recordedAt", ">=", Timestamp.fromDate(start.toJSDate()))
+ .where("recordedAt", "<", Timestamp.fromDate(end.toJSDate()))
.get()
const written = events.docs
@@ -175,6 +172,19 @@ export async function sendConferenceLetters(now: Date) {
.filter(({ e }) => e.change !== "withdrawn")
.sort((a, b) => a.e.at.toMillis() - b.e.at.toMillis())
const lastFor = new Map(written.map(({ id, e }) => [e.testimonyId, id]))
+ // An event mailed or skipped on an earlier run is decided, and a rerun
+ // leaves it: a skip is final.
+ const decided = written.length
+ ? await db.getAll(
+ ...written.flatMap(({ id }) => [
+ db.doc(`/legislatorMail/letter_${id}`),
+ db.doc(`/conferenceLetterSkips/${id}`)
+ ])
+ )
+ : []
+ const done = new Set(
+ decided.filter(d => d.exists).map(d => d.id.replace(/^letter_/, ""))
+ )
const memberContent = cached(
async key =>
(
@@ -191,7 +201,9 @@ export async function sendConferenceLetters(now: Date) {
)
let sent = 0
+ const failed: string[] = []
for (const { id, e } of written) {
+ if (done.has(id)) continue
try {
const outcome = await mailLetter(
e,
@@ -214,18 +226,22 @@ export async function sendConferenceLetters(now: Date) {
}
} catch (err) {
logger.error(`Letter for input event ${id} failed`, err)
+ failed.push(id)
}
}
logger.info(
`Mailed ${sent} conference letters from ${start.toISO()} to ${end.toISO()}`
)
+ if (failed.length) {
+ throw new Error(`Letters for input events ${failed.join(", ")} failed`)
+ }
return sent
}
export const mailConferenceLetters = onSchedule(
- { schedule: "0 9 * * *", timeZone, timeoutSeconds: 300 },
+ { schedule: "0 9 * * *", timeZone, timeoutSeconds: 300, retryCount: 3 },
// The time the run was due, not when it started, so each day's window
- // begins exactly where the last one ended.
+ // begins exactly where the last one ended, and a retry reads the same one.
event =>
sendConferenceLetters(new Date(event.scheduleTime)).then(() => undefined)
)
diff --git a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
index fea1cbde9..258f38c6b 100644
--- a/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
+++ b/functions/src/conferenceUpdates/compileWeeklyUpdates.ts
@@ -41,16 +41,17 @@ const conferee = async (court: number, c: Conference["conferees"][number]) => {
}
/** Compiles the update for each appointed conference that had a
- * change in the week up to `now`, and mails it to the conferees as
+ * change recorded in the week up to `now`, and mails it to the conferees as
* /emails/{court}_{conferenceId}_{week}, recorded at /legislatorMail under the
* same id. Both are created, so a second run in the same week sends nothing
- * again. */
+ * again. An update that fails fails the run, after the others, so the
+ * scheduler runs it again. */
export async function compileWeeklyUpdates(now: Date) {
const { week, start, end } = weekWindow(now)
const events = await db
.collection("/conferenceInputEvents")
- .where("at", ">=", Timestamp.fromDate(start.toJSDate()))
- .where("at", "<", Timestamp.fromDate(end.toJSDate()))
+ .where("recordedAt", ">=", Timestamp.fromDate(start.toJSDate()))
+ .where("recordedAt", "<", Timestamp.fromDate(end.toJSDate()))
.get()
const eventsOf = new Map()
@@ -61,6 +62,7 @@ export async function compileWeeklyUpdates(now: Date) {
}
let sent = 0
+ const failed: string[] = []
for (const [key, conferenceEvents] of eventsOf) {
const { court, conferenceId: id } = conferenceEvents[0]
try {
@@ -136,18 +138,25 @@ export async function compileWeeklyUpdates(now: Date) {
}
} catch (e) {
logger.error(`Weekly update for conference ${key} failed`, e)
+ failed.push(key)
}
}
logger.info(`Mailed ${sent} weekly conference updates`)
+ if (failed.length) {
+ throw new Error(
+ `Weekly updates for conferences ${failed.join(", ")} failed`
+ )
+ }
}
export const compileConferenceUpdates = onSchedule(
{
schedule: "0 8 * * 1", // Monday 8:00 AM
timeZone,
- timeoutSeconds: 300
+ timeoutSeconds: 300,
+ retryCount: 3
},
// The time the run was due, not when it started, so each week's window
- // begins exactly where the last one ended.
+ // begins exactly where the last one ended, and a retry reads the same one.
event => compileWeeklyUpdates(new Date(event.scheduleTime))
)
diff --git a/functions/src/conferenceUpdates/inputEvents.ts b/functions/src/conferenceUpdates/inputEvents.ts
index 4e13d6e83..76b3cc97a 100644
--- a/functions/src/conferenceUpdates/inputEvents.ts
+++ b/functions/src/conferenceUpdates/inputEvents.ts
@@ -1,5 +1,6 @@
import { onDocumentWritten } from "firebase-functions/v2/firestore"
-import { db, Timestamp } from "../firebase"
+import { createdUnlessExists } from "../common"
+import { db, FieldValue, Timestamp } from "../firebase"
import { ConferencePosition, Testimony } from "../testimony/types"
import { authorGroupOf, ConferenceInputEvent, InputState } from "./types"
@@ -24,7 +25,7 @@ export function conferenceInputEvent(
before: Publication | undefined,
after: Publication | undefined,
testimonyId: string
-): Omit | undefined {
+): Omit | undefined {
const current = after ?? before
if (!current?.conferenceId) return undefined
const b = before && stateOf(before)
@@ -42,9 +43,9 @@ export function conferenceInputEvent(
}
}
-/** 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. */
+/** Records each change to conference input, for the letters and the weekly
+ * update to read. The event's id is the trigger event's and the document is
+ * created, so a retry or a second delivery keeps the first `recordedAt`. */
export const recordConferenceInput = onDocumentWritten(
{
document: "/users/{userId}/publishedTestimony/{testimonyId}",
@@ -60,9 +61,15 @@ export const recordConferenceInput = onDocumentWritten(
event.params.testimonyId
)
if (!body) return
- await db
- .collection("/conferenceInputEvents")
- .doc(event.id)
- .set({ ...body, at: Timestamp.fromDate(new Date(event.time)) })
+ await createdUnlessExists(
+ db
+ .collection("/conferenceInputEvents")
+ .doc(event.id)
+ .create({
+ ...body,
+ at: Timestamp.fromDate(new Date(event.time)),
+ recordedAt: FieldValue.serverTimestamp()
+ })
+ )
}
)
diff --git a/functions/src/conferenceUpdates/types.ts b/functions/src/conferenceUpdates/types.ts
index 4428bcf63..87051914f 100644
--- a/functions/src/conferenceUpdates/types.ts
+++ b/functions/src/conferenceUpdates/types.ts
@@ -57,7 +57,10 @@ export const InputState = Record({
})
/** One conference input appearing, changing or going. Written by
- * `recordConferenceInput`; the weekly update reads a week of them.
+ * `recordConferenceInput`. `at` is when the input was written, `recordedAt`
+ * when this event was, which can be later; the letters and the weekly update
+ * read the events recorded in their window, so a late event falls in the next
+ * one rather than in none.
* Firestore: /conferenceInputEvents/{eventId} */
export type ConferenceInputEvent = Static
export const ConferenceInputEvent = Record({
@@ -68,7 +71,8 @@ export const ConferenceInputEvent = Record({
change: Union(L("added"), L("edited"), L("withdrawn")),
before: Optional(InputState),
after: Optional(InputState),
- at: InstanceOf(Timestamp)
+ at: InstanceOf(Timestamp),
+ recordedAt: InstanceOf(Timestamp)
})
/** What changed in a week, as the update says it. `net` is the resulting
diff --git a/tests/integration/conferenceLetters.test.ts b/tests/integration/conferenceLetters.test.ts
index 9de703f17..11636637f 100644
--- a/tests/integration/conferenceLetters.test.ts
+++ b/tests/integration/conferenceLetters.test.ts
@@ -43,12 +43,13 @@ type Seed = {
type Input = { uid: string; testimonyId: string; conferenceId: string }
/** An event saying the reader posted or edited their input, to `version`, at
- * `at`. */
+ * `at`, recorded then or at `recordedAt`. */
const event = async (
input: Input,
change: "added" | "edited",
version: number,
- at: Date
+ at: Date,
+ recordedAt = at
) => {
const eventId = id()
const state = (version: number) => ({
@@ -64,7 +65,8 @@ const event = async (
change,
...(change === "edited" && { before: state(version - 1) }),
after: state(version),
- at: Timestamp.fromDate(at)
+ at: Timestamp.fromDate(at),
+ recordedAt: Timestamp.fromDate(recordedAt)
})
return eventId
}
@@ -294,6 +296,44 @@ describe("sendConferenceLetters", () => {
expect(await record(editId)).toMatchObject({ updated: false })
})
+ it("mails an event recorded after the run on the next run", async () => {
+ const posted = await seed(conferenceId, { rep })
+ await sendConferenceLetters(run(0))
+
+ // Edited just before the next run, recorded by the trigger just after it.
+ await publishVersion(posted, 2)
+ const lateId = await event(
+ posted,
+ "edited",
+ 2,
+ new Date(run(1).getTime() - 2_000),
+ new Date(run(1).getTime() + 6_000)
+ )
+ await sendConferenceLetters(run(1))
+ expect(await email(lateId)).toBeUndefined()
+ expect(await skip(lateId)).toBeUndefined()
+
+ await sendConferenceLetters(run(2))
+ expect(await record(lateId)).toMatchObject({ version: 2, updated: true })
+ })
+
+ it("leaves a mailed event alone when a rerun finds a later one", async () => {
+ const posted = await seed(conferenceId, { rep })
+ await sendConferenceLetters(NOW)
+ expect(await email(posted.eventId)).toBeDefined()
+
+ await publishVersion(posted, 2)
+ const editId = await event(
+ posted,
+ "edited",
+ 2,
+ new Date(ADDED_AT.getTime() + 60_000)
+ )
+ await sendConferenceLetters(NOW)
+ expect(await skip(posted.eventId)).toBeUndefined()
+ expect(await record(editId)).toMatchObject({ version: 2, updated: true })
+ })
+
it("mails an input posted and edited the same day once, as new", async () => {
const first = await seed(conferenceId, { rep })
const editId = await event(
diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts
index b18558af4..7584a8965 100644
--- a/tests/integration/testimony.test.ts
+++ b/tests/integration/testimony.test.ts
@@ -1642,8 +1642,8 @@ describe("conference weekly update", () => {
const inWeek = await testDb
.collection("/conferenceInputEvents")
.where("conferenceId", "==", conferenceId)
- .where("at", ">=", start.toJSDate())
- .where("at", "<", end.toJSDate())
+ .where("recordedAt", ">=", start.toJSDate())
+ .where("recordedAt", "<", end.toJSDate())
.get()
expect(inWeek.size).toBe(0)
})
From cc9c401cbbc270b823321eaf8022c6fc1652e9db Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 20:43:58 -0500
Subject: [PATCH 14/17] Send cosign edits through the letter review
Editing a posted cosign mailed the legislators an updated letter the
reader never previewed: the edit posted straight from its note screen.
Both forms now end at one review step, with the letter preview, the
reason for the change and the post rule (changed, and a reason when
editing) worked out in one place. The write screens stay separate, as
the mockup draws them. The panel keeps one step state for both.
Posting an edit also clears the form's reason, so the next edit no
longer starts with the last one's.
Closes hyphacoop/maple#148
Verified: test:website (296), check-types, lint, formatting. On the
local stack: a cosign edit goes write, review, post with a reason
required; after the letter job mailed it, the next edit previews
"(updated)"; the next edit's reason is empty; the cosign review stacks
at about 560px.
Not verified: build:dev (the dev server held .next); exactly 390px;
the review laid beside the mockup's compose review.
Co-Authored-By: Claude Opus 5.5
---
components/conference/ConferenceDetails.tsx | 31 +--
.../conference/input/CosignForm.test.tsx | 112 ++++++++--
components/conference/input/CosignForm.tsx | 106 ++++-----
.../conference/input/InputForm.test.tsx | 23 +-
components/conference/input/InputForm.tsx | 205 ++++++++++++------
public/locales/en/conferences.json | 2 -
6 files changed, 294 insertions(+), 185 deletions(-)
diff --git a/components/conference/ConferenceDetails.tsx b/components/conference/ConferenceDetails.tsx
index c38473691..a41ecd49f 100644
--- a/components/conference/ConferenceDetails.tsx
+++ b/components/conference/ConferenceDetails.tsx
@@ -38,7 +38,7 @@ import { maple, siteUrl } from "../links"
import { usePinnedNavbar } from "./hooks"
import { InputContext } from "./input/context"
import { initialsOf } from "./input/accounts"
-import { CosignForm, CosignStep } from "./input/CosignForm"
+import { CosignForm } from "./input/CosignForm"
import { CosignLetter } from "./input/CosignLetter"
import { InputForm, Step } from "./input/InputForm"
import { Panel, usePanelWidth } from "./input/Panel"
@@ -250,11 +250,10 @@ function Details({
)
const byId = useMemo(() => new Map(items.map(x => [x.id, x])), [items])
const [open, setOpen] = useState(false)
- const [step, setStep] = useState("compose")
+ const [step, setStep] = useState("write")
// The panel files one of two things: the reader's own input, or a cosign of
// `cosigning`. Both are the reader's one draft on the conference.
const [cosigning, setCosigning] = useState()
- const [cosignStep, setCosignStep] = useState("confirm")
const [full, setFull] = useState(true)
// What the reader's draft holds, as far as the page cares: what it
// cosigns, if anything, and whether it has words.
@@ -300,12 +299,12 @@ function Details({
const close = useCallback(() => setOpen(false), [])
const compose = useCallback(() => {
setCosigning(undefined)
- setStep("compose")
+ setStep("write")
setOpen(true)
}, [])
const resumeCosign = useCallback((parent: ConferenceTestimony) => {
setCosigning(parent)
- setCosignStep("write")
+ setStep("write")
setOpen(true)
}, [])
// A posted cosign is edited as one, its note alone.
@@ -335,7 +334,7 @@ function Details({
if (!signIn()) return
if (draftWords && draftOf === parent.id) return resumeCosign(parent)
setCosigning(parent)
- setCosignStep("confirm")
+ setStep("confirm")
setFull(true)
setOpen(true)
},
@@ -480,7 +479,7 @@ function Details({
)
// Signing fills the window with what is signed beside the form; once agreed,
// the reader may fold it back to the drawer.
- const filled = !!cosigning && (cosignStep === "confirm" || full)
+ const filled = !!cosigning && (step === "confirm" || full)
return (
void
cosigning?: ConferenceTestimony
- cosignStep: CosignStep
- onCosignStep: (s: CosignStep) => void
full: boolean
onFull: (full: boolean) => void
onDraft: (draft?: ConferenceWorkingDraft, read?: boolean) => void
@@ -672,10 +665,10 @@ function InputPanel({
}, [edit.draft, edit.loading, onDraft])
const title =
- cosigning && edit.publication
- ? "input.cosign_edit_title"
- : (cosigning ? cosignStep : step) === "review"
+ step === "review"
? "input.review_title"
+ : cosigning && edit.publication
+ ? "input.cosign_edit_title"
: "input.panel_title"
return (
}
full={full}
- onFull={cosigning && cosignStep === "write" ? onFull : undefined}
+ onFull={cosigning && step === "write" ? onFull : undefined}
>
{cosigning ? (
// Keyed so another parent starts from its own draft.
@@ -699,8 +692,8 @@ function InputPanel({
conferenceId={conference.id}
subject={subject}
conferees={initials}
- step={cosignStep}
- onStep={onCosignStep}
+ step={step}
+ onStep={onStep}
onCancel={onClose}
onPosted={onPosted}
/>
diff --git a/components/conference/input/CosignForm.test.tsx b/components/conference/input/CosignForm.test.tsx
index 3e0f384c0..9ca0f7e4e 100644
--- a/components/conference/input/CosignForm.test.tsx
+++ b/components/conference/input/CosignForm.test.tsx
@@ -4,7 +4,8 @@ import type {
ConferenceTestimony,
UseEditConferenceTestimony
} from "components/db"
-import { CosignForm, CosignStep } from "./CosignForm"
+import { CosignForm } from "./CosignForm"
+import type { Step } from "./InputForm"
jest.mock("next-i18next", () => ({
useTranslation: () => ({
@@ -26,7 +27,17 @@ jest.mock("components/auth", () => ({
const mockProfile: { current: Record } = { current: {} }
jest.mock("components/db", () => ({
useProfile: () => ({ profile: mockProfile.current }),
- useMember: () => ({ member: undefined })
+ useMember: () => ({ member: undefined }),
+ // As components/db compares a draft with what was posted.
+ hasDraftChanged: (
+ draft?: Record,
+ published?: Record
+ ) =>
+ !!draft &&
+ !!published &&
+ (["position", "content"] as const).some(
+ k => (draft[k] || undefined) !== (published[k] || undefined)
+ )
}))
jest.mock("components/db/legislatorMail", () => ({
useLetterOnRecord: () => false
@@ -67,7 +78,7 @@ const editing = (
const mount = (
edit: UseEditConferenceTestimony,
- step: CosignStep,
+ step: Step,
more: { onStep?: jest.Mock; onPosted?: jest.Mock } = {}
) =>
render(
@@ -252,29 +263,82 @@ describe("CosignForm", () => {
expect(screen.getByRole("alert")).toHaveTextContent(message)
})
- it("edits a posted cosign's words alone, with a reason", () => {
- const publication = { cosignOfId: "p1", content: "Before." }
- mount(
- editing({ publication } as Partial),
- "write"
- )
- expect(screen.getByRole("textbox")).toHaveValue("Before.")
- const save = screen.getByRole("button", { name: "input.cosign_save" })
- expect(save).toBeDisabled()
- expect(screen.queryByText("publish.reasonLabel")).not.toBeInTheDocument()
- // No way to pick another position, or to leave the parent.
- expect(
- screen.queryByRole("button", { name: /input\.position\./ })
- ).not.toBeInTheDocument()
+ describe("editing a posted cosign", () => {
+ const publication = {
+ id: "c1",
+ cosignOfId: "p1",
+ position: "passSenateVersion",
+ content: "Before."
+ }
+
+ it("changes its words alone, and goes to review to post them", () => {
+ const onStep = jest.fn()
+ mount(
+ editing({ publication } as Partial),
+ "write",
+ { onStep }
+ )
+ expect(screen.getByRole("textbox")).toHaveValue("Before.")
+ // No way to pick another position, to leave the parent, or to post
+ // without seeing the letter.
+ expect(
+ screen.queryByRole("button", { name: /input\.position\./ })
+ ).not.toBeInTheDocument()
+ expect(screen.queryByText("publish.reasonLabel")).not.toBeInTheDocument()
+ fireEvent.change(screen.getByRole("textbox"), {
+ target: { value: "After." }
+ })
+ press("input.review")
+ expect(onStep).toHaveBeenCalledWith("review")
+ })
+
+ it("asks for a reason for the change at review", () => {
+ mount(
+ editing({
+ publication,
+ draft: { cosignOfId: "p1", content: "After." }
+ } as Partial),
+ "review"
+ )
+ const send = screen.getByRole("button", {
+ name: "review.cosign_and_send"
+ })
+ expect(send).toBeDisabled()
+ fireEvent.change(
+ screen.getByPlaceholderText("publish.reasonPlaceholder"),
+ { target: { value: "Clearer" } }
+ )
+ expect(send).toBeEnabled()
+ })
- fireEvent.change(screen.getByRole("textbox"), {
- target: { value: "After." }
+ it("posts the change with its reason", async () => {
+ const edit = editing({
+ publication,
+ draft: { cosignOfId: "p1", content: "After.", editReason: "Clearer" }
+ } as Partial)
+ const onPosted = jest.fn()
+ mount(edit, "review", { onPosted })
+ await act(async () => press("review.cosign_and_send"))
+ expect(edit.publishTestimony.execute).toHaveBeenCalled()
+ expect(onPosted).toHaveBeenCalled()
+ // The form stays mounted, so the next edit starts without this reason.
+ expect(
+ screen.getByPlaceholderText("publish.reasonPlaceholder")
+ ).toHaveValue("")
})
- expect(screen.getByText("publish.reasonLabel")).toBeInTheDocument()
- expect(save).toBeDisabled()
- fireEvent.change(screen.getAllByRole("textbox")[1], {
- target: { value: "Clearer" }
+
+ it("cannot post it unchanged", () => {
+ mount(
+ editing({
+ publication,
+ draft: { cosignOfId: "p1", content: "Before." }
+ } as Partial),
+ "review"
+ )
+ expect(
+ screen.getByRole("button", { name: "review.cosign_and_send" })
+ ).toBeDisabled()
+ expect(screen.queryByText("publish.reasonLabel")).not.toBeInTheDocument()
})
- expect(save).toBeEnabled()
})
})
diff --git a/components/conference/input/CosignForm.tsx b/components/conference/input/CosignForm.tsx
index ab87a7a82..fb5ec031a 100644
--- a/components/conference/input/CosignForm.tsx
+++ b/components/conference/input/CosignForm.tsx
@@ -29,21 +29,18 @@ import {
Primary,
Problem,
Quiet,
- ReasonField,
+ ReviewStep,
Rules,
Scroll,
Status,
+ Step,
useAsk
} 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" | "review"
-
/** Below lg, where the letter sits above it, the agreeing step takes the room
* it needs and leaves the letter the rest. */
const Column = styled(Body)<{ $natural?: boolean }>`
@@ -250,10 +247,10 @@ const refusalOf = (error?: Error) =>
?.refusal
/**
- * Cosigning someone else's input, in the panel's two steps: agreeing to what
- * is signed, then an optional note of one's own. Editing a cosign is the
- * second step alone, and changes the note only. The cosign is the user's one
- * draft on the conference, kept as they type.
+ * Cosigning someone else's input, in the panel's three steps: agreeing to
+ * what is signed, an optional note of one's own, and the review every input
+ * goes through. Editing a cosign starts at the note, and changes it only. The
+ * cosign is the user's one draft on the conference, kept as they type.
*/
export function CosignForm({
edit,
@@ -277,8 +274,8 @@ export function CosignForm({
edit: UseEditConferenceTestimony
/** What is being cosigned. */
parent: ConferenceTestimony
- step: CosignStep
- onStep: (s: CosignStep) => void
+ step: Step
+ onStep: (s: Step) => void
onCancel: () => void
/** The cosign is on the record. */
onPosted: () => void
@@ -288,7 +285,6 @@ export function CosignForm({
const { byId } = useInputContext()
const { profile } = useProfile()
const { saveDraft, publishTestimony, discardDraft } = edit
- const editing = !!edit.publication
const [note, setNote] = useState("")
const [noWords, setNoWords] = useState(false)
@@ -334,7 +330,11 @@ export function CosignForm({
replyTo: f.chosen || undefined
}),
active: step !== "confirm",
- onPosted
+ // As InputForm's: the form outlives the post, so its reason goes too.
+ onPosted: () => {
+ setReason("")
+ onPosted()
+ }
})
// Ready to write on arrival, without scrolling the preview out of view.
@@ -343,8 +343,7 @@ export function CosignForm({
}, [step])
const ready = !!note.trim() || noWords
- const changed = !editing || content !== edit.publication?.content
- const needsReason = editing && changed
+ const editing = !!edit.publication
// Delete Draft is offered once there are words kept to delete.
const kept = edit.draft?.cosignOfId === parent.id && !!edit.draft.content
const showDelete = !editing && kept && save === "none" && !dirty
@@ -406,49 +405,30 @@ export function CosignForm({
const name = profile?.fullName || t("input.anonymous")
const type = accountTypeOf(claims?.role ?? "user")
- if (step === "review") {
- const sends = hasLegislators(profile ?? {})
+ if (step === "review")
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")}
-
-
-
+ onStep("write")}
+ onCancel={onCancel}
+ onPost={post}
+ />
)
- }
return (
@@ -502,7 +482,6 @@ export function CosignForm({
/>
{t("input.cosign_no_words_label")}
- {needsReason && }
{problem}
@@ -521,18 +500,9 @@ export function CosignForm({
{
- if (!ready) ask.ask()
- else if (editing) post()
- else onStep("review")
- }}
+ onClick={() => (ready ? onStep("review") : ask.ask())}
>
- {t(editing ? "input.cosign_save" : "input.review")}
+ {t("input.review")}
diff --git a/components/conference/input/InputForm.test.tsx b/components/conference/input/InputForm.test.tsx
index 42f7e2664..9edcbbfd0 100644
--- a/components/conference/input/InputForm.test.tsx
+++ b/components/conference/input/InputForm.test.tsx
@@ -83,7 +83,7 @@ describe("InputForm", () => {
conferees={[]}
edit={edit}
subject="Phone-Free Schools"
- step="compose"
+ step="write"
onStep={jest.fn()}
onCancel={jest.fn()}
onPosted={jest.fn()}
@@ -138,6 +138,27 @@ describe("InputForm", () => {
).toBeInTheDocument()
})
+ it("starts the next edit without the last one's reason", async () => {
+ const edit = editing({
+ publication: { id: "t1", position: "passSomething", content: "Pass." },
+ draft: {
+ position: "passSomething",
+ content: "Pass it, please.",
+ editReason: "Clearer"
+ }
+ })
+ review(edit)
+ const reason = screen.getByPlaceholderText("publish.reasonPlaceholder")
+ expect(reason).toHaveValue("Clearer")
+ await act(async () =>
+ fireEvent.click(
+ screen.getByRole("button", { name: "review.post_and_send" })
+ )
+ )
+ expect(edit.publishTestimony.execute).toHaveBeenCalled()
+ expect(reason).toHaveValue("")
+ })
+
it("keeps another reply-to address on the draft, for this letter alone", async () => {
jest.useFakeTimers()
const edit = editing()
diff --git a/components/conference/input/InputForm.tsx b/components/conference/input/InputForm.tsx
index 695e83e4e..732116f9b 100644
--- a/components/conference/input/InputForm.tsx
+++ b/components/conference/input/InputForm.tsx
@@ -1,4 +1,12 @@
-import { RefObject, useCallback, useEffect, useRef, useState } from "react"
+import {
+ ComponentProps,
+ ReactNode,
+ RefObject,
+ useCallback,
+ useEffect,
+ useRef,
+ useState
+} from "react"
import { Trans, useTranslation } from "next-i18next"
import styled, { keyframes } from "styled-components"
import {
@@ -11,12 +19,13 @@ import {
} from "components/db"
import { Internal } from "components/links"
import { hasLegislators } from "functions/src/conferenceLetters/letter"
-import { LetterReview } from "./Review"
+import { LetterReview, NoLine } from "./Review"
import { conferencePositions, positionColours } from "./positions"
import { brand, ink, line, red, wash } from "./tokens"
import { useAutosave } from "./useAutosave"
-export type Step = "compose" | "review"
+/** The panel's steps. Only a cosign is agreed to first. */
+export type Step = "confirm" | "write" | "review"
export const Body = styled.div`
display: flex;
@@ -208,7 +217,7 @@ export function useAsk(field: RefObject) {
}
/** The edit reason a republished filing needs, as it does everywhere else. */
-export function ReasonField({
+function ReasonField({
value,
onChange
}: {
@@ -245,9 +254,102 @@ export function ConductRule() {
)
}
+/** The step before posting, for input and cosigns alike: where it goes, the
+ * letter as it will be sent, the reason when it changes what was posted, and
+ * the way to post it. */
+export function ReviewStep({
+ edit,
+ ready,
+ posting,
+ problem,
+ reason,
+ onReason,
+ onBack,
+ onCancel,
+ onPost,
+ ...letter
+}: Omit, "testimonyId"> & {
+ /** The user's draft and publication on the conference. */
+ edit: UseEditConferenceTestimony
+ /** There is something to post: input's words, or a cosign with or without. */
+ ready: boolean
+ posting: boolean
+ problem?: ReactNode
+ reason: string
+ onReason: (reason: string) => void
+ onBack: () => void
+ onCancel: () => void
+ onPost: () => void
+}) {
+ const { t } = useTranslation("conferences")
+ const { profile } = useProfile()
+ // Whether the letter goes anywhere, from the profile alone.
+ const sends = hasLegislators(profile ?? {})
+ // Republishing needs a reason for the change, as it does everywhere else.
+ const editing = !!edit.publication
+ const changed =
+ !editing ||
+ hasDraftChanged(
+ {
+ position: letter.position,
+ content: letter.content
+ } as unknown as WorkingDraft,
+ edit.publication as unknown as Testimony
+ )
+ const needsReason = editing && changed
+ const canPost =
+ ready &&
+ changed &&
+ (!needsReason || !!reason.trim()) &&
+ !posting &&
+ !edit.publishTestimony.loading
+ const cosign = !!letter.cosign
+ return (
+
+
+ {cosign && !letter.content.trim() && }
+
+ {!cosign && (
+
+ {t("input.rules_label")}
+
+ {t("input.rule_revise")}
+
+
+
+ )}
+ {needsReason && }
+ {problem}
+
+
+ {!ready &&
{t("input.empty_hint")} }
+
+
+ {t("input.back")}
+
+
+ {t("input.cancel")}
+
+
+ {t(
+ cosign
+ ? sends
+ ? "review.cosign_and_send"
+ : "input.cosign"
+ : sends
+ ? "review.post_and_send"
+ : "input.post"
+ )}
+
+
+
+
+ )
+}
+
/**
* Writing, reviewing and posting testimony on a conference, in the panel's
- * three steps. The draft is the user's own, kept as they type and published
+ * two steps. The draft is the user's own, kept as they type and published
* from the review step.
*/
export function InputForm({
@@ -322,7 +424,12 @@ export function InputForm({
replyTo: f.chosen || undefined
}),
idle: f => !f.content.trim(),
- onPosted
+ // Publishing clears the draft's reason; the form outlives the post, behind
+ // the closed panel, so it clears its own for the next edit too.
+ onPosted: () => {
+ setReason("")
+ onPosted()
+ }
})
const written = content.trim().length > 0
@@ -344,20 +451,11 @@ export function InputForm({
saveDraft.error ||
publishTestimony.error ||
discardDraft.error
- // Republishing needs a reason for the change, as it does everywhere else.
- const editing = !!edit.publication
- const changed =
- !editing ||
- hasDraftChanged(
- { position, content } as unknown as WorkingDraft,
- edit.publication as unknown as Testimony
- )
- const needsReason = editing && changed
const showDelete = !!edit.draft && save === "none" && !dirty
const name = profile?.fullName || t("input.anonymous")
- if (step === "compose")
+ if (step !== "review")
return (
@@ -411,61 +509,26 @@ export function InputForm({
)
- // Whether the letter goes anywhere, from the profile alone.
- const sends = hasLegislators(profile ?? {})
return (
-
-
-
-
- {t("input.rules_label")}
-
- {t("input.rule_revise")}
-
-
-
- {needsReason && }
- {failed && {t("input.error")} }
-
-
- {!written &&
{t("input.empty_hint")} }
-
- onStep("compose")}
- style={{ marginRight: "auto" }}
- >
- {t("input.back")}
-
-
- {t("input.cancel")}
-
-
- {t(sends ? "review.post_and_send" : "input.post")}
-
-
-
-
+ {t("input.error")}}
+ reason={reason}
+ onReason={setReason}
+ onBack={() => onStep("write")}
+ onCancel={onCancel}
+ onPost={post}
+ />
)
}
diff --git a/public/locales/en/conferences.json b/public/locales/en/conferences.json
index 2620be262..f04e8be00 100644
--- a/public/locales/en/conferences.json
+++ b/public/locales/en/conferences.json
@@ -279,7 +279,6 @@
"cosign_optional": "Optional",
"cosign_prompt": "Why does this matter to you? One or two sentences in your own words will have more impact with lawmakers.",
"cosign_no_words_label": "I do not want to add input of my own",
- "cosign_save": "Save Cosign",
"cosign_edit_title": "Edit Your Cosign",
"cosign_parent_gone": "The input you are cosigning has been removed, so your cosign could not be filed.",
"cosign_closed": "This conference committee is no longer taking input, so your cosign could not be filed.",
@@ -321,7 +320,6 @@
"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"
From 84fac8b3bdb068b215cb409b9f149c59d94b8126 Mon Sep 17 00:00:00 2001
From: violet
Date: Thu, 12 Nov 2026 15:03:16 -0500
Subject: [PATCH 15/17] Refuse to run the integration tests without Typesense
The functions sync every testimony and bill write to Typesense, which CI
runs as a service. Without it locally, each sync threw, the emulator
killed that function's worker, and every later event waited on a cold
start; the backlog timed out triggers the tests wait on, the cosign tally
most often. The runner now checks the TYPESENSE_API_URL in the demo
project's env file and stops with the command to start it.
Verified: without Typesense, 160 worker loads and tally timeouts; with a
stand-in answering on 8108, 16 loads and testimony plus conferenceLetters
pass (88/88); the runner stops with its message when nothing answers.
Not verified: a full run against a real Typesense container.
Co-Authored-By: Claude Opus 5.5
---
AGENTS.md | 18 ++++++------
scripts/test-integration-ci.js | 51 ++++++++++++++++++++++++----------
2 files changed, 45 insertions(+), 24 deletions(-)
diff --git a/AGENTS.md b/AGENTS.md
index 26de4101a..d652c885b 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -171,15 +171,15 @@ your branch ──PR──▶ main ──(automatic)──▶ dev site + dev bac
[Repo Checks](.github/workflows/repo-checks.yml) runs on every push and PR. Run the same commands before you push:
-| Check | Command |
-| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
-| Lint | `yarn lint` |
-| Formatting (all files, including Markdown) | `yarn check-formatting`; fix with `yarn fix` |
-| Types | `yarn check-types` |
-| Cloud Functions tests | `yarn test` |
-| Website tests | `yarn test:website` |
-| Integration tests | `yarn test:integration-ci ` (needs Java for the emulators) |
-| Python in `llm/` | `ruff check llm/`, `ruff format --check llm/`, `pytest llm/` (after `pip install -r llm/requirements.txt`) |
+| Check | Command |
+| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
+| Lint | `yarn lint` |
+| Formatting (all files, including Markdown) | `yarn check-formatting`; fix with `yarn fix` |
+| Types | `yarn check-types` |
+| Cloud Functions tests | `yarn test` |
+| Website tests | `yarn test:website` |
+| Integration tests | `yarn test:integration-ci ` (needs Java for the emulators, and Typesense: `docker compose -f infra/docker-compose.yml up -d search`) |
+| Python in `llm/` | `ruff check llm/`, `ruff format --check llm/`, `pytest llm/` (after `pip install -r llm/requirements.txt`) |
CI runs only the website tests named in the `test:website` script and the integration test files listed in [repo-checks.yml](.github/workflows/repo-checks.yml). Add a new test to one of those lists, or it can fail on `main` unnoticed.
diff --git a/scripts/test-integration-ci.js b/scripts/test-integration-ci.js
index 3ddbf9184..b6eab22c8 100755
--- a/scripts/test-integration-ci.js
+++ b/scripts/test-integration-ci.js
@@ -1,17 +1,38 @@
+const { readFileSync } = require("fs")
const { demoProjectId, env, runOrExit } = require("./common")
-runOrExit("yarn", ["--cwd", "functions", "build"], { stdio: "inherit" })
-runOrExit(
- "./node_modules/.bin/firebase",
- [
- "--project",
- demoProjectId,
- "emulators:exec",
- "--only",
- "auth,functions,pubsub,firestore,storage",
- "--import",
- "tests/integration/exportedTestData",
- `yarn test:integration --forceExit ${process.argv.slice(2).join(" ")}`
- ],
- { stdio: "inherit", env }
-)
+// The functions sync every testimony and bill write to Typesense, as CI's
+// typesense service provides. Without it each sync throws, the emulator kills
+// that function's worker, and the next event waits on a fresh one: the backlog
+// starves triggers the tests wait on, such as the cosign tally.
+async function requireSearch() {
+ const url = readFileSync(`functions/.env.${demoProjectId}`, "utf8").match(
+ /^TYPESENSE_API_URL=(.*)$/m
+ )[1]
+ try {
+ if ((await fetch(`${url}/health`)).ok) return
+ } catch {}
+ console.error(
+ `Integration tests need Typesense at ${url}, as CI runs it.\n` +
+ "Start it with: docker compose -f infra/docker-compose.yml up -d search"
+ )
+ process.exit(1)
+}
+
+requireSearch().then(() => {
+ runOrExit("yarn", ["--cwd", "functions", "build"], { stdio: "inherit" })
+ runOrExit(
+ "./node_modules/.bin/firebase",
+ [
+ "--project",
+ demoProjectId,
+ "emulators:exec",
+ "--only",
+ "auth,functions,pubsub,firestore,storage",
+ "--import",
+ "tests/integration/exportedTestData",
+ `yarn test:integration --forceExit ${process.argv.slice(2).join(" ")}`
+ ],
+ { stdio: "inherit", env }
+ )
+})
From 50c999ad0f84f06d731e8c194f887c9dd319c2e5 Mon Sep 17 00:00:00 2001
From: violet
Date: Fri, 13 Nov 2026 12:56:44 -0500
Subject: [PATCH 16/17] Matt gets test emails in dev.
---
functions/.env.digital-testimony-dev | 3 +++
1 file changed, 3 insertions(+)
diff --git a/functions/.env.digital-testimony-dev b/functions/.env.digital-testimony-dev
index 55f7b792f..848b93511 100644
--- a/functions/.env.digital-testimony-dev
+++ b/functions/.env.digital-testimony-dev
@@ -4,3 +4,6 @@ TRANSCRIPTION_WEBHOOK_URL=https://transcriptionv2-ke6znoupgq-uc.a.run.app
# Each conference stream sends from its own authenticated subdomain.
LETTER_FROM=noreply@letters.mapletestimony.org
CONFEREE_UPDATE_FROM=noreply@updates.mapletestimony.org
+
+# Matt gets test emails in dev
+LETTER_TEST_RECIPIENT=msvictor51@gmail.com
From 96438b08678c6fef3d55954d9afb3a3db2533a34 Mon Sep 17 00:00:00 2001
From: violet
Date: Mon, 16 Nov 2026 11:31:05 -0500
Subject: [PATCH 17/17] Send replies and the copy to the reader's own email
A letter's Reply-To, Cc and signature line were the address the reader
chose for that letter, kept on their private draft. They are now always the
reader's verified account email. The review step shows that address
instead of offering another, and the sender no longer reads the draft.
Verified: functions jest, test:website, check-types, eslint on the changed
files, integration tests for conferenceLetters and testimony on a throwaway
emulator, and build:dev.
Not verified: the review step in a browser on the local stack.
Co-Authored-By: Claude Sonnet 5.5
---
.../conference/input/CosignForm.test.tsx | 34 ----
components/conference/input/CosignForm.tsx | 13 +-
.../conference/input/InputForm.test.tsx | 18 ---
components/conference/input/InputForm.tsx | 13 +-
components/conference/input/Review.tsx | 152 +++---------------
components/db/testimony/types.ts | 5 +-
components/db/testimony/useEditTestimony.ts | 6 +-
.../src/conferenceLetters/letter.test.ts | 2 +-
functions/src/conferenceLetters/letter.ts | 16 +-
.../src/conferenceLetters/letters.test.ts | 4 +-
.../src/conferenceLetters/sendLetters.ts | 13 +-
functions/src/testimony/types.ts | 8 +-
public/locales/en/conferences.json | 5 -
tests/integration/conferenceLetters.test.ts | 26 +--
tests/integration/testimony.test.ts | 19 ---
15 files changed, 43 insertions(+), 291 deletions(-)
diff --git a/components/conference/input/CosignForm.test.tsx b/components/conference/input/CosignForm.test.tsx
index 9ca0f7e4e..bcf8bf140 100644
--- a/components/conference/input/CosignForm.test.tsx
+++ b/components/conference/input/CosignForm.test.tsx
@@ -210,40 +210,6 @@ describe("CosignForm", () => {
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 fb5ec031a..6cabf2fad 100644
--- a/components/conference/input/CosignForm.tsx
+++ b/components/conference/input/CosignForm.tsx
@@ -289,8 +289,6 @@ 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)
@@ -308,26 +306,23 @@ 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, chosen },
+ fields: { content, reason },
matches: (draft, f) =>
draft?.cosignOfId === parent.id &&
draft?.content === f.content &&
- (draft?.editReason ?? "") === f.reason &&
- (draft?.replyTo ?? "") === f.chosen,
+ (draft?.editReason ?? "") === f.reason,
write: f =>
saveDraft.execute({
position: parent.position,
content: f.content,
cosignOfId: parent.id,
cosignOfAuthorUid: parent.authorUid,
- editReason: f.reason || undefined,
- replyTo: f.chosen || undefined
+ editReason: f.reason || undefined
}),
active: step !== "confirm",
// As InputForm's: the form outlives the post, so its reason goes too.
@@ -417,8 +412,6 @@ export function CosignForm({
position={parent.position}
content={content}
cosign={{ author: nameOf(parent), content: parent.content }}
- chosen={chosen}
- onChoose={setChosen}
ready={ready}
posting={posting}
problem={problem}
diff --git a/components/conference/input/InputForm.test.tsx b/components/conference/input/InputForm.test.tsx
index 9edcbbfd0..e8e8aee7e 100644
--- a/components/conference/input/InputForm.test.tsx
+++ b/components/conference/input/InputForm.test.tsx
@@ -158,23 +158,5 @@ describe("InputForm", () => {
expect(edit.publishTestimony.execute).toHaveBeenCalled()
expect(reason).toHaveValue("")
})
-
- 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 732116f9b..7d2e1459c 100644
--- a/components/conference/input/InputForm.tsx
+++ b/components/conference/input/InputForm.tsx
@@ -385,8 +385,6 @@ 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)
@@ -402,26 +400,23 @@ 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, chosen },
+ fields: { position, content, reason },
// 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?.replyTo ?? "") === f.chosen,
+ (draft?.editReason ?? "") === f.reason,
write: f =>
saveDraft.execute({
position: f.position,
content: f.content,
- editReason: f.reason || undefined,
- replyTo: f.chosen || undefined
+ editReason: f.reason || undefined
}),
idle: f => !f.content.trim(),
// Publishing clears the draft's reason; the form outlives the post, behind
@@ -519,8 +514,6 @@ export function InputForm({
conference={subject}
position={position}
content={content}
- chosen={chosen}
- onChoose={setChosen}
ready={written}
posting={posting}
problem={failed && {t("input.error")} }
diff --git a/components/conference/input/Review.tsx b/components/conference/input/Review.tsx
index b5ec46b27..a198898f0 100644
--- a/components/conference/input/Review.tsx
+++ b/components/conference/input/Review.tsx
@@ -1,4 +1,3 @@
-import { useState } from "react"
import { useTranslation } from "next-i18next"
import styled from "styled-components"
import { useAuth } from "components/auth"
@@ -9,7 +8,6 @@ import {
hasLegislators,
Letter,
LetterMember,
- looksLikeEmail,
renderLetter
} from "functions/src/conferenceLetters/letter"
import { Disc as Avatar } from "./Entry"
@@ -93,61 +91,11 @@ const Box = styled.div`
`
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};
@@ -260,12 +208,20 @@ export function useLegislators(court: number) {
}
}
-/** 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 }
+/** The reader's own address, which replies go to and which is copied. */
+function YourEmail({ email }: { email: string }) {
+ const { t } = useTranslation("conferences")
+ return (
+ <>
+ {t("review.email_label")}
+
+ {t("review.email_note")}
+
+ {email}
+
+
+ >
+ )
}
/**
@@ -342,73 +298,6 @@ export function Delivery({
)
}
-/** 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 ? (
-
- {t("review.reply_to_label")}
-
- {
- setText(e.target.value)
- setInvalid(false)
- }}
- onKeyDown={e => e.key === "Enter" && done()}
- />
-
- {t("review.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")
@@ -470,9 +359,7 @@ export function LetterReview({
testimonyId,
position,
content,
- cosign,
- chosen,
- onChoose
+ cosign
}: {
court: number
conferenceId: string
@@ -488,12 +375,9 @@ export function LetterReview({
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)
+ const email = useAuth().user?.email ?? ""
const edited = useLetterOnRecord(testimonyId)
return (
<>
@@ -505,11 +389,11 @@ export function LetterReview({
/>
{hasLegislators({ representative, senator }) && (
<>
-
+
const DraftTestimonyFields = BaseTestimony.extend({
publishedVersion: Optional(Number),
/** If present, array of legislator member codes */
- 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)
+ recipientMemberCodes: Maybe(Array(String))
})
export const DraftTestimony =
DraftTestimonyFields.withConstraint(oneTarget).withConstraint(cosignShape)
diff --git a/components/db/testimony/useEditTestimony.ts b/components/db/testimony/useEditTestimony.ts
index 8ced82d16..65ee9c365 100644
--- a/components/db/testimony/useEditTestimony.ts
+++ b/components/db/testimony/useEditTestimony.ts
@@ -283,7 +283,6 @@ type SaveDraftRequest = Pick<
| "editReason"
| "cosignOfId"
| "cosignOfAuthorUid"
- | "replyTo"
> & { ballotQuestionId?: string | null }
type SaveConferenceDraftRequest = Omit & {
position: ConferencePosition
@@ -312,7 +311,6 @@ function useSaveDraft(
editReason,
cosignOfId,
cosignOfAuthorUid,
- replyTo,
ballotQuestionId: requestBallotQuestionId
}: SaveDraftRequest) => {
if (draftLoading) {
@@ -330,8 +328,7 @@ function useSaveDraft(
requestBallotQuestionId ?? ballotQuestionId ?? null,
conferenceId: conferenceId ?? null,
cosignOfId: cosignOfId ?? null,
- cosignOfAuthorUid: cosignOfAuthorUid ?? null,
- replyTo: replyTo ?? null
+ cosignOfAuthorUid: cosignOfAuthorUid ?? null
}
const result = await addDoc(
collection(firestore, `/users/${uid}/draftTestimony`),
@@ -354,7 +351,6 @@ function useSaveDraft(
requestBallotQuestionId ?? ballotQuestionId ?? null,
cosignOfId: cosignOfId ?? null,
cosignOfAuthorUid: cosignOfAuthorUid ?? null,
- replyTo: replyTo ?? null,
publishedVersion: hasChanges ? deleteField() : undefined
})
}
diff --git a/functions/src/conferenceLetters/letter.test.ts b/functions/src/conferenceLetters/letter.test.ts
index 03af9a3bc..124f6bfc6 100644
--- a/functions/src/conferenceLetters/letter.test.ts
+++ b/functions/src/conferenceLetters/letter.test.ts
@@ -2,7 +2,7 @@ import { hasLegislators, LetterInput, renderLetter } from "./letter"
const base: LetterInput = {
author: "Ada Example",
- replyTo: "ada@example.org",
+ email: "ada@example.org",
court: 194,
conferenceId: "H4321-S2100",
conference: "Data privacy conference committee",
diff --git a/functions/src/conferenceLetters/letter.ts b/functions/src/conferenceLetters/letter.ts
index 64eee58f9..c3a91a624 100644
--- a/functions/src/conferenceLetters/letter.ts
+++ b/functions/src/conferenceLetters/letter.ts
@@ -6,19 +6,13 @@ import { CONFERENCE_STANCES } from "../shared/conferences"
import type { ConferencePosition } from "../testimony/types"
export type { ConferencePosition }
-/** A single address, as a reply-to must be: the draft is the reader's to
- * write, so the sender checks it again, and a list could copy anyone. */
-export const looksLikeEmail = (s: string) =>
- /^[^\s@,;]+@[^\s@,;]+\.[^\s@,;]+$/.test(s)
-
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
+ /** The reader's own address: where replies go, and who is copied. */
+ email: string
court: number
conferenceId: string
/** The conference's name. */
@@ -73,13 +67,13 @@ export function renderLetter(i: LetterInput): Letter {
i.author,
...(rep ? [`House district: ${rep.district}`] : []),
...(sen ? [`Senate district: ${sen.district}`] : []),
- i.replyTo
+ i.email
]
const where = i.cosign ? "this cosign" : "this input"
return {
from: `${i.author} via MAPLE`,
- replyTo: i.replyTo,
- cc: i.replyTo,
+ replyTo: i.email,
+ cc: i.email,
to: [rep && `Rep. ${rep.name}`, sen && `Sen. ${sen.name}`]
.filter(Boolean)
.join(", "),
diff --git a/functions/src/conferenceLetters/letters.test.ts b/functions/src/conferenceLetters/letters.test.ts
index 9812724a5..2ab3991f6 100644
--- a/functions/src/conferenceLetters/letters.test.ts
+++ b/functions/src/conferenceLetters/letters.test.ts
@@ -10,7 +10,7 @@ import {
const letter = renderLetter({
author: 'Ada "Ace" ',
- replyTo: "ada@example.org",
+ email: "ada@example.org",
court: 194,
conferenceId: "H1-S1",
conference: "Phone-Free Schools",
@@ -86,7 +86,7 @@ describe("renderLetterText", () => {
it("quotes the cosigned input", () => {
const cosigned = renderLetter({
author: "Ada",
- replyTo: "ada@example.org",
+ email: "ada@example.org",
court: 194,
conferenceId: "H1-S1",
conference: "Phone-Free Schools",
diff --git a/functions/src/conferenceLetters/sendLetters.ts b/functions/src/conferenceLetters/sendLetters.ts
index 119e08d2a..96255cfa3 100644
--- a/functions/src/conferenceLetters/sendLetters.ts
+++ b/functions/src/conferenceLetters/sendLetters.ts
@@ -10,7 +10,7 @@ import { Testimony } from "../testimony/types"
import { queueLegislatorEmail } from "../legislatorMail/queue"
import { lastLetterFor } from "../legislatorMail/records"
import { conferenceName } from "../shared/conferences"
-import { hasLegislators, looksLikeEmail, renderLetter } from "./letter"
+import { hasLegislators, renderLetter } from "./letter"
import { buildLetterEmail, dayWindow, SkipReason } from "./letters"
const DEFAULT_FROM = "notifications@mapletestimony.org"
@@ -52,16 +52,10 @@ async function mailLetter(
if (lastFor.get(e.testimonyId) !== eventId) return "superseded"
const letterId = `letter_${eventId}`
- const [last, pubSnap, readerEmail, draft, conference] = await Promise.all([
+ const [last, pubSnap, readerEmail, conference] = await Promise.all([
lastLetterFor(e.testimonyId),
db.doc(`/users/${e.authorUid}/publishedTestimony/${e.testimonyId}`).get(),
verifiedEmail(e.authorUid),
- db
- .collection(`/users/${e.authorUid}/draftTestimony`)
- .where("conferenceId", "==", e.conferenceId)
- .where("court", "==", e.court)
- .limit(1)
- .get(),
conferenceOf(`${e.court}/${e.conferenceId}`)
])
const pub = pubSnap.data() as Testimony | undefined
@@ -117,10 +111,9 @@ async function mailLetter(
}
const author = readerAuthor(pub)
- const chosen = (draft.docs[0]?.data().replyTo as string | null)?.trim()
const letter = renderLetter({
author,
- replyTo: chosen && looksLikeEmail(chosen) ? chosen : readerEmail,
+ email: readerEmail,
court: e.court,
conferenceId: e.conferenceId,
conference: conferenceName(conference),
diff --git a/functions/src/testimony/types.ts b/functions/src/testimony/types.ts
index 853a3728a..fd0845433 100644
--- a/functions/src/testimony/types.ts
+++ b/functions/src/testimony/types.ts
@@ -13,7 +13,6 @@ import {
import { Role } from "../auth/types"
import { Id, Maybe, withDefaults } from "../common"
import { Timestamp } from "../firebase"
-import { looksLikeEmail } from "../conferenceLetters/letter"
import { cosignShape } from "./cosign"
const maxTestimonyLength = 10_000
@@ -123,12 +122,7 @@ export const oneTarget = ({
export type DraftTestimony = Static
export const DraftTestimony = BaseTestimony.extend({
- 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(s => looksLikeEmail(s) || "Not a single address")
- )
+ publishedVersion: Optional(Number)
})
.withConstraint(positionMatchesTarget)
.withConstraint(oneTarget)
diff --git a/public/locales/en/conferences.json b/public/locales/en/conferences.json
index f04e8be00..9d27f49e7 100644
--- a/public/locales/en/conferences.json
+++ b/public/locales/en/conferences.json
@@ -308,10 +308,6 @@
"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",
@@ -320,7 +316,6 @@
"subject": "Subject",
"post_and_send": "Post and Send",
"cosign_and_send": "Cosign and Send",
- "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/tests/integration/conferenceLetters.test.ts b/tests/integration/conferenceLetters.test.ts
index 11636637f..4059a2e28 100644
--- a/tests/integration/conferenceLetters.test.ts
+++ b/tests/integration/conferenceLetters.test.ts
@@ -37,7 +37,6 @@ type Seed = {
rep?: string
sen?: string
withdrawn?: boolean
- replyTo?: string
}
type Input = { uid: string; testimonyId: string; conferenceId: string }
@@ -96,12 +95,6 @@ const seed = async (conferenceId: string, s: Seed = {}) => {
...(s.sen ? { senatorId: s.sen } : {})
})
}
- await testDb.doc(`users/${uid}/draftTestimony/${id()}`).set({
- conferenceId,
- billId: "H9901",
- court: COURT,
- ...(s.replyTo ? { replyTo: s.replyTo } : {})
- })
const input = { uid, testimonyId, conferenceId }
const eventId = await event(input, "added", 1, ADDED_AT)
return { ...input, eventId }
@@ -150,15 +143,16 @@ describe("sendConferenceLetters", () => {
it("mails a letter once, to the test address, copying the reader", async () => {
const { eventId, uid, testimonyId } = await seed(conferenceId, {
rep,
- sen,
- replyTo: "chosen@example.com"
+ sen
})
await sendConferenceLetters(NOW)
const sent = await email(eventId)
+ // The auth emulator lowercases addresses.
+ const own = `${uid}@example.com`.toLowerCase()
expect(sent).toMatchObject({
to: [TEST_RECIPIENT],
- cc: ["chosen@example.com"],
- replyTo: "chosen@example.com",
+ cc: [own],
+ replyTo: own,
from: '"Ada \\"Ace\\" Lovelace via MAPLE" '
})
expect(sent!.message.subject).toBe(
@@ -195,16 +189,6 @@ describe("sendConferenceLetters", () => {
expect((await email(eventId))!.createdAt).toEqual(sent!.createdAt)
})
- 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)
diff --git a/tests/integration/testimony.test.ts b/tests/integration/testimony.test.ts
index 7584a8965..c8db11a0a 100644
--- a/tests/integration/testimony.test.ts
+++ b/tests/integration/testimony.test.ts
@@ -577,25 +577,6 @@ 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(