Skip to content

Commit

Permalink
feat(various): gql api (tests & resolvers), added eslint & editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
didimitrie committed Jul 20, 2020
1 parent 9715129 commit 2b33ab5
Show file tree
Hide file tree
Showing 11 changed files with 710 additions and 113 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2020": true
},
"parserOptions": {
"ecmaVersion": 11
},
"rules": {
"arrow-spacing": [ 2, { "before": true, "after": true } ],
"array-bracket-spacing": [ 2, "always" ],
"block-spacing": [ 2, "always" ],
"camelcase": [ 1, { "properties": "always" } ],
"space-in-parens": [ 2, "always" ],
"keyword-spacing": 2
}
}
34 changes: 21 additions & 13 deletions modules/core/graph/resolvers/branches.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const appRoot = require( 'app-root-path' )
const { AuthorizationError, ApolloError } = require( 'apollo-server-express' )
const { ForbiddenError, ApolloError } = require( 'apollo-server-express' )
const { validateServerRole, validateScopes, authorizeResolver } = require( `${appRoot}/modules/shared` )

const {
Expand All @@ -25,10 +25,10 @@ module.exports = {
Query: {},
Stream: {
async branches( parent, args, context, info ) {
throw new ApolloError('not implemented')
throw new ApolloError( 'not implemented' )
},
async branch( parent, args, context, info ) {
throw new ApolloError('not implemented')
async branch( parent, args, context, info) {
throw new ApolloError( 'not implemented' )
},
},
Branch: {
Expand All @@ -40,26 +40,34 @@ module.exports = {
async branchCreate( parent, args, context, info ) {
await validateServerRole( context, 'server:user' )
await validateScopes( context.scopes, 'streams:write' )
await authorizeResolver( context.userId, args.streamId, 'stream:contributor' )
await authorizeResolver( context.userId, args.branch.streamId, 'stream:contributor' )

let id = await createBranch( args.branch, args.streamId, context.userId )
let id = await createBranch( { ...args.branch, authorId: context.userId } )
return id
},

async branchUpdate( parent, args, context, info ) {
await validateServerRole( context, 'server:user' )
await validateScopes( context.scopes, 'streams:write' )
await authorizeResolver( context.userId, args.streamId, 'stream:contributor' )
await authorizeResolver( context.userId, args.branch.streamId, 'stream:contributor' )

await updateBranch( args.branch )
return true
return await updateBranch( { ...args.branch } )
},

async branchDelete( parent, args, context, info ) {
await validateServerRole( context, 'server:user' )
await validateScopes( context.scopes, 'streams:write' )
await authorizeResolver( context.userId, args.streamId, 'stream:contributor' )
let role = await authorizeResolver( context.userId, args.branch.streamId, 'stream:contributor' )

let branch = await getBranchById( { id: args.branch.id } )
if ( !branch ) {
throw new ApolloError( 'Branch not found.' )
}

if ( branch.authorId !== context.userId && role !== 'stream:owner' )
throw new ForbiddenError( 'Only the branch creator or stream owners are allowed to delete branches.' )

await deleteBranchById( args.branchId )
return true
return await deleteBranchById( { id: args.branch.id } )
}
}
}
}
2 changes: 1 addition & 1 deletion modules/core/graph/resolvers/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
await validateScopes( context.scopes, 'streams:write' )
await authorizeResolver( context.userId, args.id, 'stream:owner' )

await deleteStream( args.id )
await deleteStream( { streamId: args.id } )
return true
},
async streamGrantPermission( parent, args, context, info ) {
Expand Down
2 changes: 1 addition & 1 deletion modules/core/services/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ module.exports = {
async deleteBranchById( { id } ) {
return await Branches( ).where( { id: id } ).del( )
},
}
}
3 changes: 3 additions & 0 deletions modules/core/services/commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = {
},

async createCommitByBranchName( { streamId, branchName, objectId, authorId, message, previousCommitIds } ) {
branchName = branchName.toLowerCase()
let branches = await getBranchesByStreamId( { streamId: streamId } )
let myBranch = branches.find( b => b.name === branchName )

Expand Down Expand Up @@ -74,6 +75,7 @@ module.exports = {
},

async getCommitsTotalCountByBranchName( { streamId, branchName } ) {
branchName = branchName.toLowerCase()
let branches = await getBranchesByStreamId( { streamId: streamId } )
let myBranch = branches.find( b => b.name === branchName )

Expand Down Expand Up @@ -102,6 +104,7 @@ module.exports = {
},

async getCommitsByBranchName( { streamId, branchName, limit, cursor } ) {
branchName = branchName.toLowerCase()
let branches = await getBranchesByStreamId( { streamId: streamId } )
let myBranch = branches.find( b => b.name === branchName )

Expand Down
2 changes: 1 addition & 1 deletion modules/core/tests/branches.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ describe( 'Branches', ( ) => {
expect( branches ).to.have.lengthOf( 3 )
} )

} )
} )
Loading

0 comments on commit 2b33ab5

Please sign in to comment.