import {DeepPartial,IActionsCreator,IQuery,IQueryResults} from "@onebro/oba-common"; import {AppMaster} from "@onebro/appmaster"; import { SwiftTradersUserProfileActions, SwiftTradersUserProfile, SwiftTradersUserProfileModelMap, SwiftTradersUserProfileInstanceMap, SwiftTradersRoles, SwiftTradersRole} from "../types"; import {mapUserRole} from "../../utils"; const wrapup = async (o:SwiftTradersUserProfileInstanceMap) => (await o.profile.save()).json(); const wrapupQ = async (o:IQueryResults) => o; export const swiftTradersUserProfileActions:IActionsCreator = async (m,{SwiftTradersUserProfile}:SwiftTradersUserProfileModelMap) => { const actions:SwiftTradersUserProfileActions = { create$:async ({body,appuser:username,authtkn:{role}}) => await Promise.resolve() .then(() => SwiftTradersUserProfile._create({...body,username,role:mapUserRole(SwiftTradersRoles,role) as SwiftTradersRole})) .then(async o => await SwiftTradersUserProfile._populate(o)) .then(o => wrapup({profile:o})), query$:async ({query}) => await Promise.resolve() .then(async () => await SwiftTradersUserProfile._query(new IQuery(query as any))) .then(async o => await wrapupQ(o)), fetch$:async ({params:{id}}) => await Promise.resolve() .then(() => SwiftTradersUserProfile._find(id)) .then(o => SwiftTradersUserProfile._populate(o)) .then(o => wrapup({profile:o})), fetchByUsername$:async ({appuser:username}) => await Promise.resolve() .then(() => SwiftTradersUserProfile._find({username})) .then(o => wrapup({profile:o})), update$:async ({params:{id},body:updates}) => await Promise.resolve() .then(() => SwiftTradersUserProfile._find(id)) .then(o => SwiftTradersUserProfile._populate(o)) .then(o => SwiftTradersUserProfile._update(o,updates)) .then(o => wrapup({profile:o})), remove$:async ({params:{id}}) => await Promise.resolve() .then(() => SwiftTradersUserProfile._find(id)) .then(o => o.remove()) .then(() => ({removed:id})), updateMany$:async ({body:{ids,updates}}:{body:{ids:string[],updates:DeepPartial}}) => await Promise.resolve() .then(async () => { if(ids.length){ const arr:SwiftTradersUserProfile[] = []; for(let i = 0,l = ids.length;i { for(let i = 0,l = ids.length;i ({updated:n} as any)), removeMany$:async ({body:{ids}}:{body:{ids:string[]}}) => await Promise.resolve() .then(async () => { if(ids.length){ const arr:SwiftTradersUserProfile[] = []; for(let i = 0,l = ids.length;i { for(let i = 0,l = o.length;i ({removed:n} as any)), search$:async ({body}) => await Promise.resolve({results:[]}), }; return actions; };