Skip to content

Commit 76ea0a4

Browse files
Merge pull request Human-Connection#53 in HC/hc-api-feathers from develop to master
* commit '371c91a5a9486897ab087bc628bcd2a591d49b58': fixed two issues add safety for find query in associated can dos add associated can dos add associated can dos add cando title fix missing srcset / renamed candoId started can dos
2 parents 136a766 + 371c91a commit 76ea0a4

18 files changed

+385
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/helper/seed-helpers.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@ const ngoLogos = [
3535
null
3636
];
3737

38+
const difficulties = ['easy', 'medium', 'hard'];
39+
3840
module.exports = {
39-
randomItem: (items) => {
40-
let randomIds = _.shuffle(_.keys(items));
41+
randomItem: (items, filter) => {
42+
let ids = filter
43+
? Object.keys(items)
44+
.filter(id => {
45+
return filter(items[id]);
46+
})
47+
: _.keys(items);
48+
let randomIds = _.shuffle(ids);
4149
return items[randomIds.pop()];
4250
},
4351
randomItems: (items, key = '_id', min = 1, max = 1) => {
@@ -55,6 +63,9 @@ module.exports = {
5563
}
5664
return res;
5765
},
66+
randomDifficulty: () => {
67+
return _.shuffle(difficulties).pop();
68+
},
5869
randomLogo: () => {
5970
return _.shuffle(ngoLogos).pop();
6071
},

server/hooks/is-single-item.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Check if the query is for a single item
2+
module.exports = () => hook => {
3+
if (hook && hook.params && hook.params.query && hook.params.query.$limit === 1) {
4+
return true;
5+
} else {
6+
return false;
7+
}
8+
};

server/models/contributions.model.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
44
// for more of what you can do here.
55
module.exports = function (app) {
66
const mongooseClient = app.get('mongooseClient');
7+
8+
const candoSchema = mongooseClient.Schema({
9+
difficulty: {
10+
type: String,
11+
enum: ['easy', 'medium', 'hard']
12+
},
13+
reasonTitle: { type: String },
14+
reason: { type: String }
15+
});
16+
717
const contributions = new mongooseClient.Schema({
818
userId: { type: String, required: true },
919
categoryIds: { type: Array },
1020
title: { type: String, required: true },
1121
// Generated from title
1222
slug: { type: String, required: true, unique: true },
1323
type: { type: String, required: true },
24+
cando: candoSchema,
1425
content: { type: String, required: true },
1526
// Generated from content
1627
contentExcerpt: { type: String, required: true },

server/models/users-candos.model.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// users-candos-model.js - A mongoose model
2+
//
3+
// See http://mongoosejs.com/docs/models.html
4+
// for more of what you can do here.
5+
module.exports = function (app) {
6+
const mongooseClient = app.get('mongooseClient');
7+
const usersCandos = new mongooseClient.Schema({
8+
userId: { type: String, required: true },
9+
contributionId: { type: String, required: true },
10+
done: { type: Boolean, default: false },
11+
doneAt: { type: Date },
12+
createdAt: { type: Date, default: Date.now },
13+
updatedAt: { type: Date, default: Date.now },
14+
wasSeeded: { type: Boolean }
15+
});
16+
17+
usersCandos.index(
18+
{ userId: 1, contributionId: 1 },
19+
{ unique: true }
20+
);
21+
22+
usersCandos.index(
23+
{ userId: 1 }
24+
);
25+
26+
usersCandos.index(
27+
{ contributionId: 1 }
28+
);
29+
30+
return mongooseClient.model('usersCandos', usersCandos);
31+
};

server/seeder/development/candos.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const seedHelpers = require('../../helper/seed-helpers');
2+
const { size } = require('lodash');
3+
4+
module.exports = (seederstore) => {
5+
return {
6+
services: [{
7+
count: 30,
8+
path: 'contributions',
9+
templates: [
10+
{
11+
userId: () => seedHelpers.randomItem(seederstore.users)._id,
12+
title: '{{lorem.words}}',
13+
type: 'cando',
14+
categoryIds: () => seedHelpers.randomCategories(seederstore),
15+
cando: {
16+
difficulty: () => seedHelpers.randomDifficulty(),
17+
reasonTitle: '{{lorem.words}}?',
18+
reason: '{{lorem.text}} {{lorem.text}}'
19+
},
20+
content: '{{lorem.text}} {{lorem.text}}',
21+
teaserImg: seedHelpers.randomUnsplashUrl,
22+
language: () => seedHelpers.randomItem(['de', 'en']),
23+
shouts: () => seedHelpers.randomItems(seederstore.users, '_id', 0, Math.floor(size(seederstore.users) / 2)),
24+
visibility: 'public',
25+
createdAt: '{{date.recent}}',
26+
updatedAt: '{{date.recent}}',
27+
wasSeeded: true
28+
},
29+
{
30+
userId: () => seedHelpers.randomItem(seederstore.users)._id,
31+
title: '{{lorem.sentence}}',
32+
type: 'cando',
33+
categoryIds: () => seedHelpers.randomCategories(seederstore),
34+
cando: {
35+
difficulty: () => seedHelpers.randomDifficulty(),
36+
reasonTitle: '{{lorem.words}}?',
37+
reason: '{{lorem.text}} {{lorem.text}}'
38+
},
39+
content: '{{lorem.text}} {{lorem.text}}',
40+
language: () => seedHelpers.randomItem(['de', 'en']),
41+
shouts: () => seedHelpers.randomItems(seederstore.users, '_id', 0, Math.floor(size(seederstore.users) / 2)),
42+
visibility: 'public',
43+
createdAt: '{{date.recent}}',
44+
updatedAt: '{{date.recent}}',
45+
wasSeeded: true
46+
},
47+
{
48+
userId: () => seedHelpers.randomItem(seederstore.users)._id,
49+
title: '{{lorem.sentence}}',
50+
type: 'cando',
51+
categoryIds: () => seedHelpers.randomCategories(seederstore),
52+
cando: {
53+
difficulty: () => seedHelpers.randomDifficulty(),
54+
reasonTitle: '{{lorem.words}}?',
55+
reason: '{{lorem.text}} {{lorem.text}}'
56+
},
57+
content: '{{lorem.text}} {{lorem.text}}',
58+
language: () => seedHelpers.randomItem(['de', 'en']),
59+
shouts: () => seedHelpers.randomItems(seederstore.users, '_id', 0, Math.floor(size(seederstore.users) / 2)),
60+
visibility: 'public',
61+
isEnabled: false,
62+
createdAt: '{{date.recent}}',
63+
updatedAt: '{{date.recent}}',
64+
wasSeeded: true
65+
}
66+
]
67+
}]
68+
};
69+
};

server/seeder/development/contributions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ module.exports = (seederstore) => {
4949
wasSeeded: true
5050
}
5151
]
52-
}],
52+
}]
5353
};
5454
};

