Skip to content

Commit

Permalink
add donations service
Browse files Browse the repository at this point in the history
  • Loading branch information
ewingrj committed Aug 25, 2017
1 parent ba70570 commit 5409ed2
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"max": 50
},
"nedb": "../data",
"uploads": "../uploads"
"uploads": "../uploads",
"uploadsBaseUrl": "http://localhost:3030/uploads/"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"test": "npm run eslint && npm run mocha",
"eslint": "eslint src/. test/. --config .eslintrc.json",
"start": "babel-watch src",
"serve": "npm run build && pm2 start ./build --name 'feathers'",
"serve-restart": "npm run build && pm2 restart feathers",
"serve": "npm run build && pm2 startOrRestart ecosystem.config.js --env production",
"serve-restart": "npm run build && pm2 startOrRestart ecosystem.config.js --env production",
"mocha": "mocha test/ --recursive --compilers js:babel-core/register"
},
"dependencies": {
Expand Down
12 changes: 12 additions & 0 deletions src/models/donations.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const NeDB = require('nedb');
const path = require('path');

module.exports = function (app) {
const dbPath = app.get('nedb');
const Model = new NeDB({
filename: path.join(dbPath, 'donations.db'),
autoload: true
});

return Model;
};
6 changes: 6 additions & 0 deletions src/services/donations/donations.filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint no-console: 1 */
console.warn('You are using the default filter for the donations service. For more information about event filters see https://docs.feathersjs.com/api/events.html#event-filtering'); // eslint-disable-line no-console

module.exports = function (data, connection, hook) { // eslint-disable-line no-unused-vars
return data;
};
90 changes: 90 additions & 0 deletions src/services/donations/donations.hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* eslint-disable no-unused-vars */
import errors from 'feathers-errors';
import { discard, setByDot } from 'feathers-hooks-common';
import { sanitizeAddress, validateAddress } from '../../hooks/address';
import { restrictToOwner } from 'feathers-authentication-hooks';

const restrict = [
restrictToOwner({
idField: 'address',
ownerField: 'donorAddress',
}),
];

const setAddress = context => {
setByDot(context.data, 'donorAddress', context.params.user.address);
return context;
};

const address = [
discard('donorAddress'),
setAddress,
sanitizeAddress('donorAddress'),
validateAddress('donorAddress'),
];

const updateType = () => {
return context => {
const { data } = context;

switch (data.type) {
case 'cause': {
const service = context.app.service('causes');

return service.get(data._id)
.then(cause => {
//TODO update counters on the cause

})
.catch(() => context);
}
case 'campaign': {
const service = context.app.service('campaigns');
//TODO update counters on the campaign
return context;
}
case 'milestone': {
const service = context.app.service('milestones');
//TODO update counters on the milestone
return context;
}
default: {
return new errors.BadRequest('Invalid type. Must be one of [\'cause\', \'campaign\', \'milestone\'].');
}
}

};
};


module.exports = {
before: {
all: [],
find: [],
get: [],
create: [ ...address ],
update: [ ...restrict, ...address ],
patch: [ ...restrict, ...address ],
remove: [ ...restrict ],
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},

error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: [],
},
};
29 changes: 29 additions & 0 deletions src/services/donations/donations.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Initializes the `donations` service on path `/donations`
const createService = require('feathers-nedb');
const createModel = require('../../models/donations.model');
const hooks = require('./donations.hooks');
const filters = require('./donations.filters');

module.exports = function () {
const app = this;
const Model = createModel(app);
const paginate = app.get('paginate');

const options = {
name: 'donations',
Model,
paginate
};

// Initialize our service with any options it requires
app.use('/donations', createService(options));

// Get our initialized service so that we can register hooks and filters
const service = app.service('donations');

service.hooks(hooks);

if (service.filter) {
service.filter(filters);
}
};
2 changes: 2 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const completionRequests = require('./completion-requests/completion-requests.se
const campaigns = require('./campaigns/campaigns.service.js');
const users = require('./users/users.service.js');
const uploads = require('./uploads/uploads.service.js');
const donations = require('./donations/donations.service.js');
module.exports = function () {
const app = this; // eslint-disable-line no-unused-vars
app.configure(skunkworks);
Expand All @@ -20,4 +21,5 @@ module.exports = function () {
app.configure(campaigns);
app.configure(users);
app.configure(uploads);
app.configure(donations);
};
10 changes: 10 additions & 0 deletions test/services/donations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const assert = require('assert');
const app = require('../../src/app');

describe('\'donations\' service', () => {
it('registered the service', () => {
const service = app.service('donations');

assert.ok(service, 'Registered the service');
});
});

0 comments on commit 5409ed2

Please sign in to comment.