ekyc

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

customer_info.ts (2459B)


      1 import {
      2   CustomerInfoRequest,
      3   CustomerInfoResponse,
      4   CustomerInfoUseCase,
      5 } from "#core/application/customer_info.ts";
      6 import { InvalidUUID, UUID } from "#core/domain/uuid.ts";
      7 import { Pool } from "$postgres";
      8 
      9 export class PostgresCustomerInfoAdapter implements CustomerInfoUseCase {
     10   constructor(private readonly pool: Pool) {
     11   }
     12 
     13   async execute(request: CustomerInfoRequest): Promise<CustomerInfoResponse> {
     14     const connection = await this.pool.connect();
     15     try {
     16       const uuid = new UUID(request.uuid);
     17       const result = await connection.queryObject<
     18         Omit<CustomerInfoResponse, "exists">
     19       >`
     20         select
     21           a."uuid",
     22           a."email",
     23           a."emailVerified",
     24           p."phoneNumber",
     25           p."phoneNumberVerified",
     26           i."firstName",
     27           i."lastName",
     28           i."birthDate",
     29           i."sex",
     30           i."nationality",
     31           i."country",
     32           (i."state" = 'approved') "idDocumentVerified",
     33           (i."state" != 'capturing') "idDocumentRegistered"
     34         from "auth" a
     35           left join "phone" p using (uuid)
     36           left join "id_document" i using (uuid)
     37         where a.uuid = ${uuid.toString()};
     38       `;
     39       const row = result.rows[0];
     40       return {
     41         exists: row !== undefined,
     42         uuid: row.uuid ?? null,
     43         email: row.email ?? null,
     44         emailVerified: row.emailVerified ?? false,
     45         phoneNumber: row.phoneNumber ?? null,
     46         phoneNumberVerified: row.phoneNumberVerified ?? false,
     47         firstName: row.firstName ?? null,
     48         lastName: row.lastName ?? null,
     49         birthDate: row.birthDate ?? null,
     50         sex: row.sex ?? null,
     51         nationality: row.nationality ?? null,
     52         country: row.country ?? null,
     53         idDocumentVerified: row.idDocumentVerified ?? false,
     54         idDocumentRegistered: row.idDocumentRegistered ?? false,
     55       };
     56     } catch (error) {
     57       if (error instanceof InvalidUUID) {
     58         return {
     59           exists: false,
     60           uuid: null,
     61           email: null,
     62           emailVerified: false,
     63           phoneNumber: null,
     64           phoneNumberVerified: false,
     65           firstName: null,
     66           lastName: null,
     67           birthDate: null,
     68           sex: null,
     69           nationality: null,
     70           country: null,
     71           idDocumentVerified: false,
     72           idDocumentRegistered: false,
     73         };
     74       }
     75       throw error;
     76     } finally {
     77       connection.release();
     78     }
     79   }
     80 }