const mongoose = require('../db/connect') const { Schema } = mongoose const crmContactSchema = new Schema({ companyId: { type: String, required: true, }, crm: { type: mongoose.Schema.ObjectId, ref: 'crm', required: true }, crmBaseURL: { type: String, required: true, }, contactId: { type: String, required: true, }, phone: { type: String, required: true, } }, { timestamps: true }) crmContactSchema.virtual('crm_tickets', { ref: 'CRM_Ticket', localField: '_id', foreignField: 'contact', justOne: false, // match: { rating: 4 } }) crmContactSchema.pre('deleteOne', { document: true, query: false }, async function () { await this.model('CRM_Ticket').deleteMany({ contact: this._id }) } ) const CRM_Contact = mongoose.model('CRM_Contact', crmContactSchema) module.exports = CRM_Contact