Changed form submissions list logic, added user's forms to user fetching

This commit is contained in:
Dmitriy Shishkov 2020-11-06 23:13:01 +05:00
parent 3367930690
commit a554b91e53
No known key found for this signature in database
GPG Key ID: D76D70029F55183E
10 changed files with 191 additions and 24 deletions

View File

@ -0,0 +1,41 @@
# Migration `20201104091229-renamed-user-form-submissions-name`
This migration has been generated by Dm1tr1y147 at 11/4/2020, 2:12:29 PM.
You can check out the [state of the schema](./schema.prisma) after the migration.
## Database Steps
```sql
```
## Changes
```diff
diff --git schema.prisma schema.prisma
migration 20201009145620-add-user-email..20201104091229-renamed-user-form-submissions-name
--- datamodel.dml
+++ datamodel.dml
@@ -2,9 +2,9 @@
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgres"
- url = "***"
+ url = "***"
}
generator client {
provider = "prisma-client-js"
@@ -60,10 +60,10 @@
name String
email String @unique @default("test@mail.com")
forms Form[]
- id Int @id @default(autoincrement())
- formsSubmissions FormSubmission[]
+ id Int @id @default(autoincrement())
+ formSubmissions FormSubmission[]
}
model FormSubmission {
answers Answer[]
```

View File

@ -0,0 +1,92 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgres"
url = "***"
}
generator client {
provider = "prisma-client-js"
}
model Form {
title String
choisesQuestions ChoisesQuestion[]
inputQuestions InputQuestion[]
submissions FormSubmission[]
dateCreated DateTime @default(now())
author User @relation(fields: [userId], references: [id])
id Int @id @default(autoincrement())
userId Int
}
model ChoisesQuestion {
title String
variants Variant[]
type ChoiseType
number Int
id Int @id @default(autoincrement())
Form Form? @relation(fields: [formId], references: [id])
formId Int?
}
model Variant {
text String
id Int @id @default(autoincrement())
ChoisesQuestion ChoisesQuestion? @relation(fields: [choisesQuestionId], references: [id])
choisesQuestionId Int?
}
model InputQuestion {
title String
number Int
id Int @id @default(autoincrement())
Form Form? @relation(fields: [formId], references: [id])
formId Int?
}
enum ChoiseType {
SELECT
CHECK
CHOOSE
}
model User {
name String
email String @unique @default("test@mail.com")
forms Form[]
id Int @id @default(autoincrement())
formSubmissions FormSubmission[]
}
model FormSubmission {
answers Answer[]
date DateTime @default(now())
user User @relation(fields: [userId], references: [id])
id Int @id @default(autoincrement())
userId Int
Form Form? @relation(fields: [formId], references: [id])
formId Int?
}
model Answer {
userInput String?
userChoise Int?
type AnswerType
id Int @id @default(autoincrement())
FormSubmission FormSubmission? @relation(fields: [formSubmissionId], references: [id])
formSubmissionId Int?
}
enum AnswerType {
INPUT
CHOISE
}

View File

@ -0,0 +1,17 @@
{
"version": "0.3.14-fixed",
"steps": [
{
"tag": "CreateField",
"model": "User",
"field": "formSubmissions",
"type": "FormSubmission",
"arity": "List"
},
{
"tag": "DeleteField",
"model": "User",
"field": "formsSubmissions"
}
]
}

View File

@ -3,4 +3,5 @@
20201006125838-initial-migration
20201006185953-improved-schema-structure
20201007134933-fix-optional-values
20201009145620-add-user-email
20201009145620-add-user-email
20201104091229-renamed-user-form-submissions-name

View File

