Added form submission mutation, some code refactoring

This commit is contained in:
Dmitriy Shishkov 2020-10-10 12:07:34 +05:00
parent 9439730f4c
commit a07e0d752f
9 changed files with 128 additions and 40 deletions

View File

@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client'
import { PrismaClient, Answer } from '@prisma/client'
import { ApolloError } from 'apollo-server-express'
import { createDBForm, getDBForm, getDBFormByUser } from '../db'
import { createDBForm, getDBForm, getDBFormByUser, submitDBAnswer } from '../db'
import { FullForm } from '../db/types'
import {
ChoisesQuestion,
@ -9,6 +9,7 @@ import {
FormSubmission,
InputQuestion,
MutationCreateFormArgs,
MutationFormSubmitArgs,
Question
} from '../typeDefs/typeDefs.gen'
@ -98,4 +99,14 @@ const createFormFrom = async (
return createDBForm(db, newForm, id)
}
export { getForm, getForms, createFormFrom }
const submitAnswer = async (
db: PrismaClient,
{ answers, formId }: MutationFormSubmitArgs,
userId: number
) => {
const parsedAnswers = <Answer[]>JSON.parse(answers)
return submitDBAnswer(db, userId, formId, parsedAnswers)
}
export { getForm, getForms, createFormFrom, submitAnswer }

View File

@ -1,5 +1,6 @@
import { getForm, getForms, createFormFrom } from './form'
import { checkRightsAndResolve, getFormAuthor, sendTokenEmail } from './auth'
import { findUserBy } from './user'
export {
checkRightsAndResolve,
@ -7,5 +8,6 @@ export {
getFormAuthor,
getForms,
createFormFrom,
sendTokenEmail
sendTokenEmail,
findUserBy
}

View File

@ -1,5 +1,8 @@
import { AnswerType } from '@prisma/client'
import {
Answer,
ChoisesQuestion,
FormSubmission,
InputQuestion,
Variant
} from '../typeDefs/typeDefs.gen'

29
src/controllers/user.ts Normal file
View File

@ -0,0 +1,29 @@
import { PrismaClient } from '@prisma/client'
import { UserInputError } from 'apollo-server-express'
import { createDBUser, findDBUserBy } from '../db'
import { IFindUserParams } from '../db/types'
import { MutationRegisterArgs, User } from '../typeDefs/typeDefs.gen'
const createUser = async (
db: PrismaClient,
{ email, name }: MutationRegisterArgs
): Promise<User> => {
if (!email || !name)
throw new UserInputError(
'Provide full user information',
[!email ? [email] : [], !name ? [name] : []].flat()
)
return await createDBUser(db, { email, name })
}
const findUserBy = async (db: PrismaClient, params: IFindUserParams) => {
const user = await findDBUserBy(db, params)
if (!user) throw new UserInputError('No such user')
return user
}
export { createUser, findUserBy }

View File

@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client'
import { UserInputError } from 'apollo-server-express'
import { newForm } from '../controllers/types'
import { MutationRegisterArgs } from '../typeDefs/typeDefs.gen'
import { Answer, MutationRegisterArgs } from '../typeDefs/typeDefs.gen'
import { IFindUserParams } from './types'
const getDBForm = async (db: PrismaClient, id: number) => {
@ -95,8 +95,6 @@ const createDBForm = async (
{ title, inputQuestions, choisesQuestions }: newForm,
id: number
) => {
console.log(title, inputQuestions, choisesQuestions)
return await db.form.create({
data: {
author: { connect: { id } },
@ -107,11 +105,39 @@ const createDBForm = async (
})
}
const submitDBAnswer = async (
db: PrismaClient,
userId: number,
formId: number,
formAnswers: Answer[]
) => {
const res = await db.formSubmission.create({
data: {
user: {
connect: {
id: userId
}
},
Form: {
connect: {
id: formId
}
},
answers: { create: formAnswers }
}
})
if (!res) throw new UserInputError("Can't submit form")
return { success: true }
}
export {
getDBForm,
getDBFormByUser,
getDBFormAuthor,
createDBUser,
findDBUserBy,
createDBForm
createDBForm,
submitDBAnswer
}

View File

@ -5,13 +5,16 @@ import {
getForms,
createFormFrom
} from '../controllers'
import { submitAnswer } from '../controllers/form'
import {
Form,
QueryFormArgs,
QuestionResolvers,
Resolver,
AnswerResolvers,
MutationCreateFormArgs
MutationCreateFormArgs,
ServerAnswer,
MutationFormSubmitArgs
} from '../typeDefs/typeDefs.gen'
import { ApolloContextType } from '../types'
@ -67,16 +70,8 @@ const createForm: Resolver<
{},
ApolloContextType,
MutationCreateFormArgs
> = async (_, { questions, title }, { db, user }) => {
const createNewForm = (id: number) =>
createFormFrom(
db,
{
title,
questions
},
id
)
> = async (_, params, { db, user }) => {
const createNewForm = (id: number) => createFormFrom(db, params, id)
return await checkRightsAndResolve({
user,
@ -85,6 +80,21 @@ const createForm: Resolver<
})
}
const formSubmit: Resolver<
ServerAnswer,
{},
ApolloContextType,
MutationFormSubmitArgs
> = async (_, params, { db, user }) => {
const submitNewAnswer = (userId: number) => submitAnswer(db, params, userId)
return await checkRightsAndResolve({
user,
expected: { id: 0, self: true },
controller: submitNewAnswer
})
}
const QuestionResolver: QuestionResolvers = {
__resolveType(obj: any) {
if (obj.type) {
@ -103,4 +113,11 @@ const AnswerResolver: AnswerResolvers = {
}
}
export { formQuery, formsQuery, QuestionResolver, AnswerResolver, createForm }
export {
formQuery,
formsQuery,
QuestionResolver,
AnswerResolver,
createForm,
formSubmit
}

View File

@ -1,27 +1,27 @@
import { UserInputError } from 'apollo-server-express'
import { checkRightsAndResolve, sendTokenEmail } from '../controllers'
import { createDBUser, findDBUserBy } from '../db'
import {
checkRightsAndResolve,
findUserBy,
sendTokenEmail
} from '../controllers'
import { createUser } from '../controllers/user'
import {
MutationLoginArgs,
MutationRegisterArgs,
Resolver,
LoginResult,
ServerAnswer,
User,
QueryUserArgs
} from '../typeDefs/typeDefs.gen'
import { ApolloContextType } from '../types'
const loginResolver: Resolver<
LoginResult,
ServerAnswer,
{},
ApolloContextType,
MutationLoginArgs
> = async (_, { email }, { db }) => {
try {
const user = await findDBUserBy(db, { email })
if (!user) throw new UserInputError('No such user')
const user = await findUserBy(db, { email })
await sendTokenEmail(email, user)
@ -32,13 +32,13 @@ const loginResolver: Resolver<
}
const registerResolver: Resolver<
LoginResult,
ServerAnswer,
{},
ApolloContextType,
MutationRegisterArgs
> = async (_, { email, name }, { db }) => {
try {
const user = await createDBUser(db, { email, name })
const user = await createUser(db, { email, name })
await sendTokenEmail(email, user)
@ -54,7 +54,7 @@ const userResolver: Resolver<
ApolloContextType,
QueryUserArgs
> = async (_, { id }, { db, user }) => {
const findUserById = (id: number) => findDBUserBy(db, { id })
const findUserById = (id: number) => findUserBy(db, { id })
try {
return await checkRightsAndResolve({

View File

@ -5,7 +5,8 @@ import {
QuestionResolver as Question,
AnswerResolver as Answer,
formsQuery as forms,
createForm
createForm,
formSubmit
} from './Form'
import {
loginResolver as login,
@ -22,7 +23,8 @@ const resolvers: Resolvers<ApolloContextType> = {
Mutation: {
login,
register,
createForm
createForm,
formSubmit
},
Question,
Answer

View File

@ -5,9 +5,10 @@ type Query {
}
type Mutation {
login(email: String!): LoginResult
register(name: String!, email: String!): LoginResult
login(email: String!): serverAnswer
register(name: String!, email: String!): serverAnswer
createForm(title: String!, questions: String!): Form!
formSubmit(formId: Int!, answers: String!): serverAnswer
}
type Form {
@ -47,18 +48,15 @@ type FormSubmission {
}
interface Answer {
id: Int!
type: AnswerType!
}
type InputAnswer implements Answer {
id: Int!
type: AnswerType!
userInput: String
}
type ChoiseAnswer implements Answer {
id: Int!
type: AnswerType!
userChoise: Int!
}
@ -81,6 +79,6 @@ type User {
forms: [Form!]
}
type LoginResult {
type serverAnswer {
success: Boolean!
}