---
# System prepended metadata

title: Hudarsono Hu - System Design Interview

---

# Hudarsono Hu - System Design Interview

## Context
Users can have up to 4 documents:

ID front
ID back
Proof of address
Selfie
If all 4 documents are verified, user is verified else user is unverified.

User can resubmit for documents that have been rejected.

## API Endpoint Design
Design APIs for clients to query for user statuses 
Design APIs for clients to submit user documents


## Database Design
Design tables to store user documents

## Add On
- Use 3rd party to verify documents
- 3rd party takes up to 24 hours to process documents
- 3rd party sends callbacks once verification is completed

Task:
How should we integrate with 3rd party verification system

---

# Solution

Database
==================
users
---------
id(bigint)
email(varchar)
name(varchar)


user_documents
--------------
user_id(bigint)
type(tinyint)
url(text)
status(bool) ; default: false

3party_document_verification
---------------------------
user_id 
external_record_id
status 'FINISHED'


Endpoints
====================
POST /users/{hash_id}/upload
 - images 
 - hash_id(user_id) 
 - type

GET /users/{hash_id}/status
statuses = Users.find(id).user_documents.pluck(:status)
return verified/unverified


POST /users/{hash_id}/verify_document
return submited
queue


POST /3party/upload_documents       
   {
       'user_id':
       'documents': [
           {
               type: "ID_FRONT",
               file: file,
           }
       ]
   }
       
       
POST /3party_callback/complete_verification
    {
        user_id:
        record_id: 
        'documents': [
           {
               type: "ID_FRONT",
               status: "valid",
           }
       ]
        
    }
    
    
    