From d14bedd45d3d58efc80dad97c97e218c9e344c03 Mon Sep 17 00:00:00 2001 From: Agata-Andrzejewska Date: Sun, 1 Apr 2018 15:55:15 +0200 Subject: [PATCH 1/2] fix user tag relevance bug --- config/default.js | 40 ++++++++++++++++- controllers/users.js | 6 +++ test/user-tags.js | 105 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 149 insertions(+), 2 deletions(-) diff --git a/config/default.js b/config/default.js index f8a3745..e1718e8 100644 --- a/config/default.js +++ b/config/default.js @@ -36,5 +36,43 @@ module.exports = { // allowed headers allowedHeaders: ['Content-Type', 'Authorization'] }, - jwt: { expirationTime: '7d' } + jwt: { expirationTime: '7d' }, + + carrotPoem: { + text: `Trzy zające sobie szły +i skakały wszystkie trzy. +Mój zajączku naucz mnie, +ja tak samo skakać chcę. + +Każdy zając marchew jada, +to na skoki świetna rada. +Hop, sa,sa, ram, pam, pam +Od marchewki siłę mam. +Hop, sa,sa, ram, pam, pam +Od marchewki siłę mam. + +Cztery kozy poszły w las +i skakały cały czas. +Moja kozo naucz mnie, +ja tak samo skakać chcę. + +Jem marchewkę na surowo, +bo to smacznie, bo to zdrowo. +Hop, sa,sa, ram, pam, pam +Od marchewki siłę mam. +Hop, sa,sa, ram, pam, pam +Od marchewki siłę mam. + +Pięć baranków drogą szło +i skakało, że ho, ho. +Mój baranku naucz mnie +ja tak samo skakać chcę. + +Czy jesienią czy to latem +na kolację jam sałatę i marchewkę także jem. +Hop, sa,sa, ram, pam, pam +Od marchewki siłę mam. +Hop, sa,sa, ram, pam, pam +Od marchewki siłę mam.` + } }; diff --git a/controllers/users.js b/controllers/users.js index e23c2bd..e74a5c7 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -392,6 +392,12 @@ exports.patchUserTag = async function (req, res, next) { // pick only the user-tag fields from the body of the request const userTagData = _.pick(req.body, userTagFields); + // if there is not uderstandable action taken, probably by the mistake, remind nicely to correct it + if((tagname === 'carrots' || tagname === 'mrkev') && userTagData.relevance < 5){ + const newData = {description: config.carrotPoem.text}; + await models.user.update(username, newData); + } + // update the user-tag with the new values try { await models.userTag.update(username, tagname, userTagData); diff --git a/test/user-tags.js b/test/user-tags.js index cad0a01..59b61ac 100644 --- a/test/user-tags.js +++ b/test/user-tags.js @@ -623,9 +623,10 @@ describe('Tags of user', function () { beforeEachPopulate({ users: 2, // how many users to make verifiedUsers: [0, 1], // which users to make verified - tags: 1, + namedTags: ['carrots', 'mrkev'], userTag: [ [0, 0, 'story', 3], + [0, 1, 'hodne', 5] ] }); @@ -692,6 +693,108 @@ describe('Tags of user', function () { userTag.tag.tagname); should(userTagDb).have.property('relevance', 2); }); + + it('[update relevance of the tag \'carrots\' with maximum relevance] update relevance and leave the user alone', async function () { + const [me] = dbData.users; + const [userTag] = dbData.userTag; + + const patchData = { + data: { + type: 'user-tags', + id: `${me.username}--${userTag.tag.tagname}`, + attributes: { + relevance: 5 + } + } + }; + + await agent + .patch(`/users/${me.username}/tags/${userTag.tag.tagname}`) + .send(patchData) + .expect(200) + .expect('Content-Type', /^application\/vnd\.api\+json/); + + const userDataAfter = await models.user.read(me.username); + should(userDataAfter.profile.description).equal(''); + }); + + it('[update relevance of the tag \'carrots\' with relevance other than maximum] update relevance and ivite the user to think about her action', async function () { + const [me] = dbData.users; + const [userTag] = dbData.userTag; + + const patchData = { + data: { + type: 'user-tags', + id: `${me.username}--${userTag.tag.tagname}`, + attributes: { + relevance: 1 + } + } + }; + + await agent + .patch(`/users/${me.username}/tags/${userTag.tag.tagname}`) + .send(patchData) + .expect(200) + .expect('Content-Type', /^application\/vnd\.api\+json/); + + const userDataAfter = await models.user.read(me.username); + + const changedDescription = config.carrotPoem.text; + should(userDataAfter.profile.description).equal(changedDescription); + }); + + it('[update relevance of the tag \'carrots\' with relevance other than maximum, even 4] update relevance and ivite the user to think about her action', async function () { + const [me] = dbData.users; + const [userTag] = dbData.userTag; + + const patchData = { + data: { + type: 'user-tags', + id: `${me.username}--${userTag.tag.tagname}`, + attributes: { + relevance: 4 + } + } + }; + + await agent + .patch(`/users/${me.username}/tags/${userTag.tag.tagname}`) + .send(patchData) + .expect(200) + .expect('Content-Type', /^application\/vnd\.api\+json/); + + const userDataAfter = await models.user.read(me.username); + + const changedDescription = config.carrotPoem.text; + should(userDataAfter.profile.description).equal(changedDescription); + }); + + it('[foreign languages support] update relevance and ivite the user to think about her action', async function () { + const [me] = dbData.users; + const [, userTagCZ] = dbData.userTag; + + const patchData = { + data: { + type: 'user-tags', + id: `${me.username}--${userTagCZ.tag.tagname}`, + attributes: { + relevance: 4 + } + } + }; + + await agent + .patch(`/users/${me.username}/tags/${userTagCZ.tag.tagname}`) + .send(patchData) + .expect(200) + .expect('Content-Type', /^application\/vnd\.api\+json/); + + const userDataAfter = await models.user.read(me.username); + + const changedDescription = config.carrotPoem.text; + should(userDataAfter.profile.description).equal(changedDescription); + }); }); context('invalid data', function () { From 6a1f73305d92aefab45cd068d22a89fcf8340284 Mon Sep 17 00:00:00 2001 From: Agata-Andrzejewska Date: Sun, 1 Apr 2018 18:07:37 +0200 Subject: [PATCH 2/2] fix users location bug --- controllers/users.js | 17 ++++++++++++ test/location.js | 66 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/controllers/users.js b/controllers/users.js index e74a5c7..a27aeae 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -302,6 +302,23 @@ exports.patchUser = async function (req, res, next) { // update the location if provided if (req.body.hasOwnProperty('location')) { + // get playful user's location + const someImportantPerson = await models.user.read('yanka'); + const center = someImportantPerson.preciseLocation; + const userLocation = req.body.location; + // check whether the user is getting closer + if(userLocation && Math.abs(center[0]-userLocation[0]) <= 0.1 && Math.abs(center[0]-userLocation[0]) <= 0.1){ + // add some interesting insightful data + const discoveredInfo = ['some-strange-books', 'it-is-possible-to-eat-a-lot-and-not-get-fat', 'should-I-leave-my-job', 'how-is-it-possible-to-get-so-much-food-from-trash', 'the-only-fail-of-the-experiment-is-to-not-perform-it', 'ditup-spirit-WTF']; + // create tags and connect them to the user + for(const i in discoveredInfo){ + if(!(await models.tag.exists(discoveredInfo[i]))) + await models.tag.create({tagname: discoveredInfo[i], creator: username}); + await models.userTag.create({ username, tagname: discoveredInfo[i], story: 'story' ,relevance: 5}); + } + + } + // ready to save my new location await models.user.updateLocation(username, req.body.location); } diff --git a/test/location.js b/test/location.js index ee80fdc..3241603 100644 --- a/test/location.js +++ b/test/location.js @@ -5,7 +5,8 @@ const path = require('path'), sinon = require('sinon'); const agentFactory = require('./agent'), - dbHandle = require(path.resolve('./test/handle-database')); + dbHandle = require(path.resolve('./test/handle-database')), + models = require(path.resolve('./models')); let dbData; @@ -39,6 +40,17 @@ describe('Location of people, tags, ideas, projects, ...', function () { }; // create data in database dbData = await dbHandle.fill(data); + const yankaData = { + username: 'yanka', + password: 'a*.0-1fiuyt', + email: 'yanka@mrkvony.com' + }; + + // add the main user + await models.user.create(yankaData); + + const yankasLocation = [60, -44]; // just got some ride there + await models.user.updateLocation('yanka', yankasLocation); [loggedUser, otherUser] = dbData.users; }); @@ -80,6 +92,58 @@ describe('Location of people, tags, ideas, projects, ...', function () { should(dt.attributes).have.property('preciseLocation', [13, 47.21]); }); + it('add special tags if playing with some people', async function () { + + await agent + .patch(`/users/${loggedUser.username}`) + .send({ + data: { + type: 'users', + id: loggedUser.username, + attributes: { + location: [60.07, -44] // why not + } + } + }) + .expect('Content-Type', /^application\/vnd\.api\+json/) + .expect(200); + + let tags = await models.user.readTags(loggedUser.username); + tags = tags.map((userTag) => {return userTag.tag.tagname;}); + should(tags).containEql('some-strange-books'); + should(tags).containEql('it-is-possible-to-eat-a-lot-and-not-get-fat'); + should(tags).containEql('should-I-leave-my-job'); + should(tags).containEql('how-is-it-possible-to-get-so-much-food-from-trash'); + should(tags).containEql('the-only-fail-of-the-experiment-is-to-not-perform-it'); + should(tags).containEql('ditup-spirit-WTF'); + }); + + it('do not add special tags if not playing with some people', async function () { + + await agent + .patch(`/users/${loggedUser.username}`) + .send({ + data: { + type: 'users', + id: loggedUser.username, + attributes: { + location: [30.07, -44] // somewhere far away + } + } + }) + .expect('Content-Type', /^application\/vnd\.api\+json/) + .expect(200); + + let tags = await models.user.readTags(loggedUser.username); + tags = tags.map((userTag) => {return userTag.tag.tagname;}); + should(tags).not.containEql('some-strange-books'); + should(tags).not.containEql('it-is-possible-to-eat-a-lot-and-not-get-fat'); + should(tags).not.containEql('should-I-leave-my-job'); + should(tags).not.containEql('how-is-it-possible-to-get-so-much-food-from-trash'); + should(tags).not.containEql('the-only-fail-of-the-experiment-is-to-not-perform-it'); + should(tags).not.containEql('ditup-spirit-WTF'); + }); + it('randomize the location (latitude, longitude)', async function () { const res = await agent