import mongooseUniqueValidator from "mongoose-unique-validator"; import {AppMaster} from "@onebro/appmaster"; import { DeepPartial, IModelCreator, IStatus, IQuery, mapSelectedData, getSchemaRefs, ObjectId} from "@onebro/oba-common"; import { FinavigatorAppointmentConfig, FinavigatorAppointmentModel, FinavigatorAppointment, FinavigatorAppointmentJson, FinavigatorAppointmentStatuses as statuses, FinavigatorAppointmentStatus, FinavigatorLeadJson, FinavigatorVenueJson, FinavigatorMessageJson} from "../types"; import {finavigatorAppointmentSchema as appt} from "../schemas"; const refs = getSchemaRefs(appt); export const finavigatorAppointmentModels:IModelCreator = async m => { appt.plugin(mongooseUniqueValidator); appt.virtual("lastStatus").get(function(){return this.status?(this.status[this.status.length - 1] as any).json(statuses):null;}); appt.virtual("lastStatus").set(function(o:IStatus){this.status = [...this.status,o];}); appt.virtual("statusStr").get(function(){return this.lastStatus?this.lastStatus.name + " @ " + this.lastStatus.time:"";}); appt.statics._populate = async function(o?:FinavigatorAppointment){ await o.populate(refs).execPopulate(); !o.populated("notes.author")?await o.populate("notes.author").execPopulate():null; return o;}; appt.statics._find = async function(o:DeepPartial|string){ if(!o) return m.e.badinfo(); const p = typeof o == "string"? await FinavigatorAppointment.findById(o): await FinavigatorAppointment.findOne(FinavigatorAppointment.translateAliases(o)); if(!p) throw m.e.doesNotExist("appointment"); return p;}; appt.statics._create = async function(o:FinavigatorAppointmentConfig){return new FinavigatorAppointment({ ...o, agent:new ObjectId(o.agent), client:new ObjectId(o.client), ...(o.lead?{lead:new ObjectId(o.lead)}:null), ...(o.venue?{venue:new ObjectId(o.venue)}:null), });}; appt.statics._update = async function(o:FinavigatorAppointment,p:DeepPartial & {note?:string;}){ if(p.note){p.notes = [...o.notes,new ObjectId(p.note)];delete p.note;} await o.set(p).save(); return await FinavigatorAppointment._populate(o);}; appt.statics._remove = async function(o:FinavigatorAppointment){return await o.remove().then(() => ({removed:o._id}));}; appt.statics._query = async function(o:IQuery){ const {query,populate,limit,skip,sort,select} = o; const results:FinavigatorAppointment[] = await this.find(this.translateAliases(query)) .populate(populate) //.where(where) .limit(limit) .skip(skip) .sort(sort) .exec(); const appts = mapSelectedData(select,results); return {appts};}; appt.statics._lookup = async function({path,query}:{path:string;query:any}){ const pipeline:any[] = []; return FinavigatorAppointment.aggregate(pipeline) .exec() .then((R:Partial<{[k in keyof (FinavigatorAppointment & FinavigatorAppointmentJson)]:any}>[]) => { return {appts:R.map(o => { if(o.status) o.status = { name:statuses[o.status.a0 as FinavigatorAppointmentStatus], time:o.status.a1, until:o.status.a2, info:o.status.a3}; delete o._id; return o;})}; });}; appt.methods.json = function(){ const { id,created,updated,lastStatus:status,statusStr, agent,client,date,time,type, venue,platform,notes,lead} = this as FinavigatorAppointment; const json:FinavigatorAppointmentJson = { id,created,updated,status,statusStr, agent:agent.preview(), client:client.preview(), date,time,type,platform, lead:lead?lead.json() as FinavigatorLeadJson:null, venue:venue?venue.json() as FinavigatorVenueJson:null, notes:notes.map(m => m.json() as FinavigatorMessageJson), }; return json;}; const FinavigatorAppointment = m.db.model("onebrother","FinavigatorAppointment",appt); await FinavigatorAppointment.init(); return {FinavigatorAppointment}; };