From 5a5594a48ee2632535273e200b8828e31956a597 Mon Sep 17 00:00:00 2001 From: AidanLoran Date: Thu, 4 Jun 2026 15:38:08 -0500 Subject: [PATCH 1/4] unpub/repub working admin-side, need to fix api call on reader side --- app/composables/useAdmin.ts | 8 +++++++- server/api/form/[id].put.ts | 24 ++++++++++++++++++++++++ server/utils/schemas.ts | 18 +++++++++--------- 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 server/api/form/[id].put.ts diff --git a/app/composables/useAdmin.ts b/app/composables/useAdmin.ts index 685b6bb..3535c71 100644 --- a/app/composables/useAdmin.ts +++ b/app/composables/useAdmin.ts @@ -440,8 +440,14 @@ export const useAdmin = () => { navigateTo('/admin/builder') } - const toggleFormPublish = (form: any) => { + const toggleFormPublish = async (form: any) => { form.status = form.status === 'Active' ? 'Unpublished' : 'Active' + const newStatus = form.status === 'Active' ? true : false + + await useFetch(`/api/form/${form.id}`, { + method: 'PUT', + body: {published: newStatus} + }) } const viewFormDetails = (form: any) => { diff --git a/server/api/form/[id].put.ts b/server/api/form/[id].put.ts new file mode 100644 index 0000000..29a3718 --- /dev/null +++ b/server/api/form/[id].put.ts @@ -0,0 +1,24 @@ +import { Prisma } from '~~/prisma/generated/client' +import { auth } from '~~/server/utils/auth' +import { prisma } from '~~/server/utils/prisma' +import { getQuery, setResponseStatus, type H3Event } from 'h3' +import { formUpdateSchema } from '~~/server/utils/schemas' +import { z } from 'zod' + +export default eventHandler(async (event) => { + + //require sessions + const body = formUpdateSchema.safeParse(await readBody(event)) + + if (!body.success) {throw createError({ statusCode: 400, message: body.error.message })} + const id = z.coerce.number().safeParse(event.context.params?.id) + + if (!id.success || !id.data) {throw createError({ statusCode: 400, message: 'Missing or Invalid form ID'})} + + console.log("\n\nupdating form:", id.data) + + return await prisma.form.update({ + where: { id: id.data }, + data: body.data + }) + }) \ No newline at end of file diff --git a/server/utils/schemas.ts b/server/utils/schemas.ts index 1350b7b..6520b80 100644 --- a/server/utils/schemas.ts +++ b/server/utils/schemas.ts @@ -45,11 +45,6 @@ export const formGroupCreateSchema = z.object({ endDate: z.coerce.date(), }) -export const formGroupGETSchema = formGroupCreateSchema.extend({ - id: z.int(), - raffleWinner: z.int().nullish() -}) - export const formCreateSchema = z.object({ order: z.int(), startDate: z.coerce.date(), @@ -60,8 +55,14 @@ export const formCreateSchema = z.object({ title: z.string().min(1).max(250), }) -export const formGETSchema = formCreateSchema.extend({ - id: z.int() +export const formUpdateSchema = z.object({ + order: z.int().optional(), + startDate: z.coerce.date().optional(), + endDate: z.coerce.date().nullish(), + published: z.boolean().optional(), + author: z.cuid2().nullish(), + formGroup: z.int().optional(), + title: z.string().min(1).max(250).optional(), }) export const formComponentCreateSchema = z.object({ @@ -94,8 +95,7 @@ export type StudentCreate = z.infer export type StudentUpdate = z.infer export type AnnouncementCreate = z.infer export type FormCreate = z.infer -export type FormGET = z.infer -export type FormGroupGET = z.infer +export type FormUpdate = z.infer export type FormGroupCreate = z.infer export type FormComponentCreate = z.infer export type FormSubmissionCreate = z.infer From da46ee0bd579b539f6463b04cc79826d00a05e82 Mon Sep 17 00:00:00 2001 From: AidanLoran Date: Thu, 4 Jun 2026 15:39:55 -0500 Subject: [PATCH 2/4] removed testing log --- server/api/form/[id].put.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/api/form/[id].put.ts b/server/api/form/[id].put.ts index 29a3718..e21c7a1 100644 --- a/server/api/form/[id].put.ts +++ b/server/api/form/[id].put.ts @@ -15,8 +15,6 @@ export default eventHandler(async (event) => { if (!id.success || !id.data) {throw createError({ statusCode: 400, message: 'Missing or Invalid form ID'})} - console.log("\n\nupdating form:", id.data) - return await prisma.form.update({ where: { id: id.data }, data: body.data From 1e2b374664de28ff9a39ed96eef272add28baadf Mon Sep 17 00:00:00 2001 From: AidanLoran Date: Thu, 4 Jun 2026 16:28:41 -0500 Subject: [PATCH 3/4] handler updated to only get published forms for reader side --- app/composables/useCurrentFormGroup.ts | 1 - server/api/form/index.ts | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/composables/useCurrentFormGroup.ts b/app/composables/useCurrentFormGroup.ts index b3c2974..7d44568 100644 --- a/app/composables/useCurrentFormGroup.ts +++ b/app/composables/useCurrentFormGroup.ts @@ -29,7 +29,6 @@ export const useCurrentFormGroup = () => { try { const formGroupAPIResponse = await $fetch('/api/formGroup?active=true') - // Handle if the API returns an array or single item const activeFg = Array.isArray(formGroupAPIResponse) ? formGroupAPIResponse[0] : formGroupAPIResponse if (activeFg) { diff --git a/server/api/form/index.ts b/server/api/form/index.ts index f78e1e0..4fe717c 100644 --- a/server/api/form/index.ts +++ b/server/api/form/index.ts @@ -466,8 +466,7 @@ export default defineEventHandler(async (event) => { const where: Prisma.FormWhereInput = {} if (query.formGroup !== null) { where.formGroup = Number(query.formGroup) } - - if (query.published) { where.published = toBoolean(query.published) } + where.published = true return await prisma.form.findMany({ where, From 5293710c737a51d17a4c75e8bf8d4d2e0d0b028f Mon Sep 17 00:00:00 2001 From: AidanLoran <81245796+AidanLoran@users.noreply.github.com> Date: Fri, 12 Jun 2026 23:41:30 -0500 Subject: [PATCH 4/4] Update app/composables/useAdmin.ts with suggested comment Co-authored-by: Swarna Sre <67798786+chanabyte@users.noreply.github.com> --- app/composables/useAdmin.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/composables/useAdmin.ts b/app/composables/useAdmin.ts index 3535c71..5dca189 100644 --- a/app/composables/useAdmin.ts +++ b/app/composables/useAdmin.ts @@ -441,6 +441,8 @@ export const useAdmin = () => { } const toggleFormPublish = async (form: any) => { +// "unpublished" = "nonactive" b +// a form being "published" is measured with True and False - boolean value form.status = form.status === 'Active' ? 'Unpublished' : 'Active' const newStatus = form.status === 'Active' ? true : false