( data: InputTypeCreate, )
| 22 | import { ROLES } from '../types'; |
| 23 | |
| 24 | const createQuestionHandler = async ( |
| 25 | data: InputTypeCreate, |
| 26 | ): Promise<ReturnTypeCreate> => { |
| 27 | const session = await getServerSession(authOptions); |
| 28 | |
| 29 | if (!session || !session.user.id) { |
| 30 | return { |
| 31 | error: 'Unauthorized', |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | const { title, content, tags } = data; |
| 36 | |
| 37 | // Create initial slug |
| 38 | let slug = generateHandle(title); |
| 39 | |
| 40 | try { |
| 41 | const userExists = await db.user.findUnique({ |
| 42 | where: { id: session.user.id }, |
| 43 | }); |
| 44 | |
| 45 | if (!userExists) { |
| 46 | return { |
| 47 | error: 'User not found.', |
| 48 | }; |
| 49 | } |
| 50 | // Check if slug already exists |
| 51 | const existingQuestion = await db.question.findFirst({ |
| 52 | where: { slug }, |
| 53 | }); |
| 54 | |
| 55 | if (existingQuestion) { |
| 56 | slug += `-${Math.random().toString(36).substring(2, 5)}`; |
| 57 | } |
| 58 | |
| 59 | const question = await db.question.create({ |
| 60 | data: { |
| 61 | title, |
| 62 | content, |
| 63 | tags, |
| 64 | authorId: session.user.id, |
| 65 | slug, // Include the slug |
| 66 | }, |
| 67 | }); |
| 68 | revalidatePath(`/question/${question.id}`); |
| 69 | revalidatePath(`/question`); |
| 70 | |
| 71 | return { data: question }; |
| 72 | } catch (error) { |
| 73 | console.error(error); |
| 74 | return { |
| 75 | error: 'Failed to create question.', |
| 76 | }; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | const updateQuestionHandler = async ( |
| 81 | data: InputTypeUpadate, |
nothing calls this directly
no test coverage detected