github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/routers/payment-instances.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 paymentInstancesHandler from '../handlers/payment-instances';
    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 paymentInstancesHandler.handleGetPaymentInstancesRequest({}, req.body.sort || {}, skip, limit));
    32    } catch (err) {
    33      next(err);
    34    }
    35  });
    36  
    37  router.get('/:assetInstanceID', async (req, res, next) => {
    38    try {
    39      res.send(await paymentInstancesHandler.handleGetPaymentInstanceRequest(req.params.assetInstanceID));
    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 paymentInstancesHandler.handleCountPaymentInstancesRequest(req.body.query) :
    57        await paymentInstancesHandler.handleGetPaymentInstancesRequest(req.body.query, req.body.sort || {}, 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.paymentDefinitionID) {
    67        throw new RequestError('Missing payment definition ID', 400);
    68      }
    69      if (!utils.isAuthorValid(req.body.author, config.protocol)) {
    70        throw new RequestError('Missing or invalid payment author', 400);
    71      }
    72      if (!utils.isAuthorValid(req.body.member, config.protocol)) {
    73        throw new RequestError('Missing or invalid payment member', 400);
    74      }
    75      if (req.body.author === req.body.member) {
    76        throw new RequestError('Author and member cannot be the same', 400);
    77      }
    78      if (!(Number.isInteger(req.body.amount) && req.body.amount > 0)) {
    79        throw new RequestError('Missing or invalid payment amount', 400);
    80      }
    81      const sync = req.query.sync === 'true';
    82      const paymentInstanceID = await paymentInstancesHandler.handleCreatePaymentInstanceRequest(req.body.author, req.body.paymentDefinitionID, req.body.member, req.body.description, req.body.amount, req.body.participants, sync);
    83      res.send({ status: sync? 'success' : 'submitted', paymentInstanceID });
    84    } catch (err) {
    85      next(err);
    86    }
    87  
    88  });
    89  
    90  export default router;