-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from wolox-training/buy-album
Buy album
- Loading branch information
Showing
18 changed files
with
213 additions
and
12 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
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,47 @@ | ||
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( | ||
`Failed to buy album. Error: 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('Failed to buy album. Error: 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