github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/build-blockchain-insurance-app-master/web/www/routers/insurance.router.js (about)

     1  import express from 'express';
     2  
     3  import * as InsurancePeer from '../blockchain/insurancePeer';
     4  
     5  const router = express.Router();
     6  
     7  // Render main page
     8  router.get('/', (req, res) => {
     9    res.render('insurance-main', { insuranceActive: true });
    10  });
    11  
    12  // Claim Processing
    13  
    14  router.post('/api/claims', async (req, res) => {
    15    let { status } = req.body;
    16    if (typeof status === 'string' && status[0]) {
    17      status = status[0].toUpperCase();
    18    }
    19    try {
    20      let claims = await InsurancePeer.getClaims(status);
    21      res.json(claims);
    22    } catch (e) {
    23      res.json({ error: 'Error accessing blockchain.' });
    24    }
    25  });
    26  
    27  router.post('/api/process-claim', async (req, res) => {
    28    let { contractUuid, uuid, status, reimbursable } = req.body;
    29    if (typeof contractUuid !== 'string'
    30      || typeof uuid !== 'string'
    31      || !(typeof status === 'string' && status[0])
    32      || typeof reimbursable !== 'number') {
    33      res.json({ error: 'Invalid request.' });
    34      return;
    35    }
    36    status = status[0].toUpperCase();
    37  
    38    try {
    39      const success = await InsurancePeer.processClaim(
    40        contractUuid, uuid, status, reimbursable);
    41      res.json({ success });
    42    } catch (e) {
    43      res.json({ error: 'Error accessing blockchain.' });
    44    }
    45  });
    46  
    47  // Contract Management
    48  router.post('/api/contract-types', async (req, res) => {
    49    try {
    50      const contractTypes = await InsurancePeer.getContractTypes();
    51      res.json(contractTypes || []);
    52    } catch (e) {
    53      res.json({ error: 'Error accessing blockchain.' });
    54    }
    55  });
    56  
    57  router.post('/api/create-contract-type', async (req, res) => {
    58    let {
    59      shopType,
    60      formulaPerDay,
    61      maxSumInsured,
    62      theftInsured,
    63      description,
    64      conditions,
    65      minDurationDays,
    66      maxDurationDays,
    67      active
    68    } = req.body;
    69    if (!(typeof shopType === 'string' && shopType[0])
    70      || typeof formulaPerDay !== 'string'
    71      || typeof maxSumInsured !== 'number'
    72      || typeof theftInsured !== 'boolean'
    73      || typeof description !== 'string'
    74      || typeof conditions !== 'string'
    75      || typeof minDurationDays !== 'number'
    76      || typeof maxDurationDays !== 'number'
    77      || typeof active !== 'boolean') {
    78      res.json({ error: 'Invalid request.' });
    79      return;
    80    }
    81    shopType = shopType.toUpperCase();
    82  
    83    try {
    84      const uuid = await InsurancePeer.createContractType({
    85        shopType,
    86        formulaPerDay,
    87        maxSumInsured,
    88        theftInsured,
    89        description,
    90        conditions,
    91        minDurationDays,
    92        maxDurationDays,
    93        active
    94      });
    95      res.json({ success: true, uuid });
    96    } catch (e) {
    97      res.json({ error: 'Error accessing blockchain.' });
    98    }
    99  });
   100  
   101  router.post('/api/set-contract-type-active', async (req, res) => {
   102    const { uuid, active } = req.body;
   103    if (typeof uuid !== 'string'
   104      || typeof active !== 'boolean') {
   105      res.json({ error: 'Invalid request.' });
   106      return;
   107    }
   108    try {
   109      const success = await InsurancePeer.setActiveContractType(
   110        uuid, active);
   111      res.json({ success });
   112    } catch (e) {
   113      res.json({ error: 'Error accessing blockchain.' });
   114    }
   115  });
   116  
   117  // Self Service
   118  
   119  router.post('/api/contracts', async (req, res) => {
   120    if (typeof req.body.user !== 'object') {
   121      res.json({ error: 'Invalid request!' });
   122      return;
   123    }
   124  
   125    try {
   126      const { username, password } = req.body.user;
   127      if (await InsurancePeer.authenticateUser(username, password)) {
   128        const contracts = await InsurancePeer.getContracts(username);
   129        res.json({ success: true, contracts });
   130        return;
   131      } else {
   132        res.json({ error: 'Invalid login!' });
   133        return;
   134      }
   135    } catch (e) {
   136      console.log(e);
   137      res.json({ error: 'Error accessing blockchain!' });
   138      return;
   139    }
   140  });
   141  
   142  router.post('/api/file-claim', async (req, res) => {
   143    if (typeof req.body.user !== 'object' ||
   144      typeof req.body.contractUuid !== 'string' ||
   145      typeof req.body.claim != 'object') {
   146      res.json({ error: 'Invalid request!' });
   147      return;
   148    }
   149  
   150    try {
   151      const { user, contractUuid, claim } = req.body;
   152      const { username, password } = user;
   153      if (await InsurancePeer.authenticateUser(username, password)) {
   154        await InsurancePeer.fileClaim({
   155          contractUuid,
   156          date: new Date(),
   157          description: claim.description,
   158          isTheft: claim.isTheft
   159        });
   160        res.json({ success: true });
   161        return;
   162      } else {
   163        res.json({ error: 'Invalid login!' });
   164        return;
   165      }
   166    } catch (e) {
   167      console.log(e);
   168      res.json({ error: 'Error accessing blockchain!' });
   169      return;
   170    }
   171  });
   172  
   173  router.post('/api/authenticate-user', async (req, res) => {
   174    if (!typeof req.body.user === 'object') {
   175      res.json({ error: 'Invalid request!' });
   176      return;
   177    }
   178  
   179    try {
   180      const { username, password } = req.body.user;
   181      const success = await InsurancePeer.authenticateUser(username, password);
   182      res.json({ success });
   183      return;
   184    } catch (e) {
   185      console.log(e);
   186      res.json({ error: 'Error accessing blockchain!' });
   187      return;
   188    }
   189  });
   190  
   191  router.post('/api/blocks', async (req, res) => {
   192    const { noOfLastBlocks } = req.body;
   193    if (typeof noOfLastBlocks !== 'number') {
   194      res.json({ error: 'Invalid request' });
   195    }
   196    try {
   197      const blocks = await InsurancePeer.getBlocks(noOfLastBlocks);
   198      res.json(blocks);
   199    } catch (e) {
   200      res.json({ error: 'Error accessing blockchain.' });
   201    }
   202  });
   203  
   204  // Block explorer
   205  router.post('/api/blocks', async (req, res) => {
   206    const { noOfLastBlocks } = req.body;
   207    if (typeof noOfLastBlocks !== 'number') {
   208      res.json({ error: 'Invalid request' });
   209    }
   210    try {
   211      const blocks = await InsurancePeer.getBlocks(noOfLastBlocks);
   212      res.json(blocks);
   213    } catch (e) {
   214      res.json({ error: 'Error accessing blockchain.' });
   215    }
   216  });
   217  
   218  // Otherwise redirect to the main page
   219  router.get('*', (req, res) => {
   220    res.render('insurance', {
   221      insuranceActive: true,
   222      selfServiceActive: req.originalUrl.includes('self-service'),
   223      claimProcessingActive: req.originalUrl.includes('claim-processing'),
   224      contractManagementActive: req.originalUrl.includes('contract-management')
   225    });
   226  });
   227  
   228  function wsConfig(io) {
   229    InsurancePeer.on('block', block => {
   230      io.emit('block', block);
   231    });
   232  }
   233  
   234  export default router;
   235  export { wsConfig };