import CustomerAPGroup from '../models/CustomerAPGroup.model';
import MappedGroup from '../models/MappedGroup.model';

export const getAPCustomerGroups = async (borrowerId) => {
  return CustomerAPGroup.aggregate([
    {
      $match: {
        'borrowerId': borrowerId
      }
    },
    {
      $unwind: {
        'path': '$items'
      }
    },
  ])
};


export const getMappedGroups = async ({ group1, group2 }: { group1?: string, group2?: string }) => {
  if (!group1 && !group2) {
    return Promise.resolve(null);
  }
  if (!group2) {
    return MappedGroup.aggregate([
      {
        $match: {
          'group1': group1
        }
      },
    ])
  }
  if (!group1) {
    return MappedGroup.aggregate([
      {
        $match: {
          'group2': group2
        }
      },
    ])
  }
  return MappedGroup.aggregate([
    {
      $match: {
        $and: [
          { 'group1': group1 },
          { 'group2': group2 },
        ]
      }
    },
  ])
};
