From d0803b629efdfcaed88844361b69292c23e30b29 Mon Sep 17 00:00:00 2001 From: mino323 <86011419+mino323@users.noreply.github.com> Date: Mon, 31 Jan 2022 18:34:26 +0100 Subject: [PATCH 1/5] adds mod crud --- .env.example | 34 ---------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 .env.example 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 From c08c7cd027a6a35ca57387d43aac2b42f9b5e28f Mon Sep 17 00:00:00 2001 From: mino323 <86011419+mino323@users.noreply.github.com> Date: Mon, 31 Jan 2022 18:35:55 +0100 Subject: [PATCH 2/5] adds modulecrud --- graphql/queryResolvers/moduleCrud.test.js | 77 +++++++++++++++++++++++ graphql/queryResolvers/moduleCrud.ts | 40 ++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 graphql/queryResolvers/moduleCrud.test.js create mode 100644 graphql/queryResolvers/moduleCrud.ts diff --git a/graphql/queryResolvers/moduleCrud.test.js b/graphql/queryResolvers/moduleCrud.test.js new file mode 100644 index 000000000..d46e926a5 --- /dev/null +++ b/graphql/queryResolvers/moduleCrud.test.js @@ -0,0 +1,77 @@ +/** + * @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 have an id', async () => { + expect(deleteModule({}, {})).rejects.toThrowError('Missing parameter') + }) +}) diff --git a/graphql/queryResolvers/moduleCrud.ts b/graphql/queryResolvers/moduleCrud.ts new file mode 100644 index 000000000..2a4fcadeb --- /dev/null +++ b/graphql/queryResolvers/moduleCrud.ts @@ -0,0 +1,40 @@ +import { Context } from '../../@types/helpers' +import type { MutationDeleteModuleArgs, MutationAddModuleArgs } from '..' +import prisma from '../../prisma' +import { isAdminOrThrow } from '../../helpers/isAdmin' + +export const modules = async () => { + return prisma.module.findMany({ + include: { + author: true + } + }) +} + +export const addModule = async ( + _parent: void, + args: MutationAddModuleArgs, + ctx: Context +) => { + //isAdminOrThrow(req) will add after, easier for testing + // const authorId = ctx.req.user?.id + 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, + { req }: Context +) => { + // isAdminOrThrow(req) + const { id } = args + if (!id) throw new Error('Missing parameter') + await prisma.module.delete({ where: { id } }) + return { success: true } +} From 42299eef7d8f3d7793b45e7fbea682f6acbbe043 Mon Sep 17 00:00:00 2001 From: mino323 <86011419+mino323@users.noreply.github.com> Date: Mon, 31 Jan 2022 21:06:18 +0100 Subject: [PATCH 3/5] modulecrud --- graphql/queryResolvers/moduleCrud.test.js | 4 ++++ graphql/queryResolvers/moduleCrud.ts | 13 ++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/graphql/queryResolvers/moduleCrud.test.js b/graphql/queryResolvers/moduleCrud.test.js index d46e926a5..8039f88cf 100644 --- a/graphql/queryResolvers/moduleCrud.test.js +++ b/graphql/queryResolvers/moduleCrud.test.js @@ -74,4 +74,8 @@ describe('It should test delete', () => { test('It should have an id', async () => { expect(deleteModule({}, {})).rejects.toThrowError('Missing parameter') }) + 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 index 2a4fcadeb..42cbf32ed 100644 --- a/graphql/queryResolvers/moduleCrud.ts +++ b/graphql/queryResolvers/moduleCrud.ts @@ -1,7 +1,7 @@ -import { Context } from '../../@types/helpers' +// import { Context } from '../../@types/helpers' import type { MutationDeleteModuleArgs, MutationAddModuleArgs } from '..' import prisma from '../../prisma' -import { isAdminOrThrow } from '../../helpers/isAdmin' +// import { isAdminOrThrow } from '../../helpers/isAdmin' export const modules = async () => { return prisma.module.findMany({ @@ -11,11 +11,7 @@ export const modules = async () => { }) } -export const addModule = async ( - _parent: void, - args: MutationAddModuleArgs, - ctx: Context -) => { +export const addModule = async (_parent: void, args: MutationAddModuleArgs) => { //isAdminOrThrow(req) will add after, easier for testing // const authorId = ctx.req.user?.id const { authorId, content, lessonId, name } = args @@ -29,8 +25,7 @@ export const addModule = async ( export const deleteModule = async ( _parent: void, - args: MutationDeleteModuleArgs, - { req }: Context + args: MutationDeleteModuleArgs ) => { // isAdminOrThrow(req) const { id } = args From a0f5ab0dd98d99e16898c6505d64deb7f30a88a4 Mon Sep 17 00:00:00 2001 From: mino323 <86011419+mino323@users.noreply.github.com> Date: Tue, 1 Feb 2022 13:41:05 +0100 Subject: [PATCH 4/5] takes module crud comments out --- graphql/queryResolvers/moduleCrud.test.js | 3 --- graphql/queryResolvers/moduleCrud.ts | 6 ------ 2 files changed, 9 deletions(-) diff --git a/graphql/queryResolvers/moduleCrud.test.js b/graphql/queryResolvers/moduleCrud.test.js index 8039f88cf..6fce1db0d 100644 --- a/graphql/queryResolvers/moduleCrud.test.js +++ b/graphql/queryResolvers/moduleCrud.test.js @@ -71,9 +71,6 @@ describe('It should add a modules', () => { }) describe('It should test delete', () => { - test('It should have an id', async () => { - expect(deleteModule({}, {})).rejects.toThrowError('Missing parameter') - }) 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 index 42cbf32ed..e185df83e 100644 --- a/graphql/queryResolvers/moduleCrud.ts +++ b/graphql/queryResolvers/moduleCrud.ts @@ -1,7 +1,5 @@ -// import { Context } from '../../@types/helpers' import type { MutationDeleteModuleArgs, MutationAddModuleArgs } from '..' import prisma from '../../prisma' -// import { isAdminOrThrow } from '../../helpers/isAdmin' export const modules = async () => { return prisma.module.findMany({ @@ -12,8 +10,6 @@ export const modules = async () => { } export const addModule = async (_parent: void, args: MutationAddModuleArgs) => { - //isAdminOrThrow(req) will add after, easier for testing - // const authorId = ctx.req.user?.id const { authorId, content, lessonId, name } = args if (!authorId || !content || !name || !lessonId) { throw new Error('Missing parameters') @@ -27,9 +23,7 @@ export const deleteModule = async ( _parent: void, args: MutationDeleteModuleArgs ) => { - // isAdminOrThrow(req) const { id } = args - if (!id) throw new Error('Missing parameter') await prisma.module.delete({ where: { id } }) return { success: true } } From a4c31a3d085de45c7ee7f02f7549e16bae84aae3 Mon Sep 17 00:00:00 2001 From: mino323 <86011419+mino323@users.noreply.github.com> Date: Wed, 2 Feb 2022 10:56:53 +0100 Subject: [PATCH 5/5] fixes module red and module crud, puts crud into resolvers --- graphql/queryResolvers/moduleCrud.ts | 3 ++- graphql/resolvers.ts | 5 ++++- graphql/typeDefs.ts | 2 -- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/graphql/queryResolvers/moduleCrud.ts b/graphql/queryResolvers/moduleCrud.ts index e185df83e..960304ac3 100644 --- a/graphql/queryResolvers/moduleCrud.ts +++ b/graphql/queryResolvers/moduleCrud.ts @@ -4,7 +4,8 @@ import prisma from '../../prisma' export const modules = async () => { return prisma.module.findMany({ include: { - author: true + author: true, + lesson: 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! }