github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/routers/payment-definitions.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 RequestError from '../lib/request-handlers';
    17  import * as paymentDefinitionsHandler from '../handlers/payment-definitions';
    18  import { constants } from '../lib/utils';
    19  import * as utils from '../lib/utils';
    20  import { config } from '../lib/config';
    21  
    22  const router = Router();
    23  
    24  router.get('/', async (req, res, next) => {
    25    try {
    26      const skip = Number(req.query.skip || 0);
    27      const limit = Number(req.query.limit || constants.DEFAULT_PAGINATION_LIMIT);
    28      if (isNaN(skip) || isNaN(limit)) {
    29        throw new RequestError('Invalid skip / limit', 400);
    30      }
    31      res.send(await paymentDefinitionsHandler.handleGetPaymentDefinitionsRequest({}, skip, limit));
    32    } catch (err) {
    33      next(err);
    34    }
    35  });
    36  
    37  router.get('/:paymentDefinitionID', async (req, res, next) => {
    38    try {
    39      res.send(await paymentDefinitionsHandler.handleGetPaymentDefinitionRequest(req.params.paymentDefinitionID));
    40    } catch (err) {
    41      next(err);
    42    }
    43  });
    44  
    45  router.post('/search', async (req, res, next) => {
    46    try {
    47      const skip = Number(req.body.skip || 0);
    48      const limit = Number(req.body.limit || constants.DEFAULT_PAGINATION_LIMIT);
    49      if (req.body.count !== true && (isNaN(skip) || isNaN(limit))) {
    50        throw new RequestError('Invalid skip / limit', 400);
    51      }
    52      if (!req.body.query) {
    53        throw new RequestError('Missing search query', 400);
    54      }
    55      res.send(req.body.count === true ?
    56        await paymentDefinitionsHandler.handleCountPaymentDefinitionsRequest(req.body.query) :
    57        await paymentDefinitionsHandler.handleGetPaymentDefinitionsRequest(req.body.query, skip, limit)
    58      );
    59    } catch (err) {
    60      next(err);
    61    }
    62  });
    63  
    64  router.post('/', async (req, res, next) => {
    65    try {
    66      if (!req.body.name || req.body.name === '') {
    67        throw new RequestError('Missing or invalid payment definition name', 400);
    68      }
    69      if (!utils.isAuthorValid(req.body.author, config.protocol)) {
    70        throw new RequestError('Missing or invalid payment definition author', 400);
    71      }
    72      const sync = req.query.sync === 'true';
    73      const paymentDefinitionID = await paymentDefinitionsHandler.handleCreatePaymentDefinitionRequest(req.body.name,
    74        req.body.author, req.body.descriptionSchema, sync);
    75      res.send({ status: sync? 'success' : 'submitted', paymentDefinitionID });
    76    } catch (err) {
    77      next(err);
    78    }
    79  });
    80  
    81  export default router;