diff --git a/app/composables/useAdmin.ts b/app/composables/useAdmin.ts index 685b6bb..5dca189 100644 --- a/app/composables/useAdmin.ts +++ b/app/composables/useAdmin.ts @@ -440,8 +440,16 @@ export const useAdmin = () => { navigateTo('/admin/builder') } - const toggleFormPublish = (form: any) => { + 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 + + await useFetch(`/api/form/${form.id}`, { + method: 'PUT', + body: {published: newStatus} + }) } const viewFormDetails = (form: any) => { 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/[id].put.ts b/server/api/form/[id].put.ts new file mode 100644 index 0000000..e21c7a1 --- /dev/null +++ b/server/api/form/[id].put.ts @@ -0,0 +1,22 @@ +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'})} + + return await prisma.form.update({ + where: { id: id.data }, + data: body.data + }) + }) \ No newline at end of file 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, 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