Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/api/delete-testimony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import { mapleClient } from "./maple-client"
* @param tid testimony id
* @returns 204 response (no body) if successful, or 4XX if not.
*/
export async function deleteTestimony(uid: string, tid: string) {
export async function deleteTestimonyv2(uid: string, tid: string) {
return mapleClient.delete(`/api/users/${uid}/testimony/${tid}`)
}
8 changes: 4 additions & 4 deletions components/auth/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { useAsyncCallback } from "react-async-hook"
import { setProfile } from "../db"
import { auth } from "../firebase"
import { finishSignup, OrgCategory } from "./types"
import { finishSignupv2, OrgCategory } from "./types"

const errorMessages: Record<string, string | undefined> = {
"auth/email-already-exists": "You already have an account.",
Expand Down Expand Up @@ -68,15 +68,15 @@ export function useCreateUserWithEmailAndPassword(isOrg: boolean) {
password
)
if (isOrg) {
await finishSignup({
await finishSignupv2({
requestedRole: "organization",
fullName,
orgCategories: orgCategory ? [orgCategory] : "",
notificationFrequency: "Weekly",
email: credentials.user.email
})
} else {
await finishSignup({
await finishSignupv2({
requestedRole: "user",
fullName,
notificationFrequency: "Weekly",
Expand Down Expand Up @@ -125,7 +125,7 @@ export function useSignInWithPopUp() {
const { claims } = await credentials.user.getIdTokenResult()
if (!claims?.role) {
// The user has not yet finished signing up
await finishSignup({ requestedRole: "user" })
await finishSignupv2({ requestedRole: "user" })
await Promise.all([
setProfile(credentials.user.uid, {
fullName: credentials.user.displayName ?? "New User",
Expand Down
5 changes: 5 additions & 0 deletions components/auth/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ export const finishSignup = httpsCallable<
{ requestedRole: Role } | Partial<Profile>,
void
>(functions, "finishSignup")

export const finishSignupv2 = httpsCallable<
{ requestedRole: Role } | Partial<Profile>,
void
>(functions, "finishSignupv2")
14 changes: 14 additions & 0 deletions components/db/testimony/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,26 @@ export const deleteTestimony = httpsCallable<
{ deleted: boolean }
>(functions, "deleteTestimony")

export const deleteTestimonyv2 = httpsCallable<
{ publicationId: string },
{ deleted: boolean }
>(functions, "deleteTestimonyv2")

export const publishTestimony = httpsCallable<
{ draftId: string },
{ publicationId: string }
>(functions, "publishTestimony")

export const publishTestimonyv2 = httpsCallable<
{ draftId: string },
{ publicationId: string }
>(functions, "publishTestimonyv2")

export const resolveReport = httpsCallable<report.Request, report.Response>(
functions,
"adminResolveReport"
)
export const resolveReportv2 = httpsCallable<report.Request, report.Response>(
functions,
"adminResolveReportv2"
)
6 changes: 4 additions & 2 deletions components/db/testimony/useEditTestimony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import { firestore } from "../../firebase"
import { resolveBillTestimony } from "./resolveTestimony"
import {
deleteTestimony,
deleteTestimonyv2,
DraftTestimony,
hasDraftChanged,
publishTestimony,
publishTestimonyv2,
Testimony,
WorkingDraft
} from "./types"
Expand Down Expand Up @@ -175,7 +177,7 @@ function usePublishTestimony(
DraftTestimony.check(workingDraft)
// TODO: don't publish again if draft.publishedVersion is defined
if (draftRef) {
const result = await publishTestimony({ draftId: draftRef.id })
const result = await publishTestimonyv2({ draftId: draftRef.id })
dispatch({ type: "resolvePublication", id: result.data.publicationId })
}
}, [dispatch, draftRef, workingDraft]),
Expand All @@ -190,7 +192,7 @@ function useDeleteTestimony(
return useAsyncCallback(
useCallback(async () => {
if (publicationRef) {
const result = await deleteTestimony({
const result = await deleteTestimonyv2({
publicationId: publicationRef.id
})
if (result.data.deleted) dispatch({ type: "deletePublication" })
Expand Down
3 changes: 2 additions & 1 deletion components/moderation/ListProfiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Internal } from "components/links"
import { ButtonGroup } from "@mui/material"
import { Role } from "components/auth"
import { createFakeOrg } from "components/moderation"
import { createFakeOrgv2 } from "components/moderation"
import { loremIpsum } from "lorem-ipsum"
import { nanoid } from "nanoid"

Expand All @@ -46,7 +47,7 @@ const UserRoleToolBar = () => {
const fullName = loremIpsum({ count: 2, units: "words" })
const email = `${uid}@example.com`

await createFakeOrg({ uid, fullName, email })
await createFakeOrgv2({ uid, fullName, email })

if (filterValues["role"] === "organization")
setFilters({ role: "pendingUpgrade" }, [])
Expand Down
7 changes: 4 additions & 3 deletions components/moderation/RemoveTestimony.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Card, CardContent, CardHeader, Stack } from "@mui/material"
import { deleteTestimony } from "components/api/delete-testimony"
import { deleteTestimonyv2 } from "components/api/delete-testimony"
import { resolveReport } from "components/db"
import { resolveReportv2 } from "components/db"
import { getAuth } from "firebase/auth"
import { doc, getDoc } from "firebase/firestore"
import { Timestamp } from "functions/src/firebase"
Expand All @@ -24,7 +25,7 @@ export const onSubmitReport = async (
testimonyId: string,
refresh: () => void
) => {
const r = await resolveReport({
const r = await resolveReportv2({
reportId,
resolution,
reason
Expand All @@ -36,7 +37,7 @@ export const onSubmitReport = async (

if (resolution === "remove-testimony") {
// If removing testimony, call deleteTestimony to move testimony from 'published' to 'archived'
const res = await deleteTestimony(authorUid, testimonyId)
const res = await deleteTestimonyv2(authorUid, testimonyId)
}
refresh()
}
Expand Down
3 changes: 2 additions & 1 deletion components/moderation/setUp/CreateMockReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useReportTestimony } from "components/api/report"
import { Testimony } from "components/db"
import { auth, firestore } from "components/firebase"
import { createFakeTestimony } from "components/moderation"
import { createFakeTestimonyv2 } from "components/moderation"
import { doc, getDoc } from "firebase/firestore"
import { loremIpsum } from "lorem-ipsum"
import { nanoid } from "nanoid"
Expand All @@ -21,7 +22,7 @@ export const CreateMockReport = () => {
const fullName = loremIpsum({ count: 2, units: "words" })
const email = `${uid}@example.com`

const result = await createFakeTestimony({
const result = await createFakeTestimonyv2({
uid,
fullName,
email
Expand Down
13 changes: 13 additions & 0 deletions components/moderation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const modifyAccount = httpsCallable<{ uid: string; role: Role }, void>(
functions,
"modifyAccount"
)
export const modifyAccountv2 = httpsCallable<{ uid: string; role: Role }, void>(
functions,
"modifyAccountv2"
)

type Request = { uid: string; fullName: string; email: string }
type Response = { uid: string; tid: string }
Expand All @@ -41,7 +45,16 @@ export const createFakeOrg = httpsCallable<Request, void>(
"createFakeOrg"
)

export const createFakeOrgv2 = httpsCallable<Request, void>(
functions,
"createFakeOrg"
)

export const createFakeTestimony = httpsCallable<Request, Response>(
functions,
"createFakeTestimony"
)
export const createFakeTestimonyv2 = httpsCallable<Request, Response>(
functions,
"createFakeTestimonyv2"
)
31 changes: 30 additions & 1 deletion functions/src/auth/createFakeOrg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as functions from "firebase-functions"
import { checkAdmin, checkAuth } from "../common"
import { checkAdmin, checkAdminv2, checkAuth, checkAuthv2 } from "../common"
import { auth, db } from "../firebase"
import { onCall, CallableRequest } from "firebase-functions/v2/https"

// for populating admin module for testing & demonstration
//@TODO: remove
Expand Down Expand Up @@ -32,3 +33,31 @@ export const createFakeOrg = functions.https.onCall(async (data, context) => {

return { ...authUser, uid: userRecord.uid }
})

export const createFakeOrgv2 = onCall(async (request: CallableRequest) => {
checkAuthv2(request, false)
checkAdminv2(request)

const { uid, fullName, email } = request.data

const newUser = {
uid,
fullName,
email,
password: "password",
public: true,
role: "pendingUpgrade"
}

const role = "pendingUpgrade"
const userRecord = await auth.createUser(newUser)

await auth.setCustomUserClaims(newUser.uid, { role })
await db.doc(`/profiles/${newUser.uid}`).set(newUser)

const authUser = (await db.doc(`/profiles/${newUser.uid}`).get()).data()

console.log(authUser)

return { ...authUser, uid: userRecord.uid }
})
53 changes: 51 additions & 2 deletions functions/src/auth/createFakeTestimony.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as functions from "firebase-functions"
import { checkAdmin, checkAuth } from "../common"
import * as functions from "firebase-functions/v1"
import { checkAdmin, checkAdminv2, checkAuth, checkAuthv2 } from "../common"
import { auth, db } from "../firebase"
import { Testimony } from "../testimony/types"
import { Timestamp } from "../firebase"
import { onCall, CallableRequest } from "firebase-functions/v2/https"

// for populating admin module for testing & demonstration--alert--no auth checked here.
//@TODO: remove
Expand Down Expand Up @@ -54,3 +55,51 @@ export const createFakeTestimony = functions.https.onCall(
return { uid: uid, tid: id }
}
)

export const createFakeTestimonyv2 = onCall(
async (request: CallableRequest) => {
console.log("running fake testimony")
checkAuthv2(request, false)
checkAdminv2(request)

const { uid, fullName, email } = request.data

const author = {
uid,
fullName,
email,
password: "password",
public: true,
role: "user"
}

await auth.createUser({ uid })

await db.doc(`profiles/${uid}`).set(author)

const id = `${uid}ttmny`

const testimony: Testimony = {
id,
authorUid: author.uid,
authorDisplayName: "none",
authorRole: "user",
billTitle: "An act",
version: 2,
billId: "H1002",
publishedAt: Timestamp.now(),
court: 192,
position: "oppose",
fullName: fullName,
content: fullName + " " + fullName + " " + fullName + " " + fullName,
public: true,
updatedAt: Timestamp.now()
}

const testRef = db.doc(`users/${uid}/publishedTestimony/${id}`)

await testRef.set(testimony)

return { uid: uid, tid: id }
}
)
20 changes: 19 additions & 1 deletion functions/src/auth/modifyAccount.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as functions from "firebase-functions"
import { db, auth } from "../firebase"
import { z } from "zod"
import { checkRequestZod, checkAuth, checkAdmin } from "../common"
import {
checkRequestZod,
checkAuth,
checkAdmin,
checkAuthv2,
checkAdminv2
} from "../common"
import { setRole } from "."
import { onCall, CallableRequest } from "firebase-functions/v2/https"

import { ZRole } from "./types"

Expand All @@ -21,3 +28,14 @@ export const modifyAccount = functions.https.onCall(async (data, context) => {

await setRole({ role, auth, db, uid })
})

export const modifyAccountv2 = onCall(async (request: CallableRequest) => {
checkAuthv2(request, false)
checkAdminv2(request)

const { uid, role } = checkRequestZod(Request, request.data)

console.log(`Setting role for ${uid} to ${role}`)

await setRole({ role, auth, db, uid })
})
Loading
Loading