

const _ = require('lodash');
const auth = require('../lib/authentications');
const Joi = require('joi');
const handlerFactory = require('./handlers.js');

exports.register = function (server, eOptions, next) {
  const options = _.extend({ basePath: '/api/2.0' }, eOptions);
  const handlers = handlerFactory(server, 'cd-organisations');

  server.route([{
    method: 'GET',
    path: `${options.basePath}/organisations`,
    handler: handlers.actHandler('list', null, null, { ctrl: 'org' }),
    config: {
      description: 'List all possible organisations',
      tags: ['api', 'users'],
    },
  }, {
    method: 'GET',
    path: `${options.basePath}/organisations/{id}`,
    handler: handlers.actHandler('load', ['id'], null, { ctrl: 'org' }),
    config: {
      description: 'Get organisation by id',
      tags: ['api', 'users'],
      validate: {
        params: {
          id: Joi.string().required(),
        },
      },
    },
  }, {
    method: 'POST',
    path: `${options.basePath}/organisations`,
    handler: handlers.actHandlerNeedsUser('create', null, null, { ctrl: 'org' }),
    config: {
      auth: auth.apiUser,
      description: 'Create organisation',
      tags: ['api', 'users'],
      validate: {
        payload: {
          org: {
            name: Joi.string().required(),
          },
        },
      },
    },
  }, {
    method: 'PUT',
    path: `${options.basePath}/organisation/{id}`,
    handler: handlers.actHandlerNeedsUser('update', ['id'], null, { ctrl: 'org' }),
    config: {
      auth: auth.apiUser,
      description: 'Accept badge',
      tags: ['api', 'users'],
      validate: {
        payload: {
          org: {
            name: Joi.string().required(),
          },
        },
      },
    },
  }, {
    method: 'GET',
    path: `${options.basePath}/organisations/{orgId}/users`,
    handler: handlers.actHandlerNeedsUser('list', ['orgId'], null, { ctrl: 'userOrg' }),
    config: {
      auth: auth.apiUser,
      description: 'Load organisation\'s users',
      tags: ['api', 'users'],
      validate: {
        params: {
          orgId: Joi.string().required(),
        },
      },
    },
  }, { method: 'GET',
    path: `${options.basePath}/user/{userId}/organisations`,
    handler: handlers.actHandlerNeedsUser('list', ['userId'], null, { ctrl: 'userOrg' }),
    config: {
      auth: auth.apiUser,
      description: 'Load user\'s organisation\'s',
      tags: ['api', 'users'],
      validate: {
        params: {
          userId: Joi.string().guid().required(),
        },
      },
    },
  }, {
    method: 'POST',
    path: `${options.basePath}/users/organisations`,
    handler: handlers.actHandlerNeedsUser('list', null, null, { ctrl: 'userOrg' }),
    config: {
      auth: auth.apiUser,
      description: 'Load organisation\'s users',
      tags: ['api', 'users'],
      validate: {
        payload: {
          userIds: Joi.array().items(Joi.string().guid()),
        },
      },
    },
  }, {
    method: 'POST',
    path: `${options.basePath}/organisations/{orgId}/users`,
    handler: handlers.actHandlerNeedsUser('create', ['orgId'], null, { ctrl: 'userOrg' }),
    config: {
      auth: auth.apiUser,
      description: 'Create organisation member',
      tags: ['api', 'users'],
      validate: {
        payload: {
          userOrg: {
            userId: Joi.string().required(),
            orgId: Joi.string().required(),
          },
        },
      },
    },
  }, {
    method: 'DELETE',
    path: `${options.basePath}/organisations/{orgId}/users/{userId}`,
    handler: handlers.actHandlerNeedsUser('delete', ['orgId', 'userId'], null, { ctrl: 'userOrg' }),
    config: {
      auth: auth.apiUser,
      description: 'Delete organisation member',
      tags: ['api', 'users'],
      validate: {
        params: {
          userId: Joi.string().required(),
          orgId: Joi.string().required(),
        },
      },
    },
  }]);

  next();
};

exports.register.attributes = {
  name: 'api-orgs',
  dependencies: 'cd-auth',
};

@@ -1348,411 +1348,10 @@
   %7D,
+%0A
  
-%7B%0A    method: 'PUT',%0A    path: %60$%7Boptions.basePath%7D/organisation/%7Bid%7D%60,%0A    handler: handlers.actHandlerNeedsUser('update', %5B'id'%5D, null, %7B ctrl: 'org' %7D),%0A    config: %7B%0A      auth: auth.apiUser,%0A      description: 'Accept badge',%0A      tags: %5B'api', 'users'%5D,%0A      validate: %7B%0A        payload: %7B%0A          org: %7B%0A            name: Joi.string().required(),%0A          %7D,%0A        %7D,%0A      %7D,%0A    %7D,%0A  %7D,
  %7B%0A 

Remove unexisting endpoint (#1289)