import { Model } from "mongoose"; import * as uuid from "uuid"; import { mongooseLeanVirtuals } from "mongoose-lean-virtuals"; import { currentDate, MongooseModule, } from "../helpers"; import { PartnerLocationFilter, PartnerLocationType, PartnerHeartbeatType, PartnerType, } from "../types/partner"; export interface Partner extends PartnerType, Record { } export default async function PartnerModule(mongoose: MongooseModule) { const { Schema } = mongoose; const PartnerLocationFilterSchema = new Schema({ type: { type: String, default: "", }, enabled: { type: Boolean, default: false, }, values: { type: [String], default: [], }, departmentId: { type: String, default: "", }, }, { _id: false, id: false, }); const PartnerLocation = new Schema({ enabled: { type: Boolean, default: false, }, departmentIds: { type: [String], default: [], }, includeVehicleStatus: { type: Boolean, default: false, }, speedAsBinary: { type: Boolean, default: false, }, useCadVisibility: { type: Boolean, default: false, }, filters: { type: [PartnerLocationFilterSchema], default: [], }, }, { _id: false, id: false, }); const PartnerHeartbeat = new Schema({ incidentEnabled: { type: Boolean, default: false, }, departmentIds: { type: [String], default: [], }, }, { _id: false, id: false, }); const modelSchema = new Schema({ _id: { type: Schema.Types.ObjectId, auto: true, }, name: { type: String, default: "", }, active: { type: Boolean, default: false, }, partnerKey: { type: String, default: "", select: false, }, uuid: { type: String, default: uuid.v4, }, location: { type: PartnerLocation, default: { enabled: false, departmentIds: [], includeVehicleStatus: false, speedAsBinary: false, useCadVisibility: false, filters: [], }, }, heartbeat: { type: PartnerHeartbeat, default: { incidentEnabled: false, }, }, modified: { type: Date, default: currentDate, }, created: { type: Date, default: currentDate, }, }, { autoIndex: false, timestamps: { createdAt: "created", updatedAt: "modified", }, toJSON: { virtuals: true, versionKey: false, } }); modelSchema.index({ partnerKey: 1, }, { name: "partnerKey_1_unique", unique: true, }); modelSchema.virtual("id").get(function(this: Partner) { return this._id.toHexString(); }); modelSchema.plugin(mongooseLeanVirtuals); return mongoose.model("Partner", modelSchema, "massive_partner", { overwriteModels: true }); } export interface PartnerModel extends Model { }