-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from mapadoacolhimento/feat/add-prisma
[MAPA-444] Inicializar o Prisma para salvar feedback
- Loading branch information
Showing
7 changed files
with
903 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
NEXT_PUBLIC_AWS_REST_API= | ||
HASURA_SECRET= | ||
GRAPHQL_HTTP_URL= | ||
NEXT_PUBLIC_AMPLITUDE_API_KEY= | ||
NEXT_PUBLIC_AMPLITUDE_API_KEY= | ||
DATABASE_URL="postgresql://postgres:changeme@localhost:5432/mapa-org?connection_limit=1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export default prisma; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,45 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import prisma from "./db"; | ||
|
||
export const FIRST_QUESTION = | ||
'Qual foi sua percepção ao interagir com a IAna?'; | ||
export const FIRST_QUESTION = "Qual foi sua percepção ao interagir com a IAna?"; | ||
export const SECOND_QUESTION = | ||
'Em uma escala de 0 a 5, que nota você daria à sua experiência com a IAna? Por favor, use apenas números.'; | ||
|
||
const MUTATION = `mutation mapa_do_acolhimento_iana_feedback($answers: [mapa_do_acolhimento_iana_feedback_insert_input!]!) { | ||
insert_mapa_do_acolhimento_iana_feedback(objects: $answers) { | ||
returning { | ||
id | ||
user_id | ||
answer | ||
created_at | ||
question | ||
} | ||
} | ||
}`; | ||
"Em uma escala de 0 a 5, que nota você daria à sua experiência com a IAna? Por favor, use apenas números."; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<any> | ||
req: NextApiRequest, | ||
res: NextApiResponse<any> | ||
) { | ||
try { | ||
const { firstAnswer, rating, userId } = req.body; | ||
|
||
if (!firstAnswer || !rating || !userId) { | ||
throw new Error('Invalid req body'); | ||
} | ||
|
||
const answers = [ | ||
{ | ||
answer: firstAnswer, | ||
question: FIRST_QUESTION, | ||
user_id: Number(userId), | ||
}, | ||
{ | ||
answer: `${rating}`, | ||
question: SECOND_QUESTION, | ||
user_id: Number(userId), | ||
}, | ||
]; | ||
const graphqlQuery = { | ||
query: MUTATION, | ||
variables: { | ||
answers: answers, | ||
}, | ||
}; | ||
|
||
const graphqlApiRes = await fetch( | ||
`${process.env.GRAPHQL_HTTP_URL}`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'x-hasura-admin-secret': `${process.env.HASURA_SECRET}`, | ||
}, | ||
body: JSON.stringify(graphqlQuery), | ||
} | ||
); | ||
|
||
const data = await graphqlApiRes.json(); | ||
|
||
if (data && data.errors && data.errors.length > 0) { | ||
throw new Error( | ||
data.errors.map((e: any) => e.message).join(' ') | ||
); | ||
} | ||
|
||
return res.status(200).json(data); | ||
} catch (e: any) { | ||
console.error(e); | ||
return res | ||
.status(500) | ||
.send( | ||
`Something went wrong when saving the feedback answer: ${e.message}` | ||
); | ||
} | ||
try { | ||
const { firstAnswer, rating, userId } = req.body; | ||
|
||
if (!firstAnswer || !rating || !userId) { | ||
throw new Error("Invalid req body"); | ||
} | ||
|
||
const answers = [ | ||
{ | ||
answer: firstAnswer, | ||
question: FIRST_QUESTION, | ||
user_id: Number(userId), | ||
}, | ||
{ | ||
answer: `${rating}`, | ||
question: SECOND_QUESTION, | ||
user_id: Number(userId), | ||
}, | ||
]; | ||
|
||
const feedbacks = await prisma.iana_feedback.createMany({ | ||
data: answers, | ||
}); | ||
|
||
return res.status(200).json(feedbacks); | ||
} catch (e: any) { | ||
console.error(e); | ||
return res | ||
.status(500) | ||
.send( | ||
`Something went wrong when saving the feedback answer: ${e.message}` | ||
); | ||
} | ||
} |
Oops, something went wrong.