import LRU from 'lru-cache' ; import superagent from 'superagent' ; import { userSvcAuthority , UserSvcPing } from 'authorities' ; import { User } from 'entities/user' ; /**
 * manages data between this program and the user service
 */ export class UsersService { param username param playername param password returns public static async create ( username , playername , password , securityQuestion , securityAnswer ) { try { const res = await superagent . post ( `http://${ userSvcAuthority ( ) }/users/` ) . send ( { username , playername , password , security_question : securityQuestion , security_answer : securityAnswer } ) . accept ( 'json' ) ; if ( res . status !== 201 ) { return null ; } return res . body as User ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } param userId returns public static async get ( userId ) { try { const cachedUser = userCache . get ( userId ) ; if ( cachedUser != null ) { return cachedUser ; } if ( UserSvcPing . isAlive ( ) === false ) { return null ; } const res = await superagent . get ( `http://${ userSvcAuthority ( ) }/users/${ userId }` ) . accept ( 'json' ) ; if ( res . status !== 200 ) { return null ; } const result = res . body as User ; userCache . set ( result . id , result ) ; return result ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } param userName public static async getByName ( userName ) { try { if ( UserSvcPing . isAlive ( ) === false ) { return null ; } const res = await superagent . get ( `http://${ userSvcAuthority ( ) }/users/byname/${ userName }` ) . accept ( 'json' ) ; if ( res . status !== 200 ) { return null ; } const result = res . body as User ; userCache . set ( result . id , result ) ; return result ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } param userId returns public static async updatePassword ( userId , newPassword ) { try { const res = await superagent . put ( `http://${ userSvcAuthority ( ) }/users/${ userId }` ) . send ( { password : newPassword } ) . accept ( 'json' ) ; return res . status === 200 ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } param userId returns public static async delete ( userId ) { try { const res = await superagent . delete ( `http://${ userSvcAuthority ( ) }/users/${ userId }` ) . accept ( 'json' ) ; return res . status === 200 ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } param username param password returns public static async validate ( username , password ) { try { const res = await superagent . post ( `http://${ userSvcAuthority ( ) }/users/auth/validate` ) . send ( { username , password } ) . accept ( 'json' ) ; if ( res . status !== 200 ) { return null ; } const typedBody = res . body as { userId : number ; } ; return typedBody . userId ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } param username param securityAnswer returns public static async validateSecurityAnswer ( username , securityAnswer ) { try { const res = await superagent . post ( `http://${ userSvcAuthority ( ) }/users/auth/validate_security` ) . send ( { username , security_answer : securityAnswer } ) . accept ( 'json' ) ; if ( res . status !== 200 ) { return null ; } const typedBody = res . body as { userId : number ; } ; return typedBody . userId ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } public static async getSessions ( ) { try { const cachedNum = sessionNumCache . get ( 1 ) ; if ( cachedNum != null ) { return cachedNum ; } const res = await superagent . get ( `http://${ userSvcAuthority ( ) }/ping` ) . accept ( 'json' ) ; if ( res . ok === false ) { return 0 ; } const typedBody = res . body as { sessions : number ; } ; sessionNumCache . set ( 1 , typedBody . sessions ) ; return typedBody . sessions ; } catch ( error ) { await UserSvcPing . checkNow ( ) ; throw error ; } } param userId returns public static async createInventory ( userId ) { const res = await superagent . post ( `http://${ userSvcAuthority ( ) }/inventory/${ userId }` ) . accept ( 'json' ) ; return res . status === 201 ; } param userId returns public static async createCosmetics ( userId ) { const res = await superagent . post ( `http://${ userSvcAuthority ( ) }/inventory/${ userId }/cosmetics` ) . accept ( 'json' ) ; return res . status === 201 ; } param userId returns public static async createLoadouts ( userId ) { const res = await superagent . post ( `http://${ userSvcAuthority ( ) }/inventory/${ userId }/loadout` ) . accept ( 'json' ) ; return res . status === 201 ; } param userId returns public static async createBuymenu ( userId ) { const res = await superagent . post ( `http://${ userSvcAuthority ( ) }/inventory/${ userId }/buymenu` ) . accept ( 'json' ) ; return res . status === 201 ; } } const userCache = new LRU < number , User > ( { max : 100 , maxAge : 1000 * 15 } ) ; const sessionNumCache = new LRU < number , number > ( { max : 1 , maxAge : 1000 * 15 } ) ;