import mongooseUniqueValidator from "mongoose-unique-validator"; import {AppMaster} from "@onebro/appmaster"; import { DeepPartial, IModelCreator, IStatus, IQuery, mapSelectedData, getSchemaRefs, ObjectId} from "@onebro/oba-common"; import { FirstlineDistressReportConfig, FirstlineDistressReportModel, FirstlineDistressReport, FirstlineDistressReportJson, FirstlineDistressReportStatuses as statuses, FirstlineDistressReportStatus} from "../types"; import {firstlineDistressReportSchema as report} from "../schemas"; const refs = getSchemaRefs(report); export const firstlineDistressReportModels:IModelCreator = async m => { report.plugin(mongooseUniqueValidator); report.virtual("lastStatus").get(function(){return this.status?(this.status[this.status.length - 1] as any).json(statuses):null;}); report.virtual("lastStatus").set(function(o:IStatus){this.status = [...this.status,o];}); report.virtual("statusStr").get(function(){return this.lastStatus?this.lastStatus.name + " @ " + this.lastStatus.time:"";}); report.statics._populate = async function(o?:FirstlineDistressReport){ await o.populate(refs).execPopulate(); return o;}; report.statics._find = async function(o:Partial|string){ if(!o) return m.e.badinfo(); const p = typeof o == "string"? await FirstlineDistressReport.findById(o): await FirstlineDistressReport.findOne(FirstlineDistressReport.translateAliases(o)); if(!p) throw m.e.doesNotExist("report"); return p;}; report.statics._create = async function(o:FirstlineDistressReportConfig){return new FirstlineDistressReport({ ...o, author:new ObjectId(o.author), });}; report.statics._update = async function(o:FirstlineDistressReport,p:DeepPartial){ await o.set(p).save(); return await FirstlineDistressReport._populate(o);}; report.statics._remove = async function(o:FirstlineDistressReport){return await o.remove().then(() => ({removed:o._id}));}; report.statics._query = async function(o:IQuery){ const {query,populate,limit,skip,sort,select} = o; const results:FirstlineDistressReport[] = await this.find(this.translateAliases(query)) .populate(populate) //.where(where) .limit(limit) .skip(skip) .sort(sort) .exec(); const reports = mapSelectedData(select,results); return {reports};}; report.statics._lookup = async function({path,query}:{path:string;query:any}){ const pipeline:any[] = []; return FirstlineDistressReport.aggregate(pipeline) .exec() .then((R:Partial<{[k in keyof (FirstlineDistressReport & FirstlineDistressReportJson)]:any}>[]) => { return {reports:R.map(o => { if(o.status) o.status = { name:statuses[o.status.a0 as FirstlineDistressReportStatus], time:o.status.a1, until:o.status.a2, info:o.status.a3}; delete o._id; return o;})}; });}; report.methods.json = function(){ const { id,created,updated,lastStatus:status,statusStr, type,author,body,img,link,symbol,sendAt,subscribers} = this as FirstlineDistressReport; const json:FirstlineDistressReportJson = { id,created,updated,status,statusStr, type,author:author.preview(), body,img,link,symbol,sendAt, published:new Date(created), subscribers}; return json;}; const FirstlineDistressReport = m.db.model("onebrother","FirstlineDistressReport",report); await FirstlineDistressReport.init(); return {FirstlineDistressReport}; };