@uidu/client 3.21.0 → 3.23.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 (37) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/declarations/src/candidates.d.ts +69 -3
  3. package/dist/declarations/src/comments.d.ts +47 -0
  4. package/dist/declarations/src/contracts.d.ts +2 -0
  5. package/dist/declarations/src/decisions.d.ts +58 -0
  6. package/dist/declarations/src/generated/index.d.ts +6727 -1966
  7. package/dist/declarations/src/index.d.ts +10 -4
  8. package/dist/declarations/src/models.d.ts +28 -1
  9. package/dist/declarations/src/mutations.d.ts +156 -1
  10. package/dist/declarations/src/votes.d.ts +114 -0
  11. package/dist/uidu-client.cjs.dev.js +301 -3
  12. package/dist/uidu-client.cjs.prod.js +301 -3
  13. package/dist/uidu-client.esm.js +286 -4
  14. package/package.json +1 -1
  15. package/src/candidates.ts +21 -2
  16. package/src/comments.ts +44 -0
  17. package/src/decisions.test.ts +275 -0
  18. package/src/decisions.ts +135 -0
  19. package/src/generated/index.ts +1209 -36
  20. package/src/index.ts +26 -0
  21. package/src/models.ts +50 -0
  22. package/src/mutations.ts +72 -0
  23. package/src/queries/candidates/CandidateMutations.graphql +14 -9
  24. package/src/queries/candidates/CandidateQueries.graphql +1 -7
  25. package/src/queries/candidates/PoolContactFields.graphql +29 -0
  26. package/src/queries/comments/Comments.graphql +65 -0
  27. package/src/queries/contacts/AddressMutations.graphql +41 -0
  28. package/src/queries/contacts/EmailAddressMutations.graphql +39 -0
  29. package/src/queries/contacts/PhoneNumberMutations.graphql +39 -0
  30. package/src/queries/decisions/DecisionFields.graphql +83 -0
  31. package/src/queries/decisions/DecisionMutations.graphql +59 -0
  32. package/src/queries/decisions/DecisionQueries.graphql +32 -0
  33. package/src/queries/models/ModelFields.graphql +2 -0
  34. package/src/queries/models/ModelQueries.graphql +33 -0
  35. package/src/queries/votes/Votes.graphql +60 -0
  36. package/src/recruiting.test.ts +41 -0
  37. package/src/votes.ts +38 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uidu/client",
3
- "version": "3.21.0",
3
+ "version": "3.23.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/candidates.ts CHANGED
@@ -23,6 +23,8 @@ import {
23
23
  type ListPoolsQueryVariables,
24
24
  UpdateCandidateDocument,
25
25
  type UpdateCandidateMutationVariables,
26
+ UpdatePoolContactDocument,
27
+ type UpdatePoolContactMutationVariables,
26
28
  } from './generated';
27
29
  import { nodes } from './utils/relay';
28
30
 
@@ -162,8 +164,12 @@ export async function getPool(
162
164
  }
163
165
 
164
166
  /**
165
- * Add a contact to a pool (`attributes: { poolId, contactId }`). Returns the
166
- * mutation payload as-is (including any `errors`).
167
+ * Refer a contact to a pool (`attributes: { poolId, contactId, sourceId?,
168
+ * sourceChannel?, referredOn? }`). Each call is one referral, so the same
169
+ * person may enter the same pool more than once: `sourceId` is who sent them
170
+ * (the GlobalID of a referring Organization or of a User), `sourceChannel` a
171
+ * short slug like an application's, `referredOn` the day (`YYYY-MM-DD`, today
172
+ * when left out). Returns the mutation payload as-is (including any `errors`).
167
173
  */
