Skip to content

Commit

Permalink
Merge pull request #1599 from codeforboston/main
Browse files Browse the repository at this point in the history
8/20/24 Merge to PROD
  • Loading branch information
Mephistic authored Aug 21, 2024
2 parents c6a859f + bd84f77 commit 21e357d
Show file tree
Hide file tree
Showing 24 changed files with 329 additions and 47 deletions.
11 changes: 4 additions & 7 deletions components/db/profile/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ export function useProfile() {
if (profile) {
await updateUserDisplayNameTestimonies(
uid,
isPublic ? profile.fullName ?? "Anonymous" : "<private user>",
profile.fullName ?? "Anonymous"
isPublic ? profile.fullName ?? "Anonymous" : "private",
profile.fullName ?? "Anonymous",
isPublic
)
}
await updateIsPublic(uid, isPublic)
Expand Down Expand Up @@ -128,11 +129,7 @@ export function useProfile() {
if (uid && fullName !== profile?.fullName) {
dispatch({ updatingFullName: true })
// Update the displayName for user's testimonies
await updateUserDisplayNameTestimonies(
uid,
profile?.public ? fullName : "<private user>",
fullName
)
await updateUserDisplayNameTestimonies(uid, fullName, fullName)
await updateFullName(uid, fullName)
dispatch({ updatingFullName: false })
}
Expand Down
3 changes: 3 additions & 0 deletions components/db/testimony/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Timestamp } from "firebase/firestore"
import { httpsCallable } from "firebase/functions"
import {
Array,
Boolean,
InstanceOf,
Literal as L,
Number,
Expand Down Expand Up @@ -44,7 +45,9 @@ export const Testimony = BaseTestimony.extend({
authorRole: Role,
billTitle: String,
version: Number,
public: Boolean,
publishedAt: InstanceOf(Timestamp),
updatedAt: InstanceOf(Timestamp),
representativeId: Optional(String),
senatorId: Optional(String),
senatorDistrict: Optional(String),
Expand Down
19 changes: 7 additions & 12 deletions components/db/testimony/updateUserTestimonies.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import {
collection,
collectionGroup,
getDocs,
query,
updateDoc,
where,
writeBatch
} from "firebase/firestore"
import { collection, getDocs, writeBatch } from "firebase/firestore"
import { firestore } from "../../firebase"

// Updates the displayName for all testimonies under specified user
export const updateUserDisplayNameTestimonies = async (
uid: string,
displayName: string,
fullName: string
fullName: string,
isPublic?: boolean
) => {
const batch = writeBatch(firestore)
return getAllTestimony(uid).then(({ publishedTestimony, draftTestimony }) => {
publishedTestimony.forEach(doc =>
batch.update(doc.ref, {
authorDisplayName: displayName,
fullName: fullName
fullName: fullName,
public: isPublic
})
)
draftTestimony.forEach(doc =>
batch.update(doc.ref, {
authorDisplayName: displayName,
fullName: fullName
fullName: fullName,
public: isPublic
})
)
batch.commit().then(result => result)
Expand Down
3 changes: 2 additions & 1 deletion components/db/testimony/useEditTestimony.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ beforeEach(async () => {
})

let draft: DraftTestimony,
testimony: Omit<Testimony, "publishedAt" | "id">,
testimony: Omit<Testimony, "publishedAt" | "id" | "updatedAt">,
updatedDraft: typeof draft,
updatedTestimony: typeof testimony
beforeEach(() => {
Expand All @@ -46,6 +46,7 @@ beforeEach(() => {
fullName: "Anonymous",
court: court,
position: draft.position,
public: true,
version: 1
}
updatedDraft = { ...draft, content: "update", position: "oppose" }
Expand Down
4 changes: 3 additions & 1 deletion components/moderation/setUp/MockRecords.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ export const createMockTestimony = (
billTitle: "An act" + loremIpsum({ count: 2, units: "words" }),
version: 2,
publishedAt: Timestamp.fromDate(new Date()),
updatedAt: Timestamp.fromDate(new Date()),
billId: billId ?? "H1002",
court: 192,
position: "oppose",
content: loremIpsum({ count: 5, units: "words" }),
fullName: "Anonymous"
fullName: "Anonymous",
public: true
}
return testimony
}
Expand Down
6 changes: 3 additions & 3 deletions components/search/testimony/TestimonyHit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const TestimonyResult = ({ hit }: { hit: Hit<Testimony> }) => {
const committee = bill?.currentCommittee
const isOrg = hit.authorRole === "organization"
const writtenBy =
isOrg || hit.authorDisplayName !== "<private user>" ? (
<Link href={`/profile?id=${hit.authorUid}`}>{hit.authorDisplayName}</Link>
isOrg || hit.public ? (
<Link href={`/profile?id=${hit.authorUid}`}>{hit.fullName}</Link>
) : (
hit.authorDisplayName
hit.fullName
)
const { followOrg } = useFlags()

Expand Down
5 changes: 4 additions & 1 deletion components/search/testimony/TestimonySearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ const Layout = () => {
: ["user", "organization"]
return {
...prevState,
refinementList: { ...prevState.refinementList, authorRole: role }
refinementList: {
...prevState.refinementList,
authorRole: role
}
}
})
}
Expand Down
16 changes: 11 additions & 5 deletions components/search/testimony/useTestimonyRefinements.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { useRefinementListUiProps } from "@alexjball/react-instantsearch-hooks-web"
import { useRefinements } from "../useRefinements"
import { useCallback } from "react"
import { RefinementListItem } from "instantsearch.js/es/connectors/refinement-list/connectRefinementList"

export const useTestimonyRefinements = () => {
const baseProps = { limit: 500, searchable: true }
const propsList = [
useRefinementListUiProps({
transformItems: useCallback(
(i: RefinementListItem[]) => i.filter(i => i.label !== "private"),
[]
),
attribute: "authorDisplayName",
...baseProps,
searchablePlaceholder: "Author Name"
}),
useRefinementListUiProps({
attribute: "court",
...baseProps,
Expand All @@ -19,11 +30,6 @@ export const useTestimonyRefinements = () => {
...baseProps,
searchablePlaceholder: "Bill"
}),
useRefinementListUiProps({
attribute: "authorDisplayName",
...baseProps,
searchablePlaceholder: "Author Name"
}),
useRefinementListUiProps({
attribute: "authorRole",
...baseProps,
Expand Down
2 changes: 1 addition & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"rules": "storage.rules"
},
"extensions": {
"firestore-send-email": "firebase/[email protected].26"
"firestore-send-email": "firebase/[email protected].34"
}
}
10 changes: 10 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,16 @@
"collectionGroup": "bills",
"fieldPath": "id",
"indexes": [{ "queryScope": "COLLECTION_GROUP", "order": "ASCENDING" }]
},
{
"collectionGroup": "activeTopicSubscriptions",
"fieldPath": "nextDigestAt",
"indexes": [{ "queryScope": "COLLECTION_GROUP", "order": "ASCENDING" }]
},
{
"collectionGroup": "publishedTestimony",
"fieldPath": "authorUid",
"indexes": [{ "queryScope": "COLLECTION_GROUP", "order": "ASCENDING" }]
}
]
}
4 changes: 3 additions & 1 deletion functions/src/auth/createFakeTestimony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const createFakeTestimony = functions.https.onCall(
court: 192,
position: "oppose",
fullName: fullName,
content: fullName + " " + fullName + " " + fullName + " " + fullName
content: fullName + " " + fullName + " " + fullName + " " + fullName,
public: true,
updatedAt: Timestamp.now()
}

const testRef = db.doc(`users/${uid}/publishedTestimony/${id}`)
Expand Down
27 changes: 24 additions & 3 deletions functions/src/search/SearchIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class SearchIndexer {
if (!this.collection) {
const collection = this.client.collections(this.collectionName)
const exists = await collection.exists()
console.log("Collection exists", exists)
if (!exists) await this.createCollection()
this.collection = collection
}
Expand All @@ -104,7 +105,15 @@ export class SearchIndexer {
currentBatch++
if (numBatches && currentBatch > numBatches) return

const docs = batch.map(d => convert(d.data()))
const docs = batch.reduce((acc, d) => {
try {
const doc = convert(d.data())
acc.push(doc)
} catch (error: any) {
console.error(`Failed to convert document: ${error.message}`)
}
return acc
}, [] as any[])
const collection = await this.getCollection()
try {
await collection.documents().import(docs, { action: "upsert" })
Expand All @@ -131,12 +140,24 @@ export class SearchIndexer {

private async upgradeAlias() {
const { alias } = this.config
console.log("Upgrading alias", alias)
const obsoleteCollection = await this.getCurrentCollectionName()
console.log(
"Upgrading collection",
obsoleteCollection,
"to",
this.collectionName
)
await this.client
.aliases()
.upsert(alias, { collection_name: this.collectionName })
if (obsoleteCollection)
await this.client.collections(obsoleteCollection).delete()
if (obsoleteCollection && obsoleteCollection !== this.collectionName) {
const collection = this.client.collections(obsoleteCollection)
const exists = await collection.exists()
if (exists) {
await collection.delete()
}
}
}

private async *listCollection() {
Expand Down
12 changes: 9 additions & 3 deletions functions/src/testimony/publishTestimony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type PublishInfo = {
version: number
editReason?: string
publishedAt: Timestamp
updatedAt: Timestamp
}

class PublishTestimonyTransaction {
Expand Down Expand Up @@ -91,7 +92,8 @@ class PublishTestimonyTransaction {
...publishInfo,
attachmentId: this.attachments.id,
draftAttachmentId: this.attachments.draftId,
fullName: this.profile?.fullName ?? "Anonymous"
fullName: this.profile?.fullName ?? "Anonymous",
public: this.profile?.public ?? true
}
if (this.profile?.representative?.id) {
newPublication.representativeId = this.profile.representative.id
Expand Down Expand Up @@ -218,7 +220,11 @@ class PublishTestimonyTransaction {
const version = await this.getNextPublicationVersion(),
reason = this.draft.editReason

const info: PublishInfo = { version, publishedAt: Timestamp.now() }
const info: PublishInfo = {
version,
publishedAt: Timestamp.now(),
updatedAt: Timestamp.now()
}

if (version > 1) {
if (!reason) throw fail("invalid-argument", "Edit reason is required.")
Expand Down Expand Up @@ -274,7 +280,7 @@ class PublishTestimonyTransaction {
private getDisplayName(): string {
// Check if user has profile and then if they're private
if (this.profile) {
return this.profile.public ? this.profile.fullName : "<private user>"
return this.profile.public ? this.profile.fullName : "private"
} else {
return "Anonymous"
}
Expand Down
9 changes: 7 additions & 2 deletions functions/src/testimony/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export const {
{ name: "authorUid", type: "string", facet: false },
{ name: "authorRole", type: "string", facet: true },
{ name: "authorDisplayName", type: "string", facet: true },
{ name: "fullName", type: "string", facet: false },
{ name: "version", type: "int32", facet: false },
{ name: "publishedAt", type: "int64", facet: false }
{ name: "public", type: "bool", facet: false },
{ name: "publishedAt", type: "int64", facet: false },
{ name: "updatedAt", type: "int64", facet: false }
],
default_sorting_field: "publishedAt"
},
Expand All @@ -42,7 +45,9 @@ export const {
authorRole: testimony.authorRole,
version: testimony.version,
publishedAt: testimony.publishedAt.toMillis(),
fullName: testimony.fullName
updatedAt: testimony.updatedAt.toMillis(),
fullName: testimony.fullName,
public: testimony.public
}
return TestimonySearchRecord.check(record)
}
Expand Down
7 changes: 7 additions & 0 deletions functions/src/testimony/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Boolean,
InstanceOf,
Literal as L,
Number,
Expand Down Expand Up @@ -35,7 +36,9 @@ export const Testimony = withDefaults(
authorRole: Role,
billTitle: RtString,
version: Number,
public: Boolean,
publishedAt: InstanceOf(Timestamp),
updatedAt: InstanceOf(Timestamp),
representativeId: Optional(RtString),
senatorId: Optional(RtString),
senatorDistrict: Optional(RtString),
Expand All @@ -48,6 +51,8 @@ export const Testimony = withDefaults(
// ID is backfilled
id: "unknown",
publishedAt: Timestamp.fromMillis(0),
updatedAt: Timestamp.fromMillis(0),
public: true,
authorDisplayName: "Anonymous",
fullName: "Anonymous",
billTitle: ""
Expand Down Expand Up @@ -75,7 +80,9 @@ export const TestimonySearchRecord = R({
authorRole: RtString,
authorDisplayName: RtString,
version: Number,
public: Boolean,
publishedAt: Number,
updatedAt: Number,
fullName: RtString
})
export type TestimonySearchRecord = Static<typeof TestimonySearchRecord>
1 change: 1 addition & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { defineConfig, devices } from "@playwright/test"
*/
export default defineConfig({
testDir: "./tests/e2e",
timeout: 50000,
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
Expand Down
5 changes: 3 additions & 2 deletions scripts/firebase-admin/updateDisplayNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ export const script: Script = async ({ db }) => {
.then(result => result.docs)

const fullName = data.fullName ?? "Anonymous"
const displayName = data.public ? fullName : "<private user>"
const displayName = data.public ? fullName : "private"

for (const doc of publishedTestimony) {
writer.update(doc.ref, {
authorDisplayName: displayName,
fullName: fullName,
updatedAt: Timestamp.now()
updatedAt: Timestamp.now(),
public: data.public ?? false
})
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ Primary.args = {
billTitle: "Bill Title",
version: 1.0,
publishedAt: Timestamp.fromDate(new Date("2022-01-01T00:00:00.000Z")),
updatedAt: Timestamp.fromDate(new Date("2022-01-01T00:00:00.000Z")),
draftAttachmentId: "attachment123",
fullName: ""
fullName: "",
public: true
},
isUser: true,
onProfilePage: true
Expand Down
Loading

0 comments on commit 21e357d

Please sign in to comment.