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
10 changes: 9 additions & 1 deletion app/composables/useAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Comment thread
AidanLoran marked this conversation as resolved.
const newStatus = form.status === 'Active' ? true : false

await useFetch(`/api/form/${form.id}`, {
method: 'PUT',
body: {published: newStatus}
})
}

const viewFormDetails = (form: any) => {
Expand Down
1 change: 0 additions & 1 deletion app/composables/useCurrentFormGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const useCurrentFormGroup = () => {
try {
const formGroupAPIResponse = await $fetch<FormGroup | FormGroup[]>('/api/formGroup?active=true')

// Handle if the API returns an array or single item
const activeFg = Array.isArray(formGroupAPIResponse) ? formGroupAPIResponse[0] : formGroupAPIResponse

if (activeFg) {
Expand Down
22 changes: 22 additions & 0 deletions server/api/form/[id].put.ts
Original file line number Diff line number Diff line change
@@ -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
})
})
3 changes: 1 addition & 2 deletions server/api/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions server/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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({
Expand Down Expand Up @@ -94,8 +95,7 @@ export type StudentCreate = z.infer<typeof studentCreateSchema>
export type StudentUpdate = z.infer<typeof studentUpdateSchema>
export type AnnouncementCreate = z.infer<typeof announcementCreateSchema>
export type FormCreate = z.infer<typeof formCreateSchema>
export type FormGET = z.infer<typeof formGETSchema>
export type FormGroupGET = z.infer<typeof formGroupGETSchema>
export type FormUpdate = z.infer<typeof formUpdateSchema>
export type FormGroupCreate = z.infer<typeof formGroupCreateSchema>
export type FormComponentCreate = z.infer<typeof formComponentCreateSchema>
export type FormSubmissionCreate = z.infer<typeof formSubmissionCreateSchema>
Expand Down