import mongooseUniqueValidator from "mongoose-unique-validator"; import {AppMaster} from "@onebro/appmaster"; import { DeepPartial, IModelCreator, IStatus, IQuery, mapSelectedData, getSchemaRefs, ObjectId} from "@onebro/oba-common"; import { SwiftTradersDistressReportConfig, SwiftTradersDistressReportModel, SwiftTradersDistressReport, SwiftTradersDistressReportJson, SwiftTradersDistressReportStatuses as statuses, SwiftTradersDistressReportStatus} from "../types"; import {swiftTradersDistressReportSchema as report} from "../schemas"; const refs = getSchemaRefs(report); export const swiftTradersDistressReportModels: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?:SwiftTradersDistressReport){ 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 SwiftTradersDistressReport.findById(o): await SwiftTradersDistressReport.findOne(SwiftTradersDistressReport.translateAliases(o)); if(!p) throw m.e.doesNotExist("report"); return p;}; report.statics._create = async function(o:SwiftTradersDistressReportConfig){return new SwiftTradersDistressReport({ ...o, author:new ObjectId(o.author), });}; report.statics._update = async function(o:SwiftTradersDistressReport,p:DeepPartial){ await o.set(p).save(); return await SwiftTradersDistressReport._populate(o);}; report.statics._remove = async function(o:SwiftTradersDistressReport){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:SwiftTradersDistressReport[] = 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 SwiftTradersDistressReport.aggregate(pipeline) .exec() .then((R:Partial<{[k in keyof (SwiftTradersDistressReport & SwiftTradersDistressReportJson)]:any}>[]) => { return {reports:R.map(o => { if(o.status) o.status = { name:statuses[o.status.a0 as SwiftTradersDistressReportStatus], 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 SwiftTradersDistressReport; const json:SwiftTradersDistressReportJson = { id,created,updated,status,statusStr, type,author:author.preview(), body,img,link,symbol,sendAt, published:new Date(created), subscribers}; return json;}; const SwiftTradersDistressReport = m.db.model("onebrother","SwiftTradersDistressReport",report); await SwiftTradersDistressReport.init(); return {SwiftTradersDistressReport}; };