Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 64a4f69

Browse files
committed
soft delete contributions / delete related items
1 parent fc0e683 commit 64a4f69

File tree

6 files changed

+141
-69
lines changed

6 files changed

+141
-69
lines changed

server/helper/alter-items.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ module.exports = func => hook => {
1414
}
1515
let items = getItems(hook);
1616
if (Array.isArray(items)) {
17-
items.map(item => func(item));
17+
items.map(item => func(item, hook));
1818
} else if (items) {
19-
items = func(items);
19+
items = func(items, hook);
2020
}
2121
replaceItems(hook, items);
2222
return hook;

server/hooks/cleanup-related-items.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Cleanup all related items by deleting them
2+
const alterItems = require('../helper/alter-items');
3+
4+
const defaults = {
5+
connections: []
6+
};
7+
8+
module.exports = (options = defaults) => alterItems(handleItem(options));
9+
10+
const handleItem = options => (item, hook) => {
11+
options.connections.forEach(connection => {
12+
deleteItem(item, connection, hook);
13+
});
14+
return item;
15+
};
16+
17+
const deleteItem = async (item, connection, hook) => {
18+
if (!item || !connection || !connection.childField || !connection.service) {
19+
return;
20+
}
21+
const parentField = connection.parentField || '_id';
22+
let query = connection.query || {};
23+
query[connection.childField] = item[parentField];
24+
try {
25+
await hook.app.service(connection.service)
26+
.remove(null, { query });
27+
} catch (err) {
28+
throw new Error(err);
29+
}
30+
31+
};

server/hooks/null-deleted-data.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

server/hooks/patch-deleted-data.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Patch deleted records with given data
2+
const alterItems = require('../helper/alter-items');
3+
4+
const defaults = {
5+
data: {
6+
content: 'DELETED'
7+
}
8+
};
9+
10+
module.exports = (options = defaults) => alterItems(handleItem(options));
11+
12+
const handleItem = options => item => {
13+
if (item.deleted) {
14+
item = {...item, ...options.data};
15+
}
16+
return item;
17+
};

