Skip to content

Commit

Permalink
Admin: delete survey test data
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannaPeanut committed Oct 30, 2024
1 parent d498093 commit 6536fc7
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use client"
import { blueButtonStyles } from "@/src/core/components/links"
import { AllowedSurveySlugs } from "@/src/survey-public/utils/allowedSurveySlugs"
import deleteTestSurveyResponses from "@/src/survey-responses/mutations/deleteTestSurveyResponses"
import { useMutation } from "@blitzjs/rpc"
import clsx from "clsx"
import { useRouter } from "next/navigation"

type DeleteButtonProps = {
testSurveyResponseIds: number[]
surveySlug: AllowedSurveySlugs
}

export const DeleteButton = ({ testSurveyResponseIds, surveySlug }: DeleteButtonProps) => {
const [deleteTestSurveyMutation] = useMutation(deleteTestSurveyResponses)
const router = useRouter()
const handleDelete = async () => {
if (
window.confirm(
`Diese ${testSurveyResponseIds.length} Beteiligungsbeiträge unwiderruflich löschen?`,
)
) {
try {
await deleteTestSurveyMutation({ slug: surveySlug, deleteIds: testSurveyResponseIds })
} catch (error) {
alert("Beim Löschen ist ein Fehler aufgetreten.")
}
router.push("/admin/surveys")
}
}

return (
<>
<hr />
<button type="button" onClick={handleDelete} className={clsx(blueButtonStyles, "ml-2")}>
Diese {testSurveyResponseIds.length} Testeinträge und dazugehörige Sessions löschen
</button>
</>
)
}
53 changes: 53 additions & 0 deletions src/app/admin/surveys/[surveyId]/responses/test/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Breadcrumb } from "@/src/app/admin/_components/Breadcrumb"
import { HeaderWrapper } from "@/src/app/admin/_components/HeaderWrapper"
import { invoke } from "@/src/blitz-server"
import { H2 } from "@/src/core/components/text"
import getTestSurveyResponses from "@/src/survey-responses/queries/getTestSurveyResponses"
import getAdminSurvey from "@/src/surveys/queries/getAdminSurvey"
import { Metadata } from "next"
import "server-only"
import { DeleteButton } from "./_components/DeleteButton"

export const metadata: Metadata = { title: "Beteiligung bearbeiten erstellen" }

export default async function AdminSurveyEditPage({
params: { surveyId: surveyIdString },
}: {
params: { surveyId: string }
}) {
const survey = await invoke(getAdminSurvey, { id: Number(surveyIdString) })
const testSurveyResponses = await invoke(getTestSurveyResponses, {
slug: survey.slug,
})

return (
<>
<HeaderWrapper>
<Breadcrumb
pages={[
{ href: "/admin", name: "Dashboard" },
{ href: "/admin/surveys", name: "Beteiligungen" },
{ name: "Beteiligung: Testeinträge" },
]}
/>
</HeaderWrapper>
<H2>Testeinträge</H2>
<p className="mb-4 italic">
Dies sind alle Einträge, deren Hinweistext (ersten 20 Zeichen) &#39;test&#39; enthält (plus
die jeweils dazugehörige Umfrageteil)
{survey.slug === "radnetz-brandenburg" && " / die Institution 'FixMyCity' ist"}
</p>
{!testSurveyResponses.length
? "keine Testeinträge gefunden"
: testSurveyResponses.map((response) => (
<pre key={response.id}>{JSON.stringify(response, undefined, 2)}</pre>
))}
{!!testSurveyResponses.length && (
<DeleteButton
surveySlug={survey.slug}
testSurveyResponseIds={testSurveyResponses.map((r) => r.id)}
/>
)}
</>
)
}
3 changes: 3 additions & 0 deletions src/app/admin/surveys/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default async function AdminSurveysPage() {
<Link button href={`/admin/surveys/${survey.id}/responses`}>
Antworten
</Link>
<Link button href={`/admin/surveys/${survey.id}/responses/test`}>
Testeinträge prüfen und löschen
</Link>
</div>
<pre>{JSON.stringify(survey, undefined, 2)}</pre>
</li>
Expand Down
20 changes: 20 additions & 0 deletions src/survey-responses/mutations/deleteTestSurveyResponses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import db from "@/db"
import { AllowedSurveySlugsSchema } from "@/src/survey-public/utils/allowedSurveySlugs"
import { resolver } from "@blitzjs/rpc"
import { z } from "zod"

const Schema = AllowedSurveySlugsSchema.merge(z.object({ deleteIds: z.array(z.number()) }))

export default resolver.pipe(
resolver.zod(Schema),
resolver.authorize("ADMIN"),
async ({ deleteIds, slug }) => {
await db.surveyResponseTopicsOnSurveyResponses.deleteMany({
where: { surveyResponse: { id: { in: deleteIds } } },
})
await db.surveyResponse.deleteMany({ where: { id: { in: deleteIds } } })
await db.surveySession.deleteMany({
where: { responses: { some: { id: { in: deleteIds } } } },
})
},
)
43 changes: 43 additions & 0 deletions src/survey-responses/queries/getTestSurveyResponses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import db from "@/db"
import { AllowedSurveySlugsSchema } from "@/src/survey-public/utils/allowedSurveySlugs"
import { getResponseConfigBySurveySlug } from "@/src/survey-public/utils/getConfigBySurveySlug"
import { resolver } from "@blitzjs/rpc"

const Schema = AllowedSurveySlugsSchema

export default resolver.pipe(
resolver.zod(Schema),
resolver.authorize("ADMIN"),
async ({ slug }) => {
const { evaluationRefs } = getResponseConfigBySurveySlug(slug)
const userText1Id = evaluationRefs["feedback-usertext-1"]

const surveySessions = await db.surveySession.findMany({
where: { survey: { slug: slug } },
include: { responses: true },
})

let filteredSurveyResponses: (typeof surveySessions)[number]["responses"] = []

surveySessions.forEach(({ responses }) => {
if (
// if either of the conditions is met, add the responses of the sesseion to the filteredSurveyResponses
responses.some((response) => {
const data = JSON.parse(response.data)
return (
// survey part 2 / "Hinweis" and contains "test" in the first 20 characters of the user text
(response.surveyPart === 2 &&
// @ts-expect-error data is of type unknown
data[userText1Id]?.substring(0, 20).toLowerCase().includes("test")) ||
// survey is BB and survey part 1 / "Umfrage" and institution is "FixMyCity"
// @ts-expect-error data is of type unknown
(response.surveyPart === 1 && slug === "radnetz-brandenburg" && data[5] === "FixMyCity")
)
})
)
filteredSurveyResponses = [...filteredSurveyResponses, ...responses]
})

return filteredSurveyResponses
},
)

0 comments on commit 6536fc7

Please sign in to comment.