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