Source: nodeServer/api/dao/notificationDao.js

const Dao = require('./dao');

module.exports = class NotificationDao extends Dao {
  /**
   * Method that gets all  the unique device ID (expoTokens) from the DB for the
   * wanted notificationType.
   * @param {string} notificationType Type of notification you want to extract from DB.
   * @param {function} callback Function callback
   */
  getAll(notificationType, callback) {
    super.query(
      'SELECT expoToken FROM notificationTable WHERE notificationType=?',
      [notificationType],
      callback
    );
  }

  /**
   * Method to get one users notificationSettings after he logs into the app.
   * @param {object} json JSON object containing associationType and expoToken
   * @param {function} callback Function callback
   */
  getSettings(json, callback) {
    console.log('GetSettings()');
    const val = [json.associationType, json.expoToken];
    super.query(
      'SELECT notificationTable.notificationType, notificationDescription.description, notificationDescription.label, notificationTable.associationType, notificationTable.notify FROM notificationDescription JOIN notificationTable ON notificationDescription.notificationType = notificationTable.notificationType AND  notificationDescription.associationType = notificationTable.associationType WHERE notificationTable.associationType = ? AND expoToken = ?',
      val,
      callback
    );
  }

  /**
   * Method to insert a user into the notificationTable. With his association type and notification type.
   * @param {object} json JSON object containing expoToken, notificationType and associationType
   * @param {function} callback Function callback
   */
  insert(json, callback) {
    const val = [json.expoToken, json.notificationType, json.associationType];
    super.query(
      'INSERT INTO notificationTable (expoToken, notificationType, associationType) VALUES(?,?,?)',
      val,
      callback
    );
  }

  /**
   * Method to get all the notificationTypes from the wanted associationType.
   * This method was created to extract all the available notifications for either "BANK"
   * or "DEPOSITOR" from the DB.
   * When the user logs in the server gets all the notificationTypes for that user and
   * then adds him to the DB.
   * @param {string} associationType The users association type.
   * @param {function} callback Function callback
   */
  getNotificationTypes(associationType, callback) {
    super.query(
      'SELECT notificationType FROM notificationDescription WHERE associationType=?',
      [associationType],
      callback
    );
  }

  /**
   * Method to update a users subscription to a notificationType.
   * Triggered when the user changes value on a switch in notificationSettings.
   * @param {object} json JSON object containing newValue, expoToken, notificationType, associationType
   * @param {function} callback Function callback
   */
  update(json, callback) {
    const val = [json.newValue, json.expoToken, json.notificationType, json.associationType];
    super.query(
      'UPDATE notificationTable SET notify=? WHERE expoToken=? AND notificationType=? AND associationType=?',
      val,
      callback
    );
  }

  /**
   * Method to check if the user is already in the database.
   * Checks if the user is in the DB with the current expoToken and associationType.
   * Enables the user to use multiple devices.
   * @param {object} json JSON object containing expoToken and associationType.
   * @param {function} callback Function callback
   */
  checkExist(json, callback) {
    console.log('checkExist()');
    const val = [json.expoToken, json.associationType];
    super.query(
      'SELECT EXISTS (SELECT * FROM notificationTable WHERE expoToken = ? AND associationType = ?) AS exist',
      val,
      callback
    );
  }
};