Added wrong token error handling and fixed some mistakes with answers

This commit is contained in:
Dmitriy Shishkov 2020-10-19 22:47:17 +05:00
parent 673f42fd0f
commit c640602e99
No known key found for this signature in database
GPG Key ID: D76D70029F55183E
7 changed files with 34 additions and 7 deletions

2
.gitignore vendored
View File

@ -2,6 +2,8 @@
/dist /dist
.env
*.gen.ts *.gen.ts
*.local* *.local*

View File

@ -35,10 +35,14 @@ const getFormAuthor = async (db: PrismaClient, id: number) => {
} }
const tokenGenerate = (email: string, id: number) => { const tokenGenerate = (email: string, id: number) => {
return jwt.sign({ email, id }, '' + process.env.JWT_SECRET, { const token = jwt.sign({ email, id }, '' + process.env.JWT_SECRET, {
algorithm: 'HS256', algorithm: 'HS256',
expiresIn: '7 days', expiresIn: '7 days',
}) })
if (process.env.NODE_ENV != 'production') console.log(token)
return token
} }
const genAndSendToken = async ( const genAndSendToken = async (

View File

@ -39,8 +39,11 @@ const getForm = async (
author: dbForm.author, author: dbForm.author,
dateCreated: dbForm.dateCreated.toString(), dateCreated: dbForm.dateCreated.toString(),
id: dbForm.id, id: dbForm.id,
questions: [...dbForm.choisesQuestions, ...dbForm.inputQuestions], questions: [...dbForm.choisesQuestions, ...dbForm.inputQuestions].sort(
(a, b) => a.number - b.number
),
submissions: dbForm.submissions.map((submission) => ({ submissions: dbForm.submissions.map((submission) => ({
user: submission.user,
answers: submission.answers, answers: submission.answers,
date: submission.date.toString(), date: submission.date.toString(),
id: submission.id, id: submission.id,
@ -68,6 +71,7 @@ const getForms = async (
id: form.id, id: form.id,
questions: [...form.choisesQuestions, ...form.inputQuestions], questions: [...form.choisesQuestions, ...form.inputQuestions],
submissions: form.submissions.map((submission) => ({ submissions: form.submissions.map((submission) => ({
user: submission.user,
answers: submission.answers, answers: submission.answers,
date: submission.date.toString(), date: submission.date.toString(),
id: submission.id, id: submission.id,
@ -137,6 +141,8 @@ const submitAnswer = async (
try { try {
const parsedAnswers = <DbAnswer[]>JSON.parse(answers) const parsedAnswers = <DbAnswer[]>JSON.parse(answers)
console.log(parsedAnswers)
const res = await submitDBAnswer(db, userId, formId, parsedAnswers) const res = await submitDBAnswer(db, userId, formId, parsedAnswers)
if (!res) throw new UserInputError("Can't submit form") if (!res) throw new UserInputError("Can't submit form")
@ -155,15 +161,18 @@ const formatForms = (
inputQuestions: InputQuestion[] inputQuestions: InputQuestion[]
submissions: (Omit<FormSubmission, 'date'> & { date: Date })[] submissions: (Omit<FormSubmission, 'date'> & { date: Date })[]
})[] })[]
) => ): GraphqlForm[] =>
forms.map((form) => ({ forms.map<GraphqlForm>((form) => ({
dateCreated: form.dateCreated.toString(), dateCreated: form.dateCreated.toString(),
id: form.id, id: form.id,
questions: [...form.choisesQuestions, ...form.inputQuestions], questions: [...form.choisesQuestions, ...form.inputQuestions].sort(
(a, b) => a.number - b.number
),
submissions: form.submissions.map((submission) => ({ submissions: form.submissions.map((submission) => ({
answers: submission.answers, answers: submission.answers,
date: submission.date.toString(), date: submission.date.toString(),
id: submission.id, id: submission.id,
user: submission.user,
})), })),
title: form.title, title: form.title,
})) }))

View File

@ -42,6 +42,7 @@ const getDBForm = (
inputQuestions: true, inputQuestions: true,
submissions: { submissions: {
include: { include: {
user: true,
answers: true, answers: true,
}, },
where: where:
@ -77,6 +78,7 @@ const getDBFormsByUser = (db: PrismaClient, id: number) =>
inputQuestions: true, inputQuestions: true,
submissions: { submissions: {
include: { include: {
user: true,
answers: true, answers: true,
}, },
}, },
@ -126,6 +128,7 @@ const findDBUserBy = (db: PrismaClient, params: IFindUserParams) =>
inputQuestions: true, inputQuestions: true,
submissions: { submissions: {
include: { include: {
user: true,
answers: true, answers: true,
}, },
}, },

View File

@ -18,6 +18,14 @@ app.use(
}) })
) )
const errorHandler: express.ErrorRequestHandler = (err, _, res, __) => {
if (err.name === 'UnauthorizedError') {
res.status(401).send('Invalid token')
}
}
app.use(errorHandler)
const db = new PrismaClient() const db = new PrismaClient()
const server = new ApolloServer({ const server = new ApolloServer({

View File

@ -23,7 +23,7 @@ const loginMutation: Resolver<
try { try {
const user = await findUserBy(db, { email }) const user = await findUserBy(db, { email })
if (user instanceof Error) throw user // Needed to a strange error if (user instanceof Error) throw user // Needed to fix a strange error
await genAndSendToken(email, user) await genAndSendToken(email, user)
@ -42,7 +42,7 @@ const registerMutation: Resolver<
try { try {
const user = await createUser(db, { email, name }) const user = await createUser(db, { email, name })
if (user instanceof Error) throw user // Needed to a strange error if (user instanceof Error) throw user // Needed to fix a strange error
await genAndSendToken(email, user) await genAndSendToken(email, user)

View File

@ -42,6 +42,7 @@ type InputQuestion implements Question {
} }
type FormSubmission { type FormSubmission {
user: User
answers: [Answer!]! answers: [Answer!]!
date: String! date: String!
id: Int! id: Int!