diff --git a/.env.example b/.env.example deleted file mode 100644 index cd935ad28..000000000 --- a/.env.example +++ /dev/null @@ -1,34 +0,0 @@ -# -# COPY THIS FILE AND RENAME IT TO .env TO START USING THE APP -# NEVER COMMIT THE .env FILE TO GITHUB -# - -# API KEYS - GET WITH ENGINEERING TEAM -# NEXT_PUBLIC_POSTHOG_API_KEY= - -# SENTRY.iO -# NEXT_PUBLIC_SENTRY_DSN= -# SENTRY_URL= -# SENTRY_ORG= -# SENTRY_PROJECT= -# SENTRY_AUTH_TOKEN= - -SESSION_SECRET="c0d3" -SERVER_URL="/api/graphql" - -# DATABASE CONFIG -DB_USER=c0d3_admin -DB_NAME=c0d3 -DB_PW=admin -DB_HOST=localhost -DB_PORT=5432 -DB_URL="postgresql://${DB_USER}:${DB_PW}@${DB_HOST}:${DB_PORT}/${DB_NAME}?connection_limit=1" - -# EMAIL CONFIG -SMTP_HOST=smtp.example.com -SMTP_USER=user -SMTP_PASSWORD=password - -# DISCORD BOT, C0D3R CONFIG -C0D3R_URL=bot_url -C0D3R_ACCESS_TOKEN=c0d3r:token diff --git a/graphql/queryResolvers/moduleCrud.test.js b/graphql/queryResolvers/moduleCrud.test.js new file mode 100644 index 000000000..6fce1db0d --- /dev/null +++ b/graphql/queryResolvers/moduleCrud.test.js @@ -0,0 +1,78 @@ +/** + * @jest-environment node + */ +import prismaMock from '../../__tests__/utils/prismaMock' +import { modules, addModule, deleteModule } from './moduleCrud' + +const mockModules = [ + { + name: 'Quality omlet', + id: 1 + }, + { + name: 'Super booster', + id: 2 + } +] +const ctx = { + req: { + user: { isAdmin: true } + } +} +describe('It should return modules', () => { + test('Should return modules', () => { + prismaMock.module.findMany.mockResolvedValue(mockModules) + expect(modules()).resolves.toEqual(mockModules) + }) +}) + +describe('It should add a modules', () => { + test('Should create module', async () => { + prismaMock.module.create.mockResolvedValue({ + authorId: 1, + content: 'testing', + name: 'Using functions to make pizza', + lessonId: 1 + }) + expect( + await addModule( + {}, + { + authorId: 1, + content: 'testing', + name: 'Using functions to make pizza', + lessonId: 1 + } + ) + ).toEqual({ + authorId: 1, + content: 'testing', + name: 'Using functions to make pizza', + lessonId: 1 + }) + }) + test('Create modules should have all parameters', async () => { + expect( + addModule({}, { authorId: 1, name: 'Hi all', content: 'This module' }) + ).rejects.toThrowError('Missing parameters'), + expect( + addModule({}, { authorId: 1, lessonId: 1, content: 'This module' }) + ).rejects.toThrowError('Missing parameters'), + expect( + addModule( + {}, + { content: 'This Module', name: 'Hi all', content: 'This module' } + ) + ).rejects.toThrowError('Missing parameters'), + expect( + addModule({}, { authorId: 1, lessonId: 1, name: 'Hi all' }) + ).rejects.toThrowError('Missing parameters') + }) +}) + +describe('It should test delete', () => { + test('it should delete module', () => { + prismaMock.module.delete.mockResolvedValue({ success: true }) + expect(deleteModule({}, { id: 1 })).resolves.toEqual({ success: true }) + }) +}) diff --git a/graphql/queryResolvers/moduleCrud.ts b/graphql/queryResolvers/moduleCrud.ts new file mode 100644 index 000000000..960304ac3 --- /dev/null +++ b/graphql/queryResolvers/moduleCrud.ts @@ -0,0 +1,30 @@ +import type { MutationDeleteModuleArgs, MutationAddModuleArgs } from '..' +import prisma from '../../prisma' + +export const modules = async () => { + return prisma.module.findMany({ + include: { + author: true, + lesson: true + } + }) +} + +export const addModule = async (_parent: void, args: MutationAddModuleArgs) => { + const { authorId, content, lessonId, name } = args + if (!authorId || !content || !name || !lessonId) { + throw new Error('Missing parameters') + } + return await prisma.module.create({ + data: { authorId, content, lessonId, name } + }) +} + +export const deleteModule = async ( + _parent: void, + args: MutationDeleteModuleArgs +) => { + const { id } = args + await prisma.module.delete({ where: { id } }) + return { success: true } +} diff --git a/graphql/resolvers.ts b/graphql/resolvers.ts index dad4ec4e3..a8d94defd 100644 --- a/graphql/resolvers.ts +++ b/graphql/resolvers.ts @@ -30,7 +30,7 @@ import { updateLesson } from '../helpers/controllers/lessonsController' import { getPreviousSubmissions } from './queryResolvers/getPreviousSubmissions' - +import { modules, addModule, deleteModule } from './queryResolvers/moduleCrud' export default { Query: { submissions, @@ -39,6 +39,7 @@ export default { isTokenValid, userInfo, lessons, + modules, session, alerts, getPreviousSubmissions @@ -53,6 +54,8 @@ export default { rejectSubmission, createLesson, updateLesson, + addModule, + deleteModule, login, logout, signup, diff --git a/graphql/typeDefs.ts b/graphql/typeDefs.ts index 51bedee7b..56fa3761d 100644 --- a/graphql/typeDefs.ts +++ b/graphql/typeDefs.ts @@ -218,9 +218,7 @@ export default gql` type Module { id: Int! author: User! - authorId: Int! lesson: Lesson! - lessonId: Int! name: String! content: String! }