ekyc

Electronic KYC process with uploading ID document using OAuth 2.1 (experimental)
Log | Files | Refs | README | LICENSE

id_document.ts (2792B)


      1 import { MRZInfo } from "#core/application/id_document/mrzscan.ts";
      2 import { Admin } from "#core/domain/admin.ts";
      3 import { DomainError } from "#core/domain/error.ts";
      4 import { Picture } from "#core/domain/picture.ts";
      5 import { UUID } from "#core/domain/uuid.ts";
      6 
      7 export enum IDDocumentState {
      8   CAPTURING,
      9   REGISTERED,
     10   APPROVED,
     11   DECLINED,
     12 }
     13 
     14 export class IDDocument {
     15   constructor(
     16     readonly uuid: UUID,
     17     private _state: IDDocumentState = IDDocumentState.CAPTURING,
     18     private _info: MRZInfo | null = null,
     19     private _front: Picture | null = null,
     20     private _back: Picture | null = null,
     21     private _faceLeft: Picture | null = null,
     22     private _faceFront: Picture | null = null,
     23     private _faceRight: Picture | null = null,
     24     private _admin: Admin | null = null,
     25     public version: number = 0,
     26   ) {}
     27 
     28   get front() {
     29     return this._front;
     30   }
     31 
     32   get back() {
     33     return this._back;
     34   }
     35 
     36   get faceLeft() {
     37     return this._faceLeft;
     38   }
     39 
     40   get faceFront() {
     41     return this._faceFront;
     42   }
     43 
     44   get faceRight() {
     45     return this._faceRight;
     46   }
     47 
     48   get info() {
     49     return this._info;
     50   }
     51 
     52   get admin() {
     53     return this._admin;
     54   }
     55 
     56   get state() {
     57     return this._state;
     58   }
     59 
     60   capture(
     61     caption:
     62       | "doc-back"
     63       | "doc-front"
     64       | "face-left"
     65       | "face-front"
     66       | "face-right",
     67     picture: Picture,
     68     info: MRZInfo | null = null,
     69   ) {
     70     if (this.state === IDDocumentState.DECLINED) {
     71       this._state = IDDocumentState.CAPTURING;
     72     }
     73 
     74     if (this.state !== IDDocumentState.CAPTURING) {
     75       throw new IDDocumentOutOfState();
     76     }
     77 
     78     switch (caption) {
     79       case "doc-front":
     80         this._front = picture;
     81         break;
     82 
     83       case "doc-back":
     84         this._back = picture;
     85         this._info = info;
     86         break;
     87 
     88       case "face-left":
     89         this._faceLeft = picture;
     90         break;
     91 
     92       case "face-front":
     93         this._faceFront = picture;
     94         break;
     95 
     96       case "face-right":
     97         this._faceRight = picture;
     98         break;
     99     }
    100 
    101     if (
    102       this.info !== null &&
    103       this.back !== null &&
    104       this.front !== null &&
    105       this.faceLeft !== null &&
    106       this.faceFront !== null &&
    107       this.faceRight !== null
    108     ) {
    109       this._state = IDDocumentState.REGISTERED;
    110     }
    111   }
    112 
    113   approve(admin: Admin) {
    114     if (this.state !== IDDocumentState.REGISTERED) {
    115       throw new IDDocumentOutOfState();
    116     }
    117     this._state = IDDocumentState.APPROVED;
    118     this._admin = admin;
    119   }
    120 
    121   decline(admin: Admin) {
    122     if (this.state !== IDDocumentState.REGISTERED) {
    123       throw new IDDocumentOutOfState();
    124     }
    125     this._state = IDDocumentState.DECLINED;
    126     this._admin = admin;
    127   }
    128 }
    129 
    130 export class IDDocumentOutOfState extends DomainError {
    131   constructor() {
    132     super("Out of state capturing");
    133   }
    134 }