B2G/gaia/apps/communications/dialer/js/call_log_db.js
/**
   * Stores a new call in the database.
   *
   * param recentCall
   *        Object representing the new call to be stored with this form:
   *        { number: <String>,
   *          type: <String>,
   *          status: <String>,
   *          date: <Date>,
   *          emergency: <Bool>,
   *          voicemail: <Bool> }
   *
   * param callback
   *        Function to be called when the transaction is done.
   *
   * return (via callback) the object representing the group where the call
   *                       belongs to or an error message if needed.
   */
Stores a new call in the database.

param callback
Function to be called when the transaction is done.

return (via callback) the object representing the group where the call
belongs to or an error message if needed.
Stores a new call in the database . param callback Function to be called when the transaction is done . return ( via callback ) the object representing the group where the call belongs to or an error message if needed .
function add(recentCall, callback) {
    if (typeof recentCall !== 'object') {
      callback('INVALID_CALL');
      return;
    }
    var self = this;
    this._newTxn('readwrite', [this._dbRecentsStore, this._dbGroupsStore],
                 function(error, txn, stores) {
      if (error) {
        self._asyncReturn(callback, error);
        return;
      }
      var recentsStore = stores[0];
      var groupsStore = stores[1];
      var groupId = self._getGroupId(recentCall);
      groupsStore.get(groupId).onsuccess = function onsuccess() {
        var group = this.result;
        if (group) {
          if (group.lastEntryDate <= recentCall.date) {
            group.lastEntryDate = recentCall.date;
            group.emergency = recentCall.emergency;
            group.voicemail = recentCall.voicemail;
          }
          group.retryCount++;
          groupsStore.put(group).onsuccess = function onsuccess() {
            self._asyncReturn(callback, self._getGroupObject(group));
          };
        } else {
          group = {
            id: groupId,
            number: recentCall.number,
            lastEntryDate: recentCall.date,
            retryCount: 1,
            emergency: recentCall.emergency,
            voicemail: recentCall.voicemail
          };
          Contacts.findByNumber(recentCall.number,
                                function(contact, matchingTel) {
            if (contact && contact !== null) {
              group.contactId = contact.id;
              if (Array.isArray(contact.photo) && contact.photo[0]) {
                group.contactPhoto = contact.photo[0];
              }
              if (matchingTel) {
                var primaryInfo = Utils.getPhoneNumberPrimaryInfo(matchingTel,
                                                                  contact);
                if (Array.isArray(primaryInfo)) {
                  primaryInfo = primaryInfo[0];
                }
                group.contactPrimaryInfo = String(primaryInfo);
                if (Array.isArray(matchingTel.type) && matchingTel.type[0]) {
                  group.contactMatchingTelType = String(matchingTel.type[0]);
                } else {
                  group.contactMatchingTelType = String(matchingTel.type);
                }
                if (matchingTel.carrier) {
                  group.contactMatchingTelCarrier = String(matchingTel.carrier);
                }
              }
            }
            self._newTxn('readwrite', [self._dbGroupsStore],
                         function(error, txn, store) {
              store.add(group).onsuccess = function onsuccess() {
                self._keepDbPrettyAndFit(function() {
                  self._asyncReturn(callback, self._getGroupObject(group));
                });
              };
            });
          });
        }
        recentCall.groupId = groupId;
        recentsStore.put(recentCall);
      };
    });
  }
function add ( recentCall , callback ) { if ( typeof recentCall !== 'object' ) { callback ( 'INVALID_CALL' ) ; return ; } var self = this ; this . _newTxn ( 'readwrite' , [ this . _dbRecentsStore , this . _dbGroupsStore ] , function ( error , txn , stores ) { if ( error ) { self . _asyncReturn ( callback , error ) ; return ; } var recentsStore = stores [ 0 ] ; var groupsStore = stores [ 1 ] ; var groupId = self . _getGroupId ( recentCall ) ; groupsStore . get ( groupId ) . onsuccess = function onsuccess ( ) { var group = this . result ; if ( group ) { if ( group . lastEntryDate <= recentCall . date ) { group . lastEntryDate = recentCall . date ; group . emergency = recentCall . emergency ; group . voicemail = recentCall . voicemail ; } group . retryCount ++ ; groupsStore . put ( group ) . onsuccess = function onsuccess ( ) { self . _asyncReturn ( callback , self . _getGroupObject ( group ) ) ; } ; } else { group = { id : groupId , number : recentCall . number , lastEntryDate : recentCall . date , retryCount : 1 , emergency : recentCall . emergency , voicemail : recentCall . voicemail } ; Contacts . findByNumber ( recentCall . number , function ( contact , matchingTel ) { if ( contact && contact !== null ) { group . contactId = contact . id ; if ( Array . isArray ( contact . photo ) && contact . photo [ 0 ] ) { group . contactPhoto = contact . photo [ 0 ] ; } if ( matchingTel ) { var primaryInfo = Utils . getPhoneNumberPrimaryInfo ( matchingTel , contact ) ; if ( Array . isArray ( primaryInfo ) ) { primaryInfo = primaryInfo [ 0 ] ; } group . contactPrimaryInfo = String ( primaryInfo ) ; if ( Array . isArray ( matchingTel . type ) && matchingTel . type [ 0 ] ) { group . contactMatchingTelType = String ( matchingTel . type [ 0 ] ) ; } else { group . contactMatchingTelType = String ( matchingTel . type ) ; } if ( matchingTel . carrier ) { group . contactMatchingTelCarrier = String ( matchingTel . carrier ) ; } } } self . _newTxn ( 'readwrite' , [ self . _dbGroupsStore ] , function ( error , txn , store ) { store . add ( group ) . onsuccess = function onsuccess ( ) { self . _keepDbPrettyAndFit ( function ( ) { self . _asyncReturn ( callback , self . _getGroupObject ( group ) ) ; } ) ; } ; } ) ; } ) ; } recentCall . groupId = groupId ; recentsStore . put ( recentCall ) ; } ; } ) ; }
Stores a new call in the database.
Stores a new call in the database .
// For adding a call to the database we first create or update its // corresponding group and then we store the actual call adding the id // of the group where it belongs. // Groups should have the date of the newest call. // Once the group has successfully been added, we check that the // db size is below the max size set.