/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

import { SETTINGS_KEYS } from '../settings';

/**
 * Generic helper methods to handle usage of MasterLists which
 * are programs and program based requisitions and stock takes.
 */

/**
 * Finds all MasterList objects which are programs usable by this store.
 * A program: A master list which satisfies the following:
 * -- associated with this store,
 * -- masterList.isProgram = true,
 * -- has a store tag in programSettings, with a key which matches a tag
 * in this stores store tags
 * @param  {Object} settings
 * @param  {Realm}  database
 * @return {array}  MasterLists which are programs usable by this store.
 */
const getAllPrograms = (settings, database) => {
  const thisStoresNameID = settings.get(SETTINGS_KEYS.THIS_STORE_NAME_ID);
  const thisStoresTags = database
    .objects('NameTag')
    .filtered(
      'subquery(nameTagJoins, $joins, $joins.name.id == $0).@count > 0',
      settings.get(SETTINGS_KEYS.THIS_STORE_NAME_ID)
    )
    .map(({ description }) => description.toLowerCase());

  return database
    .objects('MasterListNameJoin')
    .filtered('name.id = $0 && masterList.isProgram = $1', thisStoresNameID, true)
    .filter(({ masterList }) => masterList.getStoreTagObject(thisStoresTags))
    .map(({ masterList }) => masterList);
};

/**
 * Finds all periods which are available for use for the current
 * store given the program, periodSchedule and orderType.
 * @param  {Realm}              database
 * @param  {Object/MasterList}  program             An object consisting of a programID
 * @param  {string}             periodScheduleName  the name of a periodSchedule
 * @param  {Object}             orderType           an order Type object
 * @return {array}
 */
const getAllPeriodsForProgram = (database, program, periodScheduleName, orderType) => {
  const periodScheduleNames = database
    .objects('PeriodSchedule')
    .filtered('name = $0', periodScheduleName);

  if (!periodScheduleNames.length) return [];

  return periodScheduleNames[0].getPeriodsForOrderType(program, orderType);
};

export { getAllPrograms, getAllPeriodsForProgram };

@@ -242,16 +242,996 @@
 s.%0A */%0A%0A
+/**%0A * Finds all MasterList objects which are programs usable by a passed customer.%0A * A program: A master list which satisfies the following:%0A * -- Is visible to the customer.%0A * -- masterList.isProgram = true,%0A * -- has a store tag in programSettings, with a key which matches a name_tag for this customer.%0A * @param  %7BObject%7D Customer%0A * @param  %7BRealm%7D  database%0A * @return %7Barray%7D  MasterLists which are programs usable by this customer.%0A */%0Aconst getAllProgramsForCustomer = (customer, database) =%3E %7B%0A  const %7B id %7D = customer;%0A  const customersTags = database%0A    .objects('NameTag')%0A    .filtered('subquery(nameTagJoins, $joins, $joins.name.id == $0).@count %3E 0', id)%0A    .map((%7B description %7D) =%3E description.toLowerCase());%0A%0A  return database%0A    .objects('MasterListNameJoin')%0A    .filtered('name.id = $0 && masterList.isProgram = $1', id, true)%0A    .filter((%7B masterList %7D) =%3E masterList.getStoreTagObject(customersTags))%0A    .map((%7B masterList %7D) =%3E masterList);%0A%7D;%0A%0A
 /**%0A * F
@@ -2826,16 +2826,26 @@
 rderType
+, customer
 ) =%3E %7B%0A 
@@ -3082,16 +3082,26 @@
 rderType
+, customer
 );%0A%7D;%0A%0Ae
@@ -3147,12 +3147,39 @@
 rProgram
+, getAllProgramsForCustomer
  %7D;%0A

Add byProgram utility method updates