<commit_before>// const Rx = require('rxjs/Rx');
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const BasicMissionArtifact = require('../build/contracts/BasicMission.json');
const { getLatestMission, updateMission } = require('../store/missions');

class DavSDK {
  constructor() {
    let web3Provider = new Web3
      .providers
      .HttpProvider(process.env.ETH_NODE_URL);
    this.web3 = new Web3(web3Provider);
    this.BasicMissionContract = TruffleContract(BasicMissionArtifact);
    this.BasicMissionContract.setProvider(this.web3.currentProvider);
  }

  async init() {
    this.BasicMissionContractInstance = await this.BasicMissionContract.deployed();
    this.createMissionEvent = this.BasicMissionContractInstance.Create();
    // let signedMissionEvent = this.BasicMissionContractInstance.Signed();

    // Wrap Callback
    // this.createMissionObservable = Rx.Observable.create(observer => {
    //   createMissionEvent.watch(
    //     (error, response) => {
    //       if(error) {
    //         observer.error(error);
    //       } else {
    //         observer.next(response);
    //       }
    //     }
    //   );
    // });

    this.createMissionEvent.watch(
      async (error, response) => {
        if (error) {
          console.error(error);
        } else {
          // console.log(response);
          let contractId = response.args.id.toLowerCase();
          let userId = response.args.buyerId;
          let vehicleId = response.args.sellerId;
          // console.log(userId);
          let mission = null;
          while (!mission) {
            mission = await getLatestMission(userId);
            if (mission && mission.status !== 'awaiting_signatures' || mission.vehicle_id !== vehicleId) {
              mission = null;
            }
          }
          // console.log(mission);
          await updateMission(mission.mission_id, {
            'status': 'in_progress',
            'vehicle_signed_at': Date.now(),
            'contract_id': contractId
          });
        }
      }
    );
  }

  async dispose() {
    this.contractUpdates.unsubscribe();
  }
}

module.exports = new DavSDK();<commit_msg>fix: check if contract_id has been assigned to mission<commit_after>// const Rx = require('rxjs/Rx');
const Web3 = require('web3');
const TruffleContract = require('truffle-contract');
const BasicMissionArtifact = require('../build/contracts/BasicMission.json');
const { getLatestMission, updateMission } = require('../store/missions');

class DavSDK {
  constructor() {
    let web3Provider = new Web3
      .providers
      .HttpProvider(process.env.ETH_NODE_URL);
    this.web3 = new Web3(web3Provider);
    this.BasicMissionContract = TruffleContract(BasicMissionArtifact);
    this.BasicMissionContract.setProvider(this.web3.currentProvider);
  }

  async init() {
    this.BasicMissionContractInstance = await this.BasicMissionContract.deployed();
    this.createMissionEvent = this.BasicMissionContractInstance.Create();
    // let signedMissionEvent = this.BasicMissionContractInstance.Signed();

    // Wrap Callback
    // this.createMissionObservable = Rx.Observable.create(observer => {
    //   createMissionEvent.watch(
    //     (error, response) => {
    //       if(error) {
    //         observer.error(error);
    //       } else {
    //         observer.next(response);
    //       }
    //     }
    //   );
    // });

    this.createMissionEvent.watch(
      async (error, response) => {
        if (error) {
          console.error(error);
        } else {
          // console.log(response);
          let contractId = response.args.id.toLowerCase();
          let userId = response.args.buyerId;
          let vehicleId = response.args.sellerId;
          // console.log(userId);
          let mission = null;
          while (!mission) {
            mission = await getLatestMission(userId);
            if (mission) {
              if(mission.status !== 'awaiting_signatures' 
                || mission.vehicle_id !== vehicleId 
                || mission.contract_id ) {
                mission = null;
              }
            }
          }
          // console.log(mission);
          await updateMission(mission.mission_id, {
            'status': 'in_progress',
            'vehicle_signed_at': Date.now(),
            'contract_id': contractId
          });
        }
      }
    );
  }

  async dispose() {
    this.contractUpdates.unsubscribe();
  }
}

module.exports = new DavSDK();
{'fraction_non_alphanumeric': 0.12964601769911505, 'fraction_numerical': 0.0035398230088495575, 'mean_word_length': 2.179324894514768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4073232', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1301, 'n_words': 214}