daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Apr 15th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as yup from 'yup';
  2. import { ChaincodeTx } from '@worldsibu/convector-platform-fabric';
  3. import {
  4.   Controller,
  5.   ConvectorController,
  6.   Invokable,
  7.   Param,
  8.   FlatConvectorModel
  9. } from '@worldsibu/convector-core';
  10. import { Participant } from 'participant-cc';
  11.  
  12. import { Person, Attribute } from './person.model';
  13.  
  14. @Controller('person')
  15. export class PersonController extends ConvectorController<ChaincodeTx> {
  16.   @Invokable()
  17.   public async create(
  18.     @Param(Person)
  19.     person: Person
  20.   ) {
  21.     let exists = await Person.getOne(person.id);
  22.     if (!!exists && exists.id) {
  23.       throw new Error('There is a person registered with that Id already');
  24.     }
  25.     let gov = await Participant.getOne('gov');
  26.  
  27.     if (!gov || !gov.identities) {
  28.       throw new Error('No government identity has been registered yet');
  29.     }
  30.     const govActiveIdentity = gov.identities.find(identity => identity.status === true);
  31.  
  32.     if (!govActiveIdentity) {
  33.       throw new Error('No active identity found for participant');
  34.     }
  35.     if (this.sender !== govActiveIdentity.fingerprint) {
  36.       throw new Error(`Just the government - ID=gov - can create people - requesting organization was ${this.sender}`);
  37.     }
  38.  
  39.     await person.save();
  40.   }
  41.   @Invokable()
  42.   public async addAttribute(
  43.     @Param(yup.string())
  44.     personId: string,
  45.     @Param(Attribute.schema())
  46.     attribute: Attribute
  47.   ) {
  48.     // Check if the "stated" participant as certifier of the attribute is actually the one making the request
  49.     let participant = await Participant.getOne(attribute.certifierID);
  50.  
  51.     if (!participant || !participant.identities) {
  52.       throw new Error(`No participant found with id ${attribute.certifierID}`);
  53.     }
  54.  
  55.     const participantActiveIdentity = participant.identities.find(
  56.       identity => identity.status === true);
  57.  
  58.     if (!participantActiveIdentity) {
  59.       throw new Error('No active identity found for participant');
  60.     }
  61.  
  62.     if (this.sender !== participantActiveIdentity.fingerprint) {
  63.       throw new Error(`Requester identity cannot sign with the current certificate ${this.sender}. This means that the user requesting the tx and the user set in the param certifierId do not match`);
  64.     }
  65.  
  66.     let person = await Person.getOne(personId);
  67.  
  68.     if (!person || !person.id) {
  69.       throw new Error(`No person found with id ${personId}`);
  70.     }
  71.  
  72.     if (!person.attributes) {
  73.       person.attributes = [];
  74.     }
  75.  
  76.     let exists = person.attributes.find(attr => attr.id === attribute.id);
  77.  
  78.     if (!!exists) {
  79.       let attributeOwner = await Participant.getOne(exists.certifierID);
  80.       let attributeOwnerActiveIdentity = attributeOwner.identities.find(
  81.         identity => identity.status === true);
  82.  
  83.       // Already has one, let's see if the requester has permissions to update it
  84.       if (this.sender !== attributeOwnerActiveIdentity.fingerprint) {
  85.         throw new Error(`User already has an attribute for ${attribute.id} but current identity cannot update it`);
  86.       }
  87.       // update as is the right attribute certifier
  88.       exists = attribute;
  89.     } else {
  90.       person.attributes.push(attribute);
  91.     }
  92.  
  93.     await person.save();
  94.   }
  95.  
  96.   @Invokable()
  97.   public async get(
  98.     @Param(yup.string())
  99.     id: string
  100.   ) {
  101.     const existing = await Person.getOne(id);
  102.     if (!existing || !existing.id) {
  103.       throw new Error(`No person exists with that ID ${id}`);
  104.     }
  105.     return existing;
  106.   }
  107.  
  108.   @Invokable()
  109.   public async getAll(): Promise<FlatConvectorModel<Person>[]> {
  110.     return (await Person.getAll('io.worldsibu.person')).map(person => person.toJSON() as any);
  111.   }
  112.  
  113.   @Invokable()
  114.   public async getByAttribute(
  115.     @Param(yup.string())
  116.     id: string,
  117.     @Param(yup.mixed())
  118.     value: any
  119.   ) {
  120.  
  121.     return await Person.query(Person, {
  122.       'selector': {
  123.         'attributes': {
  124.           '$elemMatch': {
  125.             'id': id,
  126.             'content': value
  127.           }
  128.         }
  129.       }
  130.     });
  131.   }
  132. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top