Yes @anon34731316 I checked but i receiving the below error when I tried to create smart relationship.
Cannot retrieve the orginal_customer_id value because of an internal error in the getter implementation: Customer.find is not a function
customers is a model.
orginal_customer_id is a smart relationship field.
const { collection } = require("forest-express-mongoose");
const logger = require("../config/logger")(module);
const { loanTransactions } = require("../models");
const AWS = require('aws-sdk');
const Customer = require('../models/onboarding/customers');
console.log("Printing Customer", Customer)
// This file allows you to add to your Forest UI:
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments
collection("loanTransactions", {
fields: [
{
field: 'orginal_customer_id',
type: 'String',
reference: 'customers._id',
get: function (transaction) {
return Customer.find({entity_id: transaction.customer_id});
}
}
],
actions: [
{
name: "Release Deal",
type: "single",
fields: [
{
field: "deal_name",
type: "String",
isRequired: true,
isReadOnly: true
},
{
field: "investor_ids",
isRequired: true,
reference: "entities._id",
type: ['String']
}
], hooks: {
load: async ({ fields, request }) => {
const deal_name = fields.find(
(field) => field.field === 'deal_name'
);
const id = request.body.data.attributes.ids[0];
const loanTransaction = await loanTransactions.findById(id);
deal_name.value = loanTransaction.deal_name;
return fields;
},
},
},
],
});
Customer schema
// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
module.exports = (mongoose, Mongoose) => {
// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = Mongoose.Schema({
'company_name': String,
'state': String,
'entity_id': { type: Mongoose.Schema.Types.ObjectId, ref: 'craEntities' },
'last_updated_by': { type: Mongoose.Schema.Types.ObjectId, ref: 'craUsers' },
'updated_at': Date,
'created_at': Date,
'created_by': String,
'esop': {
'esop_status': String,
'vesting_period': [Number],
},
}, {
timestamps: false,
});
return mongoose.model('customers', schema, 'customers');
};
Index.js
const fs = require('fs');
const path = require('path');
const Mongoose = require('mongoose');
const databasesConfiguration = require('../config/databases');
const connections = {};
const db = {};
databasesConfiguration.forEach((databaseInfo) => {
const connection = Mongoose.createConnection(databaseInfo.connection.url, databaseInfo.connection.options);
connections[databaseInfo.name] = connection;
const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
fs
.readdirSync(modelsDir)
.filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
.forEach((file) => {
try {
const model = require(path.join(modelsDir, file))(connection, Mongoose);
db[model.modelName] = model;
} catch (error) {
console.error(`Model creation error: ${error}`);
}
});
});
db.objectMapping = Mongoose;
db.connections = connections;
module.exports = db;