import { CreatePortalResponse, OrganizationCreateResponse, OrganizationProductDataResponse, OrganizationWithProductsData, Product, ProductTier, User, } from '../../interfaces' import { GetOrganizationAdminsResponse, GetOrganizationMembersResponse, MembersType, Organization, OrganizationData, OrganizationRequestData, UserAdminOrganizationsResponse, } from '../../interfaces/organization.interface' import { Api } from '../api' import { ENDPOINTS } from '.' export class Organizations { api: Api constructor(api: Api) { this.api = api } get() { return this.api.apisauce.get<{ data: Organization[] }>( `${ENDPOINTS.ORGANIZATIONS}/api/user/organization`, ) } change(id: string) { return this.api.apisauce.post('/service/users/upload/organization', { organization: id, }) } getOrganizationDetails(organizationId: string) { return this.api.apisauce.get( `${ENDPOINTS.ORGANIZATIONS}/${organizationId}`, ) } changeOrganizationDetails( organizationId: string, data: Partial, ) { return this.api.apisauce.patch( `${ENDPOINTS.ORGANIZATIONS}/${organizationId}`, data, ) } async getOrganizationMembers(organizationId: string): Promise { const organizationMembers = await this.api.apisauce.get( `${ENDPOINTS.ORGANIZATIONS}/${organizationId}/users`, undefined, { cache: false, }, ) if (!organizationMembers.ok || !organizationMembers.data) { throw Error('error_organization_get_members') } return organizationMembers.data.users } async getOrganizationAdmins(organizationId: string): Promise { const organizationAdmins = await this.api.apisauce.get( `${ENDPOINTS.ORGANIZATIONS}/${organizationId}/admins`, undefined, { cache: false, }, ) if (!organizationAdmins.ok || !organizationAdmins.data) { throw Error('error_organization_get_Admins') } return organizationAdmins.data.admins } getAdminOrganizations() { return this.api.apisauce.get( `${ENDPOINTS.ORGANIZATIONS}/admin-in-organizations`, ) } addToOrganization( organization: string, uids: string[], type: MembersType = 'users', ) { const operations = uids.map((uid) => ({ op: 'add', path: '/users/-', value: uid, })) return this.api.apisauce.patch( `${ENDPOINTS.ORGANIZATIONS}/${organization}/${type}`, operations, ) } removeFromOrganization( organization: string, type: MembersType, indexes: number[], ) { const operations = indexes.map((index) => ({ op: 'remove', path: `/${type}/${index}`, })) return this.api.apisauce.patch( `${ENDPOINTS.ORGANIZATIONS}/${organization}/${type}`, operations, ) } createOrganization(data: OrganizationRequestData) { return this.api.apisauce.post( `${ENDPOINTS.ORGANIZATIONS}`, data, ) } getProducts() { return this.api.apisauce.get>( `${ENDPOINTS.REGISTRATION}/products`, ) } getOrganizationProducts(organization: string) { return this.api.apisauce.get( `${ENDPOINTS.REGISTRATION}/products/${organization}`, undefined, { cache: false }, ) } createOrganizationWithProducts(data?: OrganizationWithProductsData) { return this.api.apisauce.post( `${ENDPOINTS.REGISTRATION}/products`, data, ) } changeOrganizationProducts( organization: string, data: { products: Record }, ) { return this.api.apisauce.put<{ checkoutUrl: string }>( `${ENDPOINTS.REGISTRATION}/products/${organization}`, data, ) } createPortal(organization: string) { return this.api.apisauce.post( `${ENDPOINTS.REGISTRATION}/portal/${organization}`, ) } }