168
174
  export async function createPoolContact(
169
175
  client: UiduClient,
@@ -173,6 +179,19 @@ export async function createPoolContact(
173
179
  return result.createPoolContact ?? null;
174
180
  }
175
181
 
182
+ /**
183
+ * Correct a referral, by the PoolContact's GlobalID (`id`, `attributes: {
184
+ * sourceId?, sourceChannel?, referredOn? }`). Returns the mutation payload
185
+ * as-is (including any `errors`).
186
+ */
187
+ export async function updatePoolContact(
188
+ client: UiduClient,
189
+ variables: UpdatePoolContactMutationVariables,
190
+ ) {
191
+ const result = await client.mutate(UpdatePoolContactDocument, variables);
192
+ return result.updatePoolContact ?? null;
193
+ }
194
+
176
195
  /**
177
196
  * Take a contact out of a pool, by the PoolContact's GlobalID. Returns the
178
197
  * mutation payload as-is (including any `errors`).
@@ -0,0 +1,44 @@
1
+ import type { UiduClient } from './client';
2
+ import {
3
+ type CommentFieldsFragment,
4
+ CreateCommentDocument,
5
+ ListCommentsDocument,
6
+ } from './generated';
7
+ import { nodes } from './utils/relay';
8
+
9
+ // Comments on a record — for a custom app, one of its ModelItems or its
10
+ // Decisions (Comments::Comment::Read / Create). The author is always the
11
+ // contact of the token's user. Replies point at their thread's comment with
12
+ // `parent.id` (a GlobalID, like `id`); the list is flat, group it by that.
13
+
14
+ export type Comment = CommentFieldsFragment;
15
+
16
+ /**
17
+ * The comments on a ModelItem or a Decision, flat (a reply
18
+ * carries `parent.id`). Empty when the record has none, isn't commentable or can't be seen.
19
+ */
20
+ export async function listComments(
21
+ client: UiduClient,
22
+ variables: { commentableId: string; first?: number; after?: string },
23
+ ): Promise<Comment[]> {
24
+ const { node } = await client.query(ListCommentsDocument, variables);
25
+ if (node?.__typename === 'ModelItem' || node?.__typename === 'Decision') {
26
+ return nodes(node.comments);
27
+ }
28
+ return [];
29
+ }
30
+
31
+ /**
32
+ * Comment on a ModelItem or a Decision as the token's user, or reply to a
33
+ * comment (`parentId`: its `id`). `bodyData` is an editor document (Tiptap JSON).
34
+ * Returns the payload (`comment`, `errors`) or null.
35
+ */
36
+ export async function createComment(
37
+ client: UiduClient,
38
+ variables: { commentableId: string; bodyData: unknown; parentId?: string },
39
+ ) {
40
+ const result = await client.mutate(CreateCommentDocument, {
41
+ input: { attributes: variables },
42
+ });
43
+ return result.createComment ?? null;
44
+ }
@@ -0,0 +1,275 @@
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 sdk = await import('./index');
14
+
15
+ const client = createClient({
16
+ endpoint: 'https://acme.uidu.org/graphql',
17
+ getToken: async () => 'session',
18
+ });
19
+
20
+ const operation = (call: unknown[]) =>
21
+ (call[0] as { definitions: Array<{ name?: { value: string } }> })
22
+ .definitions[0].name?.value;
23
+
24
+ const edges = <T>(items: T[]) => ({
25
+ pageInfo: { hasNextPage: false, endCursor: null },
26
+ edges: items.map((node) => ({ node })),
27
+ });
28
+
29
+ beforeEach(() => request.mockReset());
30
+
31
+ describe('decisions', () => {
32
+ it('listDecisions flattens the connection and passes the term as params', async () => {
33
+ request.mockResolvedValue({
34
+ currentWorkspace: {
35
+ decisions: edges([{ id: 'dec_1' }, { id: 'dec_2' }]),
36
+ },
37
+ });
38
+ await expect(
39
+ sdk.listDecisions(client, { term: 'harness', first: 10 }),
40
+ ).resolves.toEqual([{ id: 'dec_1' }, { id: 'dec_2' }]);
41
+ expect(operation(request.mock.calls[0])).toBe('ListDecisions');
42
+ expect(request.mock.calls[0][1]).toEqual({
43
+ first: 10,
44
+ params: { term: 'harness' },
45
+ });
46
+ });
47
+
48
+ it('getDecision returns the decision or null', async () => {
49
+ request.mockResolvedValue({
50
+ currentWorkspace: { decision: { id: 'dec_1' } },
51
+ });
52
+ await expect(sdk.getDecision(client, { id: 'dec_1' })).resolves.toEqual({
53
+ id: 'dec_1',
54
+ });
55
+ request.mockResolvedValue({ currentWorkspace: { decision: null } });
56
+ await expect(sdk.getDecision(client, { id: 'x' })).resolves.toBeNull();
57
+ });
58
+
59
+ it('createDecision creates a proposal of the app and opens it', async () => {
60
+ request
61
+ .mockResolvedValueOnce({
62
+ createDecision: {
63
+ decision: { node: { id: 'dec_1', aasmState: 'draft' } },
64
+ errors: [],
65
+ },
66
+ })
67
+ .mockResolvedValueOnce({
68
+ openDecision: {
69
+ decision: { id: 'dec_1', aasmState: 'open' },
70
+ errors: [],
71
+ },
72
+ });
73
+
74
+ await expect(
75
+ sdk.createDecision(client, { workspaceAppId: 'wap_1', name: 'Bet A' }),
76
+ ).resolves.toEqual({
77
+ decision: { id: 'dec_1', aasmState: 'open' },
78
+ errors: [],
79
+ });
80
+ expect(operation(request.mock.calls[0])).toBe('CreateDecision');
81
+ expect(request.mock.calls[0][1]).toEqual({
82
+ input: {
83
+ attributes: {
84
+ workspaceAppId: 'wap_1',
85
+ kind: 'proposal',
86
+ name: 'Bet A',
87
+ descriptionData: undefined,
88
+ },
89
+ },
90
+ });
91
+ expect(operation(request.mock.calls[1])).toBe('OpenDecision');
92
+ expect(request.mock.calls[1][1]).toEqual({ input: { id: 'dec_1' } });
93
+ });
94
+
95
+ it('createDecision stops at the draft on errors or open: false', async () => {
96
+ const errors = [{ key: 'name', name: "can't be blank", type: 'blank' }];
97
+ request.mockResolvedValue({
98
+ createDecision: { decision: null, errors },
99
+ });
100
+ await expect(
101
+ sdk.createDecision(client, { workspaceAppId: 'wap_1', name: '' }),
102
+ ).resolves.toEqual({ decision: null, errors });
103
+
104
+ request.mockReset();
105
+ request.mockResolvedValue({
106
+ createDecision: { decision: { node: { id: 'dec_2' } }, errors: [] },
107
+ });
108
+ await sdk.createDecision(client, {
109
+ workspaceAppId: 'wap_1',
110
+ name: 'Draft',
111
+ open: false,
112
+ });
113
+ expect(request).toHaveBeenCalledTimes(1);
114
+ });
115
+
116
+ it('answerDecision creates an answer, or updates the existing one', async () => {
117
+ request.mockResolvedValue({
118
+ createDecisionResponse: {
119
+ decisionResponse: { node: { id: 'res_1', responseType: 'agree' } },
120
+ errors: [],
121
+ },
122
+ });
123
+ await expect(
124
+ sdk.answerDecision(client, {
125
+ decisionId: 'dec_1',
126
+ responseType: 'agree',
127
+ }),
128
+ ).resolves.toEqual({
129
+ decisionResponse: { id: 'res_1', responseType: 'agree' },
130
+ errors: [],
131
+ });
132
+ expect(operation(request.mock.calls[0])).toBe('CreateDecisionResponse');
133
+ expect(request.mock.calls[0][1]).toEqual({
134
+ input: {
135
+ decisionId: 'dec_1',
136
+ attributes: { responseType: 'agree', reason: undefined },
137
+ },
138
+ });
139
+
140
+ request.mockReset();
141
+ request.mockResolvedValue({
142
+ updateDecisionResponse: {
143
+ decisionResponse: { id: 'res_1', responseType: 'block' },
144
+ errors: [],
145
+ },
146
+ });
147
+ await expect(
148
+ sdk.answerDecision(client, {
149
+ decisionId: 'dec_1',
150
+ responseId: 'res_1',
151
+ responseType: 'block',
152
+ reason: 'Too early',
153
+ }),
154
+ ).resolves.toEqual({
155
+ decisionResponse: { id: 'res_1', responseType: 'block' },
156
+ errors: [],
157
+ });
158
+ expect(operation(request.mock.calls[0])).toBe('UpdateDecisionResponse');
159
+ expect(request.mock.calls[0][1]).toEqual({
160
+ input: {
161
+ id: 'res_1',
162
+ attributes: { responseType: 'block', reason: 'Too early' },
163
+ },
164
+ });
165
+ });
166
+ });
167
+
168
+ describe('comments', () => {
169
+ it('listComments reads a ModelItem or a Decision through node(id:)', async () => {
170
+ for (const __typename of ['ModelItem', 'Decision']) {
171
+ request.mockResolvedValueOnce({
172
+ node: { __typename, id: 'rec_1', comments: edges([{ id: 'cmt_1' }]) },
173
+ });
174
+ await expect(
175
+ sdk.listComments(client, { commentableId: 'rec_1' }),
176
+ ).resolves.toEqual([{ id: 'cmt_1' }]);
177
+ }
178
+ expect(request.mock.calls[0][1]).toEqual({ commentableId: 'rec_1' });
179
+
180
+ request.mockResolvedValueOnce({ node: { __typename: 'Space' } });
181
+ await expect(
182
+ sdk.listComments(client, { commentableId: 'x' }),
183
+ ).resolves.toEqual([]);
184
+ });
185
+
186
+ it('createComment wraps the attributes', async () => {
187
+ request.mockResolvedValue({
188
+ createComment: { comment: { id: 'cmt_2' }, errors: [] },
189
+ });
190
+ const bodyData = { type: 'doc', content: [] };
191
+ await expect(
192
+ sdk.createComment(client, {
193
+ commentableId: 'itm_1',
194
+ bodyData,
195
+ parentId: 'cmt_1',
196
+ }),
197
+ ).resolves.toEqual({ comment: { id: 'cmt_2' }, errors: [] });
198
+ expect(request.mock.calls[0][1]).toEqual({
199
+ input: {
200
+ attributes: { commentableId: 'itm_1', bodyData, parentId: 'cmt_1' },
201
+ },
202
+ });
203
+ });
204
+ });
205
+
206
+ describe('votes', () => {
207
+ it('vote upvotes by default', async () => {
208
+ request.mockResolvedValue({
209
+ createVote: {
210
+ vote: { node: { id: 'vot_1', mine: true } },
211
+ votable: { __typename: 'ModelItem', id: 'itm_1', votesCount: 3 },
212
+ errors: [],
213
+ },
214
+ });
215
+ const result = await sdk.vote(client, { votableId: 'itm_1' });
216
+ expect(result?.vote.node?.id).toBe('vot_1');
217
+ expect(request.mock.calls[0][1]).toEqual({
218
+ input: {
219
+ votableId: 'itm_1',
220
+ attributes: { voteFlag: true, emoji: undefined },
221
+ },
222
+ });
223
+ });
224
+
225
+ it('unvote returns the payload, errors included', async () => {
226
+ const errors = [
227
+ {
228
+ key: 'base',
229
+ name: 'You can only take back your own vote',
230
+ type: 'invalid',
231
+ },
232
+ ];
233
+ request.mockResolvedValue({
234
+ deleteVote: { vote: null, votable: { __typename: 'ModelItem' }, errors },
235
+ });
236
+ const result = await sdk.unvote(client, { id: 'vot_9' });
237
+ expect(result?.errors).toEqual(errors);
238
+ expect(operation(request.mock.calls[0])).toBe('DeleteVote');
239
+ expect(request.mock.calls[0][1]).toEqual({ input: { id: 'vot_9' } });
240
+ });
241
+ });
242
+
243
+ describe('getModelItemLog', () => {
244
+ it('asks for Updated events by default and lifts data.changes', async () => {
245
+ request.mockResolvedValue({
246
+ node: {
247
+ __typename: 'ModelItem',
248
+ id: 'itm_1',
249
+ logs: edges([
250
+ {
251
+ id: 'evt_1',
252
+ type: 'Database::ModelItem::Updated',
253
+ data: { changes: { fld_horizon: ['opt_now', 'opt_next'] } },
254
+ timestamp: '2026-11-17T10:00:00Z',
255
+ },
256
+ { id: 'evt_2', type: 'Database::ModelItem::Updated', data: null },
257
+ ]),
258
+ },
259
+ });
260
+ const log = await sdk.getModelItemLog(client, { id: 'itm_1' });
261
+ expect(request.mock.calls[0][1]).toEqual({
262
+ id: 'itm_1',
263
+ types: ['Database::ModelItem::Updated'],
264
+ });
265
+ expect(log.map((e) => e.changes)).toEqual([
266
+ { fld_horizon: ['opt_now', 'opt_next'] },
267
+ {},
268
+ ]);
269
+ });
270
+
271
+ it('is empty when the node is not a ModelItem', async () => {
272
+ request.mockResolvedValue({ node: null });
273
+ await expect(sdk.getModelItemLog(client, { id: 'x' })).resolves.toEqual([]);
274
+ });
275
+ });
@@ -0,0 +1,135 @@
1
+ import type { UiduClient } from './client';
2
+ import {
3
+ CreateDecisionDocument,
4
+ CreateDecisionResponseDocument,
5
+ type DecisionFieldsFragment,
6
+ type DecisionResponseFieldsFragment,
7
+ GetDecisionDocument,
8
+ type GetDecisionQuery,
9
+ ListDecisionsDocument,
10
+ OpenDecisionDocument,
11
+ UpdateDecisionResponseDocument,
12
+ } from './generated';
13
+ import { nodes } from './utils/relay';
14
+
15
+ // Decisions are native to every uidu workspace: something proposed, the
16
+ // team's answers (agree, disagree, abstain, block, with a reason) and where it
17
+ // stands (`aasmState`: draft, open, closed…).
18
+ //
19
+ // A custom app sees only its own: decisions created with its `workspaceAppId`
20
+ // (`listDecisions` is narrowed to them under a session token, `getDecision` of
21
+ // another one fails). It needs the Decisions::Decision::Read / Create / Answer
22
+ // permissions. Whoever answers is the contact of the token's user — a session
23
+ // answers for the signed-in member, an `apiKey` for the account's user — never
24
+ // someone named in the call.
25
+
26
+ export type Decision = DecisionFieldsFragment;
27
+ export type DecisionResponse = DecisionResponseFieldsFragment;
28
+ export type DecisionWithResponses = NonNullable<
29
+ NonNullable<GetDecisionQuery['currentWorkspace']>['decision']
30
+ >;
31
+
32
+ /** How a person answers a proposal. */
33
+ export type DecisionResponseType = 'agree' | 'disagree' | 'abstain' | 'block';
34
+
35
+ type MutationError = { key: string; name: string; type: string };
36
+
37
+ /**
38
+ * The workspace's decisions, newest first, with their options and the
39
+ * caller's own answer (`myResponse`); a custom app's session gets only its own.
40
+ * `term` searches by text; page with `first`/`after`.
41
+ */
42
+ export async function listDecisions(
43
+ client: UiduClient,
44
+ variables: { term?: string; first?: number; after?: string } = {},
45
+ ): Promise<Decision[]> {
46
+ const { term, ...page } = variables;
47
+ const result = await client.query(ListDecisionsDocument, {
48
+ ...page,
49
+ params: term ? { term } : {},
50
+ });
51
+ return nodes(result.currentWorkspace?.decisions);
52
+ }
53
+
54
+ /** One decision with every answer (`decisionResponses`), or null when it can't be seen. */
55
+ export async function getDecision(
56
+ client: UiduClient,
57
+ variables: { id: string },
58
+ ): Promise<DecisionWithResponses | null> {
59
+ const result = await client.query(GetDecisionDocument, variables);
60
+ return result.currentWorkspace?.decision ?? null;
61
+ }
62
+
63
+ /**
64
+ * Propose something in a custom app: a `proposal` decision of the app's
65
+ * instance (`workspaceAppId`), opened straight away so it takes answers — a
66
+ * new decision is a draft until `openDecision`. Pass `open: false` to leave it
67
+ * a draft. Returns `{ decision, errors }`; on a failed open, `decision` is the
68
+ * draft that was created.
69
+ */
70
+ export async function createDecision(
71
+ client: UiduClient,
72
+ variables: {
73
+ workspaceAppId: string;
74
+ name: string;
75
+ /** Tiptap/ProseMirror JSON (`{ type: 'doc', content: [...] }`). */
76
+ descriptionData?: unknown;
77
+ open?: boolean;
78
+ },
79
+ ): Promise<{ decision: Decision | null; errors: MutationError[] }> {
80
+ const { workspaceAppId, name, descriptionData, open = true } = variables;
81
+ const created = await client.mutate(CreateDecisionDocument, {
82
+ input: {
83
+ attributes: { workspaceAppId, kind: 'proposal', name, descriptionData },
84
+ },
85
+ });
86
+ const draft = created.createDecision?.decision?.node ?? null;
87
+ const errors = created.createDecision?.errors ?? [];
88
+ if (!draft || errors.length || !open) return { decision: draft, errors };
89
+
90
+ const opened = await client.mutate(OpenDecisionDocument, {
91
+ input: { id: draft.id },
92
+ });
93
+ return {
94
+ decision: opened.openDecision?.decision ?? draft,
95
+ errors: opened.openDecision?.errors ?? [],
96
+ };
97
+ }
98
+
99
+ /**
100
+ * Answer a decision as the token's user, or change that answer: pass
101
+ * `responseId` (the decision's `myResponse.id`) when there is one already. A
102
+ * `block` wants a `reason`. Returns `{ decisionResponse, errors }`.
103
+ */
104
+ export async function answerDecision(
105
+ client: UiduClient,
106
+ variables: {
107
+ decisionId: string;
108
+ responseType: DecisionResponseType;
109
+ reason?: string | null;
110
+ responseId?: string | null;
111
+ },
112
+ ): Promise<{
113
+ decisionResponse: DecisionResponse | null;
114
+ errors: MutationError[];
115
+ }> {
116
+ const { decisionId, responseType, reason, responseId } = variables;
117
+ const attributes = { responseType, reason };
118
+ if (responseId) {
119
+ const result = await client.mutate(UpdateDecisionResponseDocument, {
120
+ input: { id: responseId, attributes },
121
+ });
122
+ return {
123
+ decisionResponse: result.updateDecisionResponse?.decisionResponse ?? null,
124
+ errors: result.updateDecisionResponse?.errors ?? [],
125
+ };
126
+ }
127
+ const result = await client.mutate(CreateDecisionResponseDocument, {
128
+ input: { decisionId, attributes },
129
+ });
130
+ return {
131
+ decisionResponse:
132
+ result.createDecisionResponse?.decisionResponse?.node ?? null,
133
+ errors: result.createDecisionResponse?.errors ?? [],
134
+ };
135
+ }