github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/handlers/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 * as database from '../clients/database';
    16  import * as apiGateway from '../clients/api-gateway';
    17  import * as utils from '../lib/utils';
    18  import { IDBBlockchainData, IDBMember, IEventMemberRegistered } from '../lib/interfaces';
    19  import RequestError from '../lib/request-handlers';
    20  import { config } from '../lib/config';
    21  
    22  export const handleGetMembersRequest = (query: object, skip: number, limit: number) => {
    23    return database.retrieveMembers(query, skip, limit);
    24  };
    25  
    26  export const handleGetMemberRequest = async (address: string) => {
    27    const member = await database.retrieveMemberByAddress(address);
    28    if (member === null) {
    29      throw new RequestError('Member not found', 404);
    30    }
    31    return member;
    32  };
    33  
    34  export const handleUpsertMemberRequest = async (address: string, name: string, assetTrailInstanceID: string,  app2appDestination: string, docExchangeDestination: string, sync: boolean) => {
    35    const timestamp = utils.getTimestamp();
    36    let memberDB: IDBMember = {
    37      address,
    38      name,
    39      assetTrailInstanceID,
    40      app2appDestination,
    41      docExchangeDestination,
    42      submitted: timestamp
    43    };
    44    if(config.protocol === 'ethereum') {
    45      const apiGatewayResponse = await apiGateway.upsertMember(address, name, app2appDestination, docExchangeDestination, sync);
    46      if(apiGatewayResponse.type === 'async') {
    47        memberDB.receipt = apiGatewayResponse.id
    48      }
    49    }
    50    await database.upsertMember(memberDB);
    51  };
    52  
    53  export const handleMemberRegisteredEvent = async ({ member, name, assetTrailInstanceID, app2appDestination, docExchangeDestination, timestamp }:
    54    IEventMemberRegistered, { blockNumber, transactionHash}: IDBBlockchainData) => {
    55    await database.upsertMember({
    56      address: member,
    57      name,
    58      app2appDestination,
    59      docExchangeDestination,
    60      assetTrailInstanceID,
    61      timestamp: Number(timestamp),
    62      blockNumber,
    63      transactionHash
    64    });
    65  };