UNPKG

xero-hero

Version:

Heroic utilities to simplify and enable your progress with the [xero-node](https://www.npmjs.com/package/xero-node) SDK.

1 lines 8.31 kB
{"version":3,"sources":["../../../../src/accounting/journals/__tests__/links.test.ts","../../../../src/common/instance/operations.ts","../../../../src/accounting/attachments/requests.ts","../../../../src/accounting/contacts/links.ts","../../../../src/utils/properties.ts","../../../../src/accounting/invoices/lineItems.ts","../../../../src/accounting/journals/links.ts","../../../../src/projects/timeEntries.ts"],"sourcesContent":["import { ManualJournal } from 'xero-node';\r\n\r\nimport { getManualJournalLink } from '../../../';\r\n\r\ndescribe('journals/links', () => {\r\n describe('getManualJournalLink()', () => {\r\n it('returns a null indicator if passed undefined', () => {\r\n // @ts-expect-error - This is an invalid type for the function.\r\n expect(getManualJournalLink()).toBe(\r\n 'https://go.xero.com/Journal/View.aspx?invoiceID=null-or-empty-manual-journal-id',\r\n );\r\n });\r\n\r\n it('returns a null indicator if passed null', () => {\r\n // @ts-expect-error - This is an invalid type for the function.\r\n expect(getManualJournalLink(null)).toBe(\r\n 'https://go.xero.com/Journal/View.aspx?invoiceID=null-or-empty-manual-journal-id',\r\n );\r\n });\r\n\r\n it('returns a null indicator if passed an empty string', () => {\r\n expect(getManualJournalLink('')).toBe(\r\n 'https://go.xero.com/Journal/View.aspx?invoiceID=null-or-empty-manual-journal-id',\r\n );\r\n });\r\n\r\n it('returns the correct URL for a manual journal object with an manualJournalID property', () => {\r\n const manualJournal = new ManualJournal();\r\n /* eslint-disable functional/immutable-data */\r\n manualJournal.manualJournalID = '25OR6TO4';\r\n /* eslint-enable functional/immutable-data */\r\n expect(getManualJournalLink(manualJournal)).toBe(\r\n 'https://go.xero.com/Journal/View.aspx?invoiceID=25OR6TO4',\r\n );\r\n });\r\n\r\n it('returns the correct URL for a string manual journal ID', () => {\r\n expect(getManualJournalLink('sdfhj-47629-sjdgdd')).toBe(\r\n 'https://go.xero.com/Journal/View.aspx?invoiceID=sdfhj-47629-sjdgdd',\r\n );\r\n });\r\n\r\n it('should work with an object that matches the interface for having a manualJournalID property', () => {\r\n const manualJournal = { manualJournalID: '25OR6TO4' };\r\n // @ts-expect-error - This is an invalid type for the function.\r\n expect(getManualJournalLink(manualJournal)).toBe(\r\n 'https://go.xero.com/Journal/View.aspx?invoiceID=25OR6TO4',\r\n );\r\n });\r\n });\r\n});\r\n","import { isObject } from 'deep-cuts';\r\n\r\nexport const deepClone = <T extends object>(instance: T): T => {\r\n if (instance && isObject(instance)) {\r\n const { constructor } = instance;\r\n // @ts-expect-error - This is not passing for TypeScript, bit will for any Xero class.\r\n const clone = new constructor();\r\n /* eslint-disable guard-for-in, functional/immutable-data */\r\n for (const key in instance) {\r\n clone[key] = deepClone(instance[key] as object);\r\n }\r\n /* eslint-enable guard-for-in, functional/immutable-data */\r\n\r\n return clone;\r\n }\r\n\r\n return instance;\r\n};\r\n","import type { ReadStream } from 'node:fs';\r\nimport type http from 'node:http';\r\n\r\nimport { bufferToStream } from 'tranquil-stream';\r\nimport type { Attachments, XeroClient } from 'xero-node';\r\n\r\ntype ICreateInvoiceAttachmentParameters = {\r\n contents: Buffer;\r\n filename: string;\r\n invoiceId: string;\r\n};\r\n\r\nexport const createInvoiceAttachment = async (\r\n client: XeroClient,\r\n tenantId: string,\r\n { invoiceId, filename, contents }: ICreateInvoiceAttachmentParameters,\r\n): Promise<{\r\n body: Attachments;\r\n response: http.IncomingMessage;\r\n}> => {\r\n return client.accountingApi.createInvoiceAttachmentByFileName(\r\n tenantId,\r\n invoiceId,\r\n filename,\r\n bufferToStream(contents) as unknown as ReadStream,\r\n );\r\n};\r\n","import qs from 'qs';\r\nimport type { Contact } from 'xero-node';\r\n\r\nimport { hasProperty } from '../../utils/properties';\r\n\r\nexport const getContactLink = (contact: Contact | string): string => {\r\n return `https://go.xero.com/Contacts/View.aspx?${qs.stringify({\r\n contactID:\r\n (hasProperty(contact, 'contactID')\r\n ? (contact as Contact).contactID\r\n : contact) || 'null-or-empty-contact-id',\r\n })}`;\r\n};\r\n","export const hasProperty = (object: any, property: string): boolean => {\r\n if (Boolean(object) && typeof object === 'object') {\r\n return property in object;\r\n }\r\n\r\n return false;\r\n};\r\n","import { isNil } from 'deep-cuts';\r\nimport type { LineItem } from 'xero-node';\r\n\r\nimport type { DecisionFunction } from '../../types';\r\n\r\nexport const filterInvoiceLineItems = (\r\n lineItems: LineItem[],\r\n minCode: DecisionFunction<LineItem> | string | number,\r\n maxCode?: string | number,\r\n): LineItem[] => {\r\n if (typeof minCode === 'function') {\r\n return (lineItems || []).filter(minCode);\r\n }\r\n\r\n const parsedMinCode = isNil(minCode)\r\n ? minCode\r\n : Number.parseInt(minCode as string, 10);\r\n const parsedMaxCode = isNil(maxCode)\r\n ? maxCode\r\n : Number.parseInt(maxCode as string, 10);\r\n if (parsedMinCode || parsedMaxCode) {\r\n return (lineItems || []).filter(({ itemCode }) => {\r\n const parsedItemCode = isNil(itemCode)\r\n ? itemCode\r\n : Number.parseInt(itemCode as string, 10);\r\n if (parsedItemCode) {\r\n const greaterThanOrEqualToMinCode = isNil(parsedMinCode)\r\n ? true\r\n : parsedItemCode >= parsedMinCode;\r\n const lessThanOrEqualToMaxCode = isNil(parsedMaxCode)\r\n ? true\r\n : parsedItemCode <= (parsedMaxCode || 0);\r\n return greaterThanOrEqualToMinCode && lessThanOrEqualToMaxCode;\r\n }\r\n\r\n return false;\r\n });\r\n }\r\n\r\n return lineItems || [];\r\n};\r\n","import qs from 'qs';\r\nimport type { ManualJournal } from 'xero-node';\r\n\r\nimport { hasProperty } from '../../utils/properties';\r\n\r\nexport const getManualJournalLink = (\r\n manualJournal: ManualJournal | string,\r\n): string => {\r\n return `https://go.xero.com/Journal/View.aspx?${qs.stringify({\r\n invoiceID:\r\n (hasProperty(manualJournal, 'manualJournalID')\r\n ? (manualJournal as ManualJournal).manualJournalID\r\n : manualJournal) || 'null-or-empty-manual-journal-id',\r\n })}`;\r\n};\r\n","import { roundToNearestFraction } from 'deep-cuts';\r\n\r\nimport type { TimeEntry } from './shimTypes';\r\n\r\nexport const hoursFromTimeEntries = (\r\n timeEntries: TimeEntry[],\r\n denominator: number = 4,\r\n maxDecimalPlaces: number = 2,\r\n): number | undefined => {\r\n const totalMinutes = timeEntries.reduce((totalMinutes, timeEntry) => {\r\n const duration = timeEntry.duration || 0;\r\n return totalMinutes + duration;\r\n }, 0);\r\n return roundToNearestFraction(\r\n totalMinutes / 60,\r\n denominator,\r\n maxDecimalPlaces,\r\n );\r\n};\r\n"],"mappings":";AAAA,SAAS,qBAAqB;;;ACA9B,SAAS,gBAAgB;;;ACGzB,SAAS,sBAAsB;;;ACH/B,OAAO,QAAQ;;;ACAR,IAAM,cAAc,CAAC,QAAa,aAA8B;AACrE,MAAI,QAAQ,MAAM,KAAK,OAAO,WAAW,UAAU;AACjD,WAAO,YAAY;AAAA,EACrB;AAEA,SAAO;AACT;;;ACNA,SAAS,aAAa;;;ACAtB,OAAOA,SAAQ;AAKR,IAAM,uBAAuB,CAClC,kBACW;AACX,SAAO,yCAAyCC,IAAG,UAAU;AAAA,IAC3D,YACG,YAAY,eAAe,iBAAiB,IACxC,cAAgC,kBACjC,kBAAkB;AAAA,EAC1B,CAAC,CAAC;AACJ;;;ACdA,SAAS,8BAA8B;;;APIvC,SAAS,kBAAkB,MAAM;AAC/B,WAAS,0BAA0B,MAAM;AACvC,OAAG,gDAAgD,MAAM;AAEvD,aAAO,qBAAqB,CAAC,EAAE;AAAA,QAC7B;AAAA,MACF;AAAA,IACF,CAAC;AAED,OAAG,2CAA2C,MAAM;AAElD,aAAO,qBAAqB,IAAI,CAAC,EAAE;AAAA,QACjC;AAAA,MACF;AAAA,IACF,CAAC;AAED,OAAG,sDAAsD,MAAM;AAC7D,aAAO,qBAAqB,EAAE,CAAC,EAAE;AAAA,QAC/B;AAAA,MACF;AAAA,IACF,CAAC;AAED,OAAG,wFAAwF,MAAM;AAC/F,YAAM,gBAAgB,IAAI,cAAc;AAExC,oBAAc,kBAAkB;AAEhC,aAAO,qBAAqB,aAAa,CAAC,EAAE;AAAA,QAC1C;AAAA,MACF;AAAA,IACF,CAAC;AAED,OAAG,0DAA0D,MAAM;AACjE,aAAO,qBAAqB,oBAAoB,CAAC,EAAE;AAAA,QACjD;AAAA,MACF;AAAA,IACF,CAAC;AAED,OAAG,+FAA+F,MAAM;AACtG,YAAM,gBAAgB,EAAE,iBAAiB,WAAW;AAEpD,aAAO,qBAAqB,aAAa,CAAC,EAAE;AAAA,QAC1C;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AACH,CAAC;","names":["qs","qs"]}