All files / src/entities Subject.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 4/4
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 364x               4x           4x             15x         19x   19x       13x      
import * as uuid from 'uuid'
 
/** A subject represents the person that's giving consent.
 *
 * A Subject might have two different ids. When he/she is not identified in the system, this SDK will generate
 * a random UUID to identify the current browser. Once the Subject is logged into the system, it can be
 * _converted_ to a verified Subject. A verified Subject has a custom id provided by the system.
 */
export class Subject {
  /** Builds a verified Subject.
   * @param id Custom id provided by the system.
   * @return the new Subject that uses the verified id.
   */
  static verified(id: string): Subject {
    return new Subject(id, undefined)
  }
 
  /** Builds an anonymous Subject and assigns a random UUID.
   * @return the new anonymous Subject.
   */
  static anonymous(id?: string): Subject {
    return new Subject(undefined, id || uuid.v4())
  }
 
  private constructor(
    /** Verified user identifier. */
    readonly verifiedId?: string,
    /** Random UUID generated by the SDK. */
    readonly anonymousId?: string
  ) {}
 
  get id(): string | undefined {
    return this.verifiedId || this.anonymousId
  }
}