import mongooseUniqueValidator from "mongoose-unique-validator"; import {AppMaster} from "@onebro/appmaster"; import { DeepPartial, IModelCreator, IStatus, IQuery, mapSelectedData, getSchemaRefs, ObjectId} from "@onebro/oba-common"; import { SwiftTradersUserProfileConfig, SwiftTradersUserProfileModel, SwiftTradersUserProfile, SwiftTradersUserProfileJson, SwiftTradersUserProfilePreview, SwiftTradersUserProfileStatuses as statuses, SwiftTradersUserProfileStatus, SwiftTradersRoles, SwiftTradersUserProfileUpdateFollowersOrFollowing} from "../types"; import {swiftTradersUserProfileSchema as profile} from "../schemas"; const refs = getSchemaRefs(profile); export const swiftTradersUserProfileModels:IModelCreator = async m => { profile.plugin(mongooseUniqueValidator); profile.virtual("lastStatus").get(function(){return this.status?(this.status[this.status.length - 1] as any).json(statuses):null;}); profile.virtual("lastStatus").set(function(o:IStatus){this.status = [...this.status,o];}); profile.virtual("statusStr").get(function(){return this.lastStatus?this.lastStatus.name + " @ " + this.lastStatus.time:"";}); profile.virtual("lang").get(function(){return this.settings.lang;}); profile.virtual("lang").set(function(lang:string){this.settings.lang = lang;}); profile.statics._populate = async function(o?:SwiftTradersUserProfile){ await o.populate(refs).execPopulate(); return o;}; profile.statics._find = async function(o:Partial|string){ if(!o) return m.e.badinfo(); const p = typeof o == "string"? await SwiftTradersUserProfile.findById(o): await SwiftTradersUserProfile.findOne(SwiftTradersUserProfile.translateAliases(o)); if(!p) throw m.e.doesNotExist("profile"); return p;}; profile.statics._create = async function(o:SwiftTradersUserProfileConfig){return new SwiftTradersUserProfile({ ...o, img:o.img || "", settings:{...o.settings,permissions:[{name:"cookies",time:Date.now()}]}, //merge permissions correctly });}; profile.statics._update = async function(o:SwiftTradersUserProfile,p:DeepPartial & SwiftTradersUserProfileUpdateFollowersOrFollowing){ if(p.settings) p.settings = {...(o.json() as any).settings,...p.settings}; if(p.addFollowers) p.followers = [...o.followers,...p.addFollowers]; if(p.removeFollowers) for(let i=0,l=p.removeFollowers.length;i f.id.toString() !== p.removeFollowers[i]); if(p.addFollowing) p.following = [...o.following,...p.addFollowing]; if(p.removeFollowing) for(let i=0,l=p.removeFollowing.length;i f.id.toString() !== p.removeFollowing[i]); await o.set(p).save(); return await SwiftTradersUserProfile._populate(o);}; profile.statics._remove = async function(o:SwiftTradersUserProfile){return await o.remove().then(() => ({removed:o._id}));}; profile.statics._query = async function(o:IQuery){ const {query,populate,limit,skip,sort,select} = o; const results:SwiftTradersUserProfile[] = await this.find(this.translateAliases(query)) .populate(populate) //.where(where) .limit(limit) .skip(skip) .sort(sort) .exec(); const profiles = mapSelectedData(select,results); return {profiles};}; profile.methods.preview = function(){ const {id,username,img,role} = this as SwiftTradersUserProfile; const preview:SwiftTradersUserProfilePreview = {id,username,img,role:SwiftTradersRoles[role]}; return preview;}; profile.methods.json = function(){ const { id,created,updated,lastStatus:status,statusStr, username,role,settings,img,bio,motto,lang, followers,following,rating, expYrs,expLvl,socials} = this as SwiftTradersUserProfile; const json:SwiftTradersUserProfileJson = { id,created,updated,lastStatus:status,statusStr, username,role:SwiftTradersRoles[role], bio,motto,img,lang, settings:(settings as any).json(), rating,expLvl,expYrs, following:following.map(o => o.preview() as SwiftTradersUserProfilePreview), followers:followers.map(o => o.preview() as SwiftTradersUserProfilePreview), socials,memberSince:created as any, }; return json;}; const SwiftTradersUserProfile = m.db.model("onebrother","SwiftTradersUserProfile",profile); await SwiftTradersUserProfile.init(); return {SwiftTradersUserProfile}; };