-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 2215-fix-cant-dismiss-an-alert
- Loading branch information
Showing
11 changed files
with
235 additions
and
199 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { withAdminContainer } from './withAdminContainer' | ||
|
||
const adminMockReq = { req: { user: { isAdmin: true, id: 1 } } } | ||
const noAdminMockReq = { req: { user: { isAdmin: false, id: 1 } } } | ||
|
||
const mockAdminContainer = withAdminContainer(async _parent => { | ||
return true | ||
}, 'errorMessage') | ||
|
||
describe('withAdminContainer test', () => { | ||
test('if there is admin error and errorMessage is provided, it should throw errorMessage', () => { | ||
expect.assertions(1) | ||
|
||
expect( | ||
mockAdminContainer({}, { name: 'John', title: 'Doe' }, noAdminMockReq) | ||
).rejects.toThrow(new Error('errorMessage')) | ||
}) | ||
|
||
test('if there is no admin error, it should execute the resolver function passed in', () => { | ||
expect.assertions(1) | ||
|
||
expect( | ||
mockAdminContainer({}, { name: 'John', title: 'Doe' }, adminMockReq) | ||
).resolves.toEqual(true) | ||
}) | ||
}) |
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,27 @@ | ||
import { Context } from '../@types/helpers' | ||
import { isAdmin } from '../helpers/isAdmin' | ||
import { withUserContainer } from './withUserContainer' | ||
import _ from 'lodash' | ||
|
||
//use when only checking if user is admin | ||
export const withAdminContainer = | ||
<Type, ArgsType>( | ||
resolver: (_parent: void, args: ArgsType, ctx: Context) => Type, | ||
errorMessage?: string | ||
) => | ||
async (_parent: void, args: ArgsType, ctx: Context) => { | ||
const { req } = ctx | ||
|
||
if (!isAdmin(req)) { | ||
throw new Error(errorMessage || 'User is not an admin') | ||
} | ||
|
||
return resolver(_parent, args, ctx) | ||
} | ||
|
||
export function withAdminUserContainer<Type, ArgsType>( | ||
resolver: (_parent: void, args: ArgsType, ctx: Context) => Type, | ||
errorMessage?: string | ||
) { | ||
return withUserContainer(withAdminContainer(resolver, errorMessage)) | ||
} |
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,8 +1,6 @@ | ||
import { Context } from '../../@types/helpers' | ||
import { isAdminOrThrow } from '../../helpers/isAdmin' | ||
import prisma from '../../prisma' | ||
import { withAdminContainer } from '../../containers/withAdminContainer' | ||
|
||
export const allUsers = (_parent: void, _args: void, { req }: Context) => { | ||
isAdminOrThrow(req) | ||
export const allUsers = withAdminContainer((_parent: void, _args: void) => { | ||
return prisma.user.findMany() | ||
} | ||
}) |
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,39 +1,28 @@ | ||
import { Context } from '../../@types/helpers' | ||
import type { | ||
CreateChallengeMutationVariables, | ||
UpdateChallengeMutationVariables | ||
} from '../../graphql' | ||
import { lessons } from './lessons' | ||
import prisma from '../../prisma' | ||
import { isAdminOrThrow } from '../../helpers/isAdmin' | ||
import { validateLessonId } from '../../helpers/validation/validateLessonId' | ||
import { withAdminContainer } from '../../containers/withAdminContainer' | ||
|
||
export const createChallenge = async ( | ||
_parent: void, | ||
arg: CreateChallengeMutationVariables, | ||
ctx: Context | ||
) => { | ||
const { req } = ctx | ||
export const createChallenge = withAdminContainer( | ||
async (_parent: void, arg: CreateChallengeMutationVariables) => { | ||
await validateLessonId(arg.lessonId) | ||
await prisma.challenge.create({ data: arg }) | ||
return lessons() | ||
} | ||
) | ||
|
||
isAdminOrThrow(req) | ||
await validateLessonId(arg.lessonId) | ||
await prisma.challenge.create({ data: arg }) | ||
return lessons() | ||
} | ||
|
||
export const updateChallenge = async ( | ||
_parent: void, | ||
arg: UpdateChallengeMutationVariables, | ||
ctx: Context | ||
) => { | ||
const { req } = ctx | ||
|
||
isAdminOrThrow(req) | ||
const { id, lessonId, ...data } = arg | ||
await validateLessonId(lessonId) | ||
await prisma.challenge.update({ | ||
where: { id }, | ||
data | ||
}) | ||
return lessons() | ||
} | ||
export const updateChallenge = withAdminContainer( | ||
async (_parent: void, arg: UpdateChallengeMutationVariables) => { | ||
const { id, lessonId, ...data } = arg | ||
await validateLessonId(lessonId) | ||
await prisma.challenge.update({ | ||
where: { id }, | ||
data | ||
}) | ||
return lessons() | ||
} | ||
) |
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
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
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
Oops, something went wrong.