Skip to content

Commit

Permalink
Merge pull request #68 from mapadoacolhimento/feat/add-prisma
Browse files Browse the repository at this point in the history
[MAPA-444] Inicializar o Prisma para salvar feedback
  • Loading branch information
vivianedias authored Dec 4, 2024
2 parents 52719cf + 74c415c commit 9e6790b
Show file tree
Hide file tree
Showing 7 changed files with 903 additions and 326 deletions.
84 changes: 14 additions & 70 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Tests, Static Analysis and Deploy

env:
IANA_STG_DOMAIN: iana-chatbot.staging.bonde.org

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand All @@ -26,106 +23,53 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install pnpm
uses: pnpm/action-setup@v2
uses: pnpm/action-setup@v4
with:
version: 8
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v3
- name: Install Node.js
uses: actions/setup-node@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
node-version: ${{ matrix.node-version }}
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --ignore-scripts
run: pnpm install --frozen-lockfile

- name: Run tests
run: pnpm run test:ci

- name: Build
run: pnpm run build

ui-deployment:
runs-on: ubuntu-latest
needs:
- integration
if: always() && needs.integration.result == 'success'
steps:
- uses: actions/checkout@v3
- name: Install Vercel CLI
run: npm install --global vercel@latest

- name: Pull Vercel Environment Information
run: |
env="preview"
if [[ ${GITHUB_REF} == "refs/heads/main" ]]; then
env="production"
fi
cd packages/ui && vercel pull --yes --environment=$env --token=${{ secrets.VERCEL_TOKEN }}
- name: Deploy Project Artifacts to Vercel
run: |
args=""
if [[ ${GITHUB_REF} == "refs/heads/main" ]]; then
args="--prod"
fi
cd packages/ui && vercel deploy $args --token=${{ secrets.VERCEL_TOKEN }} > domain.txt
if [[ ${GITHUB_REF} != "refs/heads/main" ]]; then
vercel alias --token=${{ secrets.VERCEL_TOKEN }} set `cat domain.txt` $IANA_STG_DOMAIN
fi
api-deployment:
runs-on: ubuntu-latest
needs:
- integration
if: always() && needs.integration.result == 'success'
steps:
- uses: actions/checkout@v3

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- uses: actions/checkout@v4

- name: Install pnpm
uses: pnpm/action-setup@v2
uses: pnpm/action-setup@v4
with:
version: 8
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v3
- name: Install Node.js
uses: actions/setup-node@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
node-version: ${{ matrix.node-version }}
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --ignore-scripts
run: pnpm install --frozen-lockfile

- name: Create env file
run: |
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/.env.example
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"
6 changes: 5 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"test:stage": "jest --findRelatedTests --passWithNoTests",
"test:ci": "jest --ci",
"test": "jest --watch",
"pre-commit": "lint-staged"
"pre-commit": "lint-staged",
"generate": "prisma generate",
"postinstall": "prisma generate"
},
"dependencies": {
"@amplitude/analytics-browser": "^2.4.0",
Expand All @@ -18,13 +20,15 @@
"@chakra-ui/react": "^2.8.0",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@prisma/client": "^5.22.0",
"@types/node": "20.1.4",
"@types/react": "18.2.6",
"@types/react-dom": "18.2.4",
"@vercel/examples-ui": "^1.0.5",
"eventsource-parser": "^1.0.0",
"framer-motion": "^10.16.1",
"next": "^13.5.6",
"prisma": "^5.22.0",
"react": "18.2.0",
"react-cookie": "^4.1.1",
"react-dom": "18.2.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/pages/api/db.ts
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;
112 changes: 39 additions & 73 deletions packages/ui/pages/api/feedback.ts
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}`
);
}
}
Loading

0 comments on commit 9e6790b

Please sign in to comment.