retryFailedWebReq method

Future<List<DfResponse>> retryFailedWebReq(
  1. {List<String> onlineSyncFailTrxIds}
)

Implementation

Future<List<DfResponse>> retryFailedWebReq({List<String> onlineSyncFailTrxIds}) async {
  if (! await DfWebRepository.isInternet()) {
    return [DfResponse(error: OfflineException())];
  }
  await _initWebFailRepository();
  WebReq entWebReq = WebReq();
  WebReqField fieldWebReq = WebReqField.createFieldBasedOnRepo(_webFailRepository);
  List<DfBaseField> selFields = entWebReq.fields();

  List<DfQueryCriteria> criterias = [];
  criterias.add(DfQueryCriteria(field: fieldWebReq.fSuccess, oper: OperType.eq, values: [DfConverter.boolToInt(false)] ));
  if (onlineSyncFailTrxIds != null && onlineSyncFailTrxIds.length > 0) {
    criterias.add(DfQueryCriteria(field: fieldWebReq.fId, oper: OperType.inClause, values: onlineSyncFailTrxIds ));
  }
  DfQueryOrderBy orderBy = DfQueryOrderBy(field: fieldWebReq.fCreatedAt, orderByType: OrderByType.asc);

  DfQuery query = DfQuery(purpose: DfQueryPurpose.FETCH,
                          queryFields: selFields,
                          entityJoin: DfQueryEntityJoin(entities: [entWebReq]),
                          criteriaGroups: DfQueryCriteriaGroups(queryGroups: [DfQueryCriteriaGroup(criterias: criterias)]),
                          orderBy: [orderBy]);

  List<WebReqField> dbWebReqs = List<WebReqField>();

  List<DfBaseEntity> updWebReqSuccess = List<DfBaseEntity>();
  List<DfBaseEntity> insWebReqStatus = List<DfBaseEntity>();

  DfResponse resp = await _webFailRepository.fetch(query);
  if (resp.error != null) {
    print("FetchFailWebReq Error: ${resp.error.message}");
    return [DfResponse(error: resp.error)];
  }
  else {
    List<Map<String,dynamic>> resultSet = resp.result;
    resultSet.forEach((result) {
      DfBaseEntity newWebReq = entWebReq.newInstance();
      newWebReq.fromMap(result);
      WebReqField f = newWebReq as WebReqField;
      dbWebReqs.add(f);
    });

    List<DfResponse> dfResponses = List<DfResponse>();
    for (var dbWebReq in dbWebReqs) {
      NUIWeb nuiweb = await _buildWebRef(dbWebReq.fServerUrl.value, DfConverter.jsonStringToDict(dbWebReq.fCommonHeaders.value));

      String url = "${dbWebReq.fContextPath.value}";
      List<String> pathVariables = DfConverter.stringToList(dbWebReq.fPathVariables.value);
      if (pathVariables != null) {
        pathVariables.forEach((p) {
          if (!url.endsWith("/")) {
            url = "$url/";
          }
          url = "$url$p";
        });
      }

      DfResponse dfResponse;
      switch (dbWebReq.fHttpMethod.value) {
        case DFQUERYPURPOSE_FETCH_NAME :
          NUIHttpResponse nuiResp = await nuiweb.doGET(url: url, params: DfConverter.jsonStringToDict(dbWebReq.fReqParams.value), headers: DfConverter.jsonStringToDict(dbWebReq.fHeaders.value) );
          dfResponse = DfResponse();
          dfResponse.dataFromHttpResponse(nuiResp);
          break;

        case DFQUERYPURPOSE_ADD_NAME :
          NUIHttpResponse nuiResp = await nuiweb.doPOST(url: url, body: DfConverter.jsonStringToDict(dbWebReq.fReqBody.value), headers: DfConverter.jsonStringToDict(dbWebReq.fHeaders.value) );
          dfResponse = DfResponse();
          dfResponse.dataFromHttpResponse(nuiResp);
          break;

        case DFQUERYPURPOSE_DELETE :
          NUIHttpResponse nuiResp = await nuiweb.doDELETE(url: url, headers: DfConverter.jsonStringToDict(dbWebReq.fHeaders.value) );
          dfResponse = DfResponse();
          dfResponse.dataFromHttpResponse(nuiResp);
          break;

        case DFQUERYPURPOSE_UPDATE_WHOLE_ENTITY_ATTRS_NAME :
          NUIHttpResponse nuiResp = await nuiweb.doPUT(url: url, body: DfConverter.jsonStringToDict(dbWebReq.fReqBody.value), headers: DfConverter.jsonStringToDict(dbWebReq.fHeaders.value) );
          dfResponse = DfResponse();
          dfResponse.dataFromHttpResponse(nuiResp);
          break;

        case DFQUERYPURPOSE_UPDATE_SOME_ENTITY_ATTRS_NAME :
          NUIHttpResponse nuiResp = await nuiweb.doPATCH(url: url, body: DfConverter.jsonStringToDict(dbWebReq.fReqBody.value), headers: DfConverter.jsonStringToDict(dbWebReq.fHeaders.value) );
          dfResponse = DfResponse();
          dfResponse.dataFromHttpResponse(nuiResp);
          break;

        default :
          break;
      } //switch
      if (dfResponse != null) {
        dfResponses.add(dfResponse);

        WebReqField webReqObj = WebReqField.createFieldBasedOnRepo(_webFailRepository);
        webReqObj.fId.value = dbWebReq.fId.value;
        webReqObj.fSuccess.value = DfConverter.boolToInt(dfResponse.error == null);
        updWebReqSuccess.add(webReqObj as DfBaseEntity);

        WebReqExeField webReqExeObj = WebReqExeField.creatFieldBasedOnRepo(_webFailRepository);
        Uuid exeId = Uuid();
        webReqExeObj.fId.value = exeId.v4();
        webReqExeObj.fCreatedAt.value = DfConverter.dateToString(DateTime.now());
        webReqExeObj.fWebReqId.value = dbWebReq.fId.value;
        webReqExeObj.fErrorCode.value = resp.error.code;
        webReqExeObj.fErrorMessage.value = resp.error.message;
        webReqExeObj.fResponse.value = DfConverter.arrDictToJsonString(dfResponse.result);
        insWebReqStatus.add(webReqExeObj as DfBaseEntity);

      } //end if (dfResponse != null)

    } //for loop

    if (updWebReqSuccess.length > 0) {
      List<DfQuery> webReqUpdQueries = List<DfQuery>();
      updWebReqSuccess.forEach((upd) {
        DfQuery query = DfQuery(purpose: DfQueryPurpose.UPDATE_SOME_ENTITY_ATTRS, queryFields: upd.toMap(), entityJoin: DfQueryEntityJoin(entities: [WebReq()])  );
        webReqUpdQueries.add(query);
      });
      await _webFailRepository.save(webReqUpdQueries);

      List<DfQuery> webReqExeInsQueries = List<DfQuery>();
      insWebReqStatus.forEach((ins) {
        DfQuery query = DfQuery(purpose: DfQueryPurpose.ADD, queryFields: ins.toMap(), entityJoin: DfQueryEntityJoin(entities: [WebReqExe()])  );
        webReqExeInsQueries.add(query);
      });
      await _webFailRepository.save(webReqExeInsQueries);
    }
    return dfResponses;
  }//end else - db fetch no error
}