"use strict" ; import * as fs from 'fs' ; import csvSync from 'csv-parse/lib/sync' ; import * as configsys from '../src/webpack/config' ; import { AxiosInstance } from 'axios' ; import axios from 'axios' ; import { Patient , PatientParam , Center , StatusParam } from '../src/lambda/definitions/types' ; interface LoadedStatus { patientId : string ; created : string ; temperature : string ; pulse : string ; spo2 : string ; } export class Importer { private config : any ; private loadedIDs : string [ ] = [ ] ; private api : APIcaller ; private userinfo : { username : string ; password : string ; } ; constructor ( stage , userinfo ) { this . config = this . readConfig ( configsys , stage ) ; this . api = new APIcaller ( this . config ) ; this . userinfo = userinfo ; } param data returns async importData ( data ) { await this . api . login ( this . userinfo . username , this . userinfo . password ) ; const center = await this . api . getCenter ( ) ; if ( ! center . centerId ) { throw new Error ( 'getting center ID failed' ) ; } console . log ( `# importing patients to center ${ center . centerName }` ) ; for ( let i = 0 , len = data . length ; i < len ; i ++ ) { const item = data [ i ] ; const status = { patientId : item [ 0 ] , created : item [ 1 ] , temperature : item [ 2 ] , pulse : item [ 3 ] , spo2 : item [ 4 ] } ; console . log ( `load ${ status . patientId }, ${ status . created }` ) ; if ( this . loadedIDs . indexOf ( status . patientId ) === - 1 ) { const data = await this . api . putPatient ( center . centerId , status . patientId ) ; if ( ! data . patientId ) { throw new Error ( 'creating patient failed' ) ; } else { this . loadedIDs . push ( data . patientId ) ; } } await this . api . putStatus ( status ) ; } return new Promise ( ( resolve ) => { resolve ( 'ok' ) ; } ) ; } readConfig ( configsys , stage ) { return ( configsys . default as configsys . Config [ ] ) . find ( ( item ) => item . apiGateway . stageName === stage ) as configsys . Config ; } } export class DataLoader { loadFile ( filepath ) { const data = fs . readFileSync ( filepath ) ; const matrix = csvSync ( data ) ; return matrix ; } } export class APIcaller { private entry_point : string ; private axios_admin : AxiosInstance ; constructor ( config ) { this . entry_point = `https://${ config . apiGateway . restApiId }.execute-api.${ config . region }.amazonaws.com/${ config . apiGateway . stageName }` ; this . axios_admin = axios . create ( ) ; } param username param password async login ( username , password ) { console . log ( '# login' ) ; const ret = await axios . post ( this . entry_point + '/api/admin/login' , { username : username , password : password } ) ; if ( ! ret . data . idToken ) { throw new Error ( 'login failed' ) ; } const idToken = ret . data . idToken ; console . log ( '# logged in' ) ; this . axios_admin = axios . create ( { headers : { Authorization : idToken , } , } ) ; } async getCenter ( ) { console . log ( '# getCenter' ) ; if ( ! this . axios_admin ) { throw new Error ( 'you need login before calling this API' ) ; } console . log ( `${ this . entry_point }/api/admin/nurses/testNurse` ) ; const ret = await this . axios_admin . get ( `${ this . entry_point }/api/admin/nurses/testNurse` ) ; return ret . data . manageCenters [ 0 ] ; } param patientId returns async putPatient ( centerId , patientId ) { console . log ( '# put patient' ) ; if ( ! this . axios_admin ) { throw new Error ( 'you need login before calling this API' ) ; } console . log ( `${ this . entry_point }/api/admin/patients/${ patientId }` ) ; let needdelete ; const param = { patientId : patientId , phone : '03-1239-333' + patientId , memo : '患者' + patientId , display : true , statuses : [ ] , centerId : centerId , policy_accepted : new Date ( ) . toISOString ( ) } ; try { const ret = await this . axios_admin . get ( `${ this . entry_point }/api/admin/patients/${ patientId }` ) ; if ( ret . data ) { needdelete = true ; } else { needdelete = false ; } } catch ( err ) { needdelete = false ; } if ( needdelete ) { const ret3 = await this . axios_admin . put ( `${ this . entry_point }/api/admin/patients/${ patientId }` , param ) ; return ret3 . data ; } else { const ret3 = await this . axios_admin . post ( `${ this . entry_point }/api/admin/centers/${ centerId }/patients` , param ) ; return ret3 . data ; } } param status async putStatus ( status ) { console . log ( '# put status' ) ; if ( ! this . axios_admin ) { throw new Error ( 'you need login before calling this API' ) ; } status . created = '2021/' + status . created . replace ( 'AM' , '09:00' ) . replace ( 'PM' , "21:00" ) ; const param = { SpO2 : parseInt ( status . spo2 ) , body_temperature : parseInt ( status . temperature ) , pulse : parseInt ( status . pulse ) , created : new Date ( status . created ) . toISOString ( ) , symptom : { cough : false , phlegm : false , suffocation : false , headache : false , sore_throat : false , malaise : false , nausea : false , diarrhea : false , difficulty_eating : false , no_urination : false , remarks : "" } } ; await this . axios_admin . post ( this . entry_point + `/api/admin/patients/${ status . patientId }/statuses` , param ) ; } } const main = ( ) => { const stage = process . argv [ 3 ] || 'dev' ; const buffer = fs . readFileSync ( './.secret.json' ) ; const admin = JSON . parse ( buffer . toString ( ) ) ; const importer = new Importer ( stage , { username : admin . auth_user , password : admin . auth_pass } ) ; const loader = new DataLoader ( ) ; importer . importData ( loader . loadFile ( 'sampledata.csv' ) ) . then ( ( value ) => { console . log ( value ) ; } ) . catch ( ( error ) => { console . log ( error ) ; } ) ; } ; main ( ) ;