Source

modules/DeveloperAccountManager.ts


import { UserAPI } from "../core/apis";
import { UserDataResponse, UserLastEvaluatedKey, UserType } from "../types/UserTypes";
import { PaginatedType } from "../types";
import { EnvTypes } from "../typesBundle";

/**
 * Create DeveloperAccountManager instance object
 * @class DeveloperAccountManager
 * @param {EnvTypes} env Environment types enum params (Ex: EnvTypes.DEV / EnvTypes.STAGING / EnvTypes.PREPROD / EnvTypes.PROD)
 */
export class DeveloperAccountManager {
  private userAPI: UserAPI;

  constructor(env: EnvTypes) {
    this.userAPI = new UserAPI(env);
  }


/**
   * @description Perform the retrieve user wallet by the Stark Key
   * @param {string} starkKey Stark Key of user in L2 system of Myria
   * @example <caption>Sample code</caption> 
   * 
    // Sample code on staging:

    const developerAccountManager = new DeveloperAccountManager(EnvTypes.STAGING);

    const starkKey = '0x.....';
    const userWalletData = await developerAccountManager.getUserWalletByStarkKey(starkKey);
    console.log('Testnet Data ->', userWalletData);

    // Sample code on Production:
    const developerAccountManager = new DeveloperAccountManager(EnvTypes.PRODUCTION);

    const starkKey = '0x.....';
    const userWalletData = await developerAccountManager.getUserWalletByStarkKey(starkKey);
    console.log('Production Data ->', userWalletData);

   * @returns {UserDataResponse | undefined} The details user data response for registration progress (including signature, stark key, wallet address)
   * @throws {string} Exception: Stark Key is required!
   * @throws {string} Http Status Code 404: User 0x... is not registered
   * @throws {string} Http Status Code 500: Get user data failed - unexpected with internal server error
   * @throws {string} Http Status Code 500: Internal Server Error with ${Exception}
   */
    public async getUserWalletByStarkKey(starkKey: string): Promise<UserDataResponse | undefined> {
      if (!starkKey) {
        throw new Error("Stark Key is required")
      }
  
      let res: UserDataResponse;
  
      try {
        const registerUserResponse = await this.userAPI.getUserByWalletAddress(starkKey);
        if (registerUserResponse?.status === 'success' && registerUserResponse?.data) {
          res = registerUserResponse?.data;
        } else {
          throw new Error('Get user data failed - unexpected with internal server error')
        }
      } catch (err: any) {
        throw new Error('Internal Server Error with ' + err);
      }
      return res;
    }
  
    /**
     * @description Perform the retrieve full user information by the Wallet address
     * @param {string} ethAddress The ether wallet address of user (such as Metamask wallet address)
     * @example <caption>Sample code</caption> 
     * 
      // Sample code on staging:
  
      const developerAccountManager = new DeveloperAccountManager(EnvTypes.STAGING);
  
      const ethWalletAddress = '0x.....';
      const userWalletData = await developerAccountManager.getUserInfoByWalletAddress(ethWalletAddress);
      console.log('Testnet Data ->', userWalletData);
  
      // Sample code on Production:
      const developerAccountManager = new DeveloperAccountManager(EnvTypes.PRODUCTION);
  
      const ethWalletAddress = '0x.....';
      const userWalletData = await developerAccountManager.getUserInfoByWalletAddress(ethWalletAddress);
      console.log('Production Data ->', userWalletData);
  
     * @returns {UserDataResponse | undefined} The details user data response for registration progress (including signature, stark key, wallet address)
     * @throws {string} Exception: Eth address is required!
     * @throws {string} Http Status Code 404: User 0x... is not registered
     * @throws {string} Http Status Code 500: Get user data failed - unexpected with internal server error
     * @throws {string} Http Status Code 500: Internal Server Error with ${Exception}
     */
    public async getUserInfoByWalletAddress(ethAddress: string): Promise<UserDataResponse | undefined> {
  
      if(!ethAddress) {
        throw new Error("Eth address is required!");
      }
  
      let res: UserDataResponse;
  
      try {
        const registerUserResponse = await this.userAPI.getUserByWalletAddress(ethAddress);
        if (registerUserResponse?.status === 'success' && registerUserResponse?.data) {
          res = registerUserResponse?.data;
        } else {
          throw new Error('Get user data failed - unexpected with internal server error')
        }
      } catch (err: any) {
        throw new Error('Internal Server Error with ' + err);
      }
      return res;
    }

  /**
    * @description Perform the retrieve full user information by the Wallet address
    * @param {UserType} userType Type of user references (CUSTOMER/PARTNER)
    * @param {string} referrerId Project ID/ReferrerID of referrer that end users have onboard to myria via invitation or games
    * @param {string?} limit Limit records per page for paging data
    * @param {UserLastEvaluatedKey?} lastEvaluatedKey Last evaluated key to support paging for users
    * @example <caption>Sample code</caption> 
    * 
    // Sample code on staging:
  
    const developerAccountManager = new DeveloperAccountManager(EnvTypes.STAGING);
  
    const partnerIdOfGameA = '10'; // Project game ID A
    const userWalletData = await developerAccountManager.getUserByReferrerId(UserType.PARTNER, partnerIdOfGameA);
    console.log('Testnet Data ->', userWalletData);
  
    // Sample code on Production:
    const developerAccountManager = new DeveloperAccountManager(EnvTypes.PRODUCTION);
  
    const partnerIdOfGameA = '15'; // Project game ID A
    const userWalletData = await developerAccountManager.getUserByReferrerId(UserType.PARTNER, partnerIdOfGameA);
    console.log('Production Data ->', userWalletData);
  
    * @returns {UserDataResponse | undefined} The details user data response for registration progress (including signature, stark key, wallet address)
    * @throws {string} Exception: Eth address is required!
    * @throws {string} Http Status Code 404: User 0x... is not registered
    * @throws {string} Http Status Code 500: Get user data failed - unexpected with internal server error
    * @throws {string} Http Status Code 500: Internal Server Error with ${Exception}
  */
  public async getUserByReferrerId (userType: UserType, referrerId: string, limit?: number, lastEvaluatedKey?: UserLastEvaluatedKey): Promise<PaginatedType<UserDataResponse>> {
    if(!userType) {
      throw new Error('User type is required!');
    }
    if(!referrerId) {
      throw new Error('ReferrerId is required!');
    }

    try {
      const result = await this.userAPI.getUserByReferrerIdAndType(userType.toString(), referrerId, limit, lastEvaluatedKey);
      if(result.status === 'success') {
        return result.data
      } else {
        throw new Error(`GetUserByReferrerId failed: ${result}`);
      }
    } catch(err: any) {
      throw new Error(`GetUserByReferrerId failed: ${err}`);
    }
  }
}