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