-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Buy album #11
Merged
Merged
Buy album #11
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6113c57
Added Album sequelize model and migration. Added asociations with Use…
da76635
Implemented mutation for buying album
14e9d46
Added middleware to validate authetication
804ab73
Added tests for buyAlbum resolver
8105e0d
Fixed error throw
487c0e2
Fixed assertions in tests
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,12 @@ | ||
const { queries, schema: queriesSchema } = require('./queries'); | ||
const { mutations, schema: mutationSchema } = require('./mutations'); | ||
const { typeResolvers } = require('./resolvers'); | ||
const middlewares = require('./middlewares'); | ||
|
||
module.exports = { | ||
queries, | ||
mutations, | ||
middlewares, | ||
typeResolvers, | ||
schemas: [queriesSchema] | ||
schemas: [queriesSchema, mutationSchema] | ||
}; |
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,4 @@ | ||
const usersValidators = require('../../validators/users'); | ||
|
||
exports.buyAlbum = (resolve, root, args, context) => | ||
usersValidators.validateAuthetication(root, args, context).then(() => resolve(root, args, context)); |
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,14 @@ | ||
const { gql } = require('apollo-server'); | ||
|
||
const resolvers = require('./resolvers'); | ||
|
||
module.exports = { | ||
mutations: { | ||
buyAlbum: resolvers.buyAlbum | ||
}, | ||
schema: gql` | ||
extend type Mutation { | ||
buyAlbum(albumId: ID!): Album! | ||
} | ||
` | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Album = sequelize.define( | ||
'Album', | ||
{ | ||
id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false }, | ||
title: { type: DataTypes.STRING, allowNull: false }, | ||
artist: { type: DataTypes.INTEGER, allowNull: false }, | ||
userId: { type: DataTypes.INTEGER, field: 'user_id', primaryKey: true, allowNull: false } | ||
}, | ||
{ | ||
tableName: 'albums', | ||
underscored: true | ||
} | ||
); | ||
|
||
Album.associate = models => { | ||
Album.belongsTo(models.User, { foreignKey: 'userId' }); | ||
}; | ||
|
||
return Album; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const errors = require('../errors'); | ||
const usersHelpers = require('../helpers/users'); | ||
|
||
exports.validateAuthetication = (root, args, context) => { | ||
const token = context.authorization; | ||
if (token) { | ||
return usersHelpers | ||
.validateToken(token) | ||
.then(decodedToken => { | ||
args.user = decodedToken; | ||
}) | ||
.catch(error => { | ||
throw errors.sessionError(`Session error: ${error.message}`); | ||
}); | ||
} | ||
throw errors.sessionError('Session error: no token provided'); | ||
}; |
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,37 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => | ||
queryInterface.createTable('albums', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
allowNull: false | ||
}, | ||
title: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
artist: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false | ||
}, | ||
user_id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
}, | ||
allowNull: false | ||
}, | ||
created_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
}, | ||
updated_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('albums') | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { createUser } = require('../utils/common'); | ||
const albumMocks = require('../utils/mocks/albums'); | ||
const albumsFactory = require('../utils/factories/albums'); | ||
const { mutations } = require('../../app/graphql/albums/mutations'); | ||
|
||
describe('albums', () => { | ||
describe('resolvers', () => { | ||
describe('buyAlbum', () => { | ||
it('should successfully buy album for user', () => { | ||
const albumId = 1; | ||
albumMocks.mockGetAlbumOK(albumId, albumsFactory.responseAlbumOK); | ||
return createUser().then(({ data }) => | ||
mutations.buyAlbum(undefined, { albumId, user: data.createUser }).then(response => { | ||
expect(response.id).toEqual(albumsFactory.responseAlbumOK.id); | ||
expect(response.artist).toEqual(albumsFactory.responseAlbumOK.userId); | ||
expect(response.userId).toEqual(parseInt(data.createUser.id)); | ||
}) | ||
); | ||
}); | ||
|
||
it('should fail to add album due to user already bought it', () => { | ||
const albumId = 1; | ||
albumMocks.mockGetAlbumOK(albumId, albumsFactory.responseAlbumOK); | ||
albumMocks.mockGetAlbumOK(albumId, albumsFactory.responseAlbumOK); | ||
return createUser().then(({ data }) => | ||
mutations.buyAlbum(undefined, { albumId, user: data.createUser }).then(() => | ||
mutations.buyAlbum(undefined, { albumId, user: data.createUser }).catch(error => { | ||
expect(error.message).toEqual(`The user has already bought album with id ${albumId}`); | ||
}) | ||
) | ||
); | ||
}); | ||
|
||
it('should fail to add album due album not found', () => { | ||
const albumId = 'abc'; | ||
albumMocks.mockGetAlbumNotFound(albumId); | ||
return createUser().then(({ data }) => | ||
mutations.buyAlbum(undefined, { albumId, user: data.createUser }).catch(error => { | ||
expect(error.message).toEqual('Item not found in external api'); | ||
}) | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const usersFactory = require('./factories/user'); | ||
const { mutate } = require('../server.spec'); | ||
const { createUser, logIn } = require('../users/graphql'); | ||
|
||
exports.createUserAndLogIn = () => | ||
usersFactory | ||
.attributes() | ||
.then(user => | ||
mutate(createUser(user)).then(() => mutate(logIn({ email: user.email, password: user.password }))) | ||
); | ||
|
||
exports.createUser = () => usersFactory.attributes().then(user => mutate(createUser(user))); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw new error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done