213 lines
5.6 KiB
TypeScript
213 lines
5.6 KiB
TypeScript
import { any, z } from 'zod';
|
|
|
|
// Question types
|
|
export enum QuestionType {
|
|
TITLE = 'title',
|
|
SIMPLE = 'simple',
|
|
MULTIPLE_CHOICE = 'multiple_choice',
|
|
SINGLE_CHOICE = 'single_choice',
|
|
SELECT = 'select'
|
|
}
|
|
|
|
// Base question schema
|
|
const baseQuestionSchema = z.object({
|
|
id: z.string(),
|
|
type: z.nativeEnum(QuestionType),
|
|
required: z.boolean().default(false),
|
|
position: z.number()
|
|
});
|
|
|
|
// Title question
|
|
export const titleQuestionSchema = baseQuestionSchema.extend({
|
|
type: z.literal(QuestionType.TITLE),
|
|
content: z.string()
|
|
});
|
|
|
|
// Simple question (text input)
|
|
export const simpleQuestionSchema = baseQuestionSchema.extend({
|
|
type: z.literal(QuestionType.SIMPLE),
|
|
question: z.string()
|
|
});
|
|
|
|
// Option-based questions
|
|
const optionSchema = z.object({
|
|
id: z.string(),
|
|
text: z.string()
|
|
});
|
|
|
|
// Multiple choice question
|
|
export const multipleChoiceQuestionSchema = baseQuestionSchema.extend({
|
|
type: z.literal(QuestionType.MULTIPLE_CHOICE),
|
|
question: z.string(),
|
|
options: z.array(optionSchema)
|
|
});
|
|
|
|
// Single choice question
|
|
export const singleChoiceQuestionSchema = baseQuestionSchema.extend({
|
|
type: z.literal(QuestionType.SINGLE_CHOICE),
|
|
question: z.string(),
|
|
options: z.array(optionSchema)
|
|
});
|
|
|
|
// Select question
|
|
export const selectQuestionSchema = baseQuestionSchema.extend({
|
|
type: z.literal(QuestionType.SELECT),
|
|
question: z.string(),
|
|
options: z.array(optionSchema)
|
|
});
|
|
|
|
// Union of all question types
|
|
export const questionSchema = z.discriminatedUnion('type', [
|
|
titleQuestionSchema,
|
|
simpleQuestionSchema,
|
|
multipleChoiceQuestionSchema,
|
|
singleChoiceQuestionSchema,
|
|
selectQuestionSchema
|
|
]);
|
|
|
|
// Survey schema
|
|
export const surveySchema = z.object({
|
|
id: z.number().optional(),
|
|
title: z.string(),
|
|
description: z.string(),
|
|
targetAudience: z.string(),
|
|
closingDate: z.date().optional(),
|
|
published: z.boolean(),
|
|
questions: z.array(questionSchema),
|
|
created_at: z.string().optional(),
|
|
updated_at: z.string().optional(),
|
|
});
|
|
|
|
|
|
|
|
|
|
// Survey Answer request schema
|
|
export const surveyAnswerMutateSchema = z.object({
|
|
surveyId: z.string(),
|
|
answers: z.array(
|
|
z.object({
|
|
questionId: z.string(),
|
|
value: z.union([z.string(), z.array(z.string())])
|
|
})
|
|
),
|
|
|
|
});
|
|
|
|
// Types based on schemas
|
|
export type Question = z.infer<typeof questionSchema>;
|
|
export type TitleQuestion = z.infer<typeof titleQuestionSchema>;
|
|
export type SimpleQuestion = z.infer<typeof simpleQuestionSchema>;
|
|
export type MultipleChoiceQuestion = z.infer<typeof multipleChoiceQuestionSchema>;
|
|
export type SingleChoiceQuestion = z.infer<typeof singleChoiceQuestionSchema>;
|
|
export type SelectQuestion = z.infer<typeof selectQuestionSchema>;
|
|
export type Survey = z.infer<typeof surveySchema>;
|
|
export type SurveyResponse = z.infer<typeof surveyAnswerMutateSchema>;
|
|
|
|
|
|
export const surveyApiSchema = z.object({
|
|
id: z.number().optional(),
|
|
title: z.string(),
|
|
description: z.string(),
|
|
targetAudience: z.string(),
|
|
closingDate: z.string().transform((str) => str ? new Date(str) : null),
|
|
published: z.boolean(),
|
|
questions: z.array(questionSchema),
|
|
created_at: z.string().optional(),
|
|
updated_at: z.string().optional(),
|
|
});
|
|
export type SurveyTable = z.infer<typeof surveyApiSchema>;
|
|
|
|
|
|
// Api response schemas
|
|
export const surveysApiResponseSchema = z.object({
|
|
message: z.string(),
|
|
data: z.array(surveyApiSchema),
|
|
meta: z.object({
|
|
page: z.number(),
|
|
limit: z.number(),
|
|
totalCount: z.number(),
|
|
totalPages: z.number(),
|
|
hasNextPage: z.boolean(),
|
|
hasPreviousPage: z.boolean(),
|
|
nextPage: z.number().nullable(),
|
|
previousPage: z.number().nullable(),
|
|
}),
|
|
});
|
|
|
|
|
|
// Survey response schema
|
|
export const surveyAnswerApiResponseSchema = z.object({
|
|
id: z.number().optional(),
|
|
surveyId: z.number(),
|
|
userId: z.number(),
|
|
answers: z.array(
|
|
z.object({
|
|
questionId: z.string(),
|
|
value: z.union([z.string(), z.array(z.string())])
|
|
})
|
|
),
|
|
created_at: z.string().optional(),
|
|
updated_at: z.string().optional(),
|
|
});
|
|
|
|
|
|
// Response schemas for the API create, update
|
|
export const SurveyAnswerMutate = z.object({
|
|
message: z.string(),
|
|
data: surveyAnswerApiResponseSchema,
|
|
});
|
|
|
|
// Response schemas for the API create, update
|
|
export const suveryApiMutationResponseSchema = z.object({
|
|
message: z.string(),
|
|
data: surveyApiSchema,
|
|
});
|
|
|
|
// Response schemas for the API create, update
|
|
export const suveryResponseDeleteSchema = z.object({
|
|
message: z.string(),
|
|
});
|
|
|
|
|
|
|
|
|
|
// Schema For User Survey Answer
|
|
export const surveyAnswerQuerySchema = z.object({
|
|
surverId: z.number(),
|
|
title: z.string(),
|
|
description: z.string(),
|
|
created_at: z.string().transform((str) => str ? new Date(str) : null),
|
|
closingDate: z.string().transform((str) => str ? new Date(str) : null),
|
|
targetAudience: z.string(),
|
|
user_id: z.number().nullable(),
|
|
});
|
|
|
|
export const surveyAnswerQuerySchema2 = z.object({
|
|
surveys: z.any(),
|
|
answers_surveys: z.any().optional()
|
|
// surverId: z.number(),
|
|
// title: z.string(),
|
|
// description: z.string(),
|
|
// created_at: z.string().transform((str) => str ? new Date(str) : null),
|
|
// closingDate: z.string().transform((str) => str ? new Date(str) : null),
|
|
// targetAudience: z.string(),
|
|
// user_id: z.number().nullable(),
|
|
});
|
|
|
|
// Api response schemas
|
|
export const surveysApiResponseForUserSchema = z.object({
|
|
message: z.string(),
|
|
data: z.array(surveyAnswerQuerySchema2),
|
|
meta: z.object({
|
|
page: z.number(),
|
|
limit: z.number(),
|
|
totalCount: z.number(),
|
|
totalPages: z.number(),
|
|
hasNextPage: z.boolean(),
|
|
hasPreviousPage: z.boolean(),
|
|
nextPage: z.number().nullable(),
|
|
previousPage: z.number().nullable(),
|
|
}),
|
|
});
|
|
export type SurveyAnswerForUser = z.infer<typeof surveyAnswerQuerySchema2>;
|