server/seeder/development/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module.exports = function () {
77
require('./users'),
88
require('./moderators'),
99
require('./contributions'),
10+
require('./candos'),
11+
require('./users-candos'),
1012
require('./organizations'),
1113
require('./projects'),
1214
require('./follows'),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const seedHelpers = require('../../helper/seed-helpers');
2+
3+
module.exports = (seederstore) => {
4+
let filter = ({type}) => type === 'cando';
5+
return {
6+
services: [{
7+
count: 80,
8+
path: 'users-candos',
9+
templates: [
10+
{
11+
userId: () => seedHelpers.randomItem(seederstore.users)._id,
12+
contributionId: () => seedHelpers.randomItem(seederstore.contributions, filter)._id,
13+
done: true,
14+
doneAt: '{{date.recent}}',
15+
createdAt: '{{date.recent}}',
16+
updatedAt: '{{date.recent}}',
17+
wasSeeded: true
18+
},
19+
{
20+
userId: () => seedHelpers.randomItem(seederstore.users)._id,
21+
contributionId: () => seedHelpers.randomItem(seederstore.contributions, filter)._id,
22+
done: false,
23+
createdAt: '{{date.recent}}',
24+
updatedAt: '{{date.recent}}',
25+
wasSeeded: true
26+
}
27+
]
28+
}]
29+
};
30+
};

server/services/contributions/contributions.hooks.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { authenticate } = require('feathers-authentication').hooks;
2-
const { unless, isProvider, populate } = require('feathers-hooks-common');
2+
const { when, unless, isProvider, populate } = require('feathers-hooks-common');
33
const {
44
//queryWithCurrentUser,
55
associateCurrentUser,
@@ -13,6 +13,8 @@ const search = require('feathers-mongodb-fuzzy-search');
1313
const thumbnails = require('../../hooks/thumbnails');
1414
const isModerator = require('../../hooks/is-moderator-boolean');
1515
const excludeDisabled = require('../../hooks/exclude-disabled');
16+
const getAssociatedCanDos = require('./hooks/getAssociatedCanDos');
17+
const isSingleItem = require('../../hooks/is-single-item');
1618

1719
const userSchema = {
1820
include: {
@@ -33,6 +35,16 @@ const categoriesSchema = {
3335
}
3436
};
3537

38+
const candosSchema = {
39+
include: {
40+
service: 'users-candos',
41+
nameAs: 'candoUsers',
42+
parentField: '_id',
43+
childField: 'contributionId',
44+
asArray: true
45+
}
46+
};
47+
3648
const commentsSchema = {
3749
include: {
3850
service: 'comments',
@@ -53,7 +65,6 @@ const commentsSchema = {
5365
}
5466
};
5567

56-
5768
module.exports = {
5869
before: {
5970
all: [],
@@ -120,9 +131,13 @@ module.exports = {
120131
all: [
121132
populate({ schema: userSchema }),
122133
populate({ schema: categoriesSchema }),
134+
populate({ schema: candosSchema }),
123135
populate({ schema: commentsSchema })
124136
],
125137
find: [
138+
when(isSingleItem(),
139+
getAssociatedCanDos()
140+
),
126141
thumbnails({
127142
teaserImg: {
128143
cardS: '300x0',
@@ -136,11 +151,16 @@ module.exports = {
136151
})
137152
],
138153
get: [
154+
getAssociatedCanDos(),
139155
thumbnails({
140156
teaserImg: {
157+
cardS: '300x0',
158+
cardM: '400x0',
159+
cardL: '740x0',
141160
zoom: '0x1024',
142161
cover: '800x300/smart',
143-
placeholder: '800x300/filters:blur(10)'
162+
placeholder: '800x300/filters:blur(10)',
163+
coverPlaceholder: '243x100/smart/filters:blur(30)'
144164
}
145165
})
146166
],

0 commit comments

Comments
 (0)