Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F969250
users.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
users.ts
View Options
import
axios
from
"axios"
;
import
Collection
,
{
useCollection
}
from
"./collection"
;
import
{
useItem
,
CollectionItem
}
from
"./item"
;
import
{
SingleResourceParams
,
SealiousUserRoleItem
,
SealiousCollectionResponse
,
SealiousItem
,
}
from
"./types"
;
import
url
from
"./url"
;
export
function
useUsers
(
params
:
SingleResourceParams
<
typeof
UserFields
>
)
:
[
Users
,
User
[],
SingleResourceParams
<
typeof
UserFields
>
,
number
]
{
return
useCollection
<
typeof
UserFields
,
User
,
Users
>
(
Users
,
params
);
}
export
function
useUser
(
id
:
string
)
:
[
boolean
,
typeof
UserFields
|
undefined
,
User
]
{
return
useItem
<
User
>
(
User
,
id
);
}
export
const
UserFields
=
{
username
:
""
,
prefered_hours_per_day
:
0
,
email
:
""
,
roles
:
[]
as
Array
<
string
>
,
};
export
class
User
extends
CollectionItem
<
typeof
UserFields
>
{
getInfo
()
{
return
{
collection_name
:
"users"
,
fields
:
UserFields
,
writable_fields
:
[
"username"
,
"prefered_hours_per_day"
,
"email"
,
]
as
(
keyof
typeof
UserFields
)[],
fields_with_attachments
:
[
"roles"
]
as
(
keyof
typeof
UserFields
)[],
};
}
getRoles
()
:
string
[]
{
const
roles
=
this
.
temp_data
.
roles
.
map
((
role_id
:
string
)
=>
this
.
getAttachment
(
role_id
))
.
map
((
role
:
SealiousUserRoleItem
)
=>
role
.
role
);
return
roles
;
}
hasRole
(
role
:
string
)
{
return
this
.
getRoles
().
includes
(
role
);
}
async
setRole
(
role
:
string
)
{
const
{
data
:
{
items
:
current_roles
},
}
=
(
await
axios
.
get
(
url
(
`/api/v1/collections/user-roles`
,
{
filter
:
{
user
:
this
.
id
},
})
))
as
{
data
:
SealiousCollectionResponse
<
typeof
UserFields
&
SealiousItem
>
;
};
for
(
const
role_entry
of
current_roles
)
{
await
axios
.
delete
(
`/api/v1/collections/user-roles/
${
role_entry
.
id
}
`
);
}
await
axios
.
post
(
"/api/v1/collections/user-roles"
,
{
role
:
role
,
user
:
this
.
id
,
});
await
this
.
refresh
();
}
}
export
class
Users
extends
Collection
<
typeof
UserFields
,
User
>
{
collection_name
=
"users"
;
item_constructor
=
User
;
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Nov 22, 08:48 (7 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
547663
Default Alt Text
users.ts (1 KB)
Attached To
Mode
rCOS Cosealious
Attached
Detach File
Event Timeline
Log In to Comment