|
1 |
| - |
2 |
| -const errors = require('@feathersjs/errors'); |
3 | 1 | const checkContext = require('./check-context');
|
4 | 2 |
|
5 | 3 | module.exports = function (field) {
|
6 | 4 | const deleteField = field || 'deleted';
|
7 | 5 |
|
8 | 6 | return function (context) {
|
9 |
| - const service = context.service; |
10 |
| - context.data = context.data || {}; |
11 |
| - context.params.query = context.params.query || {}; |
| 7 | + const { service, method, params: { query = {} } } = context; |
| 8 | + |
| 9 | + context.params.query = query; |
| 10 | + |
12 | 11 | checkContext(context, 'before', null, 'softDelete');
|
13 | 12 |
|
14 | 13 | if (context.params.query.$disableSoftDelete) {
|
15 | 14 | delete context.params.query.$disableSoftDelete;
|
16 | 15 | return context;
|
17 | 16 | }
|
18 | 17 |
|
19 |
| - switch (context.method) { |
20 |
| - case 'find': |
21 |
| - context.params.query[deleteField] = { $ne: true }; |
22 |
| - return context; |
23 |
| - case 'get': |
24 |
| - return throwIfItemDeleted(context.id, true) |
25 |
| - .then(data => { |
26 |
| - context.result = data; |
27 |
| - return context; |
28 |
| - }); |
29 |
| - case 'create': |
30 |
| - return context; |
31 |
| - case 'update': // fall through |
32 |
| - case 'patch': |
33 |
| - if (context.id !== null) { |
34 |
| - return throwIfItemDeleted(context.id) |
35 |
| - .then(() => context); |
36 |
| - } |
37 |
| - context.params.query[deleteField] = { $ne: true }; |
38 |
| - return context; |
39 |
| - case 'remove': |
40 |
| - return Promise.resolve() |
41 |
| - .then(() => context.id ? throwIfItemDeleted(context.id) : null) |
42 |
| - .then(() => { |
43 |
| - context.data[deleteField] = true; |
44 |
| - context.params.query[deleteField] = { $ne: true }; |
45 |
| - context.params.query.$disableSoftDelete = true; |
46 |
| - |
47 |
| - return service.patch(context.id, context.data, context.params) |
48 |
| - .then(result => { |
49 |
| - context.result = result; |
50 |
| - return context; |
51 |
| - }); |
52 |
| - }); |
53 |
| - } |
54 |
| - |
55 |
| - function throwIfItemDeleted (id, isGet) { |
56 |
| - const params = isGet ? context.params : { |
57 |
| - query: {}, |
58 |
| - provider: context.params.provider, |
59 |
| - _populate: 'skip', |
60 |
| - authenticated: context.params.authenticated, |
61 |
| - user: context.params.user |
62 |
| - }; |
| 18 | + context.params.query = Object.assign({}, { |
| 19 | + [deleteField]: { $ne: true } |
| 20 | + }, context.params.query); |
63 | 21 |
|
64 |
| - params.query.$disableSoftDelete = true; |
| 22 | + if (method === 'remove') { |
| 23 | + context.data[deleteField] = true; |
65 | 24 |
|
66 |
| - return service.get(id, params) |
67 |
| - .then(data => { |
68 |
| - delete params.query.$disableSoftDelete; |
69 |
| - |
70 |
| - if (data[deleteField]) { |
71 |
| - throw new errors.NotFound('Item not found.'); |
72 |
| - } |
73 |
| - return data; |
| 25 | + return service.patch(context.id, { [deleteField]: true }, context.params) |
| 26 | + .then(result => { |
| 27 | + context.result = result; |
| 28 | + return context; |
74 | 29 | });
|
75 | 30 | }
|
| 31 | + |
| 32 | + return context; |
76 | 33 | };
|
77 | 34 | };
|
0 commit comments