76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
// Application-Detail: AppId, zoneName, latitude, longitude, spray(on/off), targetRate, AppRate, temperature, humidity, gpsdatetime
|
|
const mongoose = require('mongoose'),
|
|
Schema = mongoose.Schema,
|
|
{ AssignStatus, UserTypes } = require('../helpers/constants');
|
|
|
|
const schema = new Schema({
|
|
job: { type: Number, ref: 'Job' },
|
|
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
|
|
|
// Partner-specific job information
|
|
notes: { type: String, required: false }, // Partner-specific notes or instructions
|
|
|
|
extJobId: {
|
|
type: String,
|
|
required: false,
|
|
index: true,
|
|
trim: true,
|
|
maxlength: 100,
|
|
description: 'External job ID generated by partner service or used to reference job in partner system'
|
|
},
|
|
|
|
status: {
|
|
type: Number,
|
|
enum: { values: Object.values(AssignStatus), default: AssignStatus.NEW }
|
|
},
|
|
date: { type: Date, required: false, default: Date.now }
|
|
});
|
|
|
|
// Static methods for common population patterns
|
|
schema.statics.populateWithPartnerInfo = function (query = {}, lean = false) {
|
|
const queryBuilder = this.find(query)
|
|
.populate({
|
|
path: 'user',
|
|
populate: {
|
|
path: 'partnerInfo.partner',
|
|
model: UserTypes.PARTNER
|
|
}
|
|
})
|
|
.populate('job');
|
|
|
|
return lean ? queryBuilder.lean() : queryBuilder;
|
|
};
|
|
|
|
schema.statics.findByIdWithPartnerInfo = function (assignIds) {
|
|
// Handle both single ID and array of IDs
|
|
const query = Array.isArray(assignIds)
|
|
? { _id: { $in: assignIds } }
|
|
: { _id: assignIds };
|
|
|
|
return this.find(query)
|
|
.populate({
|
|
path: 'user',
|
|
populate: {
|
|
path: 'partnerInfo.partner',
|
|
model: UserTypes.PARTNER
|
|
}
|
|
})
|
|
.populate('job');
|
|
};
|
|
|
|
// Instance methods
|
|
schema.methods.hasPartnerIntegration = function () {
|
|
return !!(this.user && this.user.partnerInfo && this.user.partnerInfo.partner);
|
|
};
|
|
|
|
schema.methods.getPartnerCode = function () {
|
|
return this.hasPartnerIntegration() ? this.user.partnerInfo.partner.partnerCode.toUpperCase() : null;
|
|
};
|
|
|
|
schema.methods.getPartnerAircraftId = function () {
|
|
return this.hasPartnerIntegration()
|
|
? (this.user.partnerInfo.partnerAircraftId || this.user.partnerInfo.tailNumber)
|
|
: null;
|
|
};
|
|
|
|
module.exports = mongoose.model('Job_Assign', schema); |