import mongooseUniqueValidator from "mongoose-unique-validator"; import {AppMaster} from "@onebro/appmaster"; import { DeepPartial, IModelCreator, IStatus, IQuery, mapSelectedData, getSchemaRefs} from "@onebro/oba-common"; import { FinavigatorUserProfileConfig, FinavigatorUserProfileModel, FinavigatorUserProfile, FinavigatorUserProfileJson, FinavigatorUserProfilePreview, FinavigatorUserProfileStatuses as statuses, FinavigatorUserProfileStatus, FinavigatorRoles, FinavigatorUserProfileUpdateFollowersOrFollowing} from "../types"; import {finavigatorUserProfileSchema as profile} from "../schemas"; const refs = getSchemaRefs(profile); export const finavigatorUserProfileModels: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?:FinavigatorUserProfile){ 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 FinavigatorUserProfile.findById(o): await FinavigatorUserProfile.findOne(FinavigatorUserProfile.translateAliases(o)); if(!p) throw m.e.doesNotExist("profile"); return p;}; profile.statics._create = async function(o:FinavigatorUserProfileConfig){return new FinavigatorUserProfile({ ...o, img:o.img || "", settings:{...o.settings,permissions:[{name:"cookies",time:Date.now()}]}, });}; profile.statics._update = async function(o:FinavigatorUserProfile,p:DeepPartial & FinavigatorUserProfileUpdateFollowersOrFollowing){ 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 FinavigatorUserProfile._populate(o);}; profile.statics._remove = async function(o:FinavigatorUserProfile){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:FinavigatorUserProfile[] = 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 FinavigatorUserProfile; const preview:FinavigatorUserProfilePreview = {id,username,img,role:FinavigatorRoles[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 FinavigatorUserProfile; const json:FinavigatorUserProfileJson = { id,created,updated,lastStatus:status,statusStr, username,role:FinavigatorRoles[role], bio,motto,img,lang, settings:(settings as any).json(), rating,expLvl,expYrs, following:following.map(o => o.preview() as FinavigatorUserProfilePreview), followers:followers.map(o => o.preview() as FinavigatorUserProfilePreview), socials,memberSince:created as any, }; return json;}; const FinavigatorUserProfile = m.db.model("onebrother","FinavigatorUserProfile",profile); await FinavigatorUserProfile.init(); return {FinavigatorUserProfile}; };