UNPKG

@planet-a/affinity-node

Version:
190 lines (189 loc) 7.31 kB
import { defaultTransformers } from './axios_default_transformers.js'; import { createSearchIteratorFn } from './create_search_iterator_fn.js'; import { organizationFieldsUrl, organizationsUrl } from './urls.js'; import { transformInteractionDateResponseRaw } from './transform_interaction_date_response_raw.js'; import { transformListEntryReference } from './transform_list_entry_reference.js'; /** * @module * An organization in Affinity represents an external company that your team is in touch with- this could be an organization you're trying to invest in, sell to, or establish a relationship with. * * An organization has many people associated with it - these are your team's points of contacts at that organization. Just like people, organizations can be added to multiple lists and can be assigned field values. * * To make the data quality as good as possible, we have our own proprietary database of organizations that we have developed through third-party partnerships and web scraping. We use this database to minimize data entry for you as you use Affinity's CRM products. * * *Notes*: * - If you are looking to add or remove an organization from a list, please check out the {@link ListEntries} section of the API. * - If you are looking to modify a field value (one of the cells on Affinity's spreadsheet), please check out the {@link FieldValues} section of the API. */ export class Organizations { /** @hidden */ constructor(axios) { Object.defineProperty(this, "axios", { enumerable: true, configurable: true, writable: true, value: axios }); /** * Returns an async iterator that yields all organization entries matching the given search terms * Each yielded array contains up to the number specified in {@link SearchOrganizationsRequest.page_size} of organizations. * Use this method if you want to process the organizations in a streaming fashion. * * *Please note:* the yielded organizations array may be empty on the last page. * * @example * ```typescript * let page = 0 * for await (const entries of affinity.organizations.searchIterator({ * term: 'Ltd', * page_size: 10 * })) { * console.log(`Page ${++page} of entries:`, entries) * } * ``` */ Object.defineProperty(this, "searchIterator", { enumerable: true, configurable: true, writable: true, value: createSearchIteratorFn(this.search.bind(this), 'organizations') }); } static transformSearchOrganizationsRequest(data) { return Object.fromEntries(Object.entries(data).map(([key, value]) => [ key, value instanceof Date ? value.toISOString() : value, ])); } /** * Fetches an organization with a specified organization_id. * * @returns The organization object corresponding to the organization_id. * * @example * ```typescript * const organization = await affinity.organizations.get({ * organization_id: 12345 * }) * console.log(organization) * ``` */ async get(params) { const { organization_id, ...rest } = params; const response = await this.axios.get(organizationsUrl(organization_id), { params: rest, transformResponse: [ ...defaultTransformers(), (json) => { const { list_entries, ...organization } = json; return { ...transformInteractionDateResponseRaw(organization), list_entries: json.list_entries.map(transformListEntryReference), }; }, ], }); return response.data; } /** * Searches your team's data and fetches all the organizations that meet the search criteria. * * @param request - Object containing the data for the request * * @example * ```typescript * const result = await affinity.organizations.search({ * term: 'affinity' * }) * console.log(result.organizations) * ``` */ async search(request) { const response = await this.axios.get(organizationsUrl(), { params: Organizations.transformSearchOrganizationsRequest(request), transformResponse: [ ...defaultTransformers(), (json) => { return { ...json, organizations: json.organizations.map(transformInteractionDateResponseRaw), }; }, ], }); return response.data; } /** * Creates a new organization with the supplied parameters. * * @param data - Object containing the data for creating a new organization * @returns The organization resource that was just created. * * @example * ```typescript * const newOrganization = await affinity.organizations.create({ * name: 'Acme Corporation', * domain: 'acme.co', * person_ids: [38706] * }) * console.log(newOrganization) * ``` */ async create(data) { const response = await this.axios.post(organizationsUrl(), data); return response.data; } /** * Updates an existing organization with `organization_id` with the supplied parameters. * * @param data - Object containing the data for updating an organization * @returns The organization resource that was just updated. * * @example * ```typescript * const updatedOrganization = await affinity.organizations.update({ * organization_id: 12345, * name: 'Acme Corp.', * person_ids: [38706, 89734] * }) * console.log(updatedOrganization) * ``` */ async update(data) { const { organization_id, ...rest } = data; const response = await this.axios.put(organizationsUrl(organization_id), rest); return response.data; } /** * Deletes an organization with a specified `organization_id`. * @returns true if the deletion was successful * * @example * ```typescript * const success = await affinity.organizations.delete({ * organization_id: 12345 * }) * console.log(success ? 'Organization deleted': 'Organization not deleted') * ``` */ async delete(request) { const { organization_id } = request; const response = await this.axios.delete(organizationsUrl(organization_id)); return response.data.success === true; } /** * Fetches an array of all the global fields that exist on organizations. * * @returns An array of the fields that exist on all organizations for your team. * * @example * ```typescript * const organizationFields = await affinity.organizations.getFields() * console.log(organizationFields) * ``` */ async getFields() { const response = await this.axios.get(organizationFieldsUrl()); return response.data; } }