-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d498093
commit 6536fc7
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/app/admin/surveys/[surveyId]/responses/test/_components/DeleteButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) 'test' 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)} | ||
/> | ||
)} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/survey-responses/mutations/deleteTestSurveyResponses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } } } }, | ||
}) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
) |