import { CustomerGroupModel } from '../models/CustomerGroup.model';

export const groupsMap = {
  customers: { Model: CustomerGroupModel },
  categories: { Model: CustomerGroupModel },
  vendors: { Model: CustomerGroupModel },
}

export const getBorrowerGroups = async (borrowerId: string, relationshipGroups: string | string[]) => {
  if (typeof relationshipGroups == 'string') {
    relationshipGroups = [relationshipGroups];
  }
  const allCustomerGroups = await Promise.all(relationshipGroups.map(async (groupName) => {
    const groups = await groupsMap[groupName].Model.find({ borrowerId }).sort({ 'createdAt': 1 });
    return { [groupName]: groups };
  }));
  return allCustomerGroups.reduce((acc, group) => {
    return { ...acc, ...group };
  }, {});
};
