Skip to content

Commit

Permalink
subjects service added
Browse files Browse the repository at this point in the history
  • Loading branch information
ymarcon committed Jan 20, 2022
1 parent e5dcfce commit 85534f0
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ app.use('/', express.static(app.get('public')));

// Set up Plugins and providers
app.configure(express.rest());
app.configure(socketio());
// app.configure(socketio());

app.configure(mongoose);

Expand Down
2 changes: 1 addition & 1 deletion src/services/group/group.abilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const defineRulesFor = (user) => {
// administrator can do evil
can('manage', 'all');
} else if (user.role === 'manager') {
// can list all users
// can list all groups
can('read', 'group');
}

Expand Down
2 changes: 2 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const formRevisionDigest = require('./form-revision-digest/form-revision-digest.
const crfs = require('./crfs/crfs.service.js');
const caseReport = require('./case-report/case-report.service.js');
const caseReportExport = require('./case-report-export/case-report-export.service.js');
const subjects = require('./subjects/subjects.service.js');
// eslint-disable-next-line no-unused-vars
module.exports = function (app) {
app.configure(user);
Expand All @@ -28,4 +29,5 @@ module.exports = function (app) {
app.configure(crfs);
app.configure(caseReport);
app.configure(caseReportExport);
app.configure(subjects);
};
55 changes: 55 additions & 0 deletions src/services/subjects/subjects.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { BadRequest } = require('@feathersjs/errors');

/* eslint-disable no-unused-vars */
exports.Subjects = class Subjects {
constructor (options, app) {
this.options = options || {};
this.app = app;
}

async find (params) {
const subjects = [];
const groups = await this.app.service('group').find(params);
if (params.query && params.query.name) {
params.query.email = params.query.name;
delete params.query.name;
}
const users = await this.app.service('user').find(params);

users.data.forEach(user => {
subjects.push({
type: 'user',
id: user._id,
name: user.email
});
});
groups.data.forEach(group => {
subjects.push({
type: 'group',
id: group._id,
name: group.name
});
});
return subjects;
}

async get (id, params) {
throw new BadRequest('Not implemented');
}

async create (data, params) {
throw new BadRequest('Not implemented');
}

async update (id, data, params) {
throw new BadRequest('Not implemented');
}

async patch (id, data, params) {
throw new BadRequest('Not implemented');
}

async remove (id, params) {
throw new BadRequest('Not implemented');
}
};
33 changes: 33 additions & 0 deletions src/services/subjects/subjects.hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { authenticate } = require('@feathersjs/authentication').hooks;

module.exports = {
before: {
all: [ authenticate('jwt') ],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

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

error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
17 changes: 17 additions & 0 deletions src/services/subjects/subjects.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Initializes the `subjects` service on path `/subjects`
const { Subjects } = require('./subjects.class');
const hooks = require('./subjects.hooks');

module.exports = function (app) {
const options = {
paginate: app.get('paginate')
};

// Initialize our service with any options it requires
app.use('/subjects', new Subjects(options, app));

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

service.hooks(hooks);
};
10 changes: 10 additions & 0 deletions test/services/subjects.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('\'subjects\' service', () => {
it('registered the service', () => {
const service = app.service('subjects');

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

0 comments on commit 85534f0

Please sign in to comment.