@uidu/client 3.17.0 → 3.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uidu/client",
3
- "version": "3.17.0",
3
+ "version": "3.18.0",
4
4
  "description": "Typed GraphQL client for the uidu platform. Server-first, framework-agnostic.",
5
5
  "license": "MIT",
6
6
  "author": "uidu",
package/src/contacts.ts CHANGED
@@ -6,9 +6,14 @@ import {
6
6
  GetDealDocument,
7
7
  type GetDealQuery,
8
8
  type GetDealQueryVariables,
9
+ GetOrganizationDocument,
10
+ type GetOrganizationQuery,
11
+ type GetOrganizationQueryVariables,
9
12
  ListContactsDocument,
10
13
  ListDealsDocument,
11
14
  type ListDealsQueryVariables,
15
+ ListOrganizationsDocument,
16
+ type ListOrganizationsQueryVariables,
12
17
  } from './generated';
13
18
  import { nodes } from './utils/relay';
14
19
 
@@ -18,6 +23,10 @@ export type Contact = NonNullable<
18
23
  export type Deal = NonNullable<
19
24
  NonNullable<GetDealQuery['currentWorkspace']>['deal']
20
25
  >;
26
+ /** A CRM organization (company) — the contactable a `Contact` wraps. */
27
+ export type Organization = NonNullable<
28
+ NonNullable<GetOrganizationQuery['currentWorkspace']>['organization']
29
+ >;
21
30
 
