| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import { api } from './client'
- export interface User {
- id: string
- username: string
- email: string
- role: string
- active: boolean
- lastLogin: number
- }
- export const usersApi = {
- /**
- * List users. Admin only - the endpoint requires the admin role, so a
- * non-admin caller gets a 403 and any caller must tolerate that.
- */
- list: async (page = 1, pageSize = 100) => {
- const { data } = await api.get('/users', { params: { page, pageSize } })
- return (data.users || []) as User[]
- },
- create: async (user: { username: string; password: string; email?: string; role?: string }) => {
- const { data } = await api.post('/users', user)
- return data as User & { personalProjectId?: string }
- },
- update: async (id: string, changes: { role?: string; active?: boolean; email?: string }) => {
- const { data } = await api.put(`/users/${id}`, changes)
- return data as User
- },
- remove: async (id: string) => {
- const { data } = await api.delete(`/users/${id}`)
- return data
- },
- /**
- * Changes an account's own password. The backend verifies `oldPassword`
- * against the account's current hash regardless of who is calling - there
- * is no way to set a new password for somebody else without knowing the
- * one they have now, so this only makes sense for `id === yourself`.
- */
- changeOwnPassword: async (id: string, oldPassword: string, newPassword: string) => {
- const { data } = await api.post(`/users/${id}/change-password`, { oldPassword, newPassword })
- return data
- },
- }
- export interface ProjectMember {
- userId: string
- role: 'admin' | 'editor' | 'viewer'
- username?: string
- email?: string
- addedAt?: number
- }
- export interface Project {
- _id: string
- name: string
- description?: string
- type: 'personal' | 'team'
- ownerId: string
- members: ProjectMember[]
- myRole: 'admin' | 'editor' | 'viewer' | 'none'
- workflowCount: number
- createdAt?: number
- /**
- * Anything the project carries that is not one of the fields above. Retention
- * lives here as `retention.ttlSeconds`, inherited by every workflow in the
- * project that has not set its own.
- */
- settings?: { retention?: { ttlSeconds: number } } & Record<string, any>
- }
- export const projectsApi = {
- list: async () => {
- const { data } = await api.get('/projects')
- return (data.projects || []) as Project[]
- },
- get: async (id: string) => {
- const { data } = await api.get(`/projects/${id}`)
- return data as Project
- },
- create: async (project: { name: string; description?: string }) => {
- const { data } = await api.post('/projects', project)
- return data as Project
- },
- update: async (
- id: string,
- changes: { name?: string; description?: string; settings?: Record<string, any> }
- ) => {
- const { data } = await api.put(`/projects/${id}`, changes)
- return data as Project
- },
- remove: async (id: string) => {
- const { data } = await api.delete(`/projects/${id}`)
- return data
- },
- addMember: async (id: string, userId: string, role: string) => {
- const { data } = await api.post(`/projects/${id}/members`, { userId, role })
- return data as Project
- },
- setMemberRole: async (id: string, userId: string, role: string) => {
- const { data } = await api.put(`/projects/${id}/members/${userId}`, { role })
- return data as Project
- },
- removeMember: async (id: string, userId: string) => {
- const { data } = await api.delete(`/projects/${id}/members/${userId}`)
- return data as Project
- },
- }
|