Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

4.8 KB
Raw Blame
import { http, Result } from "@fider/services/http"
import { UserRole, OAuthConfig, ImageUpload, EmailVerificationKind } from "@fider/models"
import { PrivacySettingsPageState } from "@fider/pages/Administration/pages/PrivacySettings.page"

export interface CheckAvailabilityResponse {
  message: string
}

export interface CreateTenantRequest {
  legalAgreement: boolean
  tenantName: string
  subdomain?: string
  name?: string
  token?: string
  email?: string
}

export interface CreateTenantResponse {
  token?: string
}

export const createTenant = async (request: CreateTenantRequest): Promise<Result<CreateTenantResponse>> => {
  return await http.post<CreateTenantResponse>("/_api/tenants", request)
}

export interface UpdateTenantSettingsRequest {
  logo?: ImageUpload
  title: string
  invitation: string
  welcomeMessage: string
  welcomeHeader: string
  descriptionTemplate: string
  cname: string
  locale: string
}

export const updateTenantSettings = async (request: UpdateTenantSettingsRequest): Promise<Result> => {
  return await http.post("/_api/admin/settings/general", request)
}

export const updateTenantAdvancedSettings = async (customCSS: string, allowedSchemes: string): Promise<Result> => {
  return await http.post("/_api/admin/settings/advanced", { customCSS, allowedSchemes })
}

export const updateTenantPrivacy = async (request: PrivacySettingsPageState): Promise<Result> => {
  return await http.post("/_api/admin/settings/privacy", request)
}

export const updateTenantEmailAuthAllowed = async (isEmailAuthAllowed: boolean): Promise<Result> => {
  return await http.post("/_api/admin/settings/emailauth", {
    isEmailAuthAllowed,
  })
}

export const checkAvailability = async (subdomain: string): Promise<Result<CheckAvailabilityResponse>> => {
  return await http.get<CheckAvailabilityResponse>(`/_api/tenants/${subdomain}/availability`)
}

export interface SignInResponse {
  userExists: boolean
  signUpAllowed: boolean
}

export const signIn = async (email: string): Promise<Result<SignInResponse>> => {
  return await http.post<SignInResponse>("/_api/signin", { email })
}

export const signInNewUser = async (email: string, name: string): Promise<Result> => {
  return await http.post("/_api/signin/newuser", { email, name })
}

export const verifySignInCode = async (email: string, code: string): Promise<Result> => {
  return await http.post("/_api/signin/verify", { email, code })
}

export const resendSignInCode = async (email: string): Promise<Result> => {
  return await http.post("/_api/signin/resend", { email })
}

export const completeProfile = async (kind: EmailVerificationKind, key: string, name: string): Promise<Result> => {
  return await http.post("/_api/signin/complete", {
    kind,
    key,
    name,
  })
}

export const changeUserRole = async (userID: number, role: UserRole): Promise<Result> => {
  return await http.post(`/_api/admin/roles/${role}/users`, {
    userID,
  })
}

export const blockUser = async (userID: number): Promise<Result> => {
  return await http.put(`/_api/admin/users/${userID}/block`)
}

export const unblockUser = async (userID: number): Promise<Result> => {
  return await http.delete(`/_api/admin/users/${userID}/block`)
}

export const trustUser = async (userID: number): Promise<Result> => {
  return await http.put(`/_api/admin/users/${userID}/trust`)
}

export const untrustUser = async (userID: number): Promise<Result> => {
  return await http.delete(`/_api/admin/users/${userID}/trust`)
}

export const getOAuthConfig = async (provider: string): Promise<Result<OAuthConfig>> => {
  return await http.get<OAuthConfig>(`/_api/admin/oauth/${provider}`)
}

export interface CreateEditOAuthConfigRequest {
  provider: string
  status: number
  displayName: string
  clientID: string
  clientSecret: string
  authorizeURL: string
  tokenURL: string
  scope: string
  profileURL: string
  jsonUserIDPath: string
  jsonUserNamePath: string
  jsonUserEmailPath: string
  jsonUserRolesPath: string
  allowedRoles: string
  logo?: ImageUpload
  isTrusted: boolean
}

export const saveOAuthConfig = async (request: CreateEditOAuthConfigRequest): Promise<Result> => {
  return await http.post("/_api/admin/oauth", request)
}

export const setSystemProviderStatus = async (provider: string, isEnabled: boolean): Promise<Result> => {
  return await http.post(`/_api/admin/oauth/${provider}/status`, { provider, isEnabled })
}

export const resendSignUpEmail = async (): Promise<Result> => {
  return await http.post("/_api/signup/resend", {})
}

export interface RequestTenantDeletionResponse {
  scheduledDeletionAt: string
}

export const requestTenantDeletion = async (subdomain: string): Promise<Result<RequestTenantDeletionResponse>> => {
  return await http.delete<RequestTenantDeletionResponse>("/_api/admin/tenant", { subdomain })
}

export const cancelTenantDeletion = async (): Promise<Result> => {
  return await http.post("/_api/admin/tenant/cancel-deletion")
}