github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/routers/members.ts (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { Router } from 'express';
    16  import * as membersHandler from '../handlers/members';
    17  import { config } from '../lib/config';
    18  import RequestError from '../lib/request-handlers';
    19  import { constants } from '../lib/utils';
    20  
    21  const router = Router();
    22  
    23  router.get('/', async (req, res, next) => {
    24    try {
    25      const skip = Number(req.query.skip || 0);
    26      const limit = Number(req.query.limit || constants.DEFAULT_PAGINATION_LIMIT);
    27      if (isNaN(skip) || isNaN(limit)) {
    28        throw new RequestError('Invalid skip / limit', 400);
    29      }
    30      res.send(await membersHandler.handleGetMembersRequest({}, skip, limit));
    31    } catch (err) {
    32      next(err);
    33    }
    34  });
    35  
    36  router.get('/:memberAddress', async (req, res, next) => {
    37    try {
    38      res.send(await membersHandler.handleGetMemberRequest(req.params.memberAddress));
    39    } catch (err) {
    40      next(err)
    41    }
    42  });
    43  
    44  router.put('/', async (req, res, next) => {
    45    try {
    46      if (!req.body.address) {
    47        throw new RequestError('Missing member address', 400);
    48      }
    49      if (!req.body.name) {
    50        throw new RequestError('Missing member name', 400);
    51      }
    52      let assetTrailInstanceID, app2appDestination, docExchangeDestination;
    53      switch (config.protocol) {
    54        case 'corda':
    55          if (!req.body.assetTrailInstanceID) {
    56            throw new RequestError('Missing member assetTrailInstanceID', 400);
    57          }
    58          if (!req.body.app2appDestination) {
    59            throw new RequestError('Missing member app2appDestination', 400);
    60          }
    61          if (!req.body.docExchangeDestination) {
    62            throw new RequestError('Missing member docExchangeDestination', 400);
    63          }
    64          assetTrailInstanceID = req.body.assetTrailInstanceID;
    65          app2appDestination = req.body.app2appDestination;
    66          docExchangeDestination = req.body.docExchangeDestination;
    67          break;
    68        case 'ethereum':
    69          assetTrailInstanceID = config.assetTrailInstanceID;
    70          app2appDestination = config.app2app.destinations.kat;
    71          docExchangeDestination = config.docExchange.destination;
    72          break;
    73      }
    74      const sync = req.query.sync === 'true';
    75      await membersHandler.handleUpsertMemberRequest(req.body.address, req.body.name, assetTrailInstanceID, app2appDestination, docExchangeDestination, sync);
    76      res.send({ status: sync ? 'success' : 'submitted' });
    77    } catch (err) {
    78      next(err);
    79    }
    80  });
    81  
    82  export default router;