UNPKG

@planet-a/affinity-node

Version:
186 lines (185 loc) 6.49 kB
import { defaultTransformers } from './axios_default_transformers.js'; import { notesUrl } from './urls.js'; import { createSearchIteratorFn } from './create_search_iterator_fn.js'; export var NoteType; (function (NoteType) { NoteType[NoteType["PLAIN_TEXT"] = 0] = "PLAIN_TEXT"; NoteType[NoteType["HTML"] = 2] = "HTML"; /** * Can only be created by the Notetaker AI tool from Affinity. */ NoteType[NoteType["AI_SUMMARY"] = 3] = "AI_SUMMARY"; /** * @deprecated */ NoteType[NoteType["EMAIL"] = 1] = "EMAIL"; })(NoteType || (NoteType = {})); /** * Entity files are files uploaded to a relevant entity. * Possible files, for example, would be a pitch deck for an opportunity or a physical mail correspondence for a person. */ export class Notes { /** @hidden */ constructor(axios) { Object.defineProperty(this, "axios", { enumerable: true, configurable: true, writable: true, value: axios }); /** * Returns an async iterator that yields all notes matching the given request * Each yielded array contains up to the number specified in {@link PagedRequest.page_size} of notes. * Use this method if you want to process the notes in a streaming fashion. * * *Please note:* the yielded notes array may be empty on the last page. * * @example * ```typescript * let page = 0 * for await (const entries of affinity.notes.pagedIterator({ * person_id: 123, * page_size: 10 * })) { * console.log(`Page ${++page} of entries:`, entries) * } * ``` */ Object.defineProperty(this, "pagedIterator", { enumerable: true, configurable: true, writable: true, value: createSearchIteratorFn(this.all.bind(this), 'notes') }); } static transformNote(note) { return { ...note, created_at: new Date(note.created_at), updated_at: note.updated_at ? new Date(note.updated_at) : null, }; } /** * Fetches a note with a specified `note_id`. * * @returns The Note object corresponding to the `note_id`. * * @example * ```typescript * const note = await affinity.notes.get({ * note_id: 12345 * }) * console.log(note) * ``` */ async get(params) { const { note_id, ...rest } = params; const response = await this.axios.get(notesUrl(note_id), { params: rest, transformResponse: [ ...defaultTransformers(), Notes.transformNote, ], }); return response.data; } /** * Returns all notes attached to a person, organization or opportunity. */ async all(params) { const response = await this.axios.get(notesUrl(), { params, transformResponse: [ ...defaultTransformers(), (json) => { return { ...json, notes: json.notes.map(Notes.transformNote), }; }, ], }); return response.data; } /** * Creates a new note with the supplied parameters. * * Set the `type` parameter to 2 to create an HTML note. * See [here](https://support.affinity.co/hc/en-us/articles/360016292631-Rich-text-formatting-for-notes-within-Affinity) for more information on the sorts of rich text formatting we support in notes. * Please note that `<a>` tags aren't currently clickable inside the Affinity web app - though full links are. * * It is possible to create a **reply** to an existing note by setting `parent_id`. * The parent note should not have a `parent_id` itself. * It is possible for a single parent note to have multiple reply notes - They just get displayed in order of creation. `opportunity_ids`, `person_ids`, and `organization_ids` will be ignored when a `parent_id` is provided. * * @example * ```typescript * const newNote = await affinity.notes.create({ * person_ids: [ * 38706, * 624289 * ], * organization_ids: [ * 120611418 * ], * opportunity_ids: [ * 167 * ], * content: "Had a lunch meeting with Jane and John today. They want to invest in Acme Corp." * }) * console.log(newNote) * ``` */ async create(data) { const { created_at, ...rest } = data; const request = created_at ? { ...rest, created_at: created_at.toISOString(), } : rest; const response = await this.axios.post(notesUrl(), request); return Notes.transformNote(response.data); } /** * Updates an existing person with `note_id` with the supplied parameters. * * *Caveats:* * - You cannot update the content of a note that has mentions. * - You also cannot update the content of a note associated with an email. * - You cannot update the type of a note. * * *Note from 2024-08-13*: Updating an HTML note with changed HTML content seems to result in the note being displayed as plain text in the Affinity web app. Bug is reported. * * @example * ```typescript * const updatedNote = await affinity.notes.update({ * note_id: 12345, * content: "Dinner wasn't great, but the conversation was excellent.", * }) * console.log(updatedNote) * ``` */ async update(data) { const { note_id, ...rest } = data; const response = await this.axios.put(notesUrl(note_id), rest); return Notes.transformNote(response.data); } /** * Deletes a note with a specified `note_id`. * @returns true if the deletion was successful * * @example * ```typescript * const success = await affinity.notes.delete({ * note_id: 12345 * }) * console.log(success ? 'Note deleted': 'Note not deleted') * ``` */ async delete(request) { const { note_id } = request; const response = await this.axios.delete(notesUrl(note_id)); return response.data.success === true; } }