22
31
  /**
23
32
  * One contact with its address and custom fields (`fieldValuesByShortname`),
@@ -63,3 +72,27 @@ export async function listDeals(
63
72
  const result = await client.query(ListDealsDocument, variables);
64
73
  return nodes(result.currentWorkspace?.deals);
65
74
  }
75
+
76
+ /**
77
+ * One organization (company) with its fiscal/VAT codes and custom fields, or
78
+ * null.
79
+ */
80
+ export async function getOrganization(
81
+ client: UiduClient,
82
+ variables: GetOrganizationQueryVariables,
83
+ ): Promise<Organization | null> {
84
+ const result = await client.query(GetOrganizationDocument, variables);
85
+ return result.currentWorkspace?.organization ?? null;
86
+ }
87
+
88
+ /**
89
+ * The workspace's organizations as a flat array, optionally filtered with
90
+ * search `params`; page with `first`/`after`.
91
+ */
92
+ export async function listOrganizations(
93
+ client: UiduClient,
94
+ variables: ListOrganizationsQueryVariables = {},
95
+ ) {
96
+ const result = await client.query(ListOrganizationsDocument, variables);
97
+ return nodes(result.currentWorkspace?.organizations);
98
+ }
@@ -0,0 +1,155 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ const request = vi.fn();
4
+
5
+ vi.mock('graphql-request', () => ({
6
+ GraphQLClient: class {
7
+ request = request;
8
+ },
9
+ ClientError: class extends Error {},
10
+ }));
11
+
12
+ const { createClient } = await import('./client');
13
+ const expenses = await import('./expenses');
14
+
15
+ const client = createClient({
16
+ endpoint: 'https://acme.uidu.org/graphql',
17
+ apiKey: 'key',
18
+ });
19
+
20
+ const taxi = {
21
+ id: 'e1',
22
+ amount: 2350,
23
+ currency: 'EUR',
24
+ spentAt: '2026-10-30T09:00:00Z',
25
+ category: 2,
26
+ paymentMethod: 'card',
27
+ fieldValuesByShortname: { commessa: 'C-104' },
28
+ };
29
+
30
+ const report = {
31
+ id: 'r1',
32
+ name: 'Trasferta Milano',
33
+ paidAt: null,
34
+ fieldValuesByShortname: {},
35
+ user: { id: 'u1', name: 'Ada' },
36
+ expenses: { edges: [{ node: taxi }, null] },
37
+ };
38
+
39
+ beforeEach(() => request.mockReset());
40
+
41
+ describe('reads', () => {
42
+ it('listExpenseReports flattens reports and their expenses', async () => {
43
+ request.mockResolvedValue({
44
+ currentWorkspace: {
45
+ expenseReports: {
46
+ pageInfo: { hasNextPage: false, endCursor: null },
47
+ edges: [{ node: report }],
48
+ },
49
+ },
50
+ });
51
+ await expect(expenses.listExpenseReports(client)).resolves.toEqual([
52
+ { ...report, expenses: [taxi] },
53
+ ]);
54
+ });
55
+
56
+ it('getExpenseReport flattens its expenses', async () => {
57
+ request.mockResolvedValue({ currentWorkspace: { expenseReport: report } });
58
+ await expect(
59
+ expenses.getExpenseReport(client, { id: 'r1' }),
60
+ ).resolves.toEqual({ ...report, expenses: [taxi] });
61
+ });
62
+
63
+ it('getExpenseReport returns null when the report is not there', async () => {
64
+ request.mockResolvedValue({ currentWorkspace: { expenseReport: null } });
65
+ await expect(
66
+ expenses.getExpenseReport(client, { id: 'r1' }),
67
+ ).resolves.toBeNull();
68
+ });
69
+
70
+ it('asks for the person, not the derived contact', async () => {
71
+ request.mockResolvedValue({ currentWorkspace: { expenseReport: null } });
72
+ await expenses.getExpenseReport(client, { id: 'r1' });
73
+ const printed = JSON.stringify(request.mock.calls[0][0]);
74
+ expect(printed).toContain('"value":"user"');
75
+ expect(printed).not.toContain('"value":"contact"');
76
+ });
77
+
78
+ it('listAnalyticAccounts flattens the connection', async () => {
79
+ request.mockResolvedValue({
80
+ currentWorkspace: {
81
+ analyticAccounts: {
82
+ pageInfo: { hasNextPage: false, endCursor: null },
83
+ edges: [{ node: { id: 'a1', code: 'C-104', name: 'Rilievi' } }],
84
+ },
85
+ },
86
+ });
87
+ await expect(expenses.listAnalyticAccounts(client)).resolves.toEqual([
88
+ { id: 'a1', code: 'C-104', name: 'Rilievi' },
89
+ ]);
90
+ });
91
+ });
92
+
93
+ describe('writes', () => {
94
+ it('createExpenseReport sends userId and fieldValues as given', async () => {
95
+ const payload = { expenseReport: { node: { id: 'r1' } }, errors: [] };
96
+ request.mockResolvedValue({ createExpenseReport: payload });
97
+ const input = {
98
+ attributes: {
99
+ userId: 'u1',
100
+ name: 'Trasferta Milano',
101
+ fieldValues: { 'gid://uidu/Field/7': 'C-104' },
102
+ },
103
+ };
104
+ await expect(
105
+ expenses.createExpenseReport(client, { input }),
106
+ ).resolves.toEqual(payload);
107
+ expect(request.mock.calls[0][1]).toEqual({ input });
108
+ });
109
+
110
+ it('createExpense returns the payload with its errors', async () => {
111
+ const payload = {
112
+ expense: null,
113
+ errors: [{ key: 'amount', name: 'blank', type: 'blank' }],
114
+ };
115
+ request.mockResolvedValue({ createExpense: payload });
116
+ await expect(
117
+ expenses.createExpense(client, {
118
+ input: { attributes: { expenseReportId: 'r1' } },
119
+ }),
120
+ ).resolves.toEqual(payload);
121
+ });
122
+
123
+ it('updateExpenseReport and updateExpense return null without a payload', async () => {
124
+ request.mockResolvedValue({});
125
+ await expect(
126
+ expenses.updateExpenseReport(client, {
127
+ input: { id: 'r1', attributes: { paidAt: '2026-11-03T00:00:00Z' } },
128
+ }),
129
+ ).resolves.toBeNull();
130
+ await expect(
131
+ expenses.updateExpense(client, {
132
+ input: { id: 'e1', attributes: { amount: 2400 } },
133
+ }),
134
+ ).resolves.toBeNull();
135
+ });
136
+
137
+ it('allocateExpense books the expense on an analytic account', async () => {
138
+ const payload = {
139
+ analyticAccountAllocation: { node: { id: 'al1', amount: 2350 } },
140
+ errors: [],
141
+ };
142
+ request.mockResolvedValue({ createAnalyticAccountAllocation: payload });
143
+ await expect(
144
+ expenses.allocateExpense(client, {
145
+ input: {
146
+ attributes: {
147
+ allocatableId: 'e1',
148
+ analyticAccountId: 'a1',
149
+ amount: 2350,
150
+ },
151
+ },
152
+ }),
153
+ ).resolves.toEqual(payload);
154
+ });
155
+ });
@@ -0,0 +1,155 @@
1
+ import type { UiduClient } from './client';
2
+ import {
3
+ CreateAnalyticAccountAllocationDocument,
4
+ type CreateAnalyticAccountAllocationMutationVariables,
5
+ CreateExpenseDocument,
6
+ type CreateExpenseMutationVariables,
7
+ CreateExpenseReportDocument,
8
+ type CreateExpenseReportMutationVariables,
9
+ GetExpenseReportDocument,
10
+ type GetExpenseReportQuery,
11
+ type GetExpenseReportQueryVariables,
12
+ ListAnalyticAccountsDocument,
13
+ type ListAnalyticAccountsQuery,
14
+ type ListAnalyticAccountsQueryVariables,
15
+ ListExpenseReportsDocument,
16
+ type ListExpenseReportsQueryVariables,
17
+ UpdateExpenseDocument,
18
+ type UpdateExpenseMutationVariables,
19
+ UpdateExpenseReportDocument,
20
+ type UpdateExpenseReportMutationVariables,
21
+ } from './generated';
22
+ import { nodes } from './utils/relay';
23
+
24
+ // Expense reports: an ExpenseReport reimburses one person (`user`, written as
25
+ // `userId`) and holds their Expenses. `amount` is in cents of `currency`.
26
+ // Both carry the custom fields the workspace added to them: read them as
27
+ // `fieldValuesByShortname` (`{ shortname: value }`), write them as
28
+ // `attributes.fieldValues`, a map of the Field's GlobalID to the value.
29
+ //
30
+ // Reads and writes need an account (Bearer / apiKey), server-side: a custom
31
+ // app's session token does not reach expense reports.
32
+
33
+ type ExpenseReportRecord = NonNullable<
34
+ NonNullable<GetExpenseReportQuery['currentWorkspace']>['expenseReport']
35
+ >;
36
+
37
+ export type ExpenseReport = Omit<ExpenseReportRecord, 'expenses'> & {
38
+ expenses: Expense[];
39
+ };
40
+ export type Expense = NonNullable<
41
+ NonNullable<
42
+ NonNullable<NonNullable<ExpenseReportRecord['expenses']>['edges']>[number]
43
+ >['node']
44
+ >;
45
+ export type AnalyticAccount = NonNullable<
46
+ NonNullable<
47
+ NonNullable<
48
+ NonNullable<
49
+ ListAnalyticAccountsQuery['currentWorkspace']
50
+ >['analyticAccounts']
51
+ >['edges']
52
+ >[number]
53
+ >['node'];
54
+
55
+ function withExpenses(report: ExpenseReportRecord): ExpenseReport {
56
+ return { ...report, expenses: nodes(report.expenses) };
57
+ }
58
+
59
+ /**
60
+ * The workspace's expense reports, newest first, each with its `expenses` as a
61
+ * flat array; page with `first`/`after`.
62
+ */
63
+ export async function listExpenseReports(
64
+ client: UiduClient,
65
+ variables: ListExpenseReportsQueryVariables = {},
66
+ ): Promise<ExpenseReport[]> {
67
+ const result = await client.query(ListExpenseReportsDocument, variables);
68
+ return nodes(result.currentWorkspace?.expenseReports).map(withExpenses);
69
+ }
70
+
71
+ /** One expense report with its `expenses` as a flat array, or null. */
72
+ export async function getExpenseReport(
73
+ client: UiduClient,
74
+ variables: GetExpenseReportQueryVariables,
75
+ ): Promise<ExpenseReport | null> {
76
+ const result = await client.query(GetExpenseReportDocument, variables);
77
+ const report = result.currentWorkspace?.expenseReport;
78
+ return report ? withExpenses(report) : null;
79
+ }
80
+
81
+ /**
82
+ * Create an expense report for a person: `{ input: { attributes: { userId,
83
+ * name, fieldValues } } }`. Returns the payload (with `errors`) or null.
84
+ */
85
+ export async function createExpenseReport(
86
+ client: UiduClient,
87
+ variables: CreateExpenseReportMutationVariables,
88
+ ) {
89
+ const result = await client.mutate(CreateExpenseReportDocument, variables);
90
+ return result.createExpenseReport ?? null;
91
+ }
92
+
93
+ /**
94
+ * Update an expense report — rename it, set `paidAt`, change its custom
95
+ * `fieldValues`. Returns the payload (with `errors`) or null.
96
+ */
97
+ export async function updateExpenseReport(
98
+ client: UiduClient,
99
+ variables: UpdateExpenseReportMutationVariables,
100
+ ) {
101
+ const result = await client.mutate(UpdateExpenseReportDocument, variables);
102
+ return result.updateExpenseReport ?? null;
103
+ }
104
+
105
+ /**
106
+ * Add an expense to a report: `{ input: { attributes: { expenseReportId,
107
+ * amount, currency, spentAt, category, paymentMethod, fieldValues } } }`, with
108
+ * `amount` in cents. Returns the payload (with `errors`) or null.
109
+ */
110
+ export async function createExpense(
111
+ client: UiduClient,
112
+ variables: CreateExpenseMutationVariables,
113
+ ) {
114
+ const result = await client.mutate(CreateExpenseDocument, variables);
115
+ return result.createExpense ?? null;
116
+ }
117
+
118
+ /** Update an expense. Returns the payload (with `errors`) or null. */
119
+ export async function updateExpense(
120
+ client: UiduClient,
121
+ variables: UpdateExpenseMutationVariables,
122
+ ) {
123
+ const result = await client.mutate(UpdateExpenseDocument, variables);
124
+ return result.updateExpense ?? null;
125
+ }
126
+
127
+ /**
128
+ * The workspace's analytic accounts (commesse, cost centres) as a flat array —
129
+ * what `allocateExpense` books an expense on. `parentId` narrows to the
130
+ * children of one account; page with `first`/`after`.
131
+ */
132
+ export async function listAnalyticAccounts(
133
+ client: UiduClient,
134
+ variables: ListAnalyticAccountsQueryVariables = {},
135
+ ) {
136
+ const result = await client.query(ListAnalyticAccountsDocument, variables);
137
+ return nodes(result.currentWorkspace?.analyticAccounts);
138
+ }
139
+
140
+ /**
141
+ * Book an expense (or part of it) on an analytic account: `{ input: {
142
+ * attributes: { allocatableId: expense.id, analyticAccountId, amount } } }`,
143
+ * `amount` in cents, `currency` defaulting to the expense's. Call it once per
144
+ * account to split an expense. Returns the payload (with `errors`) or null.
145
+ */
146
+ export async function allocateExpense(
147
+ client: UiduClient,
148
+ variables: CreateAnalyticAccountAllocationMutationVariables,
149
+ ) {
150
+ const result = await client.mutate(
151
+ CreateAnalyticAccountAllocationDocument,
152
+ variables,
153
+ );
154
+ return result.createAnalyticAccountAllocation ?? null;
155
+ }