Changed form submissions list logic, added user's forms to user fetching
This commit is contained in:
parent
3367930690
commit
a554b91e53
@ -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[]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -4,3 +4,4 @@
|
|||||||
20201006185953-improved-schema-structure
|
20201006185953-improved-schema-structure
|
||||||
20201007134933-fix-optional-values
|
20201007134933-fix-optional-values
|
||||||
20201009145620-add-user-email
|
20201009145620-add-user-email
|
||||||
|
20201104091229-renamed-user-form-submissions-name
|
@ -61,8 +61,8 @@ model User {
|
|||||||
email String @unique @default("test@mail.com")
|
email String @unique @default("test@mail.com")
|
||||||
forms Form[]
|
forms Form[]
|
||||||
|
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
formsSubmissions FormSubmission[]
|
formSubmissions FormSubmission[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model FormSubmission {
|
model FormSubmission {
|
||||||
|
@ -23,7 +23,7 @@ import {
|
|||||||
getDBForm,
|
getDBForm,
|
||||||
getDBFormsByUser,
|
getDBFormsByUser,
|
||||||
submitDBAnswer,
|
submitDBAnswer,
|
||||||
getDBFormQuestions,
|
getDBFormToSubmit,
|
||||||
} from '../db'
|
} from '../db'
|
||||||
import {
|
import {
|
||||||
validateCreateFormParameters,
|
validateCreateFormParameters,
|
||||||
@ -41,7 +41,7 @@ const formatQuestions = (
|
|||||||
const getForm = async (
|
const getForm = async (
|
||||||
db: PrismaClient,
|
db: PrismaClient,
|
||||||
id: number,
|
id: number,
|
||||||
user: { requesterId: number; userId: number }
|
user: { requesterId: number; ownerId: number }
|
||||||
): Promise<GraphqlForm> => {
|
): Promise<GraphqlForm> => {
|
||||||
try {
|
try {
|
||||||
const dbForm = await getDBForm(db, id, user)
|
const dbForm = await getDBForm(db, id, user)
|
||||||
@ -57,14 +57,14 @@ const getForm = async (
|
|||||||
dbForm.inputQuestions
|
dbForm.inputQuestions
|
||||||
),
|
),
|
||||||
submissions:
|
submissions:
|
||||||
dbForm.submissions.length == 0
|
user.ownerId == user.requesterId || !(dbForm.submissions.length == 0)
|
||||||
? null
|
? dbForm.submissions.map((submission) => ({
|
||||||
: dbForm.submissions.map((submission) => ({
|
|
||||||
user: submission.user,
|
user: submission.user,
|
||||||
answers: submission.answers,
|
answers: submission.answers,
|
||||||
date: submission.date.toString(),
|
date: submission.date.toString(),
|
||||||
id: submission.id,
|
id: submission.id,
|
||||||
})),
|
}))
|
||||||
|
: null,
|
||||||
title: dbForm.title,
|
title: dbForm.title,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,11 +158,9 @@ const submitAnswer = async (
|
|||||||
userId: number
|
userId: number
|
||||||
): Promise<ServerAnswer> => {
|
): Promise<ServerAnswer> => {
|
||||||
try {
|
try {
|
||||||
const form = await getDBFormQuestions(db, formId)
|
const form = await getDBFormToSubmit(db, formId)
|
||||||
if (!form) throw new UserInputError("Can't submit form")
|
if (!form) throw new UserInputError("Can't submit form")
|
||||||
|
|
||||||
console.log(formatQuestions(form.choisesQuestions, form.inputQuestions))
|
|
||||||
|
|
||||||
form.submissions.forEach((submission) => {
|
form.submissions.forEach((submission) => {
|
||||||
if (submission.userId === userId)
|
if (submission.userId === userId)
|
||||||
throw new UserInputError("Can't submit same form more than once")
|
throw new UserInputError("Can't submit same form more than once")
|
||||||
|
@ -4,6 +4,7 @@ import { MutationRegisterArgs, User } from '../typeDefs/typeDefs.gen'
|
|||||||
import { PrismaClient } from '@prisma/client'
|
import { PrismaClient } from '@prisma/client'
|
||||||
import { ApolloError, UserInputError } from 'apollo-server-express'
|
import { ApolloError, UserInputError } from 'apollo-server-express'
|
||||||
import { formatForms } from './form'
|
import { formatForms } from './form'
|
||||||
|
import { formSubmitMutation, formsQuery } from 'resolvers/Form'
|
||||||
|
|
||||||
const createUser = async (
|
const createUser = async (
|
||||||
db: PrismaClient,
|
db: PrismaClient,
|
||||||
@ -36,9 +37,17 @@ const findUserBy = async (
|
|||||||
|
|
||||||
if (!dbUser) throw new UserInputError('No such user')
|
if (!dbUser) throw new UserInputError('No such user')
|
||||||
|
|
||||||
const user = {
|
const user: User = {
|
||||||
...dbUser,
|
...dbUser,
|
||||||
forms: formatForms(dbUser.forms),
|
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
|
return user
|
||||||
|
@ -19,10 +19,10 @@ const getDBForm = (
|
|||||||
formId: number,
|
formId: number,
|
||||||
{
|
{
|
||||||
requesterId,
|
requesterId,
|
||||||
userId,
|
ownerId: ownerId,
|
||||||
}: {
|
}: {
|
||||||
requesterId: number
|
requesterId: number
|
||||||
userId: number
|
ownerId: number
|
||||||
}
|
}
|
||||||
) =>
|
) =>
|
||||||
db.form.findOne({
|
db.form.findOne({
|
||||||
@ -46,7 +46,7 @@ const getDBForm = (
|
|||||||
answers: true,
|
answers: true,
|
||||||
},
|
},
|
||||||
where:
|
where:
|
||||||
requesterId != userId
|
requesterId != ownerId
|
||||||
? {
|
? {
|
||||||
user: {
|
user: {
|
||||||
id: requesterId,
|
id: requesterId,
|
||||||
@ -134,9 +134,10 @@ const findDBUserBy = (db: PrismaClient, params: IFindUserParams) =>
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
formsSubmissions: {
|
formSubmissions: {
|
||||||
include: {
|
include: {
|
||||||
answers: true,
|
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({
|
db.form.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: formId,
|
id: formId,
|
||||||
@ -204,5 +205,5 @@ export {
|
|||||||
getDBFormAuthor,
|
getDBFormAuthor,
|
||||||
getDBFormsByUser,
|
getDBFormsByUser,
|
||||||
submitDBAnswer,
|
submitDBAnswer,
|
||||||
getDBFormQuestions,
|
getDBFormToSubmit,
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,8 @@ const formQuery: Resolver<Form, {}, ApolloContextType, QueryFormArgs> = async (
|
|||||||
try {
|
try {
|
||||||
const ownerId = await getFormAuthor(db, id)
|
const ownerId = await getFormAuthor(db, id)
|
||||||
|
|
||||||
const getFormById = (userId: number) =>
|
const getFormById = (requesterId: number) =>
|
||||||
getForm(db, id, { requesterId: userId, userId: ownerId })
|
getForm(db, id, { requesterId, ownerId })
|
||||||
|
|
||||||
return await checkRightsAndResolve({
|
return await checkRightsAndResolve({
|
||||||
controller: getFormById,
|
controller: getFormById,
|
||||||
@ -74,7 +74,10 @@ const createFormMutation: Resolver<
|
|||||||
> = async (_, params, { db, user }) => {
|
> = async (_, params, { db, user }) => {
|
||||||
const createNewForm = (id: number) => createFormFrom(db, params, id)
|
const createNewForm = (id: number) => createFormFrom(db, params, id)
|
||||||
|
|
||||||
return await checkRightsAndResolve({
|
return await checkRightsAndResolve<
|
||||||
|
ServerAnswer,
|
||||||
|
(id: number) => Promise<ServerAnswer>
|
||||||
|
>({
|
||||||
controller: createNewForm,
|
controller: createNewForm,
|
||||||
expected: {
|
expected: {
|
||||||
id: 0,
|
id: 0,
|
||||||
@ -92,7 +95,10 @@ const formSubmitMutation: Resolver<
|
|||||||
> = async (_, params, { db, user }) => {
|
> = async (_, params, { db, user }) => {
|
||||||
const submitNewAnswer = (userId: number) => submitAnswer(db, params, userId)
|
const submitNewAnswer = (userId: number) => submitAnswer(db, params, userId)
|
||||||
|
|
||||||
return await checkRightsAndResolve({
|
return await checkRightsAndResolve<
|
||||||
|
ServerAnswer,
|
||||||
|
(userId: number) => Promise<ServerAnswer>
|
||||||
|
>({
|
||||||
controller: submitNewAnswer,
|
controller: submitNewAnswer,
|
||||||
expected: {
|
expected: {
|
||||||
id: 0,
|
id: 0,
|
||||||
|
@ -15,7 +15,7 @@ type Form {
|
|||||||
author: User
|
author: User
|
||||||
dateCreated: String!
|
dateCreated: String!
|
||||||
id: Int!
|
id: Int!
|
||||||
questions: [Question!]!
|
questions: [Question!]
|
||||||
submissions: [FormSubmission!]
|
submissions: [FormSubmission!]
|
||||||
title: String!
|
title: String!
|
||||||
}
|
}
|
||||||
@ -46,6 +46,7 @@ type FormSubmission {
|
|||||||
answers: [Answer!]!
|
answers: [Answer!]!
|
||||||
date: String!
|
date: String!
|
||||||
id: Int!
|
id: Int!
|
||||||
|
form: Form
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Answer {
|
interface Answer {
|
||||||
@ -78,6 +79,7 @@ type User {
|
|||||||
forms: [Form!]
|
forms: [Form!]
|
||||||
id: Int!
|
id: Int!
|
||||||
name: String!
|
name: String!
|
||||||
|
formSubmissions: [FormSubmission!]
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverAnswer {
|
type serverAnswer {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user