import mongoose from 'mongoose';

import { ILoanChargeView, LoanCharge } from '../models/LoanCharges.model';
import { fieldsToUnset } from './loan-products.db';

export const getLoanChargeForProduct = async (productId: string): Promise<ILoanChargeView[]> => {
  return LoanCharge.aggregate<ILoanChargeView>([
    {
      $match: {
        'productId': new mongoose.Types.ObjectId(productId),
        'deletedAt': { $exists: false },
      },
    }, {
      $sort: {
        'order': 1,
      },
    }, {
      $unset: fieldsToUnset,
    },
  ]);
};