@ -61,8 +61,8 @@ model User {
email String @unique @default("test@mail.com")
forms Form[]
id Int @id @default(autoincrement())
formsSubmissions FormSubmission[]
id Int @id @default(autoincrement())
formSubmissions FormSubmission[]
}
model FormSubmission {

View File

@ -23,7 +23,7 @@ import {
getDBForm,
getDBFormsByUser,
submitDBAnswer,
getDBFormQuestions,
getDBFormToSubmit,
} from '../db'
import {
validateCreateFormParameters,
@ -41,7 +41,7 @@ const formatQuestions = (
const getForm = async (
db: PrismaClient,
id: number,
user: { requesterId: number; userId: number }
user: { requesterId: number; ownerId: number }
): Promise<GraphqlForm> => {
try {
const dbForm = await getDBForm(db, id, user)
@ -57,14 +57,14 @@ const getForm = async (
dbForm.inputQuestions
),
submissions:
dbForm.submissions.length == 0
? null
: dbForm.submissions.map((submission) => ({
user.ownerId == user.requesterId || !(dbForm.submissions.length == 0)
? dbForm.submissions.map((submission) => ({
user: submission.user,
answers: submission.answers,
date: submission.date.toString(),
id: submission.id,
})),
}))
: null,
title: dbForm.title,
}
@ -158,11 +158,9 @@ const submitAnswer = async (
userId: number
): Promise<ServerAnswer> => {
try {
const form = await getDBFormQuestions(db, formId)
const form = await getDBFormToSubmit(db, formId)
if (!form) throw new UserInputError("Can't submit form")
console.log(formatQuestions(form.choisesQuestions, form.inputQuestions))
form.submissions.forEach((submission) => {
if (submission.userId === userId)
throw new UserInputError("Can't submit same form more than once")

View File

@ -4,6 +4,7 @@ import { MutationRegisterArgs, User } from '../typeDefs/typeDefs.gen'
import { PrismaClient } from '@prisma/client'
import { ApolloError, UserInputError } from 'apollo-server-express'
import { formatForms } from './form'
import { formSubmitMutation, formsQuery } from 'resolvers/Form'
const createUser = async (
db: PrismaClient,
@ -36,9 +37,17 @@ const findUserBy = async (
if (!dbUser) throw new UserInputError('No such user')
const user = {
const user: User = {
...dbUser,
forms: formatForms(dbUser.forms),
formSubmissions: dbUser.formSubmissions.map((formSubmission) => ({
...formSubmission,
date: formSubmission.date.toString(),
form: formSubmission.Form && {
...formSubmission.Form,
dateCreated: formSubmission.Form?.dateCreated.toString(),
},
})),
}
return user

View File

@ -19,10 +19,10 @@ const getDBForm = (
formId: number,
{
requesterId,
userId,
ownerId: ownerId,
}: {
requesterId: number
userId: number
ownerId: number
}
) =>
db.form.findOne({
@ -46,7 +46,7 @@ const getDBForm = (
answers: true,
},
where:
requesterId != userId
requesterId != ownerId
? {
user: {
id: requesterId,
@ -134,9 +134,10 @@ const findDBUserBy = (db: PrismaClient, params: IFindUserParams) =>
},
},
},
formsSubmissions: {
formSubmissions: {
include: {
answers: true,
Form: true,
},
},
},
@ -176,7 +177,7 @@ const submitDBAnswer = (
},
})
const getDBFormQuestions = async (db: PrismaClient, formId: number) =>
const getDBFormToSubmit = async (db: PrismaClient, formId: number) =>
db.form.findOne({
where: {
id: formId,
@ -204,5 +205,5 @@ export {
getDBFormAuthor,
getDBFormsByUser,
submitDBAnswer,
getDBFormQuestions,
getDBFormToSubmit,
}

View File

@ -26,8 +26,8 @@ const formQuery: Resolver<Form, {}, ApolloContextType, QueryFormArgs> = async (
try {
const ownerId = await getFormAuthor(db, id)
const getFormById = (userId: number) =>
getForm(db, id, { requesterId: userId, userId: ownerId })
const getFormById = (requesterId: number) =>
getForm(db, id, { requesterId, ownerId })
return await checkRightsAndResolve({
controller: getFormById,
@ -74,7 +74,10 @@ const createFormMutation: Resolver<
> = async (_, params, { db, user }) => {
const createNewForm = (id: number) => createFormFrom(db, params, id)
return await checkRightsAndResolve({
return await checkRightsAndResolve<
ServerAnswer,
(id: number) => Promise<ServerAnswer>
>({
controller: createNewForm,
expected: {
id: 0,
@ -92,7 +95,10 @@ const formSubmitMutation: Resolver<
> = async (_, params, { db, user }) => {
const submitNewAnswer = (userId: number) => submitAnswer(db, params, userId)
return await checkRightsAndResolve({
return await checkRightsAndResolve<
ServerAnswer,
(userId: number) => Promise<ServerAnswer>
>({
controller: submitNewAnswer,
expected: {
id: 0,

View File

@ -15,7 +15,7 @@ type Form {
author: User
dateCreated: String!
id: Int!
questions: [Question!]!
questions: [Question!]
submissions: [FormSubmission!]
title: String!
}
@ -46,6 +46,7 @@ type FormSubmission {
answers: [Answer!]!
date: String!
id: Int!
form: Form
}
interface Answer {
@ -78,6 +79,7 @@ type User {
forms: [Form!]
id: Int!
name: String!
formSubmissions: [FormSubmission!]
}
type serverAnswer {