Skip to content

Commit 96e68b2

Browse files
committed
add handling for missing committee documents to updateCommitteeRosters so it doesn't create weird stub committee docs with only rosters on them
1 parent 23086e8 commit 96e68b2

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

functions/src/committees/updateCommitteeRosters.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
1-
import { runWith } from "firebase-functions"
1+
import { logger, runWith } from "firebase-functions"
22
import { DocUpdate } from "../common"
33
import { db } from "../firebase"
44
import { Member } from "../members/types"
55
import { Committee } from "./types"
66
import { currentGeneralCourt } from "../shared"
77

8+
export class MissingCommitteeDocsError extends Error {
9+
constructor(
10+
public readonly court: number | string,
11+
public readonly missingIds: string[]
12+
) {
13+
super(
14+
`updateCommitteeRosters: ${missingIds.length} committee doc(s) missing for court ${court}: ${missingIds.join(", ")}. ` +
15+
`Run startCommitteeBatches first, or investigate stale member->committee references.`
16+
)
17+
this.name = "MissingCommitteeDocsError"
18+
}
19+
}
20+
821
export async function runUpdateCommitteeRosters(court: number | string) {
922
const members = await db
1023
.collection(`/generalCourts/${court}/members`)
1124
.get()
1225
.then(c => c.docs.map(d => d.data()).filter(Member.guard))
1326
const rosters = computeRosters(members)
1427

28+
const committeesRef = db.collection(`/generalCourts/${court}/committees`)
29+
const existingIds = new Set(
30+
(await committeesRef.listDocuments()).map(ref => ref.id)
31+
)
32+
const missing = Array.from(rosters.keys())
33+
.filter(id => !existingIds.has(id))
34+
.sort()
35+
if (missing.length > 0) {
36+
const err = new MissingCommitteeDocsError(court, missing)
37+
logger.error(err.message)
38+
throw err
39+
}
40+
1541
const writer = db.bulkWriter()
1642
rosters.forEach((roster, id) => {
1743
const update: DocUpdate<Committee> = {
1844
members: roster.map(m => ({ id: m.id, name: m.content.Name }))
1945
}
20-
writer.set(db.doc(`/generalCourts/${court}/committees/${id}`), update, {
46+
writer.set(committeesRef.doc(id), update, {
2147
merge: true
2248
})
2349
})

0 commit comments

Comments
 (0)