import { assertExists } from '@ulangi/assert' ; import { SQLiteDatabase , Transaction } from '@ulangi/sqlite-adapter' ; import { ActionType , createAction } from '@ulangi/ulangi-action' ; import { UploadSetsRequest } from '@ulangi/ulangi-common/interfaces' ; import { UploadSetsResponseResolver } from '@ulangi/ulangi-common/resolvers' ; import { DirtySetModel , SessionModel } from '@ulangi/ulangi-local-database' ; import axios , { AxiosResponse } from 'axios' ; import { call , put } from 'redux-saga/effects' ; import { PromiseType } from 'utility-types' ; import { errorConverter } from '../converters/ErrorConverter' ; import { createRequest } from '../utils/createRequest' ; export class UploadSetSaga { private uploadSetsResponseResolver = new UploadSetsResponseResolver ( ) ; private userDb : SQLiteDatabase ; private sharedDb : SQLiteDatabase ; private sessionModel : SessionModel ; private dirtySetModel : DirtySetModel ; public constructor ( userDb , sharedDb , sessionModel , dirtySetModel ) { this . userDb = userDb ; this . sharedDb = sharedDb ; this . sessionModel = sessionModel ; this . dirtySetModel = dirtySetModel ; } public * uploadSets ( apiUrl , uploadLimit ) { let success , noMore ; try { const result = yield call ( [ this . dirtySetModel , 'getDirtySetsForSyncing' ] , this . userDb , uploadLimit , true ) ; const { setList , markSetsAsSynced } = result ; yield put ( createAction ( ActionType . SET__UPLOADING_SETS , { setList } ) ) ; const accessToken = yield call ( [ this . sessionModel , 'getAccessToken' ] , this . sharedDb ) ; noMore = setList . length === 0 ? true : false ; if ( noMore === false ) { const response = yield call ( [ axios , 'request' ] , createRequest < UploadSetsRequest > ( 'post' , apiUrl , '/upload-sets' , null , { setList , } , { accessToken : assertExists ( accessToken ) } ) ) ; const { acknowledged : syncedSetIds , } = this . uploadSetsResponseResolver . resolve ( response . data , true ) ; yield call ( [ this . userDb , 'transaction' ] , ( tx ) : void => { markSetsAsSynced ( tx , syncedSetIds ) ; } ) ; yield put ( createAction ( ActionType . SET__UPLOAD_SETS_SUCCEEDED , { noMore } ) ) ; } else { yield put ( createAction ( ActionType . SET__UPLOAD_SETS_SUCCEEDED , { noMore } ) ) ; } success = true ; } catch ( error ) { yield put ( createAction ( ActionType . SET__UPLOAD_SETS_FAILED , { errorCode : errorConverter . getErrorCode ( error ) , error , } ) ) ; success = false ; } return { success , noMore } ; } }