diff --git a/src/SplidClient.ts b/src/SplidClient.ts index 8b57d1f..d4d4763 100644 --- a/src/SplidClient.ts +++ b/src/SplidClient.ts @@ -1,7 +1,8 @@ import { BatchClient } from './BatchClient'; -import { getBalance } from './getBalance'; +import { Balance, getBalance } from './getBalance'; import { getSuggestedPayments } from './getSuggestedPayments'; import { ScopedLogger } from './logging'; +import { createEntry } from './methods/createEntry'; import { createExpense } from './methods/createExpense'; import { createGroup } from './methods/createGroup'; import { createPayment } from './methods/createPayment'; @@ -209,6 +210,8 @@ export default class SplidClient { } return data; }.bind(this) as (groupId: string) => Promise, + create: this.injectRequestConfig(wrapRequestObject(createEntry)), + expense: { create: this.injectRequestConfig(wrapRequestObject(createExpense)), }, @@ -285,4 +288,11 @@ export default class SplidClient { static getSuggestedPayments = getSuggestedPayments; static dedupeByGlobalId = dedupeByGlobalId; static getRoundedBalance = toFixed; + static getTotal = (balance: Balance) => + SplidClient.getRoundedBalance( + Object.values(balance).reduce( + (sum, i) => sum + (i.payedBy - i.payedFor), + 0 + ) + ); } diff --git a/src/methods/createEntry.ts b/src/methods/createEntry.ts new file mode 100644 index 0000000..333946c --- /dev/null +++ b/src/methods/createEntry.ts @@ -0,0 +1,34 @@ +import { RequestConfig } from '../requestConfig'; +import { Entry } from '../types/entry'; + +export function createEntry( + config: RequestConfig, + entry: Omit< + Entry, + | 'GlobalId' + | 'UpdateInstallationID' + | 'createdGlobally' + | 'created' + | 'UpdateID' + > +) { + const requestObj = { + id: 'createExpense', + path: '/parse/classes/Entry', + method: 'POST', + body: { + ...entry, + UpdateInstallationID: config.installationId, + GlobalId: config.randomUUID(), + notes: { + __op: 'Delete', + }, + createdGlobally: { + __type: 'Date', + iso: new Date().toISOString(), + }, + UpdateID: config.randomUUID(), + }, + } as const; + return requestObj; +} diff --git a/src/splidErrors.ts b/src/splidErrors.ts index 8c1bd15..8f7babd 100644 --- a/src/splidErrors.ts +++ b/src/splidErrors.ts @@ -14,4 +14,8 @@ export const SplidError = { error: 'Access denied: too many invalid codes', code: 141, }, + ADD_FIELD_ERROR: { + error: 'Permission denied for action addField on class Entry.', + code: 119, + }, } as const; diff --git a/src/types/entry.ts b/src/types/entry.ts index 9e0d784..d555d5c 100644 --- a/src/types/entry.ts +++ b/src/types/entry.ts @@ -73,18 +73,35 @@ export interface Entry { __type: 'Object'; className: 'Entry'; - category?: { - /** - * e.g. "Food" - */ - originalName: string; - type: EntryCategory; - }; - /** the "Purchased On" date. this is the only date you should manually modify */ - date?: { - __type: 'Date'; - iso: IsoTime; - }; + /** + * you will never get an entry with `{ __op: 'Delete' }` from the api, but you have to send it to the api like this to clear the field. + * setting `{ category: undefined }` will not work. + */ + category?: + | { + /** + * e.g. "Food" + */ + originalName: string; + type: EntryCategory; + } + | { + __op: 'Delete'; + }; + /** + * the "Purchased On" date. this is the only date you should manually modify. + * + * you will never get an entry with `{ __op: 'Delete' }` from the api, but you have to send it to the api like this to clear the field. + * setting `{ date: undefined }` will not work. + */ + date?: + | { + __type: 'Date'; + iso: IsoTime; + } + | { + __op: 'Delete'; + }; } export const EntryCategories = { diff --git a/src/types/index.ts b/src/types/index.ts index 7f1ac1a..c79d73e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,5 +1,6 @@ import { Entry as SplidJsEntry, + EntryCategory as SplidJsEntryCategory, EntryItem as SplidJsEntryItem, UseridToShareMap as SplidJsUseridToShareMap, } from './entry'; @@ -21,4 +22,6 @@ export namespace SplidJs { * a map of a userId to their share. the shares are floats between 0 and 1 and their sum is exactly 1. */ export type UseridToShareMap = SplidJsUseridToShareMap; + + export type EntryCategory = SplidJsEntryCategory; }