Skip to content

Commit

Permalink
feat: add export plugin and api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Feb 21, 2019
1 parent f330215 commit ffcf10e
Show file tree
Hide file tree
Showing 4 changed files with 1,761 additions and 81 deletions.
22 changes: 21 additions & 1 deletion lib/role.http.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ const Router = require('@lykmapipo/express-common').Router;
const API_VERSION = getString('API_VERSION', '1.0.0');
const PATH_SINGLE = '/roles/:id';
const PATH_LIST = '/roles';
const PATH_SCHEMA = '/roles/schema/';
const PATH_SCHEMA = '/roles/schema';
const PATH_EXPORT = '/roles/export';


/* declarations */
Expand Down Expand Up @@ -174,6 +175,25 @@ router.get(PATH_SCHEMA, function getSchema(request, response) {
});


/**
* @api {get} /roles/export Export Roles
* @apiVersion 1.0.0
* @apiName ExportRole
* @apiGroup Role
* @apiDescription Export roles as csv
* @apiUse RequestHeaders
*/
router.get(PATH_EXPORT, function getExport(request, response) {
//obtain request options
const options = _.merge({}, { select: { permissions: 0 } }, request.mquery);

// reply with csv export
response.attachment(`roles_exports_${Date.now()}.csv`);
response.status(200);
Role.exportCsv(options).pipe(response);
});


/**
* @api {post} /roles Create New Role
* @apiVersion 1.0.0
Expand Down
7 changes: 5 additions & 2 deletions lib/role.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const _ = require('lodash');
const { waterfall, parallel } = require('async');
const { abbreviate } = require('@lykmapipo/common');
const { getString, getStrings } = require('@lykmapipo/env');
const { Schema, SchemaTypes } = require('@lykmapipo/mongoose-common');
const { Schema, ObjectId } = require('@lykmapipo/mongoose-common');
const { model, SCHEMA_OPTIONS } = require('@lykmapipo/mongoose-common');
const actions = require('mongoose-rest-actions');
const exportable = require('@lykmapipo/mongoose-exportable');
const { Permission } = require('@lykmapipo/permission');
const { ObjectId } = SchemaTypes;


/* constants */
Expand Down Expand Up @@ -86,6 +86,7 @@ const RoleSchema = new Schema({
unique: true,
searchable: true,
taggable: true,
exportable: true,
fake: {
generator: 'hacker',
type: 'noun'
Expand Down Expand Up @@ -117,6 +118,7 @@ const RoleSchema = new Schema({
index: true,
searchable: true,
taggable: true,
exportable: true,
fake: {
generator: 'hacker',
type: 'abbreviation'
Expand Down Expand Up @@ -395,6 +397,7 @@ RoleSchema.statics.seed = function seed(seeds, done) {

/* plug mongoose rest actions*/
RoleSchema.plugin(actions);
RoleSchema.plugin(exportable);


/* export role model */
Expand Down
Loading

0 comments on commit ffcf10e

Please sign in to comment.