( data: InputTypeUpadate, )
| 78 | }; |
| 79 | |
| 80 | const updateQuestionHandler = async ( |
| 81 | data: InputTypeUpadate, |
| 82 | ): Promise<ReturnTypeUpdate> => { |
| 83 | const session = await getServerSession(authOptions); |
| 84 | |
| 85 | if (!session || !session.user.id) { |
| 86 | return { |
| 87 | error: 'Unauthorized', |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | const { title, content, tags, questionId } = data; |
| 92 | const userExists = await db.user.findUnique({ |
| 93 | where: { id: session.user.id }, |
| 94 | }); |
| 95 | |
| 96 | if (!userExists) { |
| 97 | return { |
| 98 | error: 'User not found.', |
| 99 | }; |
| 100 | } |
| 101 | // Check if the user is the author of the question |
| 102 | const existingQuestion = await db.question.findUnique({ |
| 103 | where: { id: questionId }, |
| 104 | }); |
| 105 | |
| 106 | if (!existingQuestion || existingQuestion.authorId !== session.user.id) { |
| 107 | return { |
| 108 | error: 'Unauthorized: You can only update question you have authored', |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | // Create initial slug |
| 113 | let slug = generateHandle(title); |
| 114 | |
| 115 | try { |
| 116 | // Check if slug already exists for another question |
| 117 | const anotherExistingQuestion = await db.question.findFirst({ |
| 118 | where: { |
| 119 | slug, |
| 120 | AND: { |
| 121 | id: { |
| 122 | not: questionId, // Exclude the current question from the check |
| 123 | }, |
| 124 | }, |
| 125 | }, |
| 126 | }); |
| 127 | |
| 128 | if (anotherExistingQuestion) { |
| 129 | // Modify the slug if it already exists (e.g., append a random string or number) |
| 130 | slug += `-${Math.random().toString(36).substring(2, 5)}`; |
| 131 | } |
| 132 | |
| 133 | // Update question with the new slug |
| 134 | const updatedQuestion = await db.question.update({ |
| 135 | where: { id: questionId }, |
| 136 | data: { |
| 137 | title, |
nothing calls this directly
no test coverage detected