Page MenuHomeSealhub

users.ts
No OneTemporary

users.ts

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

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)

Event Timeline