'use strict'; const mongoose = require('mongoose'), Schema = mongoose.Schema, User = require('./user'), { UserTypes, SyncStatus } = require('../helpers/constants'); // Partner Organization Schema (e.g., SatLoc, AgIDronex) const partnerSchema = new Schema({ // Partner-specific fields partnerCode: { type: String, required: true, trim: true, unique: true }, // e.g., "SATLOC", "AGIDRONEX" lastSyncAt: { type: Date, required: false }, syncStatus: { type: String, enum: [SyncStatus.ACTIVE, SyncStatus.ERROR, SyncStatus.INACTIVE], default: SyncStatus.INACTIVE } }, { strictQuery: false }); // Partner System User Schema (Customer/Applicator accounts in partner systems) // NOTE: Uses inherited 'parent' field from User model to link to the applicator/customer account // The 'customer' virtual is provided for backward API compatibility const partnerSystemUserSchema = new Schema({ // Links to AgMission entities partner: { type: Schema.Types.ObjectId, ref: 'User', required: true }, // Reference to Partner organization // NOTE: 'parent' field inherited from User model stores the customer/applicator reference // Partner system credentials partnerUserId: { type: String, required: false }, // User ID in partner system partnerUsername: { type: String, required: false }, // Username in partner system companyId: { type: String, required: false }, // Company ID in partner system // Access credentials (encrypted in production) apiKey: { type: String, required: false }, apiSecret: { type: String, required: false }, // Status and metadata - isActive inherited from base User model via 'active' field lastLoginAt: { type: Date, required: false }, lastSyncAt: { type: Date, required: false }, syncStatus: { type: String, enum: [SyncStatus.ACTIVE, SyncStatus.ERROR, SyncStatus.INACTIVE], default: SyncStatus.INACTIVE }, // Partner-specific metadata metadata: { type: Schema.Types.Mixed, required: false } }, { strictQuery: false, toJSON: { virtuals: true }, toObject: { virtuals: true } }); // Virtual 'customer' field for backward API compatibility - maps to 'parent' partnerSystemUserSchema.virtual('customer', { ref: 'User', localField: 'parent', foreignField: '_id', justOne: true }); // Also provide a getter for non-populated access partnerSystemUserSchema.virtual('customerId').get(function() { return this.parent; }); // Create discriminator models const Partner = User.discriminator(UserTypes.PARTNER, partnerSchema, { clone: false }); const PartnerSystemUser = User.discriminator(UserTypes.PARTNER_SYSTEM_USER, partnerSystemUserSchema, { clone: false }); module.exports = { Partner, PartnerSystemUser };