Skip to content

Commit

Permalink
resolved form.ts conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
sujayshanbhag committed Oct 20, 2023
2 parents 2c91a3f + 7cbdf65 commit 7b8d2ba
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 27 deletions.
93 changes: 70 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,97 @@
# Review App - Backend Repo

This repository contains the backend code for the Review App project.

[![Screenshot-86.png](https://i.postimg.cc/pXzH9rcC/Screenshot-86.png)](https://postimg.cc/9rmSKmhw)
##### This project is divided into two repositories:
![Screenshot-86.png](https://i.postimg.cc/pXzH9rcC/Screenshot-86.png)

**Please Note**: This project is divided into two repositories:

1. [Review App Frontend](https://github.com/piyushgarg-dev/review-app)
2. Review App Backend (You are here)

Go to [this repository](https://github.com/piyushgarg-dev/review-app) to know more about the project.
For more information about the project, visit the [frontend repository](https://github.com/piyushgarg-dev/review-app).

## Environment Setup:

### Prerequisites:

1. Clone the repository:

```shell
git clone https://github.com/piyushgarg-dev/review-app-api.git
```

2. Move to the backend folder:

```shell
cd review-app-api
```

## Environment Setup :
3. Install and set up Docker.

1. Pre-requisites :
4. Run Docker in the background. If you have an older version of Docker, use the following command to run the `docker-compose.yml` file in detached mode:

a. Clone the Repo : `git clone https://github.com/piyushgarg-dev/review-app-api.git`
b. Move to backend foler : `cd review-app-api`
c. Install and Setup Docker
d. Run Docker in background
e. Run this command to run "docker-compose.yml" file in detach mode : `docker compose up -d`
f. Setup Backend by running command : `yarn`
```shell
docker-compose up -d
```

2. Create a `.env.local` file in the project's root directory
Otherwise, run:

3. The `.env.local` file should consist of :
```shell
docker compose up -d
```

`DATABASE_URL=postgresql://postgres:password@localhost:5432/review
JWT_SECRET=superman123`
5. Set up the backend by running the following command:
```shell
yarn
```

4. Run Migrations by running command : `yarn migrate:latest`
### Create a `.env.local` file in the project's root directory.

5. Run Backend server by running command : `yarn local`
The `.env.local` file should contain the following environment variables:

CONGRATULATIONS! your backend starts running on http://localhost:8000/local/graphql
```shell
DATABASE_URL=postgresql://postgres:password@localhost:5432/review
JWT_SECRET=superman123
```

## Communication Channels
### Run Migrations:

Run database migrations with the following command:

```shell
yarn migrate:latest
```

### Start the Backend Server:

To start the backend server, run the following command:

```shell
yarn local
```

Congratulations! Your backend is now running at http://localhost:8000/local/graphql.

## Communication Channels:

If you have any questions, need clarifications, or want to discuss ideas, feel free to reach out through the following channels:

- [Discord Server](https://discord.com/invite/YuUjtrufmT)
- [X(formerly Twitter)](https://twitter.com/piyushgarg_dev)
- [Discord Server](https://discord.com/invite/YuUjtrufmT)
- [Twitter](https://twitter.com/piyushgarg_dev) (formerly X)

We appreciate your contributions and look forward to working with you to make this project even better!

## Best Contributors
## Best Contributors:

<div align="center">
<a href="https://github.com/piyushgarg-dev/review-app-api/graphs/contributors">
<img src="https://contrib.rocks/image?repo=piyushgarg-dev/review-app-api&anon=1" />
</a>
</div>
</div>

## Thanks To All Our Contributors:

<a href="https://github.com/piyushgarg-dev/review-app-api/graphs/contributors">
<img src="https://contrib.rocks/image?repo=piyushgarg-dev/review-app-api" />
</a>
11 changes: 11 additions & 0 deletions functions/graphql/form/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ export interface SubmitFormResponseData {
export interface GetFormResponsesByFormIdInput {
formId: string
}

export interface GetFormResponsesByProjectId {
projectId: string
itemsPerPage?: number
cursor?: string
}

export interface GetPublicFormDataInput {
domain: string
formSlug: string
}
4 changes: 3 additions & 1 deletion functions/graphql/form/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const queries = `#graphql
getForms(input: GetFormsInput!): [Form]
getFormById(id: ID!): Form
getFormResponses(input: GetFormResponsesByFormIdInput!): [FormResponse]
getFormResponsesByFormId(input: GetFormResponsesByFormIdInput!): [FormResponse]
getFormResponsesByProjectId(input: GetFormResponsesByProjectIdInput!): [FormResponse]
getPublicFormData(input: GetPublicFormDataInput!): FormPublicData
`
25 changes: 24 additions & 1 deletion functions/graphql/form/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { ServerContext } from '../interfaces'
import {
CreateFormData,
GetFormResponsesByFormIdInput,
GetFormResponsesByProjectId,
GetFormsInput,
GetPublicFormDataInput,
SubmitFormResponseData,
UpdateFormData,
} from './interfaces'
Expand All @@ -25,14 +27,35 @@ const queries = {
ensureAuthenticated(ctx)
return FormService.getFormById(id)
},
getFormResponses: async (
getFormResponsesByFormId: async (
_: any,
{ input }: { input: GetFormResponsesByFormIdInput },
ctx: ServerContext
) => {
ensureAuthenticated(ctx)
return FormService.getFormResponsesByFormId(input.formId, ctx)
},
getFormResponsesByProjectId: async (
_: any,
{ input }: { input: GetFormResponsesByProjectId },
ctx: ServerContext
) => {
ensureAuthenticated(ctx)
const { projectId, itemsPerPage = 10, cursor } = input
return FormService.getFormResponsesByProjectId(projectId, ctx, {
cursor,
itemsPerPage,
})
},
getPublicFormData: async (
_: any,
{ input }: { input: GetPublicFormDataInput }
) => {
return FormService.getPublicFormData({
domain: input.domain,
slug: input.formSlug,
})
},
}

const mutations = {
Expand Down
47 changes: 45 additions & 2 deletions functions/graphql/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export const types = `#graphql
projectId: String!
}
input GetPublicFormDataInput {
domain: String!
formSlug: String!
}
input UpdateFormInput {
id: ID!
Expand Down Expand Up @@ -45,6 +50,38 @@ export const types = `#graphql
input GetFormsInput {
projectId: ID!
}
type FormPublicData {
id: ID!
primaryColor: String!
backgroundColor: String!
introTitle: String!
introMessage: String
promptTitle: String!
promptDescription: String
thankyouTitle: String!
thankyouMessage: String
name: String!
enableCTA: Boolean!
ctaTitle: String
ctaURL: String
collectVideoTestimonials: Boolean!
collectTextTestimonials: Boolean!
collectRatings: Boolean!
collectImages: Boolean!
collectEmail: Boolean!
collectJobTitle: Boolean!
collectUserImage: Boolean!
collectWebsiteURL: Boolean!
collectCompany: Boolean!
lang: String
}
type Form {
id: ID!
Expand Down Expand Up @@ -75,7 +112,7 @@ export const types = `#graphql
primaryColor: String!
backgroundColor: String!
lang: String!
lang: String
collectVideoTestimonials: Boolean!
collectTextTestimonials: Boolean!
Expand Down Expand Up @@ -112,7 +149,7 @@ export const types = `#graphql
tags: [String]
approved: Boolean
reatedAt: Date
createdAt: Date
updatedAt: Date
}
Expand All @@ -132,4 +169,10 @@ export const types = `#graphql
input GetFormResponsesByFormIdInput {
formId: ID!
}
input GetFormResponsesByProjectIdInput {
projectId: ID!
itemsPerPage: Int
cursor: String
}
`
75 changes: 75 additions & 0 deletions services/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import AccessDeniedError from '../errors/AccessDeniedError'
import { UpdateFormData } from '../functions/graphql/form/interfaces'
import { ServerContext } from '../functions/graphql/interfaces'

interface GetFormResponsesByProjectIdOptions {
itemsPerPage?: number
cursor?: string
}

class FormService {
public static createForm = prismaClient.form.create

Expand All @@ -14,6 +19,49 @@ class FormService {
return prismaClient.form.findUnique({ where: { id } })
}

public static getPublicFormData({
domain,
slug,
}: {
domain: string
slug: string
}) {
return prismaClient.form.findFirst({
where: {
AND: [
{
project: { OR: [{ subdomain: domain }, { customDomain: domain }] },
},
{ slug },
],
},
select: {
backgroundColor: true,
collectCompany: true,
collectEmail: true,
collectImages: true,
collectJobTitle: true,
collectRatings: true,
collectUserImage: true,
collectTextTestimonials: true,
collectVideoTestimonials: true,
collectWebsiteURL: true,
ctaTitle: true,
enableCTA: true,
ctaURL: true,
introMessage: true,
introTitle: true,
name: true,
primaryColor: true,
promptDescription: true,
promptTitle: true,
thankyouMessage: true,
thankyouTitle: true,
id: true,
},
})
}

public static updateFormById(id: string, formData: UpdateFormData) {
return prismaClient.form.update({
data: {
Expand Down Expand Up @@ -43,6 +91,33 @@ class FormService {



public static getFormResponsesByProjectId(
projectId: string,
ctx: ServerContext,
options?: GetFormResponsesByProjectIdOptions
) {
if (!ctx.user?.id) throw new AccessDeniedError()

return prismaClient.formResponse.findMany({
where: {
form: {
project: {
id: projectId,
ProjectAccessMapping: { every: { user: { id: ctx.user.id } } }, // TODO: Need to test more deeply
},
},
},
cursor: options?.cursor
? {
id: options?.cursor,
}
: undefined,
take: options?.itemsPerPage ?? 10,
skip: options?.cursor ? 1 : 0, // Skip the cursor
orderBy: { createdAt: 'desc' },
})
}

public static getFormResponsesByFormId(formId: string, ctx: ServerContext) {
if (!ctx.user?.id) throw new AccessDeniedError()

Expand Down

0 comments on commit 7b8d2ba

Please sign in to comment.