@andrewcaires/api 5.0.6 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +2 -2
- package/dist/index.d.ts +215 -217
- package/dist/index.esm.js +2 -2
- package/package.json +14 -14
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/api v5.0
|
|
2
|
+
* @andrewcaires/api v5.1.0
|
|
3
3
|
* Simple api for small applications using express, jsonwebtoken, sequelize and websocket
|
|
4
4
|
* (c) 2026 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
import{Use as e,UnauthorizedError as t,parameterFactory as i,Context as r,NotFoundError as n,InternalServerError as s,ValidationError as o,StatusCodes as d,BadRequestError as a,Application as l,Ctx as u,Headers as c,Session as p,Extends as g,Get as v,Query as y,Controller as h,Post as m,Validate as _,Body as f,App as E,Middleware as A,Delete as S,Put as O,Params as T,Upload as I}from"@andrewcaires/express";import{Env as P,Log as w,LogColor as b,Hash as x,sha256 as C}from"@andrewcaires/node";import{isUndefined as D,isFunction as R,includesArray as k,getConstructor as N,noop as j,loop as U,uuidv7 as K,isString as B,clamp as M,merge as L,isArray as F,clone as H,forEachAsync as q,upperCamelCase as Y,forEachSeries as $,sleep as G,EventEmitter as V,denied as W,Validation as J,isDef as Q,uniqueID as z,uniqueArray as X,map as Z}from"@andrewcaires/utils.js";import{WebSocketServer as ee}from"ws";import{Sequelize as te,Hooks as ie,Column as re,Association as ne,Database as se,ModelName as oe,Version as de,Model as ae,Unique as le,HasMany as ue,Value as ce,DefaultValue as pe,Op as ge,Where as ve}from"@andrewcaires/sequelize";import{config as ye}from"dotenv";import{hashSync as he,compareSync as me}from"bcrypt";import{Store as _e}from"express-session";import fe from"jsonwebtoken";import{Trait as Ee,Inject as Ae}from"@andrewcaires/decorator";import{existsSync as Se,statSync as Oe,readdirSync as Te,mkdirSync as Ie,lstatSync as Pe,rmSync as we}from"fs";import{join as be}from"path";function xe(e,t,i,r){var n,s=arguments.length,o=s<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,i):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(e,t,i,r);else for(var d=e.length-1;d>=0;d--)(n=e[d])&&(o=(s<3?n(o):s>3?n(t,i,o):n(t,i))||o);return s>3&&o&&Object.defineProperty(t,i,o),o}function Ce(e,t){return function(i,r){t(i,r,e)}}function De(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function Re(e,t,i,r){return new(i||(i=Promise))(function(n,s){function o(e){try{a(r.next(e))}catch(e){s(e)}}function d(e){try{a(r.throw(e))}catch(e){s(e)}}function a(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i(function(e){e(t)})).then(o,d)}a((r=r.apply(e,t||[])).next())})}var ke;"function"==typeof SuppressedError&&SuppressedError,ye({quiet:!0});class Env extends P{}ke=Env,Env.BASE_NAME=ke.getString("BASE_NAME",""),Env.CONSOLE_ERROR=ke.getBool("CONSOLE_ERROR",!1),Env.DATABASE_URI=ke.getString("DATABASE_URI","",!0),Env.DATABASE_LOG=ke.getBool("DATABASE_LOG",!1),Env.DATABASE_AUDIT=ke.getBool("DATABASE_AUDIT",!0),Env.DATABASE_PREFIX=ke.getString("DATABASE_PREFIX",""),Env.DATABASE_TIMEZONE=ke.getString("DATABASE_TIMEZONE","+00:00"),Env.HTTP_CRT=ke.getFile("HTTP_CRT"),Env.HTTP_KEY=ke.getFile("HTTP_KEY"),Env.MODEL_LIMIT=ke.getInt("MODEL_LIMIT",10),Env.MODEL_LIMIT_MAX=ke.getInt("MODEL_LIMIT_MAX",100),Env.SLEEP_AUTH=ke.getMs("SLEEP_AUTH","2s"),Env.SLEEP_FORGOT=ke.getMs("SLEEP_FORGOT","5s"),Env.SLEEP_REGISTRY=ke.getMs("SLEEP_REGISTRY","5s"),Env.TOKEN_CRT=ke.getFile("TOKEN_CRT"),Env.TOKEN_KEY=ke.getFile("TOKEN_KEY"),Env.TOKEN_TTL=ke.getMs("TOKEN_TTL","1d"),Env.API_BODYPARSER_STRICT=ke.getBool("API_BODYPARSER_STRICT",!0),Env.API_BODYPARSER_INFLATE=ke.getBool("API_BODYPARSER_INFLATE",!0),Env.API_BODYPARSER_LIMIT=ke.getString("API_BODYPARSER_LIMIT","10mb"),Env.API_BODYPARSER_TYPE=ke.getString("API_BODYPARSER_TYPE","json"),Env.API_HTTP_PORT=ke.getInt("API_HTTP_PORT",3e3),Env.API_HTTP_PUBLIC=ke.getString("API_HTTP_PUBLIC",""),Env.API_HTTP_CROSS=ke.getBool("API_HTTP_CROSS",!1),Env.API_HTTP_HYBRID=ke.getBool("API_HTTP_HYBRID",!1),Env.API_HTTP_NOTFOUND=ke.getString("API_HTTP_NOTFOUND",""),Env.API_HTTP_PATH=ke.getString("API_HTTP_PATH","/api"),Env.API_HTTP_REDIRECT=ke.getString("API_HTTP_REDIRECT",""),Env.API_KEY_HASH=ke.getString("API_KEY_HASH",""),Env.API_KEY_HEADER=ke.getString("API_KEY_HEADER","x-api-key"),Env.API_SESSION_NAME=ke.getString("API_SESSION_NAME","session.id"),Env.API_SESSION_LIFETIME=ke.getMs("API_SESSION_LIFETIME","1d"),Env.API_SESSION_SECRET=ke.getString("API_SESSION_SECRET","yoursecret"),Env.API_TOKEN_COOKIE=ke.getString("API_TOKEN_COOKIE","authorization"),Env.API_TOKEN_COOKIE_SET=ke.getBool("API_TOKEN_COOKIE_SET",!0),Env.API_TOKEN_FORGOT=ke.getMs("API_TOKEN_FORGOT","1d"),Env.API_TOKEN_HEADER=ke.getString("API_TOKEN_HEADER","authorization"),Env.API_TOKEN_HEADER_KEY=ke.getString("API_TOKEN_HEADER","bearer"),Env.API_TOKEN_RENEW=ke.getMs("API_TOKEN_RENEW","1d"),Env.API_USER_HISTORY=ke.getBool("API_USER_HISTORY",!0),Env.API_WEBSOCKET_START=ke.getBool("API_WEBSOCKET_START",!1);const Ne=new te({uri:Env.DATABASE_URI,logging:!!Env.DATABASE_LOG&&(e=>w.console(b.Magenta,"sql",e)),prefix:Env.DATABASE_PREFIX,timezone:Env.DATABASE_TIMEZONE}),je=i=>r=>(n,s,o)=>{e(e=>Re(void 0,void 0,void 0,function*(){var n;let s=null!=r?r:"";D(r)&&(s=R(e.controller.name)?e.controller.name():null===(n=e.controller.constructor)||void 0===n?void 0:n.name);const{permissions:o}=e.req;if((null!=o?o:[]).includes(`${s}.${i}`))return e.next();e.error(new t("Access Denied"))}))(n,s,o)},Ue=je("create"),Ke=je("delete"),Be=je("read"),Me=je("update"),Le=(...i)=>(r,n,s)=>{e(e=>Re(void 0,void 0,void 0,function*(){const{groups:r}=e.req;if(k(i,null!=r?r:[]))return e.next();e.error(new t("Access Denied"))}))(r,n,s)},Fe=(...i)=>(r,n,s)=>{e(e=>Re(void 0,void 0,void 0,function*(){const{permissions:r}=e.req;if(k(i,null!=r?r:[]))return e.next();e.error(new t("Access Denied"))}))(r,n,s)},He=()=>(i,r,n)=>{e(e=>Re(void 0,void 0,void 0,function*(){yield(e=>Re(void 0,void 0,void 0,function*(){var i,r,n,s;if(e.req.auth&&e.req.user)return e.next();const{[Env.API_KEY_HEADER]:o,[Env.API_TOKEN_HEADER]:d,"user-agent":a}=e.getHeaders(),l=e.getQuery(Env.API_KEY_HEADER),u=(null===(i=o||l)||void 0===i?void 0:i.toString())||"";let c=e.getCookie(Env.API_TOKEN_COOKIE);c=`${Env.API_TOKEN_HEADER_KEY} ${c}`;const[p,g]=null!==(n=null===(r=d||c)||void 0===r?void 0:r.split(" "))&&void 0!==n?n:["",""];if(!(u||g&&p.toLowerCase()==Env.API_TOKEN_HEADER_KEY))return e.error(new t("Invalid token"));const{authService:v,userService:y}=e.app,{auth:h,user:m}=null!==(s=yield(u?v.key(u.toString()):v.user(g)).catch(t=>{e.error(t)}))&&void 0!==s?s:{};if(h&&m){if(!u&&h.user_agent&&h.user_agent!=a)return e.error(new t("Invalid token"));e.req.auth=h,e.req.user=m,e.req.groups=yield y.getGroups(m.id),e.req.permissions=yield y.getPermissions(m.id),e.req.profile=yield y.getProfile(m.id),e.next()}}))(e)}))(i,r,n)},qe=i(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.auth}),Ye=i(({ctx:e,key:t})=>{var i,r;return(null!==(r=null===(i=e.req)||void 0===i?void 0:i.groups)&&void 0!==r?r:[]).includes(t)}),$e=i(({ctx:e})=>{var t,i;return null!==(i=null===(t=e.req)||void 0===t?void 0:t.groups)&&void 0!==i?i:[]}),Ge=i(({ctx:e,key:t})=>{var i,r;return(null!==(r=null===(i=e.req)||void 0===i?void 0:i.permissions)&&void 0!==r?r:[]).includes(t)}),Ve=i(({ctx:e})=>{var t,i;return null!==(i=null===(t=e.req)||void 0===t?void 0:t.permissions)&&void 0!==i?i:[]}),We=i(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.user}),Je=(e,t,i)=>{var n,s,o,d,a,l,u,c,p,g;const v=i.ctx instanceof r?i.ctx:null,y=null!==(d=null!==(o=null===(s=null===(n=null==v?void 0:v.req)||void 0===n?void 0:n.user)||void 0===s?void 0:s.id)&&void 0!==o?o:i.user_id)&&void 0!==d?d:null,h={type:e,model:null!==(a=null==t?void 0:t.getTableName())&&void 0!==a?a:null,app_name:null!==(l=Env.BASE_NAME)&&void 0!==l?l:"",state:!0,user_id:y};return v?Object.assign(Object.assign({},h),{full_url:null!==(u=null==v?void 0:v.getUrl())&&void 0!==u?u:"",ip_address:null!==(c=null==v?void 0:v.getIp())&&void 0!==c?c:"",user_agent:null!==(g=null===(p=null==v?void 0:v.req)||void 0===p?void 0:p.headers["user-agent"])&&void 0!==g?g:""}):h},Qe={afterCreate:(e,t)=>Re(void 0,void 0,void 0,function*(){const i=N(e),{primaryKeyAttribute:r}=null!=i?i:{};yield dt.create(Object.assign(Object.assign({},Je("CREATE",i,t)),{record:r?e.getDataValue(r):null,data:e.toJSON()})).catch(w.catch)}),afterUpdate:(e,t)=>Re(void 0,void 0,void 0,function*(){const i=N(e),{primaryKeyAttribute:r}=null!=i?i:{},n={},s=e.changed();s&&s.forEach(t=>n[t]=e.getDataValue(t)),yield dt.create(Object.assign(Object.assign({},Je("UPDATE",i,t)),{record:r?e.getDataValue(r):null,data:n,previously:e.previous()})).catch(w.catch)}),afterDestroy:(e,t)=>Re(void 0,void 0,void 0,function*(){const i=N(e),{primaryKeyAttribute:r}=null!=i?i:{};yield dt.create(Object.assign(Object.assign({},Je("DELETE",i,t)),{record:r?e.getDataValue(r):null,data:e.toJSON()})).catch(w.catch)})},ze=e=>(D(e)&&(e=Env.DATABASE_AUDIT),ie(e?Qe:{}));let Xe=class RegisterModel extends ae{};xe([re.UUID(7),De("design:type",String)],Xe.prototype,"id",void 0),xe([re.String(),De("design:type",String)],Xe.prototype,"secret",void 0),xe([re.FullDate(),De("design:type",Number)],Xe.prototype,"expired_at",void 0),xe([re.Boolean(),De("design:type",Boolean)],Xe.prototype,"state",void 0),xe([re.CreatedAt(),De("design:type",Date)],Xe.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],Xe.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],Xe.prototype,"deleted_at",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],Xe.prototype,"user_id",void 0),Xe=xe([se(Ne),oe("register"),de("5.0.0")],Xe);let Ze=class UserAuthModel extends ae{};xe([re.UUID(7),De("design:type",String)],Ze.prototype,"id",void 0),xe([re.String(),De("design:type",String)],Ze.prototype,"secret",void 0),xe([re.String(),De("design:type",String)],Ze.prototype,"ip_address",void 0),xe([re.String(),De("design:type",String)],Ze.prototype,"user_agent",void 0),xe([re.FullDate(),De("design:type",Object)],Ze.prototype,"revoked_at",void 0),xe([re.CreatedAt(),De("design:type",Date)],Ze.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],Ze.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],Ze.prototype,"deleted_at",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],Ze.prototype,"user_id",void 0),Ze=xe([se(Ne),oe("users_auth"),de("5.0.0")],Ze);let et=class PermissionModel extends ae{};xe([re.UUID(7),De("design:type",String)],et.prototype,"id",void 0),xe([re.String(),De("design:type",String)],et.prototype,"name",void 0),xe([re.String(),le(!0),De("design:type",String)],et.prototype,"key",void 0),xe([re.String(),De("design:type",String)],et.prototype,"tags",void 0),xe([re.Text(),De("design:type",String)],et.prototype,"description",void 0),xe([re.Boolean(),De("design:type",Boolean)],et.prototype,"state",void 0),xe([re.CreatedAt(),De("design:type",Date)],et.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],et.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],et.prototype,"deleted_at",void 0),xe([ue(()=>tt,"permission_id"),De("design:type",Array)],et.prototype,"groups_permissions",void 0),et=xe([se(Ne),oe("permission"),ze(),de("5.0.1")],et);let tt=class GroupPermissionModel extends ae{};xe([re.UUID(7),De("design:type",String)],tt.prototype,"id",void 0),xe([re.CreatedAt(),De("design:type",Date)],tt.prototype,"created_at",void 0),xe([ne.BelongsTo(()=>it),De("design:type",String)],tt.prototype,"group_id",void 0),xe([ne.BelongsTo(()=>et),De("design:type",String)],tt.prototype,"permission_id",void 0),tt=xe([se(Ne),oe("groups_permissions"),ze(),de("5.0.2")],tt);let it=class GroupModel extends ae{};xe([re.UUID(7),De("design:type",String)],it.prototype,"id",void 0),xe([re.String(),De("design:type",String)],it.prototype,"name",void 0),xe([re.String(),De("design:type",String)],it.prototype,"key",void 0),xe([re.String(),De("design:type",String)],it.prototype,"tags",void 0),xe([re.Text(),De("design:type",String)],it.prototype,"description",void 0),xe([re.Boolean(),De("design:type",Boolean)],it.prototype,"state",void 0),xe([re.CreatedAt(),De("design:type",Date)],it.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],it.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],it.prototype,"deleted_at",void 0),xe([ne.HasMany(()=>rt,"group_id"),De("design:type",Array)],it.prototype,"users_groups",void 0),xe([ne.HasMany(()=>tt,"group_id"),De("design:type",Array)],it.prototype,"groups_Permission",void 0),it=xe([se(Ne),oe("group"),ze(),de("5.0.1")],it);let rt=class UserGroupModel extends ae{};xe([re.UUID(7),De("design:type",String)],rt.prototype,"id",void 0),xe([re.CreatedAt(),De("design:type",Date)],rt.prototype,"created_at",void 0),xe([ne.BelongsTo(()=>it),De("design:type",String)],rt.prototype,"group_id",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],rt.prototype,"user_id",void 0),rt=xe([se(Ne),oe("users_groups"),ze(),de("5.0.2")],rt);let nt=class UserPasswordModel extends ae{};xe([re.UUID(7),De("design:type",String)],nt.prototype,"id",void 0),xe([re.String(),ce(e=>he(e.value,12)),De("design:type",String)],nt.prototype,"value",void 0),xe([re.Boolean(),De("design:type",Boolean)],nt.prototype,"state",void 0),xe([re.CreatedAt(),De("design:type",Date)],nt.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],nt.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],nt.prototype,"deleted_at",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],nt.prototype,"user_id",void 0),nt=xe([se(Ne),oe("users_password"),ze(),de("5.0.0")],nt);let st=class UserProfileModel extends ae{};xe([re.UUID(7),De("design:type",String)],st.prototype,"id",void 0),xe([re.Json(),pe({}),De("design:type",Object)],st.prototype,"data",void 0),xe([re.CreatedAt(),De("design:type",Date)],st.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],st.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],st.prototype,"deleted_at",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],st.prototype,"user_id",void 0),st=xe([se(Ne),oe("users_profile"),ze(),de("5.0.1")],st);let ot=class UserModel extends ae{};xe([re.UUID(7),De("design:type",String)],ot.prototype,"id",void 0),xe([re.String(),De("design:type",String)],ot.prototype,"name",void 0),xe([re.String(),De("design:type",String)],ot.prototype,"email",void 0),xe([re.String(),De("design:type",String)],ot.prototype,"username",void 0),xe([re.Text(),De("design:type",String)],ot.prototype,"description",void 0),xe([re.Boolean(),De("design:type",Boolean)],ot.prototype,"state",void 0),xe([re.CreatedAt(),De("design:type",Date)],ot.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],ot.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],ot.prototype,"deleted_at",void 0),xe([ne.HasMany(()=>dt,"user_id"),De("design:type",Array)],ot.prototype,"audits",void 0),xe([ne.HasMany(()=>Ze,"user_id"),De("design:type",Array)],ot.prototype,"users_auths",void 0),xe([ne.HasMany(()=>Xe,"user_id"),De("design:type",Array)],ot.prototype,"registers",void 0),xe([ne.HasMany(()=>rt,"user_id"),De("design:type",Array)],ot.prototype,"users_groups",void 0),xe([ne.HasMany(()=>nt,"user_id"),De("design:type",Array)],ot.prototype,"users_passwords",void 0),xe([ne.HasMany(()=>st,"user_id"),De("design:type",Array)],ot.prototype,"users_profiles",void 0),ot=xe([se(Ne),oe("user"),ze(),de("5.0.0")],ot);let dt=class AuditModel extends ae{};xe([re.UUID(7),De("design:type",String)],dt.prototype,"id",void 0),xe([re.Enum("CREATE","DELETE","UPDATE"),De("design:type",String)],dt.prototype,"type",void 0),xe([re.String(),De("design:type",String)],dt.prototype,"model",void 0),xe([re.String(),De("design:type",String)],dt.prototype,"record",void 0),xe([re.Json(),De("design:type",Object)],dt.prototype,"data",void 0),xe([re.Json(),De("design:type",Object)],dt.prototype,"previously",void 0),xe([re.Text(),De("design:type",String)],dt.prototype,"full_url",void 0),xe([re.String(),De("design:type",String)],dt.prototype,"ip_address",void 0),xe([re.String(),De("design:type",String)],dt.prototype,"user_agent",void 0),xe([re.String(),De("design:type",String)],dt.prototype,"app_name",void 0),xe([re.Boolean(),De("design:type",Boolean)],dt.prototype,"state",void 0),xe([re.CreatedAt(),De("design:type",Date)],dt.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],dt.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],dt.prototype,"deleted_at",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],dt.prototype,"user_id",void 0),dt=xe([se(Ne),oe("audit"),de("5.0.1")],dt);let at=class ConfigModel extends ae{};xe([re.UUID(7),De("design:type",String)],at.prototype,"id",void 0),xe([re.String(),De("design:type",String)],at.prototype,"key",void 0),xe([re.String(),De("design:type",String)],at.prototype,"value",void 0),xe([re.CreatedAt(),De("design:type",Date)],at.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],at.prototype,"updated_at",void 0),at=xe([se(Ne),oe("configs"),de("5.0.0")],at);let lt=class ForgotModel extends ae{};xe([re.UUID(7),De("design:type",String)],lt.prototype,"id",void 0),xe([re.String(),De("design:type",String)],lt.prototype,"secret",void 0),xe([re.FullDate(),De("design:type",Number)],lt.prototype,"expired_at",void 0),xe([re.Boolean(),De("design:type",Boolean)],lt.prototype,"state",void 0),xe([re.CreatedAt(),De("design:type",Date)],lt.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],lt.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],lt.prototype,"deleted_at",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],lt.prototype,"user_id",void 0),lt=xe([se(Ne),oe("forgot"),de("5.0.0")],lt);let ut=class LogModel extends ae{};xe([re.UUID(7),De("design:type",String)],ut.prototype,"id",void 0),xe([re.Enum("info","warn","error"),pe("info"),De("design:type",String)],ut.prototype,"level",void 0),xe([re.Text(),De("design:type",String)],ut.prototype,"message",void 0),xe([re.String(),De("design:type",String)],ut.prototype,"source",void 0),xe([re.CreatedAt(),De("design:type",Date)],ut.prototype,"created_at",void 0),ut=xe([se(Ne),oe("log"),de("5.0.0")],ut);let ct=class MigrationModel extends ae{};xe([re.UUID(7),De("design:type",String)],ct.prototype,"id",void 0),xe([re.String(),De("design:type",String)],ct.prototype,"model",void 0),xe([re.String(),De("design:type",String)],ct.prototype,"version",void 0),xe([re.CreatedAt(),De("design:type",Date)],ct.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],ct.prototype,"updated_at",void 0),ct=xe([se(Ne),oe("migration"),de("5.0.2")],ct);let pt=class SessionModel extends ae{};xe([re.UUID(7),De("design:type",String)],pt.prototype,"id",void 0),xe([re.String(),De("design:type",String)],pt.prototype,"session",void 0),xe([re.Text(),De("design:type",String)],pt.prototype,"json",void 0),xe([re.BigInt(),De("design:type",Number)],pt.prototype,"expired",void 0),xe([re.CreatedAt(),De("design:type",Date)],pt.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],pt.prototype,"updated_at",void 0),pt=xe([se(Ne),oe("session"),de("5.0.2")],pt);let gt=class UserMetadataModel extends ae{};xe([re.UUID(7),De("design:type",String)],gt.prototype,"id",void 0),xe([re.Json(),pe({}),De("design:type",Object)],gt.prototype,"data",void 0),xe([re.CreatedAt(),De("design:type",Date)],gt.prototype,"created_at",void 0),xe([re.UpdatedAt(),De("design:type",Date)],gt.prototype,"updated_at",void 0),xe([re.DeletedAt(),De("design:type",Object)],gt.prototype,"deleted_at",void 0),xe([ne.BelongsTo(()=>ot),De("design:type",String)],gt.prototype,"user_id",void 0),gt=xe([se(Ne),oe("users_metadata"),ze(),de("5.0.1")],gt);class Log{static error(e,t,i){return Re(this,void 0,void 0,function*(){yield this.writeRaw("error",e,t,i)})}static info(e,t,i){return Re(this,void 0,void 0,function*(){yield this.writeRaw("info",e,t,i)})}static warn(e,t,i){return Re(this,void 0,void 0,function*(){yield this.writeRaw("warn",e,t,i)})}static writeRaw(e,t,i,r){return Re(this,void 0,void 0,function*(){var n;r&&w.console(null!==(n=this.colors[e])&&void 0!==n?n:b.White,e,t),Ne&&(yield ut.create({level:e,message:t,source:null!=i?i:""}).catch(w.catch))})}}Log.colors={error:b.Red,info:b.Cyan,warn:b.Yellow};class Loggable{get log(){return e=>Loggable.logInfo(this,e)}static get log(){return e=>this.logInfo(this,e)}get error(){return e=>Loggable.logError(this,e)}static get error(){return e=>this.logError(this,e)}static logError(e,t){var i,r;Log.error(null!==(r=null!==(i=null==t?void 0:t.message)&&void 0!==i?i:t)&&void 0!==r?r:"",this.source(e),Env.CONSOLE_ERROR)}static logInfo(e,t){Log.info(null!=t?t:"",this.source(e),Env.CONSOLE_ERROR)}static source(e){var t,i,r;const n=null!==(r=null!==(i=null===(t=null==e?void 0:e.constructor)||void 0===t?void 0:t.name)&&void 0!==i?i:null==e?void 0:e.name)&&void 0!==r?r:"null";return Env.BASE_NAME?`${Env.BASE_NAME}.${n}`:n}}class Config extends Loggable{static all(e){return Re(this,void 0,void 0,function*(){var t;const i=null!==(t=yield at.findAll({where:(null==e?void 0:e.length)?{key:{[ge.in]:e}}:{}}).catch(this.error))&&void 0!==t?t:[];return Object.fromEntries(i.map(e=>[e.key,e.value]))})}static del(e){return Re(this,void 0,void 0,function*(){return((yield at.destroy({where:{key:e}}).catch(this.error))||0)>0})}static get(e){return Re(this,arguments,void 0,function*(e,t=""){const[i]=(yield at.findOrCreate({where:{key:e},defaults:{key:e,value:t}}).catch(this.error))||[];return i?i.value:t})}static set(e,t){return Re(this,void 0,void 0,function*(){const[i,r]=(yield at.findOrCreate({where:{key:e},defaults:{key:e,value:t}}).catch(this.error))||[];i&&!r&&(i.value=t,yield i.save().catch(this.error))})}}class SessionStore extends _e{constructor(){super(),this.all=e=>Re(this,void 0,void 0,function*(){e=e||j;const t=yield pt.findAll({attributes:["session"],where:{expired:{[ge.gte]:Date.now()}}}).catch(t=>(e(t),null));t&&e(null,t.map(e=>e.session)),e()}),this.clear=e=>Re(this,void 0,void 0,function*(){yield pt.destroy().catch(j),e&&e()}),this.destroy=(e,t)=>Re(this,void 0,void 0,function*(){yield pt.destroy({where:{session:e}}).catch(j),t&&t()}),this.expired=()=>Re(this,void 0,void 0,function*(){yield pt.destroy({where:{expired:{[ge.lt]:Date.now()}}}).catch(j)}),this.get=(e,t)=>Re(this,void 0,void 0,function*(){t=t||j;const i=yield pt.findOne({where:{session:e,expired:{[ge.gte]:Date.now()}}}).catch(e=>(t(e),null));if(i){const e=JSON.parse(i.json);if(e.cookie){if(new Date(e.cookie.expires).getTime()<=Date.now())return yield i.destroy().catch(j),void t(null)}return void t(null,e)}t(null)}),this.length=e=>Re(this,void 0,void 0,function*(){e&&e(null,(yield pt.count().catch(j))||0)}),this.set=(e,t,i)=>Re(this,void 0,void 0,function*(){i=i||j;const r=Date.now()+Env.API_SESSION_LIFETIME,n=JSON.stringify(t),[s,o]=(yield pt.findOrCreate({where:{session:e},defaults:{session:e,json:n,expired:r}}).catch(j))||[];s&&(o||(s.json=n,s.expired=r,yield s.save().catch(j))),i()}),this.loop=U(this.expired,3e4),this.loop.start()}}let vt=class Service{};vt=xe([Ee(Loggable)],vt);class AuthService extends vt{create(e){return Re(this,void 0,void 0,function*(){if(!Env.TOKEN_KEY)throw new n("TOKEN_KEY not found");const{id:t}=e,i=K(),r=yield Ze.create({secret:i,user_id:t}).catch(this.error);if(!r)throw new s;return{auth:r,token:fe.sign({id:t,secret:i},Env.TOKEN_KEY,this.options())}})}destroy(e){return Re(this,void 0,void 0,function*(){return!(!e||e.revoked_at)&&(e.revoked_at=new Date,yield e.save(),!0)})}renew(e){return Re(this,void 0,void 0,function*(){if(!Env.TOKEN_KEY)throw new n("TOKEN_KEY not found");const{decode:i}=yield this.verify(e,Env.API_TOKEN_RENEW/1e3);if(!i)throw new t("Invalid token");const{id:r,secret:s}=i;if(r&&s){const e=yield ot.findOne({where:{id:r,state:!0}}).catch(this.error);if(e){const t=yield Ze.findOne({where:{secret:s,user_id:e.id}}).catch(this.error);if(t&&!t.revoked_at){t.secret=K(),yield t.save().catch(this.error);return{auth:t,token:fe.sign({id:r,secret:t.secret},Env.TOKEN_KEY,this.options())}}}}throw new t("Invalid token")})}user(e){return Re(this,void 0,void 0,function*(){const{decode:i}=yield this.verify(e);if(!i)throw new t("Invalid token");const{id:r,secret:n}=i;if(r&&n){const e=yield ot.findOne({where:{id:r,state:!0}}).catch(this.error);if(e){const t=yield Ze.findOne({where:{secret:n,user_id:e.id}}).catch(this.error);if(t&&!t.revoked_at)return{auth:t,user:e}}}throw new t("Invalid token")})}options(){return{algorithm:"RS256",expiresIn:`${Env.TOKEN_TTL}ms`}}verify(e,i){return Re(this,void 0,void 0,function*(){if(!Env.TOKEN_CRT)throw new n("TOKEN_CRT not found");if(!e||e.split(".").length<3)throw new t("Invalid token");try{return{decode:fe.verify(e,Env.TOKEN_CRT,{clockTolerance:i})}}catch(e){throw new t("Invalid token")}})}key(e){return Re(this,void 0,void 0,function*(){if(!e)throw new t("Invalid token");const i=yield ot.findOne({where:{token:e,state:!0}}).catch(this.error);if(i){const t=e.replace(/(-[\w]{4})/g,"-****"),[r]=(yield Ze.findOrCreate({where:{secret:t,user_id:i.id},defaults:{secret:t,user_id:i.id}}).catch(this.error))||[];if(r)return r.revoked_at&&(r.revoked_at=null,yield r.save().catch(this.error)),{auth:r,user:i}}throw new t("Invalid token")})}}class ModelService extends vt{constructor(e,t){var i,r;super(),this.attributes=[],this.permissions=[],this.search=[],this.model=e,(null==t?void 0:t.permissions)&&(this.permissions=t.permissions);const n=Object.keys(e.getAttributes());this.attributes=this.filterAttributes(n,null!==(i=null==t?void 0:t.attributes)&&void 0!==i?i:[]),this.search=this.filterAttributes(n,null!==(r=null==t?void 0:t.search)&&void 0!==r?r:this.attributes)}applySelect(e,t,i){if(!t)return;const r=null==e?void 0:e.getQuery(i?`${i}_select`:"select"),n=t.attributes,s=e=>n.includes(e);t.attributes=[],B(r)&&r.length&&t.attributes.push(...r.split(/\s*,\s*/g).filter(s)),t.attributes.length||(t.attributes=n)}applyOrders(e,t){if(!t)return;const i=null==e?void 0:e.getQuery("asc"),r=null==e?void 0:e.getQuery("desc"),n=[],s=t.attributes,o=e=>s.includes(e);B(i)&&i.length&&n.push(...i.split(/\s*,\s*/g).filter(o).map(e=>[e,"ASC"])),B(r)&&r.length&&n.push(...r.split(/\s*,\s*/g).filter(o).map(e=>[e,"DESC"])),n.length&&(t.order=n)}applyPagination(e,t){if(!t)return;const i=null==e?void 0:e.getQuery("limit"),r=null==e?void 0:e.getQuery("offset");D(t.limit)&&(t.limit=Env.MODEL_LIMIT,B(i)&&(t.limit=parseInt(i)||0),t.limit=M(t.limit,-1,Env.MODEL_LIMIT_MAX)),t.limit<0&&(t.limit=void 0),B(r)&&(t.offset=parseInt(r)||0,t.offset<0&&(t.offset=void 0))}applySearch(e,t){var i;if(!t)return;const r=null==e?void 0:e.getQuery("search");r&&B(r)&&(t.where=Object.assign(Object.assign({},null!==(i=t.where)&&void 0!==i?i:{}),{[ge.or]:this.search.map(e=>({[e]:{[ge.substring]:r}}))}))}applyWhere(e,t,i){var r,n;if(!t)return null;const s=null==e?void 0:e.getQuery(i?`${i}_where`:"where");if(!B(s))return null;const d=t.attributes,a=ve.parse(d,s);return D(a.error)?(t.where=Object.assign(Object.assign({},null!==(r=t.where)&&void 0!==r?r:{}),null!==(n=a.value)&&void 0!==n?n:{}),null):new o(a.error.join(", "))}countAll(e){return Re(this,void 0,void 0,function*(){var t,i;const{ctx:r,options:n={}}=e,s=L({},n);s.attributes=this.getAttributes(null!==(t=null==s?void 0:s.attributes)&&void 0!==t?t:[]),this.applySearch(r,s);const o=this.applyWhere(r,s);if(o)return null==r||r.error(o),0;this.applySelect(r,s);const a=null!==(i=yield this.model.count(Object.assign(Object.assign({},s),{distinct:!!s.include,col:this.attributes.length?this.attributes[0]:void 0})).catch(this.error))&&void 0!==i?i:0;return null==r||r.prepare(d.OK,{count:a}),a})}create(e){return Re(this,void 0,void 0,function*(){var t,i;const{body:r={},ctx:n,options:o={}}=e,a=L({},o),l=yield this.model.create(Object.assign({},L({},null!==(i=null===(t=null==n?void 0:n.req)||void 0===t?void 0:t.body)&&void 0!==i?i:{},r)),Object.assign(Object.assign({},a),{ctx:n})).catch(this.error);if(!l)return null==n||n.error(new s),null;const{primaryKeyAttribute:u}=this.model;return null==n||n.prepare(d.OK,{[u]:l.getDataValue(u)}),l})}delete(e){return Re(this,void 0,void 0,function*(){var t,i;const{ctx:r,id:s,options:o={}}=e,{primaryKeyAttribute:d}=this.model,l=null!==(t=null==r?void 0:r.getParam(d))&&void 0!==t?t:s;if(D(l))return null==r||r.error(new a),0;const u=L({},o),c=yield this.model.destroy(Object.assign(Object.assign(Object.assign({},u),{where:Object.assign(Object.assign({},null!==(i=u.where)&&void 0!==i?i:{}),{[d]:l}),individualHooks:!0}),{ctx:r})).catch(this.error);return c?(null==r||r.success("OK"),c):(null==r||r.error(new n),0)})}filterAttributes(e,t){if(F(t))return t.length?t.filter(t=>B(t)&&e.includes(t)):e;const{exclude:i,include:r}=t,n=D(r)?e:r.filter(t=>B(t)&&e.includes(t)),s=null!=i?i:[];return s.length?n.filter(e=>!s.includes(e)):n}findAll(e){return Re(this,void 0,void 0,function*(){var t,i;const{ctx:r,callback:n,limited:s,options:o={},query:a=!0}=e,l=L({},o);if(l.attributes=this.getAttributes(null!==(t=null==l?void 0:l.attributes)&&void 0!==t?t:[]),a&&this.applySelect(r,l),this.applyOrders(r,l),this.applyPagination(r,l),s&&(null!==(i=null==l?void 0:l.limit)&&void 0!==i?i:0)>Env.MODEL_LIMIT_MAX&&(l.limit=Env.MODEL_LIMIT_MAX),this.applySearch(r,l),a){const e=this.applyWhere(r,l);if(e)return null==r||r.error(e),{count:0,records:[]}}const[u,c]=yield Promise.all([this.model.count(Object.assign(Object.assign({},l),{distinct:!!l.include,col:this.attributes.length?this.attributes[0]:void 0})).catch(this.error).then(e=>null!=e?e:0),this.model.findAll(l).catch(this.error).then(e=>null!=e?e:[])]),p={count:u,records:n?yield n(c):c};return null==r||r.prepare(d.OK,p),p})}findOne(e){return Re(this,void 0,void 0,function*(){var t,i,r;const{callback:s,ctx:o,id:l,options:u={},query:c=!0}=e,{primaryKeyAttribute:p}=this.model,g=null!==(t=null==o?void 0:o.getParam(p))&&void 0!==t?t:l;if(D(g))return null==o||o.error(new a),null;const v=L({},u);v.attributes=this.getAttributes(null!==(i=null==v?void 0:v.attributes)&&void 0!==i?i:[]),c&&this.applySelect(o,v);const y=yield this.model.findOne(Object.assign(Object.assign({},v),{where:Object.assign(Object.assign({},null!==(r=null==v?void 0:v.where)&&void 0!==r?r:{}),{[p]:g})})).catch(this.error);return y?(null==o||o.prepare(d.OK,s?yield s(y):y.toJSON()),y):(null==o||o.error(new n),null)})}getAttributes(e){return this.filterAttributes(this.attributes,e)}include(e,t){var i;const r=H(t),{ctx:n}=null!=t?t:{};r.attributes=this.filterAttributes(Object.keys(e.getAttributes()),null!==(i=r.attributes)&&void 0!==i?i:[]),this.applySearch(n,r);const s=this.applyWhere(n,r,t.name);return s?(null==n||n.error(s),null):(this.applySelect(n,r,t.name),Object.assign({model:e},r))}name(){return this.model.tableName}sync(){return Re(this,void 0,void 0,function*(){if(!this.permissions.length)return;const e=this.name().toLowerCase(),t={description:"",state:!0};yield q(this.permissions,i=>Re(this,void 0,void 0,function*(){const r=`${e}.${i}`,n=Y(e.replace(/_/g," ")," ");yield et.findOrCreate({where:{key:r},defaults:Object.assign({name:`${n} ${Y(i," ")}`,key:r,tags:n},t)})}))})}update(e){return Re(this,void 0,void 0,function*(){var t,i,r,n,s;const{ctx:o,body:d,id:l,options:u={}}=e,{primaryKeyAttribute:c}=this.model,p=null!==(t=null==o?void 0:o.getParam(c))&&void 0!==t?t:l;if(D(p))return null==o||o.error(new a),!1;const g=L({},u),[v]=null!==(s=yield this.model.update(Object.assign({},L({},null!==(r=null===(i=null==o?void 0:o.req)||void 0===i?void 0:i.body)&&void 0!==r?r:{},null!=d?d:{})),Object.assign(Object.assign(Object.assign({},g),{where:Object.assign(Object.assign({},null!==(n=null==g?void 0:g.where)&&void 0!==n?n:{}),{[c]:p}),individualHooks:!0}),{ctx:o})).catch(this.error))&&void 0!==s?s:[];return v?(null==o||o.success("OK"),!0):(null==o||o.success("Record not changed"),!1)})}}class UserService extends vt{getMetadata(e){return Re(this,void 0,void 0,function*(){var t,i;const[r]=null!==(t=yield gt.findOrCreate({where:{user_id:e},defaults:{user_id:e}}).catch(this.error))&&void 0!==t?t:[];return null!==(i=null==r?void 0:r.data)&&void 0!==i?i:{}})}getProfile(e){return Re(this,void 0,void 0,function*(){var t,i;const[r]=null!==(t=yield st.findOrCreate({where:{user_id:e},defaults:{user_id:e}}).catch(this.error))&&void 0!==t?t:[];return null!==(i=null==r?void 0:r.data)&&void 0!==i?i:{}})}getGroups(e){return Re(this,void 0,void 0,function*(){var t;return(null!==(t=yield it.findAll({attributes:["key"],where:{state:!0},include:[{model:rt,required:!0,where:{user_id:e}}]}).catch(this.error))&&void 0!==t?t:[]).map(e=>e.key)})}getPermissions(e){return Re(this,void 0,void 0,function*(){var t;return(null!==(t=yield et.findAll({attributes:["key"],where:{state:!0},include:[{model:tt,required:!0,include:[{model:it,required:!0,attributes:["id"],where:{state:!0},include:[{model:rt,required:!0,where:{user_id:e}}]}]}]}).catch(this.error))&&void 0!==t?t:[]).map(e=>e.key)})}}class Application extends l{constructor(e,t){super(e,L({},{cert:Env.HTTP_CRT,cross:Env.API_HTTP_CROSS,hybrid:Env.API_HTTP_HYBRID,json:{strict:Env.API_BODYPARSER_STRICT,inflate:Env.API_BODYPARSER_INFLATE,limit:Env.API_BODYPARSER_LIMIT,type:Env.API_BODYPARSER_TYPE},key:Env.HTTP_KEY,notfound:Env.API_HTTP_NOTFOUND,path:Env.API_HTTP_PATH,port:Env.API_HTTP_PORT,public:Env.API_HTTP_PUBLIC,redirect:Env.API_HTTP_REDIRECT,session:{store:new SessionStore,name:Env.API_SESSION_NAME,secret:Env.API_SESSION_SECRET,cookie:{maxAge:Env.API_SESSION_LIFETIME},genid:K}},null!=t?t:{})),this.hash=new x(Env.API_KEY_HASH)}error(e,t){Env.BASE_NAME&&(t=t?`${Env.BASE_NAME}.${t}`:Env.BASE_NAME),Log.error(e.message,t,Env.CONSOLE_ERROR)}log(e,t){Env.BASE_NAME&&(t=t?`${Env.BASE_NAME}.${t}`:Env.BASE_NAME),Log.info(e,t,!0)}listen(){const e=Object.create(null,{listen:{get:()=>super.listen}});return Re(this,void 0,void 0,function*(){yield Ne.sync(),yield this.migrations(Ne),yield $(this.controllers,e=>Re(this,void 0,void 0,function*(){R(e.sync)&&(yield e.sync(this))})),this.authService=new AuthService,this.userService=new UserService,yield e.listen.call(this),Env.API_WEBSOCKET_START&&this.server&&(this._webSocket=new ee({server:this.server,verifyClient:(e,t)=>t(Env.API_WEBSOCKET_START)}),this._webSocket.on("connection",(e,t)=>Re(this,void 0,void 0,function*(){t.url||(t.url=""),e.on("close",()=>{}),yield G(2e3)})))})}migrations(e){return Re(this,void 0,void 0,function*(){const t=[];yield $(Object.keys(e.models),i=>Re(this,void 0,void 0,function*(){var r;const n=e.models[i],s=n.version();if(!s)return;const[o,d]=null!==(r=yield ct.findOrCreate({where:{model:n.tableName},defaults:{model:n.tableName,version:""}}).catch(this.error))&&void 0!==r?r:[];o&&(d||o.version!=s)&&(o.version=s,yield o.save().catch(this.error),t.push(n))})),yield $(t,e=>Re(this,void 0,void 0,function*(){return yield e.updateAttributes()}))})}websocket(){return this._webSocket}}let yt=class BaseController extends V{constructor(){super(...arguments),this.permissions=[]}name(){var e,t;return(null!==(t=null===(e=this.constructor)||void 0===e?void 0:e.name)&&void 0!==t?t:"api").replace(/(([a-z])([A-Z]))/g,"$2_$3")}session(e,t,i){return Re(this,void 0,void 0,function*(){i.auth&&i.user&&(i.user_agent?i.user_agent!=t&&i.regenerate(j):(i.user_agent=t,i.save(j))),e.next()})}sync(){return Re(this,void 0,void 0,function*(){const e=this.name().toLowerCase(),t={description:"",state:!0};yield $(this.permissions,i=>Re(this,void 0,void 0,function*(){const r=`${e}.${i}`,n=Y(e.replace(/_/g," ")," ");yield et.findOrCreate({where:{key:r},defaults:Object.assign({name:`${n} ${Y(i," ")}`,key:r,tags:n},t)})}))})}};xe([Ce(0,u()),Ce(1,c("user-agent")),Ce(2,p()),De("design:type",Function),De("design:paramtypes",[r,String,Object]),De("design:returntype",Promise)],yt.prototype,"session",null),yt=xe([Ee(Loggable)],yt);let ht=class ModelController extends yt{constructor(){super(...arguments),this.attributes=[],this.permissions=["create","read","update","delete"],this.search=[]}countAll(e){return Re(this,void 0,void 0,function*(){return yield this.service.countAll(e)})}create(e){return Re(this,void 0,void 0,function*(){return yield this.service.create(e)})}delete(e){return Re(this,void 0,void 0,function*(){return yield this.service.delete(e)})}findAll(e){return Re(this,void 0,void 0,function*(){return yield this.service.findAll(e)})}findOne(e){return Re(this,void 0,void 0,function*(){return yield this.service.findOne(e)})}include(e,t){return this.service.include(e,t)}sync(){return Re(this,void 0,void 0,function*(){const e=this.name().toLowerCase(),t={description:"",state:!0};yield $(this.permissions,i=>Re(this,void 0,void 0,function*(){const r=`${e}.${i}`,n=Y(e.replace(/_/g," ")," ");yield et.findOrCreate({where:{key:r},defaults:Object.assign({name:`${n} ${Y(i," ")}`,key:r,tags:n},t)})})),this.service=new ModelService(this.model,{attributes:this.attributes.length?this.attributes:void 0,search:this.search.length?this.search:void 0})})}update(e){return Re(this,void 0,void 0,function*(){return yield this.service.update(e)})}name(){return this.model.tableName}};ht=xe([g(yt)],ht);let mt=class AuditsController extends ht{constructor(){super(...arguments),this.model=dt,this.permissions=["read"]}all(e,t,i){return Re(this,void 0,void 0,function*(){const r={};t&&(r.type=t),i&&(r.table=i),yield this.findAll({ctx:e,options:{attributes:{exclude:["data","previously"]},where:r,order:[["created_at","DESC"]]},limited:!0})})}get(e){return Re(this,void 0,void 0,function*(){yield this.findOne({ctx:e})})}};xe([v(),He(),Be(),Ce(0,u()),Ce(1,y("type")),Ce(2,y("table")),De("design:type",Function),De("design:paramtypes",[r,String,String]),De("design:returntype",Promise)],mt.prototype,"all",null),xe([v("/:id"),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],mt.prototype,"get",null),mt=xe([h("/audits"),g(ht)],mt);let _t=class AuthController extends yt{constructor(e){super(),this.authService=e}login(e,i,r,n,s,o){return Re(this,void 0,void 0,function*(){var d;yield G(Env.SLEEP_AUTH);const a=yield ot.findOne({where:{[ge.or]:[{email:r},{username:r}]}}).catch(this.error);if(!a)return e.error(new t("Invalid credentials")),o.emit("auth.failure",{ctx:e}),void this.emit("auth.failure",{ctx:e});const l=yield nt.findOne({where:{user_id:a.id,state:!0}}).catch(this.error);if(!l||!me(n,l.value))return e.error(new t("Invalid credentials")),o.emit("auth.failure",{ctx:e}),void this.emit("auth.failure",{ctx:e});const u=yield Xe.findOne({where:{state:!0,user_id:a.id}}).catch(this.error);if(u)return e.error("ACTIVATION REQUIRED"),o.emit("auth.activation",{ctx:e,register:u,user:a}),void this.emit("auth.activation",{ctx:e,register:u,user:a});if(!a.state)return e.error(new t("Invalid credentials")),o.emit("auth.failure",{ctx:e,user:a}),void this.emit("auth.failure",{ctx:e,user:a});const{auth:c,token:p}=null!==(d=yield this.authService.create(a).catch(t=>{e.error(t),o.emit("auth.failure",{ctx:e}),this.emit("auth.failure",{ctx:e})}))&&void 0!==d?d:{};c&&p&&(c.user_agent=i,c.ip_address=e.getIp(),yield c.save().catch(this.error),Env.API_TOKEN_COOKIE_SET&&e.setCookie(Env.API_TOKEN_COOKIE,p,{maxAge:s?Env.TOKEN_TTL:void 0}),e.json({token:p}),o.emit("auth.login",{ctx:e,auth:c,user:a}),this.emit("auth.login",{ctx:e,auth:c,user:a}))})}logout(e,i,r,n){return Re(this,void 0,void 0,function*(){if(yield this.authService.destroy(i))return Env.API_TOKEN_COOKIE_SET&&e.clearCookie(Env.API_TOKEN_COOKIE),e.success("OK"),n.emit("auth.logout",{auth:i,ctx:e,user:r}),void this.emit("auth.logout",{auth:i,ctx:e,user:r});e.error(new t("Access Denied"))})}renew(e,t,i,r){return Re(this,void 0,void 0,function*(){var n;const{auth:s,token:o}=null!==(n=yield this.authService.renew(i).catch(t=>{e.error(t)}))&&void 0!==n?n:{};s&&o&&(s.user_agent=t,yield s.save().catch(this.error),e.json({token:o}),r.emit("auth.renew",{auth:s,ctx:e}),this.emit("auth.renew",{auth:s,ctx:e}))})}user(e,t){return Re(this,void 0,void 0,function*(){const{groups:i,permissions:r,profile:n}=e.req;if(!i||!r)return e.error(new s);e.json(Object.assign(Object.assign({},W(t.toJSON(),["username","description"])),{groups:i,permissions:r,profile:n}))})}};xe([m("/login"),_(e=>({username:e.string().min(3).required(),password:e.string().min(5).required(),remember:e.boolean().parse().empty(!1)})),Ce(0,u()),Ce(1,c("user-agent")),Ce(2,f("username")),Ce(3,f("password")),Ce(4,f("remember")),Ce(5,E()),De("design:type",Function),De("design:paramtypes",[r,String,String,String,Boolean,Application]),De("design:returntype",Promise)],_t.prototype,"login",null),xe([v("/logout"),He(),Ce(0,u()),Ce(1,qe()),Ce(2,We()),Ce(3,E()),De("design:type",Function),De("design:paramtypes",[r,Ze,ot,Application]),De("design:returntype",Promise)],_t.prototype,"logout",null),xe([m("/renew"),_(e=>({token:e.string().required()})),Ce(0,u()),Ce(1,c("user-agent")),Ce(2,f("token")),Ce(3,E()),De("design:type",Function),De("design:paramtypes",[r,String,String,Application]),De("design:returntype",Promise)],_t.prototype,"renew",null),xe([v(),v("/me"),He(),Ce(0,u()),Ce(1,We()),De("design:type",Function),De("design:paramtypes",[r,ot]),De("design:returntype",Promise)],_t.prototype,"user",null),_t=xe([h("/auth"),g(yt),Ce(0,Ae(AuthService)),De("design:paramtypes",[AuthService])],_t);let ft=class CrudController extends ht{constructor(){super(...arguments),this._crud={}}CrudArgumentsCountAll(e){return{ctx:e}}CrudArgumentsCreate(e){return{ctx:e}}CrudArgumentsFindAll(e){return{ctx:e}}CrudArgumentsFindOne(e){return{ctx:e}}CrudArgumentsDelete(e){return{ctx:e}}CrudArgumentsUpdate(e){return{ctx:e}}CrudValidateCreate(){return this._crud}CrudValidateUpdate(){return this._crud}add(e){return Re(this,void 0,void 0,function*(){yield this.create(this.CrudArgumentsCreate(e))})}all(e){return Re(this,void 0,void 0,function*(){yield this.findAll(this.CrudArgumentsFindAll(e))})}rows(e){return Re(this,void 0,void 0,function*(){yield this.countAll(this.CrudArgumentsCountAll(e))})}del(e){return Re(this,void 0,void 0,function*(){yield this.delete(this.CrudArgumentsDelete(e))})}get(e){return Re(this,void 0,void 0,function*(){yield this.findOne(this.CrudArgumentsFindOne(e))})}set(e){return Re(this,void 0,void 0,function*(){yield this.update(this.CrudArgumentsUpdate(e))})}validate(e,t){return Re(this,void 0,void 0,function*(){var i,r;const n=J.object(t).parse(),{error:s,message:d,value:a}=yield n.validateAsync(null!==(r=null===(i=e.req)||void 0===i?void 0:i.body)&&void 0!==r?r:{});if(s)return e.error(new o(null!=d?d:s));e.req.body=a,e.next()})}validateCreate(e){return Re(this,void 0,void 0,function*(){const t=this.CrudValidateCreate();if(t==this._crud)return e.error(new s);yield this.validate(e,t)})}validateUpdate(e){return Re(this,void 0,void 0,function*(){const t=this.CrudValidateUpdate();if(t==this._crud)return e.error(new s);yield this.validate(e,t)})}};xe([m(),He(),Ue(),A("validateCreate"),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"add",null),xe([v(),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"all",null),xe([v("/count"),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"rows",null),xe([S("/:id"),He(),Ke(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"del",null),xe([v("/:id"),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"get",null),xe([O("/:id"),He(),Me(),A("validateUpdate"),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"set",null),xe([Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"validateCreate",null),xe([Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],ft.prototype,"validateUpdate",null),ft=xe([g(ht)],ft);let Et=class FilesController extends yt{constructor(){super(...arguments),this.directory=Env.API_HTTP_PUBLIC||"./",this.permissions=["delete","download","edit","list","read","rename","upload","write"]}files(e){return Re(this,void 0,void 0,function*(){let{path:t}=e.req;t=t.replace(/(^\/path(\/)?)|([\\.]{1,2}\/)/g,"");const i=be(this.directory,t);if(!Se(i)||!Oe(i).isDirectory())return e.error(new n);let r;return r=Te(i),r=r.map(e=>this.info(be(i,e))),r=r.filter(Q),Object.assign(Object.assign({},this.info(i)),{files:r})})}info(e){if(!Se(e))return null;const t=Oe(e),i=e.split("/").pop();e=e.replace(this.directory,"/").replace(/^\/\//,"/");const r=t.isDirectory();return{name:i,path:e.replace(/^\/\//,"/"),size:t.size,extension:r?"":"."+e.split(".").pop(),created:t.birthtime,modified:t.mtime,directory:r,symlink:t.isSymbolicLink()}}name(){return"files"}sync(){return Re(this,void 0,void 0,function*(){const e=this.name().toLowerCase(),t=Y(e.replace(/_/g," ")," "),i={description:"",state:!0};yield $(this.permissions,r=>Re(this,void 0,void 0,function*(){const n=`${e}.${r}`;yield et.findOrCreate({where:{key:n},defaults:Object.assign({name:`${t} ${Y(r," ")}`,key:n,tags:t},i)})}))})}};xe([v("/path"),v("/path/*path"),He(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],Et.prototype,"files",null),Et=xe([h("/files"),g(yt)],Et);let At=class ForgotController extends yt{check(e,t){return Re(this,void 0,void 0,function*(){const i=yield this.find(t);if(!i||!i.user)return e.error(new n);i.secret=z(),yield i.save(),e.json({token:i.secret})})}forgot(e,t,i){return Re(this,void 0,void 0,function*(){yield G(Env.SLEEP_FORGOT);const r=t,s=yield ot.findOne({where:{[ge.or]:[{email:r},{username:t}]}}).catch(this.error);if(!s)return e.error(new n);const o=z(),d=Date.now()+6e4*Env.API_TOKEN_FORGOT,a=yield lt.create({token:o,timestamp:d,state:!0,user_id:s.id});e.success("OK"),i.emit("forgot.send",{ctx:e,forgot:a,user:s}),this.emit("forgot.send",{ctx:e,forgot:a,user:s})})}reset(e,t,i,r){return Re(this,void 0,void 0,function*(){const i=yield this.find(t);if(!i)return e.error(new n);i.state=!1,yield i.save().catch(this.error),yield i.user.save().catch(this.error),e.success("OK"),r.emit("forgot.reset",{ctx:e,user:i.user}),this.emit("forgot.reset",{ctx:e,user:i.user})})}find(e){return Re(this,void 0,void 0,function*(){return yield lt.findOne({where:{token:e,state:!0},include:[{model:ot,required:!0}]}).catch(this.error)})}};xe([v("/check"),_(e=>({token:e.string().required()})),Ce(0,u()),Ce(1,f("token")),De("design:type",Function),De("design:paramtypes",[r,String]),De("design:returntype",Promise)],At.prototype,"check",null),xe([m(),_(e=>({username:e.string().min(4).required()})),Ce(0,u()),Ce(1,f("username")),Ce(2,E()),De("design:type",Function),De("design:paramtypes",[r,String,Application]),De("design:returntype",Promise)],At.prototype,"forgot",null),xe([m("/reset"),_(e=>({token:e.string().required(),password:e.string().min(5).required()})),Ce(0,u()),Ce(1,f("token")),Ce(2,f("password")),Ce(3,E()),De("design:type",Function),De("design:paramtypes",[r,String,String,Application]),De("design:returntype",Promise)],At.prototype,"reset",null),At=xe([h("/forgot"),g(yt)],At);let St=class GroupsController extends ft{constructor(){super(...arguments),this.model=it,this.schemaCreate={name:J.string().required(),key:J.string().required(),tags:J.string().empty(""),description:J.string().empty(""),state:J.boolean().parse().required()},this.schemaUpdate={name:J.string().empty(),tags:J.string().empty(),description:J.string().empty(),state:J.boolean().parse().empty()}}CrudValidateCreate(){return this.schemaCreate}CrudalidateUpdate(){return this.schemaUpdate}};St=xe([h("/groups"),g(ft)],St);let Ot=class GroupsPermissionsController extends ht{constructor(){super(...arguments),this.model=tt,this.permissions=[]}all(e,t){return Re(this,void 0,void 0,function*(){const i=this.include(et,{ctx:e,required:!0});i&&(yield this.findAll({ctx:e,options:{where:{group_id:t},include:[i]},callback:e=>e.map(e=>e.permission),query:!1}))})}set(e,t,i){return Re(this,void 0,void 0,function*(){const r=yield it.findByPk(t,{attributes:["id"]}).catch(this.error);if(!r)return e.error(new n);const s=r.id,o=[];yield $(X(i),t=>Re(this,void 0,void 0,function*(){var i;if(!(yield et.count({where:{id:t}})))return;const[r]=null!==(i=yield tt.findOrCreate(Object.assign({where:{group_id:s,permission_id:t},defaults:{group_id:s,permission_id:t}},{ctx:e})).catch(this.error))&&void 0!==i?i:[];r&&o.push(t)})),yield tt.destroy(Object.assign({where:{group_id:s,permission_id:{[ge.notIn]:o}},individualHooks:!0},{ctx:e})).catch(this.error),e.success("OK")})}};xe([v("/:id/permissions"),He(),Be("groups"),Ce(0,u()),Ce(1,T("id")),De("design:type",Function),De("design:paramtypes",[r,String]),De("design:returntype",Promise)],Ot.prototype,"all",null),xe([O("/:id/permissions"),He(),Me("groups"),_(e=>({permissions:e.array(e.string())})),Ce(0,u()),Ce(1,T("id")),Ce(2,f("permissions")),De("design:type",Function),De("design:paramtypes",[r,String,Array]),De("design:returntype",Promise)],Ot.prototype,"set",null),Ot=xe([h("/groups"),g(ht)],Ot);let Tt=class LogsController extends ht{constructor(){super(...arguments),this.model=ut,this.permissions=["read"]}all(e){return Re(this,void 0,void 0,function*(){yield this.findAll({ctx:e,options:{order:[["created_at","DESC"]]},limited:!0})})}};xe([v(),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],Tt.prototype,"all",null),Tt=xe([h("/logs"),g(ht)],Tt);let It=class PermissionsController extends ft{constructor(){super(...arguments),this.model=et,this.schemaCreate={name:J.string().required(),key:J.string().required(),tags:J.string().empty(""),description:J.string().empty(""),state:J.boolean().parse().required()},this.schemaUpdate={name:J.string().empty(),tags:J.string().empty(),description:J.string().empty(),state:J.boolean().parse().empty()}}CrudValidateCreate(){return this.schemaCreate}CrudValidateUpdate(){return this.schemaUpdate}};It=xe([h("/permissions"),g(ft)],It);let Pt=class RegisterController extends yt{register(e,t,i){return Re(this,void 0,void 0,function*(){yield G(Env.SLEEP_REGISTRY);const{name:r,date:n,email:o,username:d,password:a,confirm:l}=t,u=d||o,c=C(a);if(yield ot.count({where:{[ge.or]:[{email:o},{username:u}]}}).catch(this.error))return e.error("INVALID EMAIL OR USERNAME");if(c!=C(l))return e.error("INVALID PASSWORD");const p=yield ot.create({name:r,email:o,username:u,password:c,avatar:"",date:n,description:"",state:!1}).catch(this.error);if(!p)return e.error(new s);const g=yield Xe.create({token:yield this.getId(),user_id:p.id,state:!0}).catch(this.error);if(!g)return yield p.destroy().catch(this.error),e.error(new s);e.success("OK"),i.emit("register.user",{ctx:e,register:g,user:p}),this.emit("register.user",{ctx:e,register:g,user:p})})}confirm(e,t,i){return Re(this,void 0,void 0,function*(){const r=yield Xe.findOne({where:{token:t,state:!0},include:[{model:ot,required:!0}]}).catch(this.error);if(!r)return e.error(new n);r.state=!1,yield r.save().catch(this.error),r.user.state=!0,yield r.user.save().catch(this.error),e.success("OK"),i.emit("register.confirm",{ctx:e,register:r,user:r.user}),this.emit("register.confirm",{ctx:e,register:r,user:r.user})})}send(e,t,i){return Re(this,void 0,void 0,function*(){const r=t,s=yield Xe.findOne({where:{state:!0},include:[{model:ot,required:!0,where:{[ge.or]:[{email:r},{username:t}]}}]}).catch(this.error);if(!s)return e.error(new n);e.success("OK"),i.emit("register.send",{ctx:e,register:s,user:s.user}),this.emit("register.send",{ctx:e,register:s,user:s.user})})}getId(){return Re(this,void 0,void 0,function*(){const e=K();return(yield Xe.count({where:{token:e}}))?this.getId():e})}};xe([m(),_(e=>({name:e.string().required(),date:e.date().required(),email:e.string().email().required(),username:e.string().min(4).empty(),password:e.string().min(5).required(),confirm:e.string().min(5).required()})),Ce(0,u()),Ce(1,f()),Ce(2,E()),De("design:type",Function),De("design:paramtypes",[r,Object,Application]),De("design:returntype",Promise)],Pt.prototype,"register",null),xe([m("/confirm"),_(e=>({token:e.string().required()})),Ce(0,u()),Ce(1,f("token")),Ce(2,E()),De("design:type",Function),De("design:paramtypes",[r,String,Application]),De("design:returntype",Promise)],Pt.prototype,"confirm",null),xe([m("/send"),_(e=>({username:e.string().min(4).required()})),Ce(0,u()),Ce(1,f("username")),Ce(2,E()),De("design:type",Function),De("design:paramtypes",[r,String,Application]),De("design:returntype",Promise)],Pt.prototype,"send",null),Pt=xe([h("/register"),g(yt)],Pt);let wt=class SetupController extends yt{setup(e){return Re(this,void 0,void 0,function*(){var t;if(yield ot.count())return e.success("OK");const i=yield ot.create({name:"Admin",email:"admin@localhost.com",username:"admin",description:"DEFAULT",state:!0},Object.assign({},{ctx:e}));yield nt.create({value:"admin",state:!0,user_id:i.id});const r={description:"API CORE",state:!0},n=yield it.create(Object.assign({name:"Admin",key:"admin",tags:"Admin"},r),Object.assign({},{ctx:e}));yield rt.create({user_id:i.id,group_id:n.id},Object.assign({},{ctx:e})),yield it.create(Object.assign({name:"User",key:"user",tags:"user"},r),Object.assign({},{ctx:e}));const s=null!==(t=yield et.findAll({where:{state:!0}}).catch(this.error))&&void 0!==t?t:[];yield $(s,t=>Re(this,void 0,void 0,function*(){yield tt.create({group_id:n.id,permission_id:t.id},Object.assign({},{ctx:e}))})),e.success("OK")})}};xe([v(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],wt.prototype,"setup",null),wt=xe([h("/setup"),g(yt)],wt);let bt=class UsersController extends ht{constructor(){super(),this.model=ot,this.path=Env.API_HTTP_PUBLIC+"/avatar",Env.API_HTTP_PUBLIC&&Se(Env.API_HTTP_PUBLIC)&&!Se(this.path)&&Ie(this.path)}avatar(e,t){return Re(this,void 0,void 0,function*(){var i,r;const n=e.getFiles();if(n.file&&n.file.length){const{filename:o,path:d}=null!==(i=n.file[0])&&void 0!==i?i:{};if(o){const[i]=null!==(r=yield st.findOrCreate(Object.assign({where:{user_id:t.id},defaults:{user_id:t.id}},{ctx:e})).catch(this.error))&&void 0!==r?r:[];if(!i)return Pe(d).isFile()&&we(d),e.error(new s);const n=this.path+"/"+i.data.avatar;if(Se(n)&&Pe(n).isFile()&&we(n),i.changed("data",!0),i.data.avatar=o,yield i.save(Object.assign({},{ctx:e})).catch(this.error))return e.success("OK")}}e.success("Record not Changed")})}token_new(){return Re(this,void 0,void 0,function*(){var e;const t=K();return(null!==(e=yield this.model.count({where:{token:t}}).catch(this.error))&&void 0!==e?e:0)?this.token_new():t})}add(e){return Re(this,void 0,void 0,function*(){yield this.create({ctx:e})})}all(e){return Re(this,void 0,void 0,function*(){yield this.findAll({ctx:e})})}rows(e){return Re(this,void 0,void 0,function*(){yield this.countAll({ctx:e})})}del(e,t,i){return Re(this,void 0,void 0,function*(){if(i.id==t)return e.error(new n);yield this.delete({ctx:e})})}get(e){return Re(this,void 0,void 0,function*(){yield this.findOne({ctx:e})})}set(e){return Re(this,void 0,void 0,function*(){yield this.update({ctx:e})})}};xe([m("/avatar"),He(),I({file:{count:1,path:`${Env.API_HTTP_PUBLIC}/avatar`,types:["image/bmp","image/jpeg","image/png"],size:32}}),Ce(0,u()),Ce(1,We()),De("design:type",Function),De("design:paramtypes",[r,ot]),De("design:returntype",Promise)],bt.prototype,"avatar",null),xe([m(),He(),Ue(),_(e=>({name:e.string().required(),email:e.string().email().required(),username:e.string().min(3).required(),description:e.string().empty(""),state:e.boolean().parse().empty(!0)})),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],bt.prototype,"add",null),xe([v(),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],bt.prototype,"all",null),xe([v("/count"),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],bt.prototype,"rows",null),xe([S("/:id"),He(),Ke(),Ce(0,u()),Ce(1,T("id")),Ce(2,We()),De("design:type",Function),De("design:paramtypes",[r,String,ot]),De("design:returntype",Promise)],bt.prototype,"del",null),xe([v("/:id"),He(),Be(),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],bt.prototype,"get",null),xe([O("/:id"),He(),Me(),_(e=>({name:e.string().empty(),email:e.string().email().empty(),username:e.string().min(3).empty(),description:e.string().empty(),state:e.boolean().parse().empty()})),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],bt.prototype,"set",null),bt=xe([h("/users"),g(ht),De("design:paramtypes",[])],bt);let xt=class UsersGroupsController extends ht{constructor(){super(...arguments),this.model=rt,this.permissions=[]}all(e,t){return Re(this,void 0,void 0,function*(){if(!(yield ot.findByPk(t,{attributes:["id"]}).catch(this.error)))return e.error(new n("User Not Found"));const i=this.include(it,{ctx:e,required:!0});i&&(yield this.findAll({ctx:e,options:{where:{user_id:t},include:[i]},callback:e=>e.map(e=>e.group),query:!1}))})}set(e,t,i){return Re(this,void 0,void 0,function*(){const r=yield ot.findByPk(t,{attributes:["id"]}).catch(this.error);if(!r)return e.error(new n("User Not Found"));const s=r.id,o=[];yield $(X(i),t=>Re(this,void 0,void 0,function*(){var i;if(!(yield it.count({where:{id:t}})))return;const[r]=null!==(i=yield rt.findOrCreate(Object.assign({where:{user_id:s,group_id:t},defaults:{user_id:s,group_id:t}},{ctx:e})).catch(this.error))&&void 0!==i?i:[];r&&o.push(t)})),yield rt.destroy(Object.assign({where:{user_id:s,group_id:{[ge.notIn]:o}},individualHooks:!0},{ctx:e})).catch(this.error),e.success("OK")})}};xe([v("/:id/groups"),He(),Be("users"),Ce(0,u()),Ce(1,T("id")),De("design:type",Function),De("design:paramtypes",[r,String]),De("design:returntype",Promise)],xt.prototype,"all",null),xe([O("/:id/groups"),He(),Me("users"),_(e=>({groups:e.array(e.string())})),Ce(0,u()),Ce(1,T("id")),Ce(2,f("groups")),De("design:type",Function),De("design:paramtypes",[r,String,Array]),De("design:returntype",Promise)],xt.prototype,"set",null),xt=xe([h("/users"),g(ht)],xt);let Ct=class UsersProfilesController extends ht{constructor(){super(...arguments),this.model=st,this.permissions=[]}all(e){return Re(this,void 0,void 0,function*(){yield this.findAll({ctx:e})})}get(e,t){return Re(this,void 0,void 0,function*(){var i;const[r]=null!==(i=yield st.findOrCreate(Object.assign({where:{user_id:t},defaults:{user_id:t}},{ctx:e})).catch(this.error))&&void 0!==i?i:[];if(!r)return e.error(new n);e.json(r)})}set(e,t,i){return Re(this,void 0,void 0,function*(){var r;const s=yield ot.findByPk(t,{attributes:["id"]}).catch(this.error);if(!s)return e.error(new n);const o=s.id,d=Z(i,e=>String(e)),[a,l]=null!==(r=yield st.findOrCreate(Object.assign({where:{user_id:o},defaults:{data:d,user_id:o}},{ctx:e})).catch(this.error))&&void 0!==r?r:[];a&&!l&&(a.changed("data",!0),a.data=L(a.data,d),yield a.save(Object.assign({},{ctx:e})).catch(this.error)),e.success("OK")})}};xe([v("/profiles"),He(),Be("users"),Ce(0,u()),De("design:type",Function),De("design:paramtypes",[r]),De("design:returntype",Promise)],Ct.prototype,"all",null),xe([v("/:id/profiles"),v("/profiles/:id"),He(),Be("users"),Ce(0,u()),Ce(1,T("id")),De("design:type",Function),De("design:paramtypes",[r,String]),De("design:returntype",Promise)],Ct.prototype,"get",null),xe([O("/:id/profiles"),O("/profiles/:id"),He(),Me("users"),Ce(0,u()),Ce(1,T("id")),Ce(2,f()),De("design:type",Function),De("design:paramtypes",[r,String,Object]),De("design:returntype",Promise)],Ct.prototype,"set",null),Ct=xe([h("/users"),g(ht)],Ct);export{Application,ze as Audit,dt as AuditModel,mt as AuditsController,_t as AuthController,AuthService,yt as BaseController,Ue as CanCreate,Ke as CanDelete,Be as CanRead,Me as CanUpdate,Config,at as ConfigModel,ft as CrudController,Env,Et as FilesController,At as ForgotController,lt as ForgotModel,it as GroupModel,tt as GroupPermissionModel,St as GroupsController,Ot as GroupsPermissionsController,Le as HasGroups,Fe as HasPermissions,He as IsAuthenticated,Log,ut as LogModel,Loggable,Tt as LogsController,ct as MigrationModel,ht as ModelController,ModelService,et as PermissionModel,It as PermissionsController,Pt as RegisterController,Xe as RegisterModel,qe as ReqAuth,Ye as ReqGroup,$e as ReqGroups,Ge as ReqPermission,Ve as ReqPermissions,We as ReqUser,vt as Service,pt as SessionModel,SessionStore,wt as SetupController,Ze as UserAuthModel,rt as UserGroupModel,gt as UserMetadataModel,ot as UserModel,nt as UserPasswordModel,st as UserProfileModel,UserService,bt as UsersController,xt as UsersGroupsController,Ct as UsersProfilesController,je as canFactory,Ne as sequelize};
|
|
7
|
+
import{Use as e,UnauthorizedError as t,parameterFactory as i,Context as r,NotFoundError as n,InternalServerError as s,ValidationError as o,StatusCodes as d,BadRequestError as a,Application as l,Ctx as u,Headers as c,Session as p,Extends as v,Get as g,Query as y,Controller as h,Post as m,Validate as E,Body as _,App as f,Middleware as A,Delete as S,Put as O,Params as T,All as P,Upload as I}from"@andrewcaires/express";import{Env as w,Log as b,LogColor as C,Hash as R,sha256 as x}from"@andrewcaires/node";import{isUndefined as D,isFunction as N,includesArray as L,getConstructor as M,noop as k,uuidv7 as K,isString as j,clamp as B,merge as F,isArray as H,clone as U,forEachAsync as q,upperCamelCase as Y,uniqueArray as $,forEachSeries as W,sleep as G,EventEmitter as V,Validation as z,isDef as J,uniqueID as Q}from"@andrewcaires/utils.js";import{WebSocketServer as X}from"ws";import{Sequelize as Z,Hooks as ee,Column as te,Association as ie,Database as re,ModelName as ne,Version as se,Model as oe,Unique as de,DefaultValue as ae,Op as le,Where as ue}from"@andrewcaires/sequelize";import{config as ce}from"dotenv";import{Store as pe}from"express-session";import ve from"jsonwebtoken";import{Trait as ge,Inject as ye}from"@andrewcaires/decorator";import{hashSync as he,verify as me}from"@node-rs/argon2";import{existsSync as Ee,statSync as _e,readdirSync as fe,mkdirSync as Ae,lstatSync as Se,rmSync as Oe}from"fs";import{join as Te}from"path";function Pe(e,t,i,r){var n,s=arguments.length,o=s<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,i):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(e,t,i,r);else for(var d=e.length-1;d>=0;d--)(n=e[d])&&(o=(s<3?n(o):s>3?n(t,i,o):n(t,i))||o);return s>3&&o&&Object.defineProperty(t,i,o),o}function Ie(e,t){return function(i,r){t(i,r,e)}}function we(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function be(e,t,i,r){return new(i||(i=Promise))(function(n,s){function o(e){try{a(r.next(e))}catch(e){s(e)}}function d(e){try{a(r.throw(e))}catch(e){s(e)}}function a(e){var t;e.done?n(e.value):(t=e.value,t instanceof i?t:new i(function(e){e(t)})).then(o,d)}a((r=r.apply(e,t||[])).next())})}var Ce;"function"==typeof SuppressedError&&SuppressedError,ce({quiet:!0});class Env extends w{}Ce=Env,Env.BASE_NAME=Ce.getString("BASE_NAME",""),Env.CONSOLE_ERROR=Ce.getBool("CONSOLE_ERROR",!1),Env.DATABASE_URI=Ce.getString("DATABASE_URI","",!0),Env.DATABASE_LOG=Ce.getBool("DATABASE_LOG",!1),Env.DATABASE_AUDIT=Ce.getBool("DATABASE_AUDIT",!0),Env.DATABASE_PREFIX=Ce.getString("DATABASE_PREFIX",""),Env.DATABASE_TIMEZONE=Ce.getString("DATABASE_TIMEZONE","+00:00"),Env.HTTP_CROSS=Ce.getString("HTTP_CROSS",""),Env.HTTP_CRT=Ce.getFile("HTTP_CRT"),Env.HTTP_KEY=Ce.getFile("HTTP_KEY"),Env.MODEL_LIMIT=Ce.getInt("MODEL_LIMIT",10),Env.MODEL_LIMIT_MAX=Ce.getInt("MODEL_LIMIT_MAX",100),Env.PASSWORD_ALGORITHM=Ce.getInt("PASSWORD_ALGORITHM",2),Env.PASSWORD_MEMORY_COST=Ce.getInt("PASSWORD_MEMORY_COST",4096),Env.PASSWORD_PARALLELISM=Ce.getInt("PASSWORD_PARALLELISM",1),Env.PASSWORD_PEPPER=Ce.getString("PASSWORD_PEPPER",""),Env.PASSWORD_TIME_COST=Ce.getInt("PASSWORD_TIME_COST",3),Env.SLEEP_AUTH=Ce.getMs("SLEEP_AUTH","2s"),Env.SLEEP_FORGOT=Ce.getMs("SLEEP_FORGOT","5s"),Env.SLEEP_REGISTRY=Ce.getMs("SLEEP_REGISTRY","5s"),Env.TOKEN_CRT=Ce.getFile("TOKEN_CRT"),Env.TOKEN_KEY=Ce.getFile("TOKEN_KEY"),Env.TOKEN_TTL=Ce.getMs("TOKEN_TTL","1d"),Env.TOKEN_TYPE=Ce.getString("TOKEN_TYPE","bearer"),Env.API_BODYPARSER_STRICT=Ce.getBool("API_BODYPARSER_STRICT",!0),Env.API_BODYPARSER_INFLATE=Ce.getBool("API_BODYPARSER_INFLATE",!0),Env.API_BODYPARSER_LIMIT=Ce.getString("API_BODYPARSER_LIMIT","10mb"),Env.API_BODYPARSER_TYPE=Ce.getString("API_BODYPARSER_TYPE","json"),Env.API_HTTP_PORT=Ce.getInt("API_HTTP_PORT",3e3),Env.API_HTTP_PUBLIC=Ce.getString("API_HTTP_PUBLIC",""),Env.API_HTTP_HYBRID=Ce.getBool("API_HTTP_HYBRID",!1),Env.API_HTTP_NOTFOUND=Ce.getString("API_HTTP_NOTFOUND",""),Env.API_HTTP_PATH=Ce.getString("API_HTTP_PATH","/api"),Env.API_HTTP_REDIRECT=Ce.getString("API_HTTP_REDIRECT",""),Env.API_KEY_HASH=Ce.getString("API_KEY_HASH",""),Env.API_KEY_HEADER=Ce.getString("API_KEY_HEADER","x-api-key"),Env.API_SESSION_NAME=Ce.getString("API_SESSION_NAME","session.id"),Env.API_SESSION_LIFETIME=Ce.getMs("API_SESSION_LIFETIME","1d"),Env.API_SESSION_SECRET=Ce.getString("API_SESSION_SECRET","yoursecret"),Env.API_TOKEN_COOKIE=Ce.getString("API_TOKEN_COOKIE","authorization"),Env.API_TOKEN_COOKIE_SET=Ce.getBool("API_TOKEN_COOKIE_SET",!0),Env.API_TOKEN_FORGOT=Ce.getMs("API_TOKEN_FORGOT","1d"),Env.API_TOKEN_HEADER=Ce.getString("API_TOKEN_HEADER","authorization"),Env.API_TOKEN_RENEW=Ce.getMs("API_TOKEN_RENEW","1d"),Env.API_USER_HISTORY=Ce.getBool("API_USER_HISTORY",!0),Env.API_WEBSOCKET_START=Ce.getBool("API_WEBSOCKET_START",!1);const Re=new Z({uri:Env.DATABASE_URI,logging:!!Env.DATABASE_LOG&&(e=>b.console(C.Magenta,"sql",e)),prefix:Env.DATABASE_PREFIX,timezone:Env.DATABASE_TIMEZONE});var xe,De,Ne;!function(e){e.CREATE="CREATE",e.DELETE="DELETE",e.UPDATE="UPDATE"}(xe||(xe={})),function(e){e.INFO="info",e.WARN="warn",e.ERROR="error"}(De||(De={})),function(e){e.ACTIVE="active",e.PENDING="pending",e.DISABLED="disabled",e.LOCKED="locked",e.ARCHIVED="archived"}(Ne||(Ne={}));const Le=i=>r=>(n,s,o)=>{e(e=>be(void 0,void 0,void 0,function*(){var n;D(r)&&(r=N(e.controller.name)?e.controller.name():null===(n=e.controller.constructor)||void 0===n?void 0:n.name);const{permissions:s=[]}=e.req;if(s.includes(`${r}.${i}`))return e.next();e.error(new t("Access Denied"))}))(n,s,o)},Me=Le("create"),ke=Le("delete"),Ke=Le("read"),je=Le("update"),Be=(...i)=>(r,n,s)=>{e(e=>be(void 0,void 0,void 0,function*(){const{groups:r=[]}=e.req;if(L(i,r))return e.next();e.error(new t("Access Denied"))}))(r,n,s)},Fe=(...i)=>(r,n,s)=>{e(e=>be(void 0,void 0,void 0,function*(){const{permissions:r}=e.req;if(L(i,null!=r?r:[]))return e.next();e.error(new t("Access Denied"))}))(r,n,s)},He=()=>(i,r,n)=>{e(e=>be(void 0,void 0,void 0,function*(){yield(e=>be(void 0,void 0,void 0,function*(){var i,r,n,s;if(e.req.auth&&e.req.user)return e.next();const{[Env.API_KEY_HEADER]:o,[Env.API_TOKEN_HEADER]:d,"user-agent":a}=e.getHeaders(),l=e.getQuery(Env.API_KEY_HEADER),u=(null===(i=o||l)||void 0===i?void 0:i.toString())||"";let c=e.getCookie(Env.API_TOKEN_COOKIE);c=`${Env.TOKEN_TYPE} ${c}`;const[p,v]=null!==(n=null===(r=d||c)||void 0===r?void 0:r.split(" "))&&void 0!==n?n:["",""];if(!(u||v&&p.toLowerCase()==Env.TOKEN_TYPE))return e.error(new t("Invalid token"));const{authService:g,userService:y}=e.app,{auth:h,user:m}=null!==(s=yield(u?g.key(u.toString()):g.user(v)).catch(t=>{e.error(t)}))&&void 0!==s?s:{};if(!h||!m)return;if(!u&&h.user_agent&&h.user_agent!=a)return e.error(new t("Invalid token"));e.req.auth=h,e.req.user=m;const{groups:E,permissions:_}=yield y.getGroupsPermissions(m.id);e.req.groups=E,e.req.permissions=_,e.next()}))(e)}))(i,r,n)},Ue=i(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.auth}),qe=i(({ctx:e,key:t})=>{var i,r;return(null!==(r=null===(i=e.req)||void 0===i?void 0:i.groups)&&void 0!==r?r:[]).includes(t)}),Ye=i(({ctx:e})=>{var t,i;return null!==(i=null===(t=e.req)||void 0===t?void 0:t.groups)&&void 0!==i?i:[]}),$e=i(({ctx:e,key:t})=>{var i,r;return(null!==(r=null===(i=e.req)||void 0===i?void 0:i.permissions)&&void 0!==r?r:[]).includes(t)}),We=i(({ctx:e})=>{var t,i;return null!==(i=null===(t=e.req)||void 0===t?void 0:t.permissions)&&void 0!==i?i:[]}),Ge=i(({ctx:e})=>{var t;return null===(t=e.req)||void 0===t?void 0:t.user}),Ve=(e,t,i)=>{var n,s,o,d,a,l,u,c,p,v;const g=i.ctx instanceof r?i.ctx:null,y=null!==(d=null!==(o=null===(s=null===(n=null==g?void 0:g.req)||void 0===n?void 0:n.user)||void 0===s?void 0:s.id)&&void 0!==o?o:i.user_id)&&void 0!==d?d:null,h={type:e,model:null!==(a=null==t?void 0:t.getTableName())&&void 0!==a?a:null,source:null!==(l=Env.BASE_NAME)&&void 0!==l?l:"",state:!0,user_id:y};return g?Object.assign(Object.assign({},h),{full_url:null!==(u=g.getUrl())&&void 0!==u?u:"",ip_address:null!==(c=g.getIp())&&void 0!==c?c:"",user_agent:null!==(v=null===(p=g.req)||void 0===p?void 0:p.headers["user-agent"])&&void 0!==v?v:""}):h},ze=e=>{const t=M(e),i=t.primaryKeyAttribute;return{model:t,record_id:i?e.getDataValue(i):null}},Je={afterCreate:(e,t)=>be(void 0,void 0,void 0,function*(){const{model:i,record_id:r}=ze(e);yield ot.create(Object.assign(Object.assign({},Ve(xe.CREATE,i,t)),{record:r,currently:e.toJSON()})).catch(b.catch)}),afterUpdate:(e,t)=>be(void 0,void 0,void 0,function*(){const{model:i,record_id:r}=ze(e),n={},s={},o=e.changed();o&&o.length&&o.forEach(t=>{n[t]=e.getDataValue(t),s[t]=e.previous(t)}),yield ot.create(Object.assign(Object.assign({},Ve(xe.UPDATE,i,t)),{record:r,currently:n,previously:s})).catch(b.catch)}),afterDestroy:(e,t)=>be(void 0,void 0,void 0,function*(){const{model:i,record_id:r}=ze(e);yield ot.create(Object.assign(Object.assign({},Ve(xe.DELETE,i,t)),{record:r,currently:e.toJSON()})).catch(b.catch)})},Qe=e=>(D(e)&&(e=Env.DATABASE_AUDIT),ee(e?Je:{}));let Xe=class RegisterModel extends oe{};Pe([te.Id(7),we("design:type",String)],Xe.prototype,"id",void 0),Pe([te.String(),we("design:type",String)],Xe.prototype,"secret",void 0),Pe([te.FullDate(),we("design:type",Number)],Xe.prototype,"expired_at",void 0),Pe([te.Boolean(),we("design:type",Boolean)],Xe.prototype,"active",void 0),Pe([te.CreatedAt(),we("design:type",Date)],Xe.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],Xe.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],Xe.prototype,"deleted_at",void 0),Pe([ie.BelongsTo(()=>st),we("design:type",String)],Xe.prototype,"user_id",void 0),Xe=Pe([re(Re),ne("register"),se("5.1.0")],Xe);let Ze=class UserAuthModel extends oe{};Pe([te.Id(7),we("design:type",String)],Ze.prototype,"id",void 0),Pe([te.String(),we("design:type",String)],Ze.prototype,"secret",void 0),Pe([te.String(),we("design:type",String)],Ze.prototype,"ip_address",void 0),Pe([te.String(),we("design:type",String)],Ze.prototype,"user_agent",void 0),Pe([te.FullDate(),we("design:type",Object)],Ze.prototype,"revoked_at",void 0),Pe([te.CreatedAt(),we("design:type",Date)],Ze.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],Ze.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],Ze.prototype,"deleted_at",void 0),Pe([ie.BelongsTo(()=>st),we("design:type",String)],Ze.prototype,"user_id",void 0),Ze=Pe([re(Re),ne("users_auth"),se("5.1.0")],Ze);let et=class PermissionModel extends oe{};Pe([te.Id(7),we("design:type",String)],et.prototype,"id",void 0),Pe([te.String(),de(!0),we("design:type",String)],et.prototype,"name",void 0),Pe([te.Text(),we("design:type",String)],et.prototype,"description",void 0),Pe([te.Boolean(),we("design:type",Boolean)],et.prototype,"active",void 0),Pe([te.CreatedAt(),we("design:type",Date)],et.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],et.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],et.prototype,"deleted_at",void 0),Pe([ie.HasMany(()=>tt),we("design:type",Array)],et.prototype,"groups_permissions",void 0),et=Pe([re(Re),ne("permission"),Qe(),se("5.1.0")],et);let tt=class GroupPermissionModel extends oe{};Pe([te.Id(7),we("design:type",String)],tt.prototype,"id",void 0),Pe([te.CreatedAt(),we("design:type",Date)],tt.prototype,"created_at",void 0),Pe([ie.BelongsTo(()=>it),we("design:type",String)],tt.prototype,"group_id",void 0),Pe([ie.BelongsTo(()=>et),we("design:type",String)],tt.prototype,"permission_id",void 0),tt=Pe([re(Re),ne("groups_permissions"),Qe(),se("5.1.0")],tt);let it=class GroupModel extends oe{};Pe([te.Id(7),we("design:type",String)],it.prototype,"id",void 0),Pe([te.String(),de(!0),we("design:type",String)],it.prototype,"name",void 0),Pe([te.Text(),we("design:type",String)],it.prototype,"description",void 0),Pe([te.Boolean(),we("design:type",Boolean)],it.prototype,"active",void 0),Pe([te.CreatedAt(),we("design:type",Date)],it.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],it.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],it.prototype,"deleted_at",void 0),Pe([ie.HasMany(()=>rt),we("design:type",Array)],it.prototype,"users_groups",void 0),Pe([ie.HasMany(()=>tt),we("design:type",Array)],it.prototype,"groups_Permission",void 0),it=Pe([re(Re),ne("group"),Qe(),se("5.1.0")],it);let rt=class UserGroupModel extends oe{};Pe([te.Id(7),we("design:type",String)],rt.prototype,"id",void 0),Pe([te.CreatedAt(),we("design:type",Date)],rt.prototype,"created_at",void 0),Pe([ie.BelongsTo(()=>it),we("design:type",String)],rt.prototype,"group_id",void 0),Pe([ie.BelongsTo(()=>st),we("design:type",String)],rt.prototype,"user_id",void 0),rt=Pe([re(Re),ne("users_groups"),Qe(),se("5.1.0")],rt);let nt=class UserPasswordModel extends oe{};Pe([te.Id(7),we("design:type",String)],nt.prototype,"id",void 0),Pe([te.String(),we("design:type",String)],nt.prototype,"hash",void 0),Pe([te.FullDate(),we("design:type",Object)],nt.prototype,"revoked_at",void 0),Pe([te.CreatedAt(),we("design:type",Date)],nt.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],nt.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],nt.prototype,"deleted_at",void 0),Pe([ie.BelongsTo(()=>st),we("design:type",String)],nt.prototype,"user_id",void 0),nt=Pe([re(Re),ne("users_password"),Qe(),se("5.1.0")],nt);let st=class UserModel extends oe{};Pe([te.Id(7),we("design:type",String)],st.prototype,"id",void 0),Pe([te.String(),we("design:type",String)],st.prototype,"name",void 0),Pe([te.String(),we("design:type",String)],st.prototype,"username",void 0),Pe([te.String(),we("design:type",String)],st.prototype,"email",void 0),Pe([te.Text(),we("design:type",String)],st.prototype,"notes",void 0),Pe([te.Enum(Ne),ae(Ne.ACTIVE),we("design:type",String)],st.prototype,"status",void 0),Pe([te.Json(),ae(()=>({})),we("design:type",Object)],st.prototype,"metadata",void 0),Pe([te.CreatedAt(),we("design:type",Date)],st.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],st.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],st.prototype,"deleted_at",void 0),Pe([ie.HasMany(()=>ot),we("design:type",Array)],st.prototype,"audits",void 0),Pe([ie.HasMany(()=>Ze),we("design:type",Array)],st.prototype,"users_auths",void 0),Pe([ie.HasMany(()=>Xe),we("design:type",Array)],st.prototype,"registers",void 0),Pe([ie.HasMany(()=>rt),we("design:type",Array)],st.prototype,"users_groups",void 0),Pe([ie.HasMany(()=>nt),we("design:type",Array)],st.prototype,"users_passwords",void 0),st=Pe([re(Re),ne("user"),Qe(),se("5.1.0")],st);let ot=class AuditModel extends oe{};Pe([te.Id(7),we("design:type",String)],ot.prototype,"id",void 0),Pe([te.Enum(xe),we("design:type",String)],ot.prototype,"type",void 0),Pe([te.String(),we("design:type",String)],ot.prototype,"model",void 0),Pe([te.String(),we("design:type",String)],ot.prototype,"record",void 0),Pe([te.Json(),we("design:type",Object)],ot.prototype,"currently",void 0),Pe([te.Json(),we("design:type",Object)],ot.prototype,"previously",void 0),Pe([te.Text(),we("design:type",String)],ot.prototype,"full_url",void 0),Pe([te.String(),we("design:type",String)],ot.prototype,"ip_address",void 0),Pe([te.String(),we("design:type",String)],ot.prototype,"user_agent",void 0),Pe([te.String(),we("design:type",String)],ot.prototype,"source",void 0),Pe([te.CreatedAt(),we("design:type",Date)],ot.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],ot.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],ot.prototype,"deleted_at",void 0),Pe([ie.BelongsTo(()=>st),we("design:type",String)],ot.prototype,"user_id",void 0),ot=Pe([re(Re),ne("audit"),se("5.1.0")],ot);let dt=class ConfigModel extends oe{};Pe([te.Id(7),we("design:type",String)],dt.prototype,"id",void 0),Pe([te.String(),we("design:type",String)],dt.prototype,"key",void 0),Pe([te.Text(),we("design:type",String)],dt.prototype,"value",void 0),Pe([te.CreatedAt(),we("design:type",Date)],dt.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],dt.prototype,"updated_at",void 0),dt=Pe([re(Re),ne("configs"),se("5.1.0")],dt);let at=class ForgotModel extends oe{};Pe([te.Id(7),we("design:type",String)],at.prototype,"id",void 0),Pe([te.String(),we("design:type",String)],at.prototype,"secret",void 0),Pe([te.FullDate(),we("design:type",Number)],at.prototype,"expired_at",void 0),Pe([te.Boolean(),we("design:type",Boolean)],at.prototype,"state",void 0),Pe([te.CreatedAt(),we("design:type",Date)],at.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],at.prototype,"updated_at",void 0),Pe([te.DeletedAt(),we("design:type",Object)],at.prototype,"deleted_at",void 0),Pe([ie.BelongsTo(()=>st),we("design:type",String)],at.prototype,"user_id",void 0),at=Pe([re(Re),ne("forgot"),se("5.1.0")],at);let lt=class LogModel extends oe{};Pe([te.Id(7),we("design:type",String)],lt.prototype,"id",void 0),Pe([te.Enum(De),ae(De.INFO),we("design:type",String)],lt.prototype,"level",void 0),Pe([te.Text(),we("design:type",String)],lt.prototype,"message",void 0),Pe([te.String(),we("design:type",String)],lt.prototype,"source",void 0),Pe([te.CreatedAt(),we("design:type",Date)],lt.prototype,"created_at",void 0),lt=Pe([re(Re),ne("log"),se("5.1.0")],lt);let ut=class MigrationModel extends oe{};Pe([te.Id(7),we("design:type",String)],ut.prototype,"id",void 0),Pe([te.String(),we("design:type",String)],ut.prototype,"model",void 0),Pe([te.String(),we("design:type",String)],ut.prototype,"version",void 0),Pe([te.CreatedAt(),we("design:type",Date)],ut.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],ut.prototype,"updated_at",void 0),ut=Pe([re(Re),ne("migration"),se("5.1.0")],ut);let ct=class SessionModel extends oe{};Pe([te.Id(7),we("design:type",String)],ct.prototype,"id",void 0),Pe([te.Json(),we("design:type",Object)],ct.prototype,"data",void 0),Pe([te.FullDate(),we("design:type",Date)],ct.prototype,"expired_at",void 0),Pe([te.CreatedAt(),we("design:type",Date)],ct.prototype,"created_at",void 0),Pe([te.UpdatedAt(),we("design:type",Date)],ct.prototype,"updated_at",void 0),ct=Pe([re(Re),ne("session"),se("5.1.0")],ct);class Log{static error(e,t,i){return be(this,void 0,void 0,function*(){yield this.writeRaw("error",e,t,i)})}static info(e,t,i){return be(this,void 0,void 0,function*(){yield this.writeRaw("info",e,t,i)})}static warn(e,t,i){return be(this,void 0,void 0,function*(){yield this.writeRaw("warn",e,t,i)})}static writeRaw(e,t,i,r){return be(this,void 0,void 0,function*(){var n;r&&b.console(null!==(n=this.colors[e])&&void 0!==n?n:C.White,e,t),Re&&(yield lt.create({level:e,message:t,source:null!=i?i:""}).catch(b.catch))})}}Log.colors={error:C.Red,info:C.Cyan,warn:C.Yellow};class Loggable{get log(){return e=>Loggable.logInfo(this,e)}static get log(){return e=>this.logInfo(this,e)}get error(){return e=>Loggable.logError(this,e)}static get error(){return e=>this.logError(this,e)}static logError(e,t){var i,r;Log.error(null!==(r=null!==(i=null==t?void 0:t.message)&&void 0!==i?i:t)&&void 0!==r?r:"",this.source(e),Env.CONSOLE_ERROR)}static logInfo(e,t){Log.info(null!=t?t:"",this.source(e),Env.CONSOLE_ERROR)}static source(e){var t,i,r;const n=null!==(r=null!==(i=null===(t=null==e?void 0:e.constructor)||void 0===t?void 0:t.name)&&void 0!==i?i:null==e?void 0:e.name)&&void 0!==r?r:"null";return Env.BASE_NAME?`${Env.BASE_NAME}.${n}`:n}}class Config extends Loggable{static all(e){return be(this,void 0,void 0,function*(){var t;const i=null!==(t=yield dt.findAll({where:(null==e?void 0:e.length)?{key:{[le.in]:e}}:{}}).catch(this.error))&&void 0!==t?t:[];return Object.fromEntries(i.map(e=>[e.key,e.value]))})}static del(e){return be(this,void 0,void 0,function*(){return((yield dt.destroy({where:{key:e}}).catch(this.error))||0)>0})}static get(e){return be(this,arguments,void 0,function*(e,t=""){const[i]=(yield dt.findOrCreate({where:{key:e},defaults:{key:e,value:t}}).catch(this.error))||[];return i?i.value:t})}static set(e,t){return be(this,void 0,void 0,function*(){const[i,r]=(yield dt.findOrCreate({where:{key:e},defaults:{key:e,value:t}}).catch(this.error))||[];i&&!r&&(i.value=t,yield i.save().catch(this.error))})}}class SessionStore extends pe{constructor(){super(),this.all=e=>be(this,void 0,void 0,function*(){e=e||k;const t=yield ct.findAll({attributes:["id","expired_at"],where:{expired_at:{[le.gte]:new Date}}}).catch(t=>(e(t),null));t&&e(null,t.map(e=>e.id)),e()}),this.clear=e=>be(this,void 0,void 0,function*(){yield ct.destroy().catch(b.catch),e&&e()}),this.destroy=(e,t)=>be(this,void 0,void 0,function*(){yield ct.destroy({where:{id:e}}).catch(b.catch),t&&t()}),this.expired=()=>be(this,void 0,void 0,function*(){yield ct.destroy({where:{expired_at:{[le.lt]:new Date}}}).catch(b.catch)}),this.get=(e,t)=>be(this,void 0,void 0,function*(){t=t||k;const i=yield ct.findOne({where:{id:e,expired_at:{[le.gte]:new Date}}}).catch(e=>(t(e),null));if(i){if(i.data.cookie){if(new Date(i.data.cookie.expires).getTime()<=Date.now())return yield i.destroy().catch(b.catch),void t(null)}t(null,i.data)}else t(null)}),this.length=e=>be(this,void 0,void 0,function*(){e&&e(null,(yield ct.count().catch(b.catch))||0)}),this.set=(e,t,i)=>be(this,void 0,void 0,function*(){i=i||k;const r=new Date;r.setTime(r.getTime()+Env.API_SESSION_LIFETIME);const[n,s]=(yield ct.findOrCreate({where:{id:e},defaults:{id:e,data:t,expired_at:r}}).catch(b.catch))||[];n?(s||(n.data=t,n.expired_at=r,yield n.save().catch(b.catch)),i()):i()}),setInterval(this.expired,6e4)}}let pt=class Service{};pt=Pe([ge(Loggable)],pt);class AuthService extends pt{constructor(){super(...arguments),this.userAttributes={exclude:["username","notes","deleted_at"]}}create(e){return be(this,void 0,void 0,function*(){if(!Env.TOKEN_KEY)throw new n("TOKEN_KEY not found");const{id:t}=e,i=K(),r=yield Ze.create({secret:i,user_id:t}).catch(this.error);if(!r)throw new s;return{auth:r,token:ve.sign({id:t,secret:i},Env.TOKEN_KEY,this.options())}})}destroy(e){return be(this,void 0,void 0,function*(){return!(!e||e.revoked_at)&&(e.revoked_at=new Date,yield e.save(),!0)})}renew(e){return be(this,void 0,void 0,function*(){if(!Env.TOKEN_KEY)throw new n("TOKEN_KEY not found");const{decode:i}=yield this.verify(e,Env.API_TOKEN_RENEW/1e3);if(!i)throw new t("Invalid token");const{id:r,secret:s}=i;if(r&&s){const e=yield st.findOne({where:{id:r,status:Ne.ACTIVE}}).catch(this.error);if(e){const t=yield Ze.findOne({where:{secret:s,user_id:e.id}}).catch(this.error);if(t&&!t.revoked_at){t.secret=K(),yield t.save().catch(this.error);return{auth:t,token:ve.sign({id:r,secret:t.secret},Env.TOKEN_KEY,this.options())}}}}throw new t("Invalid token")})}user(e){return be(this,void 0,void 0,function*(){const{decode:i}=yield this.verify(e);if(!i)throw new t("Invalid token");const{id:r,secret:n}=i;if(r&&n){const e=yield st.findOne({attributes:this.userAttributes,where:{id:r,status:Ne.ACTIVE}}).catch(this.error);if(e){const t=yield Ze.findOne({where:{secret:n,user_id:e.id}}).catch(this.error);if(t&&!t.revoked_at)return{auth:t,user:e}}}throw new t("Invalid token")})}options(){return{algorithm:"RS256",expiresIn:`${Env.TOKEN_TTL}ms`}}verify(e,i){return be(this,void 0,void 0,function*(){if(!Env.TOKEN_CRT)throw new n("TOKEN_CRT not found");if(!e||e.split(".").length<3)throw new t("Invalid token");try{return{decode:ve.verify(e,Env.TOKEN_CRT,{clockTolerance:i})}}catch(e){throw new t("Invalid token")}})}key(e){return be(this,void 0,void 0,function*(){if(!e)throw new t("Invalid token");const i=yield st.findOne({attributes:this.userAttributes,where:{token:e,status:Ne.ACTIVE}}).catch(this.error);if(i){const t=e.replace(/(-[\w]{4})/g,"-****"),[r]=(yield Ze.findOrCreate({where:{secret:t,user_id:i.id},defaults:{secret:t,user_id:i.id}}).catch(this.error))||[];if(r)return r.revoked_at&&(r.revoked_at=null,yield r.save().catch(this.error)),{auth:r,user:i}}throw new t("Invalid token")})}}class ModelService extends pt{constructor(e,t){var i,r;super(),this.attributes=[],this.permissions=[],this.search=[],this.model=e,(null==t?void 0:t.permissions)&&(this.permissions=t.permissions);const n=Object.keys(e.getAttributes());this.attributes=this.filterAttributes(n,null!==(i=null==t?void 0:t.attributes)&&void 0!==i?i:[]),this.search=this.filterAttributes(n,null!==(r=null==t?void 0:t.search)&&void 0!==r?r:this.attributes)}applySelect(e,t,i){if(!t)return;const r=null==e?void 0:e.getQuery(i?`${i}_select`:"select"),n=t.attributes,s=e=>n.includes(e);t.attributes=[],j(r)&&r.length&&t.attributes.push(...r.split(/\s*,\s*/g).filter(s)),t.attributes.length||(t.attributes=n)}applyOrders(e,t){if(!t)return;const i=null==e?void 0:e.getQuery("asc"),r=null==e?void 0:e.getQuery("desc"),n=[],s=t.attributes,o=e=>s.includes(e);j(i)&&i.length&&n.push(...i.split(/\s*,\s*/g).filter(o).map(e=>[e,"ASC"])),j(r)&&r.length&&n.push(...r.split(/\s*,\s*/g).filter(o).map(e=>[e,"DESC"])),n.length&&(t.order=n)}applyPagination(e,t){if(!t)return;const i=null==e?void 0:e.getQuery("limit"),r=null==e?void 0:e.getQuery("offset");D(t.limit)&&(t.limit=Env.MODEL_LIMIT,j(i)&&(t.limit=parseInt(i)||0),t.limit=B(t.limit,-1,Env.MODEL_LIMIT_MAX)),t.limit<0&&(t.limit=void 0),j(r)&&(t.offset=parseInt(r)||0,t.offset<0&&(t.offset=void 0))}applySearch(e,t){var i;if(!t)return;const r=null==e?void 0:e.getQuery("search");r&&j(r)&&(t.where=Object.assign(Object.assign({},null!==(i=t.where)&&void 0!==i?i:{}),{[le.or]:this.search.map(e=>({[e]:{[le.substring]:r}}))}))}applyWhere(e,t,i){var r,n;if(!t)return null;const s=null==e?void 0:e.getQuery(i?`${i}_where`:"where");if(!j(s))return null;const d=t.attributes,a=ue.parse(d,s);return D(a.error)?(t.where=Object.assign(Object.assign({},null!==(r=t.where)&&void 0!==r?r:{}),null!==(n=a.value)&&void 0!==n?n:{}),null):new o(a.error.join(", "))}countAll(e){return be(this,void 0,void 0,function*(){var t,i;const{ctx:r,options:n={}}=e,s=F({},n);s.attributes=this.getAttributes(null!==(t=null==s?void 0:s.attributes)&&void 0!==t?t:[]),this.applySearch(r,s);const o=this.applyWhere(r,s);if(o)return null==r||r.error(o),0;this.applySelect(r,s);const a=null!==(i=yield this.model.count(Object.assign(Object.assign({},s),{distinct:!!s.include,col:this.attributes.length?this.attributes[0]:void 0})).catch(this.error))&&void 0!==i?i:0;return null==r||r.prepare(d.OK,{count:a}),a})}create(e){return be(this,void 0,void 0,function*(){var t,i;const{body:r={},ctx:n,options:o={}}=e,a=F({},o),l=yield this.model.create(Object.assign({},F({},null!==(i=null===(t=null==n?void 0:n.req)||void 0===t?void 0:t.body)&&void 0!==i?i:{},r)),Object.assign(Object.assign({},a),{ctx:n})).catch(this.error);if(!l)return null==n||n.error(new s),null;const{primaryKeyAttribute:u}=this.model;return null==n||n.prepare(d.OK,{[u]:l.getDataValue(u)}),l})}delete(e){return be(this,void 0,void 0,function*(){var t,i;const{ctx:r,id:s,options:o={}}=e,{primaryKeyAttribute:d}=this.model,l=null!==(t=null==r?void 0:r.getParam(d))&&void 0!==t?t:s;if(D(l))return null==r||r.error(new a),0;const u=F({},o),c=yield this.model.destroy(Object.assign(Object.assign(Object.assign({},u),{where:Object.assign(Object.assign({},null!==(i=u.where)&&void 0!==i?i:{}),{[d]:l}),individualHooks:!0}),{ctx:r})).catch(this.error);return c?(null==r||r.success("OK"),c):(null==r||r.error(new n),0)})}filterAttributes(e,t){if(H(t))return t.length?t.filter(t=>j(t)&&e.includes(t)):e;const{exclude:i,include:r}=t,n=D(r)?e:r.filter(t=>j(t)&&e.includes(t)),s=null!=i?i:[];return s.length?n.filter(e=>!s.includes(e)):n}findAll(e){return be(this,void 0,void 0,function*(){var t,i;const{ctx:r,callback:n,options:s={},query:o=!0}=e,a=F({},s);if(a.attributes=this.getAttributes(null!==(t=null==a?void 0:a.attributes)&&void 0!==t?t:[]),o&&this.applySelect(r,a),this.applyOrders(r,a),this.applyPagination(r,a),(null!==(i=null==a?void 0:a.limit)&&void 0!==i?i:0)>Env.MODEL_LIMIT_MAX&&(a.limit=Env.MODEL_LIMIT_MAX),this.applySearch(r,a),o){const e=this.applyWhere(r,a);if(e)return null==r||r.error(e),{count:0,records:[]}}const[l,u]=yield Promise.all([this.model.count(Object.assign(Object.assign({},a),{distinct:!!a.include,col:this.attributes.length?this.attributes[0]:void 0})).catch(this.error).then(e=>null!=e?e:0),this.model.findAll(a).catch(this.error).then(e=>null!=e?e:[])]),c={count:l,records:n?yield n(u):u};return null==r||r.prepare(d.OK,c),c})}findOne(e){return be(this,void 0,void 0,function*(){var t,i,r;const{callback:s,ctx:o,id:l,options:u={},query:c=!0}=e,{primaryKeyAttribute:p}=this.model,v=null!==(t=null==o?void 0:o.getParam(p))&&void 0!==t?t:l;if(D(v))return null==o||o.error(new a),null;const g=F({},u);g.attributes=this.getAttributes(null!==(i=null==g?void 0:g.attributes)&&void 0!==i?i:[]),c&&this.applySelect(o,g);const y=yield this.model.findOne(Object.assign(Object.assign({},g),{where:Object.assign(Object.assign({},null!==(r=null==g?void 0:g.where)&&void 0!==r?r:{}),{[p]:v})})).catch(this.error);return y?(null==o||o.prepare(d.OK,s?yield s(y):y.toJSON()),y):(null==o||o.error(new n),null)})}getAttributes(e){return this.filterAttributes(this.attributes,e)}include(e,t){var i;const r=U(t),{ctx:n}=null!=t?t:{};r.attributes=this.filterAttributes(Object.keys(e.getAttributes()),null!==(i=r.attributes)&&void 0!==i?i:[]),this.applySearch(n,r);const s=this.applyWhere(n,r,t.name);return s?(null==n||n.error(s),null):(this.applySelect(n,r,t.name),Object.assign({model:e},r))}name(){return this.model.tableName}sync(){return be(this,void 0,void 0,function*(){if(!this.permissions.length)return;const e=this.name().toLowerCase(),t={description:"",active:!0};yield q(this.permissions,i=>be(this,void 0,void 0,function*(){const r=`${e}.${i}`,n=Y(e.replace(/_/g," ")," ");yield et.findOrCreate({where:{key:r},defaults:Object.assign({name:`${n} ${Y(i," ")}`,key:r,tags:n},t)})}))})}update(e){return be(this,void 0,void 0,function*(){var t,i,r,n,s;const{ctx:o,body:d,id:l,options:u={}}=e,{primaryKeyAttribute:c}=this.model,p=null!==(t=null==o?void 0:o.getParam(c))&&void 0!==t?t:l;if(D(p))return null==o||o.error(new a),!1;const v=F({},u),[g]=null!==(s=yield this.model.update(Object.assign({},F({},null!==(r=null===(i=null==o?void 0:o.req)||void 0===i?void 0:i.body)&&void 0!==r?r:{},null!=d?d:{})),Object.assign(Object.assign(Object.assign({},v),{where:Object.assign(Object.assign({},null!==(n=null==v?void 0:v.where)&&void 0!==n?n:{}),{[c]:p}),individualHooks:!0}),{ctx:o})).catch(this.error))&&void 0!==s?s:[];return g?(null==o||o.success("OK"),!0):(null==o||o.success("Record not changed"),!1)})}}class UserService extends pt{constructor(){super(...arguments),this.hashOptions={algorithm:Env.PASSWORD_ALGORITHM,memoryCost:Env.PASSWORD_MEMORY_COST,parallelism:Env.PASSWORD_PARALLELISM,timeCost:Env.PASSWORD_TIME_COST}}createPassword(e,t,i){return be(this,void 0,void 0,function*(){yield nt.update({revoked_at:new Date},{where:{revoked_at:null,user_id:e}}).catch(this.error);const r=he(t+Env.PASSWORD_PEPPER,this.hashOptions);yield nt.create({hash:r,user_id:e},Object.assign({},{ctx:i})).catch(this.error)})}findAllPermission(e){return be(this,void 0,void 0,function*(){var t;return null!==(t=yield et.findAll({attributes:["name"],where:{active:!0},include:[{association:"groups_permissions",required:!0,include:[{association:"group",required:!0,attributes:["id","name"],where:{active:!0},include:[{association:"users_groups",required:!0,where:{user_id:e}}]}]}]}).catch(this.error))&&void 0!==t?t:[]})}getGroups(e){return be(this,void 0,void 0,function*(){var t;return(null!==(t=yield it.findAll({attributes:["name"],where:{active:!0},include:[{association:"users_groups",required:!0,where:{user_id:e}}]}).catch(this.error))&&void 0!==t?t:[]).map(e=>e.name)})}getGroupsPermissions(e){return be(this,void 0,void 0,function*(){const t=[],i=[];return(yield this.findAllPermission(e)).forEach(e=>{i.push(e.name),e.groups_permissions.forEach(e=>t.push(e.group.name))}),{groups:$(t),permissions:$(i)}})}getPermissions(e){return be(this,void 0,void 0,function*(){return(yield this.findAllPermission(e)).map(e=>e.name)})}}class Application extends l{constructor(e,t){super(e,F({},{cert:Env.HTTP_CRT,cross:!!Env.HTTP_CROSS&&("*"===Env.HTTP_CROSS||{origin:Env.HTTP_CROSS.split(",")}),hybrid:Env.API_HTTP_HYBRID,json:{strict:Env.API_BODYPARSER_STRICT,inflate:Env.API_BODYPARSER_INFLATE,limit:Env.API_BODYPARSER_LIMIT,type:Env.API_BODYPARSER_TYPE},key:Env.HTTP_KEY,notfound:Env.API_HTTP_NOTFOUND,path:Env.API_HTTP_PATH,port:Env.API_HTTP_PORT,public:Env.API_HTTP_PUBLIC,redirect:Env.API_HTTP_REDIRECT,session:{store:new SessionStore,name:Env.API_SESSION_NAME,secret:Env.API_SESSION_SECRET,cookie:{maxAge:Env.API_SESSION_LIFETIME},genid:K}},null!=t?t:{})),this.hash=new R(Env.API_KEY_HASH)}error(e,t){Env.BASE_NAME&&(t=t?`${Env.BASE_NAME}.${t}`:Env.BASE_NAME),Log.error(e.message,t,Env.CONSOLE_ERROR)}log(e,t){Env.BASE_NAME&&(t=t?`${Env.BASE_NAME}.${t}`:Env.BASE_NAME),Log.info(e,t,!0)}listen(){const e=Object.create(null,{listen:{get:()=>super.listen}});return be(this,void 0,void 0,function*(){yield Re.sync(),yield this.migrations(Re),yield W(this.controllers,e=>be(this,void 0,void 0,function*(){N(e.sync)&&(yield e.sync(this))})),this.authService=new AuthService,this.userService=new UserService,yield e.listen.call(this),Env.API_WEBSOCKET_START&&this.server&&(this._webSocket=new X({server:this.server,verifyClient:(e,t)=>t(Env.API_WEBSOCKET_START)}),this._webSocket.on("connection",(e,t)=>be(this,void 0,void 0,function*(){t.url||(t.url=""),e.on("close",()=>{}),yield G(2e3)})))})}migrations(e){return be(this,void 0,void 0,function*(){const t=[];yield W(Object.keys(e.models),i=>be(this,void 0,void 0,function*(){var r;const n=e.models[i],s=n.version();if(!s)return;const[o,d]=null!==(r=yield ut.findOrCreate({where:{model:n.tableName},defaults:{model:n.tableName,version:""}}).catch(this.error))&&void 0!==r?r:[];o&&(d||o.version!=s)&&(o.version=s,yield o.save().catch(this.error),t.push(n))})),yield W(t,e=>be(this,void 0,void 0,function*(){return yield e.updateAttributes()}))})}websocket(){return this._webSocket}}let vt=class BaseController extends V{constructor(){super(...arguments),this.permissions=[]}name(){var e,t;return(null!==(t=null===(e=this.constructor)||void 0===e?void 0:e.name)&&void 0!==t?t:"api").replace(/(([a-z])([A-Z]))/g,"$2_$3")}session(e,t,i){return be(this,void 0,void 0,function*(){i.auth&&i.user&&(i.user_agent?i.user_agent!=t&&i.regenerate(k):(i.user_agent=t,i.save(k))),e.next()})}sync(){return be(this,void 0,void 0,function*(){const e=this.name().toLowerCase(),t=Y(e.replace(/_/g," ")," ");yield W(this.permissions,i=>be(this,void 0,void 0,function*(){const r=`${e}.${i}`;yield et.findOrCreate({where:{name:r},defaults:{name:r,description:`${t} ${Y(i," ")}`,active:!0}})}))})}};Pe([Ie(0,u()),Ie(1,c("user-agent")),Ie(2,p()),we("design:type",Function),we("design:paramtypes",[r,String,Object]),we("design:returntype",Promise)],vt.prototype,"session",null),vt=Pe([ge(Loggable)],vt);let gt=class ModelController extends vt{constructor(){super(...arguments),this.attributes=[],this.permissions=["create","read","update","delete"],this.search=[]}countAll(e){return be(this,void 0,void 0,function*(){return yield this.service.countAll(e)})}create(e){return be(this,void 0,void 0,function*(){return yield this.service.create(e)})}delete(e){return be(this,void 0,void 0,function*(){return yield this.service.delete(e)})}findAll(e){return be(this,void 0,void 0,function*(){return yield this.service.findAll(e)})}findOne(e){return be(this,void 0,void 0,function*(){return yield this.service.findOne(e)})}include(e,t){return this.service.include(e,t)}sync(){return be(this,void 0,void 0,function*(){const e=this.name().toLowerCase(),t=Y(e.replace(/_/g," ")," ");yield W(this.permissions,i=>be(this,void 0,void 0,function*(){const r=`${e}.${i}`;yield et.findOrCreate({where:{name:r},defaults:{name:r,description:`${t} ${Y(i," ")}`,active:!0}})})),this.service=new ModelService(this.model,{attributes:this.attributes.length?this.attributes:void 0,search:this.search.length?this.search:void 0})})}update(e){return be(this,void 0,void 0,function*(){return yield this.service.update(e)})}name(){return this.model.tableName}};gt=Pe([v(vt)],gt);let yt=class AuditsController extends gt{constructor(){super(...arguments),this.model=ot,this.permissions=["read"]}all(e,t,i){return be(this,void 0,void 0,function*(){const r={};t&&(r.type=t),i&&(r.table=i),yield this.findAll({ctx:e,options:{attributes:{exclude:["data","previously"]},where:r,order:[["created_at","DESC"]]}})})}get(e){return be(this,void 0,void 0,function*(){yield this.findOne({ctx:e})})}};Pe([g(),He(),Ke(),Ie(0,u()),Ie(1,y("type")),Ie(2,y("table")),we("design:type",Function),we("design:paramtypes",[r,String,String]),we("design:returntype",Promise)],yt.prototype,"all",null),Pe([g("/:id"),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],yt.prototype,"get",null),yt=Pe([h("/audits"),v(gt)],yt);let ht=class AuthController extends vt{constructor(e){super(),this.authService=e}login(e,i,r,n,s,o){return be(this,void 0,void 0,function*(){var d;yield G(Env.SLEEP_AUTH);const a=yield st.findOne({where:{[le.or]:[{email:r},{username:r}]}}).catch(this.error);if(!a)return e.error(new t("Invalid credentials")),o.emit("auth.failure",{ctx:e}),void this.emit("auth.failure",{ctx:e});const l=yield nt.findOne({where:{revoked_at:null,user_id:a.id}}).catch(this.error);if(n+=Env.PASSWORD_PEPPER,!l||!(yield me(l.hash,n).catch(k)))return e.error(new t("Invalid credentials")),o.emit("auth.failure",{ctx:e}),void this.emit("auth.failure",{ctx:e});const u=yield Xe.findOne({where:{state:!0,user_id:a.id}}).catch(this.error);if(u)return e.error(new t("Activation Required")),o.emit("auth.activation",{ctx:e,register:u,user:a}),void this.emit("auth.activation",{ctx:e,register:u,user:a});if(a.status!=Ne.ACTIVE)return e.error(new t("Invalid credentials")),o.emit("auth.failure",{ctx:e,user:a}),void this.emit("auth.failure",{ctx:e,user:a});const{auth:c,token:p}=null!==(d=yield this.authService.create(a).catch(t=>{e.error(t),o.emit("auth.failure",{ctx:e}),this.emit("auth.failure",{ctx:e})}))&&void 0!==d?d:{};c&&p&&(c.user_agent=i,c.ip_address=e.getIp(),yield c.save().catch(this.error),Env.API_TOKEN_COOKIE_SET&&e.setCookie(Env.API_TOKEN_COOKIE,p,{maxAge:s?Env.TOKEN_TTL:void 0}),e.json({token:p}),o.emit("auth.login",{ctx:e,auth:c,user:a}),this.emit("auth.login",{ctx:e,auth:c,user:a}))})}logout(e,i,r,n){return be(this,void 0,void 0,function*(){if(yield this.authService.destroy(i))return Env.API_TOKEN_COOKIE_SET&&e.clearCookie(Env.API_TOKEN_COOKIE),e.success("OK"),n.emit("auth.logout",{auth:i,ctx:e,user:r}),void this.emit("auth.logout",{auth:i,ctx:e,user:r});e.error(new t("Access Denied"))})}renew(e,t,i,r){return be(this,void 0,void 0,function*(){var n;const{auth:s,token:o}=null!==(n=yield this.authService.renew(i).catch(t=>{e.error(t)}))&&void 0!==n?n:{};s&&o&&(s.user_agent=t,yield s.save().catch(this.error),e.json({token:o}),r.emit("auth.renew",{auth:s,ctx:e}),this.emit("auth.renew",{auth:s,ctx:e}))})}user(e,t){return be(this,void 0,void 0,function*(){const{groups:i,permissions:r,profile:n}=e.req;if(!i||!r)return e.error(new s);e.json(Object.assign(Object.assign({},t.toJSON()),{groups:i,permissions:r,profile:n}))})}};Pe([m("/login"),E(e=>({username:e.string().min(3).required(),password:e.string().min(5).required(),remember:e.boolean().parse().empty(!1)})),Ie(0,u()),Ie(1,c("user-agent")),Ie(2,_("username")),Ie(3,_("password")),Ie(4,_("remember")),Ie(5,f()),we("design:type",Function),we("design:paramtypes",[r,String,String,String,Boolean,Application]),we("design:returntype",Promise)],ht.prototype,"login",null),Pe([g("/logout"),He(),Ie(0,u()),Ie(1,Ue()),Ie(2,Ge()),Ie(3,f()),we("design:type",Function),we("design:paramtypes",[r,Ze,st,Application]),we("design:returntype",Promise)],ht.prototype,"logout",null),Pe([m("/renew"),E(e=>({token:e.string().required()})),Ie(0,u()),Ie(1,c("user-agent")),Ie(2,_("token")),Ie(3,f()),we("design:type",Function),we("design:paramtypes",[r,String,String,Application]),we("design:returntype",Promise)],ht.prototype,"renew",null),Pe([g(),g("/me"),He(),Ie(0,u()),Ie(1,Ge()),we("design:type",Function),we("design:paramtypes",[r,st]),we("design:returntype",Promise)],ht.prototype,"user",null),ht=Pe([h("/auth"),v(vt),Ie(0,ye(AuthService)),we("design:paramtypes",[AuthService])],ht);let mt=class CrudController extends gt{constructor(){super(...arguments),this._crud={}}CrudArgumentsCountAll(e){return{ctx:e}}CrudArgumentsCreate(e){return{ctx:e}}CrudArgumentsFindAll(e){return{ctx:e}}CrudArgumentsFindOne(e){return{ctx:e}}CrudArgumentsDelete(e){return{ctx:e}}CrudArgumentsUpdate(e){return{ctx:e}}CrudValidateCreate(){return this._crud}CrudValidateUpdate(){return this._crud}add(e){return be(this,void 0,void 0,function*(){yield this.create(this.CrudArgumentsCreate(e))})}all(e){return be(this,void 0,void 0,function*(){yield this.findAll(this.CrudArgumentsFindAll(e))})}rows(e){return be(this,void 0,void 0,function*(){yield this.countAll(this.CrudArgumentsCountAll(e))})}del(e){return be(this,void 0,void 0,function*(){yield this.delete(this.CrudArgumentsDelete(e))})}get(e){return be(this,void 0,void 0,function*(){yield this.findOne(this.CrudArgumentsFindOne(e))})}set(e){return be(this,void 0,void 0,function*(){yield this.update(this.CrudArgumentsUpdate(e))})}validate(e,t){return be(this,void 0,void 0,function*(){var i,r;const n=z.object(t).parse(),{error:s,message:d,value:a}=yield n.validateAsync(null!==(r=null===(i=e.req)||void 0===i?void 0:i.body)&&void 0!==r?r:{});if(s)return e.error(new o(null!=d?d:s));e.req.body=a,e.next()})}validateCreate(e){return be(this,void 0,void 0,function*(){const t=this.CrudValidateCreate();if(t==this._crud)return e.error(new s);yield this.validate(e,t)})}validateUpdate(e){return be(this,void 0,void 0,function*(){const t=this.CrudValidateUpdate();if(t==this._crud)return e.error(new s);yield this.validate(e,t)})}};Pe([m(),He(),Me(),A("validateCreate"),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"add",null),Pe([g(),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"all",null),Pe([g("/count"),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"rows",null),Pe([S("/:id"),He(),ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"del",null),Pe([g("/:id"),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"get",null),Pe([O("/:id"),He(),je(),A("validateUpdate"),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"set",null),Pe([Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"validateCreate",null),Pe([Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],mt.prototype,"validateUpdate",null),mt=Pe([v(gt)],mt);let Et=class FilesController extends vt{constructor(){super(...arguments),this.directory=Env.API_HTTP_PUBLIC||"./",this.permissions=["delete","download","edit","list","read","rename","upload","write"]}files(e){return be(this,void 0,void 0,function*(){let{path:t}=e.req;t=t.replace(/(^\/path(\/)?)|([\\.]{1,2}\/)/g,"");const i=Te(this.directory,t);if(!Ee(i)||!_e(i).isDirectory())return e.error(new n);let r;return r=fe(i),r=r.map(e=>this.info(Te(i,e))),r=r.filter(J),Object.assign(Object.assign({},this.info(i)),{files:r})})}info(e){if(!Ee(e))return null;const t=_e(e),i=e.split("/").pop();e=e.replace(this.directory,"/").replace(/^\/\//,"/");const r=t.isDirectory();return{name:i,path:e.replace(/^\/\//,"/"),size:t.size,extension:r?"":"."+e.split(".").pop(),created:t.birthtime,modified:t.mtime,directory:r,symlink:t.isSymbolicLink()}}name(){return"files"}sync(){return be(this,void 0,void 0,function*(){const e=this.name().toLowerCase(),t=Y(e.replace(/_/g," ")," ");yield W(this.permissions,i=>be(this,void 0,void 0,function*(){const r=`${e}.${i}`;yield et.findOrCreate({where:{name:r},defaults:{name:r,description:`${t} ${Y(i," ")}`,active:!0}})}))})}};Pe([g("/path"),g("/path/*path"),He(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],Et.prototype,"files",null),Et=Pe([h("/files"),v(vt)],Et);let _t=class ForgotController extends vt{check(e,t){return be(this,void 0,void 0,function*(){const i=yield this.find(t);if(!i||!i.user)return e.error(new n);i.secret=Q(),yield i.save(),e.json({token:i.secret})})}forgot(e,t,i){return be(this,void 0,void 0,function*(){yield G(Env.SLEEP_FORGOT);const r=t,s=yield st.findOne({where:{[le.or]:[{email:r},{username:t}]}}).catch(this.error);if(!s)return e.error(new n);const o=Q(),d=Date.now()+6e4*Env.API_TOKEN_FORGOT,a=yield at.create({token:o,timestamp:d,state:!0,user_id:s.id});e.success("OK"),i.emit("forgot.send",{ctx:e,forgot:a,user:s}),this.emit("forgot.send",{ctx:e,forgot:a,user:s})})}reset(e,t,i,r){return be(this,void 0,void 0,function*(){const i=yield this.find(t);if(!i)return e.error(new n);i.state=!1,yield i.save().catch(this.error),yield i.user.save().catch(this.error),e.success("OK"),r.emit("forgot.reset",{ctx:e,user:i.user}),this.emit("forgot.reset",{ctx:e,user:i.user})})}find(e){return be(this,void 0,void 0,function*(){return yield at.findOne({where:{token:e,state:!0},include:[{association:"users",required:!0}]}).catch(this.error)})}};Pe([g("/check"),E(e=>({token:e.string().required()})),Ie(0,u()),Ie(1,_("token")),we("design:type",Function),we("design:paramtypes",[r,String]),we("design:returntype",Promise)],_t.prototype,"check",null),Pe([m(),E(e=>({username:e.string().min(4).required()})),Ie(0,u()),Ie(1,_("username")),Ie(2,f()),we("design:type",Function),we("design:paramtypes",[r,String,Application]),we("design:returntype",Promise)],_t.prototype,"forgot",null),Pe([m("/reset"),E(e=>({token:e.string().required(),password:e.string().min(5).required()})),Ie(0,u()),Ie(1,_("token")),Ie(2,_("password")),Ie(3,f()),we("design:type",Function),we("design:paramtypes",[r,String,String,Application]),we("design:returntype",Promise)],_t.prototype,"reset",null),_t=Pe([h("/forgot"),v(vt)],_t);let ft=class GroupsController extends mt{constructor(){super(...arguments),this.model=it,this.schemaCreate={name:z.string().required(),description:z.string().empty(""),active:z.boolean().parse().required()},this.schemaUpdate={description:z.string().empty(),active:z.boolean().parse().empty()}}CrudValidateCreate(){return this.schemaCreate}CrudValidateUpdate(){return this.schemaUpdate}};ft=Pe([h("/groups"),v(mt)],ft);let At=class GroupsPermissionsController extends gt{constructor(){super(...arguments),this.model=tt,this.permissions=[]}all(e,t){return be(this,void 0,void 0,function*(){const i=this.include(et,{ctx:e,required:!0});i&&(yield this.findAll({ctx:e,options:{where:{group_id:t},include:[i]},callback:e=>e.map(e=>e.permission),query:!1}))})}set(e,t,i){return be(this,void 0,void 0,function*(){const r=yield it.findByPk(t,{attributes:["id"]}).catch(this.error);if(!r)return e.error(new n);const s=r.id,o=[];yield W($(i),t=>be(this,void 0,void 0,function*(){var i;if(!(yield et.count({where:{id:t}})))return;const[r]=null!==(i=yield tt.findOrCreate(Object.assign({where:{group_id:s,permission_id:t},defaults:{group_id:s,permission_id:t}},{ctx:e})).catch(this.error))&&void 0!==i?i:[];r&&o.push(t)})),yield tt.destroy(Object.assign({where:{group_id:s,permission_id:{[le.notIn]:o}},individualHooks:!0},{ctx:e})).catch(this.error),e.success("OK")})}};Pe([g("/:id/permissions"),He(),Ke("groups"),Ie(0,u()),Ie(1,T("id")),we("design:type",Function),we("design:paramtypes",[r,String]),we("design:returntype",Promise)],At.prototype,"all",null),Pe([O("/:id/permissions"),He(),je("groups"),E(e=>({permissions:e.array(e.string())})),Ie(0,u()),Ie(1,T("id")),Ie(2,_("permissions")),we("design:type",Function),we("design:paramtypes",[r,String,Array]),we("design:returntype",Promise)],At.prototype,"set",null),At=Pe([h("/groups"),v(gt)],At);let St=class HealthController{ping(e){return be(this,void 0,void 0,function*(){e.json({status:"ok",timestamp:Date.now(),uptime:process.uptime()})})}};Pe([P("/"),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],St.prototype,"ping",null),St=Pe([h("/health")],St);let Ot=class LogsController extends gt{constructor(){super(...arguments),this.model=lt,this.permissions=["read"]}all(e){return be(this,void 0,void 0,function*(){yield this.findAll({ctx:e,options:{order:[["created_at","DESC"]]}})})}};Pe([g(),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],Ot.prototype,"all",null),Ot=Pe([h("/logs"),v(gt)],Ot);let Tt=class PermissionsController extends mt{constructor(){super(...arguments),this.model=et,this.schemaCreate={name:z.string().required(),description:z.string().empty(""),active:z.boolean().parse().required()},this.schemaUpdate={description:z.string().empty(),active:z.boolean().parse().empty()}}CrudValidateCreate(){return this.schemaCreate}CrudValidateUpdate(){return this.schemaUpdate}};Tt=Pe([h("/permissions"),v(mt)],Tt);let Pt=class RegisterController extends vt{register(e,t,i){return be(this,void 0,void 0,function*(){yield G(Env.SLEEP_REGISTRY);const{name:r,date:n,email:o,username:d,password:a,confirm:l}=t,u=d||o,c=x(a);if(yield st.count({where:{[le.or]:[{email:o},{username:u}]}}).catch(this.error))return e.error("INVALID EMAIL OR USERNAME");if(c!=x(l))return e.error("INVALID PASSWORD");const p=yield st.create({name:r,email:o,username:u,password:c,avatar:"",date:n,description:"",status:Ne.PENDING}).catch(this.error);if(!p)return e.error(new s);const v=yield Xe.create({token:yield this.getId(),user_id:p.id,state:!0}).catch(this.error);if(!v)return yield p.destroy().catch(this.error),e.error(new s);e.success("OK"),i.emit("register.user",{ctx:e,register:v,user:p}),this.emit("register.user",{ctx:e,register:v,user:p})})}confirm(e,t,i){return be(this,void 0,void 0,function*(){const r=yield Xe.findOne({where:{token:t,state:!0},include:[{association:"users",required:!0}]}).catch(this.error);if(!r)return e.error(new n);r.active=!1,yield r.save().catch(this.error),r.user.status=Ne.ACTIVE,yield r.user.save().catch(this.error),e.success("OK"),i.emit("register.confirm",{ctx:e,register:r,user:r.user}),this.emit("register.confirm",{ctx:e,register:r,user:r.user})})}send(e,t,i){return be(this,void 0,void 0,function*(){const r=t,s=yield Xe.findOne({where:{state:!0},include:[{model:st,required:!0,where:{[le.or]:[{email:r},{username:t}]}}]}).catch(this.error);if(!s)return e.error(new n);e.success("OK"),i.emit("register.send",{ctx:e,register:s,user:s.user}),this.emit("register.send",{ctx:e,register:s,user:s.user})})}getId(){return be(this,void 0,void 0,function*(){const e=K();return(yield Xe.count({where:{token:e}}))?this.getId():e})}};Pe([m(),E(e=>({name:e.string().required(),date:e.date().required(),email:e.string().email().required(),username:e.string().min(4).empty(),password:e.string().min(5).required(),confirm:e.string().min(5).required()})),Ie(0,u()),Ie(1,_()),Ie(2,f()),we("design:type",Function),we("design:paramtypes",[r,Object,Application]),we("design:returntype",Promise)],Pt.prototype,"register",null),Pe([m("/confirm"),E(e=>({token:e.string().required()})),Ie(0,u()),Ie(1,_("token")),Ie(2,f()),we("design:type",Function),we("design:paramtypes",[r,String,Application]),we("design:returntype",Promise)],Pt.prototype,"confirm",null),Pe([m("/send"),E(e=>({username:e.string().min(4).required()})),Ie(0,u()),Ie(1,_("username")),Ie(2,f()),we("design:type",Function),we("design:paramtypes",[r,String,Application]),we("design:returntype",Promise)],Pt.prototype,"send",null),Pt=Pe([h("/register"),v(vt)],Pt);let It=class SetupController extends vt{constructor(e){super(),this.userService=e}setup(e){return be(this,void 0,void 0,function*(){var t;if(yield st.count())return e.error(new n("Endpoint Not Found"));const i=yield st.create({name:"Admin",username:"admin",email:"admin@localhost.com",notes:"",status:Ne.ACTIVE},Object.assign({},{ctx:e})).catch(this.error);if(!i)return e.error(new s);yield this.userService.createPassword(i.id,"admin",e);const r=yield it.create({name:"root",description:"ROOT",active:!0},Object.assign({},{ctx:e}));yield rt.create({user_id:i.id,group_id:r.id},Object.assign({},{ctx:e})),yield it.create({name:"admin",description:"Admin",active:!0},Object.assign({},{ctx:e})),yield it.create({name:"user",description:"User",active:!0},Object.assign({},{ctx:e}));const o=null!==(t=yield et.findAll({where:{active:!0}}).catch(this.error))&&void 0!==t?t:[];yield W(o,t=>be(this,void 0,void 0,function*(){yield tt.create({group_id:r.id,permission_id:t.id},Object.assign({},{ctx:e}))})),e.success("OK")})}};Pe([g(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],It.prototype,"setup",null),It=Pe([h("/setup"),v(vt),Ie(0,ye(UserService)),we("design:paramtypes",[UserService])],It);let wt=class UsersController extends gt{constructor(){super(),this.model=st,this.path=Env.API_HTTP_PUBLIC+"/avatar",Env.API_HTTP_PUBLIC&&Ee(Env.API_HTTP_PUBLIC)&&!Ee(this.path)&&Ae(this.path)}avatar(e,t){return be(this,void 0,void 0,function*(){var i;const r=e.getFiles();if(r.file&&r.file.length){const{filename:n,path:o}=null!==(i=r.file[0])&&void 0!==i?i:{};if(n){const i=yield st.findOne({where:{user_id:t.id}}).catch(this.error);if(!i)return Se(o).isFile()&&Oe(o),e.error(new s);const r=this.path+"/"+i.metadata.avatar_url;if(Ee(r)&&Se(r).isFile()&&Oe(r),i.changed("metadata",!0),i.metadata.avatar_url=n,yield i.save(Object.assign({},{ctx:e})).catch(this.error))return e.success("OK")}}e.success("Record not Changed")})}token_new(){return be(this,void 0,void 0,function*(){var e;const t=K();return(null!==(e=yield this.model.count({where:{token:t}}).catch(this.error))&&void 0!==e?e:0)?this.token_new():t})}add(e){return be(this,void 0,void 0,function*(){yield this.create({ctx:e})})}all(e){return be(this,void 0,void 0,function*(){yield this.findAll({ctx:e})})}rows(e){return be(this,void 0,void 0,function*(){yield this.countAll({ctx:e})})}del(e,t,i){return be(this,void 0,void 0,function*(){if(i.id==t)return e.error(new n);yield this.delete({ctx:e})})}get(e){return be(this,void 0,void 0,function*(){yield this.findOne({ctx:e})})}set(e){return be(this,void 0,void 0,function*(){yield this.update({ctx:e})})}};Pe([m("/avatar"),He(),I({file:{count:1,path:`${Env.API_HTTP_PUBLIC}/avatar`,types:["image/bmp","image/jpeg","image/png"],size:32}}),Ie(0,u()),Ie(1,Ge()),we("design:type",Function),we("design:paramtypes",[r,st]),we("design:returntype",Promise)],wt.prototype,"avatar",null),Pe([m(),He(),Me(),E(e=>({name:e.string().required(),email:e.string().email().required(),username:e.string().min(3).required(),description:e.string().empty(""),state:e.boolean().parse().empty(!0)})),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],wt.prototype,"add",null),Pe([g(),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],wt.prototype,"all",null),Pe([g("/count"),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],wt.prototype,"rows",null),Pe([S("/:id"),He(),ke(),Ie(0,u()),Ie(1,T("id")),Ie(2,Ge()),we("design:type",Function),we("design:paramtypes",[r,String,st]),we("design:returntype",Promise)],wt.prototype,"del",null),Pe([g("/:id"),He(),Ke(),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],wt.prototype,"get",null),Pe([O("/:id"),He(),je(),E(e=>({name:e.string().empty(),email:e.string().email().empty(),username:e.string().min(3).empty(),description:e.string().empty(),state:e.boolean().parse().empty()})),Ie(0,u()),we("design:type",Function),we("design:paramtypes",[r]),we("design:returntype",Promise)],wt.prototype,"set",null),wt=Pe([h("/users"),v(gt),we("design:paramtypes",[])],wt);let bt=class UsersGroupsController extends gt{constructor(){super(...arguments),this.model=rt,this.permissions=[]}all(e,t){return be(this,void 0,void 0,function*(){if(!(yield st.findByPk(t,{attributes:["id"]}).catch(this.error)))return e.error(new n("User Not Found"));const i=this.include(it,{ctx:e,required:!0});i&&(yield this.findAll({ctx:e,options:{where:{user_id:t},include:[i]},callback:e=>e.map(e=>e.group),query:!1}))})}set(e,t,i){return be(this,void 0,void 0,function*(){const r=yield st.findByPk(t,{attributes:["id"]}).catch(this.error);if(!r)return e.error(new n("User Not Found"));const s=r.id,o=[];yield W($(i),t=>be(this,void 0,void 0,function*(){var i;if(!(yield it.count({where:{id:t}})))return;const[r]=null!==(i=yield rt.findOrCreate(Object.assign({where:{user_id:s,group_id:t},defaults:{user_id:s,group_id:t}},{ctx:e})).catch(this.error))&&void 0!==i?i:[];r&&o.push(t)})),yield rt.destroy(Object.assign({where:{user_id:s,group_id:{[le.notIn]:o}},individualHooks:!0},{ctx:e})).catch(this.error),e.success("OK")})}};Pe([g("/:id/groups"),He(),Ke("users"),Ie(0,u()),Ie(1,T("id")),we("design:type",Function),we("design:paramtypes",[r,String]),we("design:returntype",Promise)],bt.prototype,"all",null),Pe([O("/:id/groups"),He(),je("users"),E(e=>({groups:e.array(e.string())})),Ie(0,u()),Ie(1,T("id")),Ie(2,_("groups")),we("design:type",Function),we("design:paramtypes",[r,String,Array]),we("design:returntype",Promise)],bt.prototype,"set",null),bt=Pe([h("/users"),v(gt)],bt);export{Application,Qe as Audit,ot as AuditModel,xe as AuditTypes,yt as AuditsController,ht as AuthController,AuthService,vt as BaseController,Me as CanCreate,ke as CanDelete,Ke as CanRead,je as CanUpdate,Config,dt as ConfigModel,mt as CrudController,Env,Et as FilesController,_t as ForgotController,at as ForgotModel,it as GroupModel,tt as GroupPermissionModel,ft as GroupsController,At as GroupsPermissionsController,Be as HasGroups,Fe as HasPermissions,St as HealthController,He as IsAuthenticated,Log,lt as LogModel,Loggable,De as Loglevels,Ot as LogsController,ut as MigrationModel,gt as ModelController,ModelService,et as PermissionModel,Tt as PermissionsController,Pt as RegisterController,Xe as RegisterModel,Ue as ReqAuth,qe as ReqGroup,Ye as ReqGroups,$e as ReqPermission,We as ReqPermissions,Ge as ReqUser,pt as Service,ct as SessionModel,SessionStore,It as SetupController,Ze as UserAuthModel,rt as UserGroupModel,st as UserModel,nt as UserPasswordModel,UserService,Ne as UserStatus,wt as UsersController,bt as UsersGroupsController,Le as canFactory,Re as sequelize};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/api",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Simple api for small applications using express, jsonwebtoken, sequelize and websocket",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs.js",
|
|
@@ -49,33 +49,33 @@
|
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@andrewcaires/decorator": "^1.1.1",
|
|
51
51
|
"@andrewcaires/express": "^2.1.1",
|
|
52
|
-
"@andrewcaires/node": "^1.3.
|
|
53
|
-
"@andrewcaires/sequelize": "^3.
|
|
54
|
-
"@andrewcaires/utils.js": "^0.4.
|
|
55
|
-
"
|
|
52
|
+
"@andrewcaires/node": "^1.3.3",
|
|
53
|
+
"@andrewcaires/sequelize": "^3.2.1",
|
|
54
|
+
"@andrewcaires/utils.js": "^0.4.33",
|
|
55
|
+
"@node-rs/argon2": "^2.0.2",
|
|
56
56
|
"cors": "^2.8.6",
|
|
57
|
-
"dotenv": "^17.
|
|
57
|
+
"dotenv": "^17.4.2",
|
|
58
58
|
"express-session": "^1.19.0",
|
|
59
59
|
"jsonwebtoken": "^9.0.3",
|
|
60
|
+
"sequelize": "^6.37.8",
|
|
60
61
|
"tsx": "^4.21.0",
|
|
61
|
-
"ws": "^8.
|
|
62
|
+
"ws": "^8.20.0"
|
|
62
63
|
},
|
|
63
64
|
"devDependencies": {
|
|
64
65
|
"@eslint/js": "^10.0.1",
|
|
65
66
|
"@rollup/plugin-commonjs": "^29.0.2",
|
|
66
67
|
"@rollup/plugin-terser": "^1.0.0",
|
|
67
68
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
68
|
-
"@types/bcrypt": "^6.0.0",
|
|
69
69
|
"@types/cors": "^2.8.19",
|
|
70
|
-
"@types/express-session": "^1.
|
|
70
|
+
"@types/express-session": "^1.19.0",
|
|
71
71
|
"@types/jsonwebtoken": "^9.0.10",
|
|
72
|
-
"@types/node": "^25.
|
|
72
|
+
"@types/node": "^25.6.0",
|
|
73
73
|
"@types/ws": "^8.18.1",
|
|
74
|
-
"eslint": "^10.
|
|
74
|
+
"eslint": "^10.2.1",
|
|
75
75
|
"mariadb": "^3.5.2",
|
|
76
|
-
"rollup": "^4.
|
|
77
|
-
"rollup-plugin-dts": "^6.4.
|
|
76
|
+
"rollup": "^4.60.2",
|
|
77
|
+
"rollup-plugin-dts": "^6.4.1",
|
|
78
78
|
"tslib": "^2.8.1",
|
|
79
|
-
"typescript-eslint": "^8.
|
|
79
|
+
"typescript-eslint": "^8.59.0"
|
|
80
80
|
}
|
|
81
81
|
}
|