ekyc

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

personal_phone_number.ts (1094B)


      1 import { assert } from "$std/assert/assert.ts";
      2 import libphone from "google-libphonenumber";
      3 import { DomainError } from "#core/domain/error.ts";
      4 const { PhoneNumberUtil, PhoneNumberType: Type, PhoneNumberFormat: Format } =
      5   libphone;
      6 
      7 const phoneUtil = PhoneNumberUtil.getInstance();
      8 const ACCEPTED_TYPE = [
      9   Type.MOBILE,
     10   Type.PERSONAL_NUMBER,
     11 ];
     12 
     13 export class PersonalPhoneNumber {
     14   readonly value: string;
     15 
     16   constructor(value: string) {
     17     try {
     18       const number = phoneUtil.parse(value, "ch");
     19       assert(
     20         phoneUtil.isValidNumberForRegion(number, "ch"),
     21         "only swiss phone",
     22       );
     23       const type = phoneUtil.getNumberType(number);
     24       assert(ACCEPTED_TYPE.includes(type), "only personal phone");
     25       this.value = phoneUtil.format(number, Format.INTERNATIONAL);
     26     } catch (cause) {
     27       throw new InvalidPersonalPhoneNumber({ cause });
     28     }
     29   }
     30 
     31   toString() {
     32     return this.value;
     33   }
     34 }
     35 
     36 export class InvalidPersonalPhoneNumber extends DomainError {
     37   constructor(options: ErrorOptions) {
     38     super("Invalid personal phone number", options);
     39   }
     40 }