import OBACore from "@onebro/oba-core"; import {ModelController} from "@onebro/oba-mongoose"; import {UserRoles,UserAcctSignature} from "../00-model-types"; import {ModelFactories} from "../01-model-factories"; export interface UserAcctController extends ModelController {} export class UserAcctController extends ModelController { constructor(public api:OBACore,public factories:ModelFactories){ const users = factories["users"]; super(api as any); this.badStatuses = ["Deleted"]; this.privileges = ["use-api"]; this.adminRoles = ["ADMIN","SUPER","_SA_"]; this.create$ = async ({body:{email,...newUser},appuser:{device,okto}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await users.shouldNotExist({email})) .then(async () => await users.create({username:email,email,devices:[device],...newUser})) .then(o => ({data:o.json()})); }; this.fetch$ = async ({params,appuser:{okto}}) => { return await Promise.resolve() .then(() => this.isAuthed(okto)) .then(async () => await users.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 users.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 users.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 users.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 users.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 users.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:{next,okto}}}) => await Promise.resolve({results:[]}), */