processUpdate method

  1. @override
Future<RemoteDto?> processUpdate()
override

Implementation

@override
Future<RemoteDto?> processUpdate() async {
  FieldData? originalDto;
  RemoteDto? remoteDto = null;
  bool entryFound = true;
  bool doesCrcMatch = false;

  await _preProcessing();
  try {
    originalDto = await tableTransactions.find();
  } on SqlException catch (e) {
    if (e.sqlExceptionEnum == SqlExceptionEnum.ENTRY_NOT_FOUND) {
      entryFound = false;
      waterLine.setWaterError(WaterError.ID_NOT_FOUND);
      remoteDto = await commitChanges(null);
    } else if (e.sqlExceptionEnum == SqlExceptionEnum.FAILED_SELECT) {
      waterLine.setWaterError(WaterError.INVALID_ENTRY);
      remoteDto = await commitChanges(null);
    } else rethrow;
  }
  if (entryFound) {
    doesCrcMatch = super.doesCrcMatch(originalDto!.get("crc") as String);

    if (!doesCrcMatch && passedWaterState != WaterState.CLIENT_REJECTED) {
      waterLine.setWaterError(WaterError.CRC_NOT_MATCH);
      remoteDto = await commitChanges(null);
    } else {
      switch (passedWaterState) {
        case WaterState.CLIENT_STORED: // This is a item entered by ADMIN
          remoteDto = await super.writeUpdate(originalDto);
          try {
            await tableTransactions.updateWaterLineField();
          } on SqlException catch (e) {
            if (e.sqlExceptionEnum != SqlExceptionEnum.ENTRY_NOT_FOUND)
              rethrow;
          }
          break;
        case WaterState.CLIENT_APPROVED: // Approved by ADMIN
          remoteDto = await super.writeUpdate(originalDto);
          try {
            await tableTransactions.updateWaterLineField();
          } on SqlException catch (e) {
            if (e.sqlExceptionEnum != SqlExceptionEnum.ENTRY_NOT_FOUND)
              rethrow;
          }
          break;
        case WaterState.CLIENT_REJECTED: // Rejected by ADMIN
          waterLine.setWaterState(WaterState.SERVER_REJECTED);
          remoteDto = await commitChanges(null);
          break;
        default:
      }
    }
  }
  return remoteDto;
}