diff --git a/.gitignore b/.gitignore index e2d1a56..ccff44b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ /dist +.env + *.gen.ts *.local* diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index a523df0..d7d52a9 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -35,10 +35,14 @@ const getFormAuthor = async (db: PrismaClient, 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', expiresIn: '7 days', }) + + if (process.env.NODE_ENV != 'production') console.log(token) + + return token } const genAndSendToken = async ( diff --git a/src/controllers/form.ts b/src/controllers/form.ts index 8f41278..10fe5ac 100644 --- a/src/controllers/form.ts +++ b/src/controllers/form.ts @@ -39,8 +39,11 @@ const getForm = async ( author: dbForm.author, dateCreated: dbForm.dateCreated.toString(), 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) => ({ + user: submission.user, answers: submission.answers, date: submission.date.toString(), id: submission.id, @@ -68,6 +71,7 @@ const getForms = async ( id: form.id, questions: [...form.choisesQuestions, ...form.inputQuestions], submissions: form.submissions.map((submission) => ({ + user: submission.user, answers: submission.answers, date: submission.date.toString(), id: submission.id, @@ -137,6 +141,8 @@ const submitAnswer = async ( try { const parsedAnswers = JSON.parse(answers) + console.log(parsedAnswers) + const res = await submitDBAnswer(db, userId, formId, parsedAnswers) if (!res) throw new UserInputError("Can't submit form") @@ -155,15 +161,18 @@ const formatForms = ( inputQuestions: InputQuestion[] submissions: (Omit & { date: Date })[] })[] -) => - forms.map((form) => ({ +): GraphqlForm[] => + forms.map((form) => ({ dateCreated: form.dateCreated.toString(), 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) => ({ answers: submission.answers, date: submission.date.toString(), id: submission.id, + user: submission.user, })), title: form.title, })) diff --git a/src/db/index.ts b/src/db/index.ts index 760d31b..5be26b7 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -42,6 +42,7 @@ const getDBForm = ( inputQuestions: true, submissions: { include: { + user: true, answers: true, }, where: @@ -77,6 +78,7 @@ const getDBFormsByUser = (db: PrismaClient, id: number) => inputQuestions: true, submissions: { include: { + user: true, answers: true, }, }, @@ -126,6 +128,7 @@ const findDBUserBy = (db: PrismaClient, params: IFindUserParams) => inputQuestions: true, submissions: { include: { + user: true, answers: true, }, }, diff --git a/src/index.ts b/src/index.ts index 722b1ea..8cd6e2b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 server = new ApolloServer({ diff --git a/src/resolvers/User.ts b/src/resolvers/User.ts index 253022a..3e1eb1b 100644 --- a/src/resolvers/User.ts +++ b/src/resolvers/User.ts @@ -23,7 +23,7 @@ const loginMutation: Resolver< try { 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) @@ -42,7 +42,7 @@ const registerMutation: Resolver< try { 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) diff --git a/src/typeDefs/typeDefs.gql b/src/typeDefs/typeDefs.gql index dbcb68d..e3607fc 100644 --- a/src/typeDefs/typeDefs.gql +++ b/src/typeDefs/typeDefs.gql @@ -42,6 +42,7 @@ type InputQuestion implements Question { } type FormSubmission { + user: User answers: [Answer!]! date: String! id: Int!