import LRUCache from 'lru-cache' ; import superagent from 'superagent' ; import { UserSvcPing } from 'authorities' ; import { User } from 'user/user' ; export class UserService { private static baseUrl : string ; private static userCache : LRUCache < number , User > ; public static Init ( baseUrl ) { this . baseUrl = baseUrl ; this . userCache = new LRUCache < number , User > ( { max : 100 , maxAge : 1000 * 15 } ) ; } param userId returns public static async Login ( username , password ) { if ( UserSvcPing . isAlive ( ) === false ) { return 0 ; } try { const res = await superagent . post ( this . baseUrl + '/users/auth/login' ) . send ( { username , password } ) . accept ( 'json' ) ; if ( res . status === 200 ) { const typedBody = res . body as { userId : number ; } ; return typedBody . userId ; } } catch ( error ) { const typedError = error as { status : number ; } ; if ( typedError . status === 401 ) { return - 1 ; } console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } return 0 ; } param userId returns public static async Logout ( userId ) { if ( UserSvcPing . isAlive ( ) === false ) { return false ; } try { const res = await superagent . post ( this . baseUrl + '/users/auth/logout' ) . send ( { userId } ) . accept ( 'json' ) ; if ( res . status === 200 ) { return true ; } } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } return false ; } param userId returns public static async GetUserById ( userId ) { try { let user = this . userCache . get ( userId ) ; if ( user != null ) { return user ; } if ( UserSvcPing . isAlive ( ) === false ) { return null ; } const res = await superagent . get ( this . baseUrl + `/users/${ userId }` ) . accept ( 'json' ) ; if ( res . status === 200 ) { user = new User ( ) ; Object . assign ( user , res . body ) ; this . userCache . set ( user . id , user ) ; return user ; } return null ; } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; return null ; } } param targetUser returns public static async Update ( targetUser ) { try { const res = await superagent . put ( this . baseUrl + `/users/${ targetUser . id }` ) . send ( targetUser ) . accept ( 'json' ) ; if ( res . status === 200 ) { this . userCache . set ( targetUser . id , targetUser ) ; console . log ( 'Set buy menu successfully' ) ; return true ; } } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } return false ; } param updatedMembers returns public static async UpdatePartial ( updatedMembers , targetUserId ) { try { const res = await superagent . put ( this . baseUrl + `/users/${ targetUserId }` ) . send ( updatedMembers ) . accept ( 'json' ) ; if ( res . status === 200 ) { if ( this . userCache . has ( targetUserId ) === true ) { const cachedUser = this . userCache . get ( targetUserId ) ; Object . assign ( cachedUser , updatedMembers ) ; this . userCache . set ( targetUserId , cachedUser ) ; } console . log ( 'Updated user partially successfully' ) ; return true ; } } catch ( error ) { console . error ( error ) ; await UserSvcPing . checkNow ( ) ; } return false ; } }