Skip to content

Commit

Permalink
Merge pull request #28 from LinusBolls/feat/write-methods
Browse files Browse the repository at this point in the history
Feat/write methods
  • Loading branch information
LinusBolls authored Nov 9, 2024
2 parents 5ee7f46 + 41a0a03 commit 053c2b6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/SplidClient.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -209,6 +210,8 @@ export default class SplidClient {
}
return data;
}.bind(this) as (groupId: string) => Promise<Entry[]>,
create: this.injectRequestConfig(wrapRequestObject(createEntry)),

expense: {
create: this.injectRequestConfig(wrapRequestObject(createExpense)),
},
Expand Down Expand Up @@ -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
)
);
}
34 changes: 34 additions & 0 deletions src/methods/createEntry.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions src/splidErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
41 changes: 29 additions & 12 deletions src/types/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Entry as SplidJsEntry,
EntryCategory as SplidJsEntryCategory,
EntryItem as SplidJsEntryItem,
UseridToShareMap as SplidJsUseridToShareMap,
} from './entry';
Expand All @@ -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;
}

0 comments on commit 053c2b6

Please sign in to comment.