server/services/comments/comments.hooks.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const { authenticate } = require('feathers-authentication').hooks;
2-
const { unless, isProvider, populate, discard, softDelete, setNow } = require('feathers-hooks-common');
1+
const {authenticate} = require('feathers-authentication').hooks;
2+
const {unless, isProvider, populate, discard, softDelete, setNow} = require('feathers-hooks-common');
33
const {
44
//queryWithCurrentUser,
55
associateCurrentUser,
66
// restrictToAuthenticated,
77
restrictToOwner
88
} = require('feathers-authentication-hooks');
9-
const { isVerified } = require('feathers-authentication-management').hooks;
9+
const {isVerified} = require('feathers-authentication-management').hooks;
1010
const createExcerpt = require('../../hooks/create-excerpt');
11-
const nullDeletedData = require('../../hooks/null-deleted-data');
11+
const patchDeletedData = require('../../hooks/patch-deleted-data');
1212
const keepDeletedDataFields = require('../../hooks/keep-deleted-data-fields');
1313
const createNotifications = require('./hooks/create-notifications');
1414
const createMentionNotifications = require('./hooks/create-mention-notifications');
@@ -30,7 +30,7 @@ const xssFields = ['content', 'contentExcerpt'];
3030
module.exports = {
3131
before: {
3232
all: [
33-
xss({ fields: xssFields })
33+
xss({fields: xssFields})
3434
],
3535
find: [],
3636
get: [
@@ -43,44 +43,49 @@ module.exports = {
4343
isVerified()
4444
),
4545
associateCurrentUser(),
46-
createExcerpt({ length: 180 }),
46+
createExcerpt({length: 180}),
4747
softDelete()
4848
],
4949
update: [
5050
authenticate('jwt'),
51-
isVerified(),
5251
unless(isProvider('server'),
52+
isVerified(),
5353
restrictToOwner()
5454
),
55-
createExcerpt({ length: 180 }),
55+
createExcerpt({length: 180}),
5656
softDelete(),
5757
setNow('updatedAt')
5858
],
5959
patch: [
6060
authenticate('jwt'),
61-
isVerified(),
6261
unless(isProvider('server'),
62+
isVerified(),
6363
unless((hook) => {
6464
// TODO: change that to a more sane method by going through the server with an constum service
6565
// only allow upvoteCount increment for non owners
6666
// the data has to be the exact copy of the valid object
6767
const valid = {$inc: {upvoteCount: 1}};
6868
return (!_.difference(_.keys(valid), _.keys(hook.data)).length) &&
69-
(!_.difference(_.keys(valid.$inc), _.keys(hook.data.$inc)).length) &&
70-
(!_.difference(_.values(valid.$inc), _.values(hook.data.$inc)).length);
69+
(!_.difference(_.keys(valid.$inc), _.keys(hook.data.$inc)).length) &&
70+
(!_.difference(_.values(valid.$inc), _.values(hook.data.$inc)).length);
7171
}, restrictToOwner())
7272
),
73-
createExcerpt({ length: 180 }),
73+
createExcerpt({length: 180}),
7474
softDelete(),
7575
setNow('updatedAt'),
7676
// SoftDelete uses patch to delete items
7777
// Make changes to deleted items here
78-
nullDeletedData({ fields: [ 'content', 'contentExcerpt' ]})
78+
patchDeletedData({
79+
data: {
80+
content: 'DELETED',
81+
contentExcerpt: 'DELETED'
82+
}
83+
})
7984
],
8085
remove: [
8186
authenticate('jwt'),
82-
isVerified(),
8387
unless(isProvider('server'),
88+
isVerified(),
8489
restrictToOwner()
8590
),
8691
softDelete()
@@ -89,8 +94,8 @@ module.exports = {
8994

9095
after: {
9196
all: [
92-
populate({ schema: userSchema }),
93-
xss({ fields: xssFields }),
97+
populate({schema: userSchema}),
98+
xss({fields: xssFields}),
9499
keepDeletedDataFields()
95100
],
96101
find: [

server/services/contributions/contributions.hooks.js

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
const { authenticate } = require('feathers-authentication').hooks;
2-
const { when, unless, isProvider, populate, softDelete, setNow } = require('feathers-hooks-common');
1+
const {authenticate} = require('feathers-authentication').hooks;
2+
const {when, unless, isProvider, populate, softDelete, setNow} = require('feathers-hooks-common');
33
const {
44
//queryWithCurrentUser,
55
associateCurrentUser,
66
restrictToOwner
77
} = require('feathers-authentication-hooks');
8-
const { isVerified } = require('feathers-authentication-management').hooks;
8+
const {isVerified} = require('feathers-authentication-management').hooks;
99
const createSlug = require('../../hooks/create-slug');
1010
const saveRemoteImages = require('../../hooks/save-remote-images');
1111
const createExcerpt = require('../../hooks/create-excerpt');
12-
const nullDeletedData = require('../../hooks/null-deleted-data');
12+
const patchDeletedData = require('../../hooks/patch-deleted-data');
13+
const cleanupRelatedItems = require('../../hooks/cleanup-related-items');
1314
const keepDeletedDataFields = require('../../hooks/keep-deleted-data-fields');
1415
const search = require('feathers-mongodb-fuzzy-search');
1516
const thumbnails = require('../../hooks/thumbnails');
@@ -86,7 +87,7 @@ const xssFields = ['content', 'contentExcerpt', 'cando.reason'];
8687
module.exports = {
8788
before: {
8889
all: [
89-
xss({ fields: xssFields })
90+
xss({fields: xssFields})
9091
],
9192
find: [
9293
unless(isModerator(),
@@ -95,7 +96,8 @@ module.exports = {
9596
search(),
9697
search({
9798
fields: ['title', 'content']
98-
})
99+
}),
100+
softDelete()
99101
],
100102
get: [
101103
unless(isModerator(),
@@ -110,7 +112,7 @@ module.exports = {
110112
isVerified()
111113
),
112114
associateCurrentUser(),
113-
createSlug({ field: 'title' }),
115+
createSlug({field: 'title'}),
114116
saveRemoteImages(['teaserImg']),
115117
createExcerpt(),
116118
softDelete()
@@ -144,7 +146,25 @@ module.exports = {
144146
setNow('updatedAt'),
145147
// SoftDelete uses patch to delete items
146148
// Make changes to deleted items here
147-
nullDeletedData({ fields: [ 'content', 'contentExcerpt' ]})
149+
patchDeletedData({
150+
data: {
151+
$set: {
152+
title: 'DELETED',
153+
type: 'DELETED',
154+
content: 'DELETED',
155+
contentExcerpt: 'DELETED',
156+
categoryIds: undefined,
157+
teaserImg: undefined,
158+
shoutCount: 0,
159+
tags: undefined,
160+
emotions: undefined
161+
},
162+
$unset: {
163+
cando: '',
164+
meta: ''
165+
}
166+
}
167+
})
148168
],
149169
remove: [
150170
authenticate('jwt'),
@@ -159,17 +179,19 @@ module.exports = {
159179

160180
after: {
161181
all: [
162-
xss({ fields: xssFields }),
163-
populate({ schema: userSchema }),
164-
populate({ schema: categoriesSchema }),
165-
populate({ schema: candosSchema }),
166-
populate({ schema: commentsSchema }),
167-
keepDeletedDataFields({ fields: [
168-
'_id',
169-
'deleted',
170-
'createdAt',
171-
'updatedAt'
172-
]})
182+
xss({fields: xssFields}),
183+
populate({schema: userSchema}),
184+
populate({schema: categoriesSchema}),
185+
populate({schema: candosSchema}),
186+
populate({schema: commentsSchema}),
187+
keepDeletedDataFields({
188+
fields: [
189+
'_id',
190+
'deleted',
191+
'createdAt',
192+
'updatedAt'
193+
]
194+
})
173195
],
174196
find: [
175197
when(isSingleItem(),
@@ -193,7 +215,35 @@ module.exports = {
193215
createMentionNotifications(),
194216
thumbnails(thumbs)
195217
],
196-
remove: []
218+
remove: [
219+
cleanupRelatedItems({
220+
connections: [
221+
{
222+
service: 'comments',
223+
parentField: '_id',
224+
childField: 'contributionId'
225+
},
226+
{
227+
service: 'emotions',
228+
parentField: '_id',
229+
childField: 'contributionId'
230+
},
231+
{
232+
service: 'shouts',
233+
parentField: '_id',
234+
childField: 'foreignId',
235+
query: {
236+
foreignService: 'contributions'
237+
}
238+
},
239+
{
240+
service: 'users-candos',
241+
parentField: '_id',
242+
childField: 'contributionId'
243+
}
244+
]
245+
})
246+
]
197247
},
198248

199249
error: {

0 commit comments

Comments
 (0)