@uidu/client 3.16.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/dist/declarations/src/contacts.d.ts +22 -1
  3. package/dist/declarations/src/courses.d.ts +37 -1
  4. package/dist/declarations/src/events.d.ts +58 -1
  5. package/dist/declarations/src/expenses.d.ts +169 -0
  6. package/dist/declarations/src/generated/index.d.ts +5039 -1610
  7. package/dist/declarations/src/index.d.ts +10 -8
  8. package/dist/declarations/src/mutations.d.ts +89 -1
  9. package/dist/declarations/src/people.d.ts +13 -1
  10. package/dist/uidu-client.cjs.dev.js +219 -1
  11. package/dist/uidu-client.cjs.prod.js +219 -1
  12. package/dist/uidu-client.esm.js +197 -2
  13. package/package.json +1 -1
  14. package/src/contacts.ts +33 -0
  15. package/src/courses.ts +28 -0
  16. package/src/events.ts +42 -0
  17. package/src/expenses.test.ts +155 -0
  18. package/src/expenses.ts +155 -0
  19. package/src/generated/index.ts +4119 -1868
  20. package/src/index.ts +41 -3
  21. package/src/mutations.ts +48 -0
  22. package/src/people.ts +33 -0
  23. package/src/queries/contacts/GetOrganization.graphql +17 -0
  24. package/src/queries/contacts/ListOrganizations.graphql +21 -0
  25. package/src/queries/contacts/OrganizationMutations.graphql +17 -0
  26. package/src/queries/courses/EnrollmentMutations.graphql +27 -0
  27. package/src/queries/events/EventInstanceMutations.graphql +29 -0
  28. package/src/queries/events/UpdateAttendance.graphql +14 -0
  29. package/src/queries/expenses/ExpenseFields.graphql +29 -0
  30. package/src/queries/expenses/ExpenseMutations.graphql +81 -0
  31. package/src/queries/expenses/GetExpenseReport.graphql +14 -0
  32. package/src/queries/expenses/ListAnalyticAccounts.graphql +20 -0
  33. package/src/queries/expenses/ListExpenseReports.graphql +22 -0
  34. package/src/queries/people/EmploymentVariationKindMutations.graphql +51 -0
  35. package/src/queries/people/ListEmploymentVariationKinds.graphql +23 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uidu/client",
3
- "version": "3.16.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
+ }
package/src/courses.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import type { UiduClient } from './client';
2
2
  import {
3
+ CreateEnrollmentDocument,
4
+ type CreateEnrollmentMutationVariables,
3
5
  GetCourseDocument,
4
6
  type GetCourseQuery,
5
7
  type GetCourseQueryVariables,
@@ -15,6 +17,8 @@ import {
15
17
  type ListEnrollmentsQueryVariables,
16
18
  ListLecturesDocument,
17
19
  type ListLecturesQueryVariables,
20
+ UpdateEnrollmentDocument,
21
+ type UpdateEnrollmentMutationVariables,
18
22
  } from './generated';
19
23
  import { nodes } from './utils/relay';
20
24
 
@@ -75,6 +79,30 @@ export async function listEnrollments(
75
79
  return nodes(result.currentWorkspace?.enrollments);
76
80
  }
77
81
 
82
+ /**
83
+ * Enroll a contact in a course; returns the payload as-is,
84
+ * `{ errors, enrollment }`, validation failures included.
85
+ */
86
+ export async function createEnrollment(
87
+ client: UiduClient,
88
+ variables: CreateEnrollmentMutationVariables,
89
+ ) {
90
+ const result = await client.mutate(CreateEnrollmentDocument, variables);
91
+ return result.createEnrollment ?? null;
92
+ }
93
+
94
+ /**
95
+ * Update an enrollment — its `aasmState`, `outcome`, `certificateStatus` or
96
+ * other attributes; returns the payload as-is, `{ errors, enrollment }`.
97
+ */
98
+ export async function updateEnrollment(
99
+ client: UiduClient,
100
+ variables: UpdateEnrollmentMutationVariables,
101
+ ) {
102
+ const result = await client.mutate(UpdateEnrollmentDocument, variables);
103
+ return result.updateEnrollment ?? null;
104
+ }
105
+
78
106
  /**
79
107
  * One lecture with its `body`, its course and its `nextLecture` /
80
108
  * `previousLecture` for navigation, or null.
package/src/events.ts CHANGED
@@ -4,11 +4,17 @@ import {
4
4
  type CreateAttendanceMutationVariables,
5
5
  CreateEventDocument,
6
6
  type CreateEventMutationVariables,
7
+ CreateEventInstanceDocument,
8
+ type CreateEventInstanceMutationVariables,
7
9
  GetEventDocument,
8
10
  type GetEventQuery,
9
11
  type GetEventQueryVariables,
10
12
  ListEventsDocument,
11
13
  type ListEventsQueryVariables,
14
+ UpdateAttendanceDocument,
15
+ type UpdateAttendanceMutationVariables,
16
+ UpdateEventInstanceDocument,
17
+ type UpdateEventInstanceMutationVariables,
12
18
  } from './generated';
13
19
  import { nodes } from './utils/relay';
14
20
 
@@ -55,6 +61,18 @@ export async function createAttendance(
55
61
  return result.createAttendance ?? null;
56
62
  }
57
63
 
64
+ /**
65
+ * Update an attendance — change its `rsvpStatus`, check it in, or edit its
66
+ * other attributes; returns the payload as-is, `{ errors, attendance }`.
67
+ */
68
+ export async function updateAttendance(
69
+ client: UiduClient,
70
+ variables: UpdateAttendanceMutationVariables,
71
+ ) {
72
+ const result = await client.mutate(UpdateAttendanceDocument, variables);
73
+ return result.updateAttendance ?? null;
74
+ }
75
+
58
76
  /**
59
77
  * Create an event; returns the payload as-is, `{ errors, event }`, validation
60
78
  * failures included.
@@ -66,3 +84,27 @@ export async function createEvent(
66
84
  const result = await client.mutate(CreateEventDocument, variables);
67
85
  return result.createEvent ?? null;
68
86
  }
87
+
88
+ /**
89
+ * Create a new occurrence (date/time) of a recurring event; returns the
90
+ * payload as-is, `{ errors, eventInstance }`.
91
+ */
92
+ export async function createEventInstance(
93
+ client: UiduClient,
94
+ variables: CreateEventInstanceMutationVariables,
95
+ ) {
96
+ const result = await client.mutate(CreateEventInstanceDocument, variables);
97
+ return result.createEventInstance ?? null;
98
+ }
99
+
100
+ /**
101
+ * Update an event instance's schedule or venue; returns the payload as-is,
102
+ * `{ errors, eventInstance }`.
103
+ */
104
+ export async function updateEventInstance(
105
+ client: UiduClient,
106
+ variables: UpdateEventInstanceMutationVariables,
107
+ ) {
108
+ const result = await client.mutate(UpdateEventInstanceDocument, variables);
109
+ return result.updateEventInstance ?? null;
110
+ }
@@ -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
+ }