diff --git a/staff/marti-herms/project/V-HUB/api/handlers/registerGameHandler.js b/staff/marti-herms/project/V-HUB/api/handlers/registerGameHandler.js new file mode 100644 index 000000000..e69de29bb diff --git a/staff/marti-herms/project/V-HUB/app/logic/registerGame.js b/staff/marti-herms/project/V-HUB/app/logic/registerGame.js new file mode 100644 index 000000000..e69de29bb diff --git a/staff/marti-herms/project/V-HUB/core/data/models.js b/staff/marti-herms/project/V-HUB/core/data/models.js index 4a6df4307..66a92ca22 100644 --- a/staff/marti-herms/project/V-HUB/core/data/models.js +++ b/staff/marti-herms/project/V-HUB/core/data/models.js @@ -41,7 +41,7 @@ const user = new Schema({ const game = new Schema({ enabled: { type: Boolean, - required: true + default: false }, author: { type: ObjectId, diff --git a/staff/marti-herms/project/V-HUB/core/logic/registerGame.js b/staff/marti-herms/project/V-HUB/core/logic/registerGame.js new file mode 100644 index 000000000..8758e59d3 --- /dev/null +++ b/staff/marti-herms/project/V-HUB/core/logic/registerGame.js @@ -0,0 +1,33 @@ +import { User, Game } from '../data/models.js' + +import { validate, errors } from 'com' + +const { SystemError, NotFoundError } = errors + +export default (userId, name, image, description, link) => { + validate.string(userId, 'userId') + validate.string(name, 'name') + validate.string(image, 'image') + validate.string(description, 'description') + validate.string(link, 'link') + + return User.findById(userId).lean() + .catch(error => { throw new SystemError(error.message) }) + .then(author => { + if (!author) throw new NotFoundError('user not found') + + return Game.create({ + author, + name, + image, + description, + link + }) + .catch(error => { throw new SystemError(error.message) }) + }) + .then(game => + User.findByIdAndUpdate(userId, { $push: { games: game.id } }) + .catch(error => { throw new SystemError(error.message) }) + ) + .then(() => { }) +} \ No newline at end of file diff --git a/staff/marti-herms/project/V-HUB/core/logic/registerGame.spec.js b/staff/marti-herms/project/V-HUB/core/logic/registerGame.spec.js new file mode 100644 index 000000000..aa3431896 --- /dev/null +++ b/staff/marti-herms/project/V-HUB/core/logic/registerGame.spec.js @@ -0,0 +1,117 @@ +import 'dotenv/config' +import mongoose from 'mongoose' +import { expect } from 'chai' + +import registerGame from './registerGame.js' +import { User, Game } from '../data/models.js' + +import { errors } from 'com' + +const { ValidationError, NotFoundError } = errors + +const img = 'https://store-images.s-microsoft.com/image/apps.54354.13510798882606697.7a42c472-75d7-487e-9538-ebb5ce1657e6.372723d8-dd1a-450a-9fed-d420e7705e4e?mode=scale&q=90&h=300&w=200' + +const link = 'https://www.microsoft.com/en-us/p/candy-crush-saga/9nblggh18846?activetab=pivot:overviewtab' + +describe('registerGame', () => { + before(() => mongoose.connect(process.env.MONGODB_URI)) + + beforeEach(() => User.deleteMany()) + + it('succeds on new game', () => + User.create({ username: 'monoloco', email: 'mono@loco.com', password: '12312323' }) + .then(user => { + return registerGame(user.id, 'candy crush', img, 'candy crush game broh', link) + .then(() => User.findById(user.id)) + }) + .then(user => { + Game.findOne({ name: 'candy crush' }) + .then(game => { + expect(user.games[0]).to.equal(game.id) + expect(game.img).to.equal(img) + expect(game.description).to.equal('candy crush game broh') + expect(game.link).to.equal(link) + }) + }) + ) + + it('fails on non-existing user', () => { + let error + + return registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', img, 'candy crush game broh', link) + .catch(_error => error = _error) + .finally(() => { + expect(error).to.be.instanceof(NotFoundError) + expect(error.message).to.equal('user not found') + }) + }) + + it('fails on non-string userId', () => { + let error + + try { + registerGame(123, 'candy crush', img, 'candy crush game broh', link) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('userId is not a string') + } + }) + + it('fails on non-string name', () => { + let error + + try { + registerGame('66b9e687cfa7ce5e041652b3', 213, img, 'candy crush game broh', link) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('name is not a string') + } + }) + + it('fails on non-string image', () => { + let error + + try { + registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', 123, 'candy crush game broh', link) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('image is not a string') + } + }) + + it('fails on non-string description', () => { + let error + + try { + registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', img, 123, link) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('description is not a string') + } + }) + + it('fails on non-string link', () => { + let error + + try { + registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', img, 'candy crush game broh', 6123) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('link is not a string') + } + }) + + afterEach(() => User.deleteMany()) + + after(() => mongoose.disconnect()) +}) \ No newline at end of file diff --git a/staff/marti-herms/project/V-HUB/core/logic/registerGame.test.js b/staff/marti-herms/project/V-HUB/core/logic/registerGame.test.js new file mode 100644 index 000000000..1910608a8 --- /dev/null +++ b/staff/marti-herms/project/V-HUB/core/logic/registerGame.test.js @@ -0,0 +1,17 @@ +import 'dotenv/config' +import { User, Game } from '../data/models.js' + +import registerGame from './registerGame.js' + +import mongoose from 'mongoose' + +const img = 'https://store-images.s-microsoft.com/image/apps.54354.13510798882606697.7a42c472-75d7-487e-9538-ebb5ce1657e6.372723d8-dd1a-450a-9fed-d420e7705e4e?mode=scale&q=90&h=300&w=200' + +const link = 'https://www.microsoft.com/en-us/p/candy-crush-saga/9nblggh18846?activetab=pivot:overviewtab' + +mongoose.connect(process.env.MONGODB_URI) + .then(() => User.create({ username: 'monoloco', email: 'mono@loco.com', password: '12312323' })) + .then(user => registerGame(user.id, 'candy crush', img, 'candy crush game broh', link)) + .then(() => console.log('game registered')) + .catch(error => console.error(error)) + .finally(() => mongoose.disconnect()) \ No newline at end of file