import OBACore from "@onebro/oba-core"; import {ModelController} from "@onebro/oba-mongoose"; import {UserRoles,UserProfileSignature} from "../00-model-types"; import {ModelFactories} from "../01-model-factories"; export interface UserProfileController extends ModelController {} export class UserProfileController extends ModelController { constructor(public api:OBACore,public factories:ModelFactories){ const profiles = factories["profiles"]; super(api as any); this.badStatuses = ["Deleted"]; this.privileges = ["use-api"]; this.adminRoles = ["ADMIN","SUPER","_SA_"]; this.create$ = async ({body:newObj,appuser:{okto}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await profiles.shouldNotExist({name:newObj.name})) .then(async () => await profiles.create(newObj)) .then(o => ({data:o.json()})); }; this.fetch$ = async ({params,appuser:{okto}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await profiles.fetch(this.serializeFetch(params))) .then(o => ({data:o.json()})); }; this.update$ = async ({params:{id},body:updates,appuser:{okto}}:any) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await profiles.update(id,updates)) .then(o => ({data:o.json()})); }; this.remove$ = async ({params:{id},appuser:{name,okto,role}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await profiles.update(id,{$set:{ status:{name:"Deleted",time:new Date(),info:{user:name,role}} }})) .then(o => ({data:o.json()})); }; this.remove$$ = async ({params:{id,admin},appuser:{okto,role}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(() => this.isAdmin(role)) .then(async () => await profiles.remove(id))//activity line .then(o => ({data:o.json()})); }; this.query$ = async ({query,appuser:{okto}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await profiles.query(this.parseQueryObj(query))) .then(o => ({data:{results:o}})); }; this.search$ = async ({query:{text},appuser:{okto}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await profiles.search(text)) .then(o => ({data:{results:o}})); }; } } /*async updateMany$ = async ({body:{ids,updates}}:{body:{ids:string[];updates:DeepPartial}}) => await Promise.resolve() //.then(() => {if(okto !== "auth") throw factories.e.unAuth("api privileges");}) //.then(() => {if(role !== "Admin") throw factories.e.unAuth("api privileges");}) .then(async () => { if(ids.length){ const arr:OBAuthAcct[] = []; for(let i = 0,l = ids.length;i { for(let i = 0,l = ids.length;i ({updated:n} as any)), async removeMany$ = async ({body:{ids}}:{body:{ids:string[]}}) => await Promise.resolve() //.then(() => {if(okto !== "auth") throw factories.e.unAuth("api privileges");}) //.then(() => {if(role !== "Admin") throw factories.e.unAuth("api privileges");}) .then(async () => { if(ids.length){ const arr:OBAuthAcct[] = []; for(let i = 0,l = ids.length;i { for(let i = 0,l = o.length;i ({removed:n} as any)), async search$ = async ({query,appuser:{authtkn:{next,okto}}}) => await Promise.resolve({results:[]}), */