@robosystems/client 0.3.14 → 0.3.16

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.
@@ -3,14 +3,20 @@
3
3
  /**
4
4
  * Investor Client for RoboSystems API
5
5
  *
6
- * High-level facade for the RoboInvestor domain: portfolios, securities,
7
- * positions, and portfolio holdings aggregation. Follows the same
8
- * hybrid transport pattern as `LedgerClient`:
6
+ * High-level facade for the RoboInvestor domain: portfolios (via the
7
+ * Portfolio Block molecule), securities (Master Data CRUD), positions
8
+ * (folded into Portfolio Block writes), and portfolio holdings
9
+ * aggregation. Same hybrid transport pattern as `LedgerClient`:
9
10
  *
10
11
  * - **Reads** go through GraphQL at `/extensions/{graph_id}/graphql`.
11
12
  * - **Writes** go through named operations at
12
13
  * `/extensions/roboinvestor/{graph_id}/operations/{operation_name}`.
13
14
  *
15
+ * Portfolio + position writes flow through the **Portfolio Block**
16
+ * envelope ops (`create-portfolio-block`, `update-portfolio-block`,
17
+ * `delete-portfolio-block`); atom-level CRUD on portfolios/positions
18
+ * has been retired. Securities remain Master Data CRUD.
19
+ *
14
20
  * Every write returns an `OperationEnvelope`; this facade unwraps
15
21
  * `envelope.result` and returns a typed shape derived from the GraphQL
16
22
  * codegen output (which is the source of truth for read payload types).
@@ -19,37 +25,35 @@
19
25
  import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
20
26
  import { ClientError } from 'graphql-request'
21
27
  import {
22
- opCreatePortfolio,
23
- opCreatePosition,
28
+ opCreatePortfolioBlock,
24
29
  opCreateSecurity,
25
- opDeletePortfolio,
26
- opDeletePosition,
30
+ opDeletePortfolioBlock,
27
31
  opDeleteSecurity,
28
- opUpdatePortfolio,
29
- opUpdatePosition,
32
+ opUpdatePortfolioBlock,
30
33
  opUpdateSecurity,
31
34
  } from '../sdk.gen'
32
35
  import type {
33
- CreatePortfolioRequest,
34
- CreatePositionRequest,
36
+ CreatePortfolioBlockRequest,
35
37
  CreateSecurityRequest,
38
+ DeletePortfolioBlockOperation,
36
39
  OperationEnvelope,
37
- UpdatePortfolioOperation,
38
- UpdatePositionOperation,
40
+ PortfolioBlockPortfolioPatch,
41
+ PortfolioBlockPositions,
42
+ UpdatePortfolioBlockOperation,
39
43
  UpdateSecurityOperation,
40
44
  } from '../types.gen'
41
45
  import type { TokenProvider } from './graphql/client'
42
46
  import { GraphQLClientCache } from './graphql/client'
43
47
  import {
44
48
  GetInvestorHoldingsDocument,
45
- GetInvestorPortfolioDocument,
49
+ GetInvestorPortfolioBlockDocument,
46
50
  GetInvestorPositionDocument,
47
51
  GetInvestorSecurityDocument,
48
52
  ListInvestorPortfoliosDocument,
49
53
  ListInvestorPositionsDocument,
50
54
  ListInvestorSecuritiesDocument,
51
55
  type GetInvestorHoldingsQuery,
52
- type GetInvestorPortfolioQuery,
56
+ type GetInvestorPortfolioBlockQuery,
53
57
  type GetInvestorPositionQuery,
54
58
  type GetInvestorSecurityQuery,
55
59
  type ListInvestorPortfoliosQuery,
@@ -61,7 +65,11 @@ import {
61
65
 
62
66
  export type InvestorPortfolioList = NonNullable<ListInvestorPortfoliosQuery['portfolios']>
63
67
  export type InvestorPortfolioSummary = InvestorPortfolioList['portfolios'][number]
64
- export type InvestorPortfolio = NonNullable<GetInvestorPortfolioQuery['portfolio']>
68
+
69
+ export type InvestorPortfolioBlock = NonNullable<GetInvestorPortfolioBlockQuery['portfolioBlock']>
70
+ export type InvestorPositionBlock = InvestorPortfolioBlock['positions'][number]
71
+ export type InvestorSecurityLite = InvestorPositionBlock['security']
72
+ export type InvestorEntityLite = InvestorPortfolioBlock['owner']
65
73
 
66
74
  export type InvestorSecurityList = NonNullable<ListInvestorSecuritiesQuery['securities']>
67
75
  export type InvestorSecuritySummary = InvestorSecurityList['securities'][number]
@@ -99,7 +107,7 @@ export class InvestorClient {
99
107
  this.gql = new GraphQLClientCache(config)
100
108
  }
101
109
 
102
- // ── Portfolios ──────────────────────────────────────────────────────
110
+ // ── Portfolios (reads) ──────────────────────────────────────────────
103
111
 
104
112
  /** List portfolios with pagination. */
105
113
  async listPortfolios(
@@ -118,56 +126,85 @@ export class InvestorClient {
118
126
  )
119
127
  }
120
128
 
121
- /** Get a single portfolio by id. Returns null if it doesn't exist. */
122
- async getPortfolio(graphId: string, portfolioId: string): Promise<InvestorPortfolio | null> {
129
+ /**
130
+ * Get the Portfolio Block molecule envelope with portfolio,
131
+ * positions, securities, and entity references in a single read.
132
+ * Returns null if the portfolio doesn't exist.
133
+ */
134
+ async getPortfolioBlock(
135
+ graphId: string,
136
+ portfolioId: string
137
+ ): Promise<InvestorPortfolioBlock | null> {
123
138
  return this.gqlQuery(
124
139
  graphId,
125
- GetInvestorPortfolioDocument,
140
+ GetInvestorPortfolioBlockDocument,
126
141
  { portfolioId },
127
- 'Get portfolio',
128
- (data) => data.portfolio
142
+ 'Get portfolio block',
143
+ (data) => data.portfolioBlock
129
144
  )
130
145
  }
131
146
 
132
- /** Create a new portfolio. Returns the created portfolio. */
133
- async createPortfolio(graphId: string, body: CreatePortfolioRequest): Promise<InvestorPortfolio> {
147
+ // ── Portfolio Block (writes) ────────────────────────────────────────
148
+
149
+ /**
150
+ * Create a portfolio (with optional initial positions) atomically.
151
+ * Each position references an existing security; this op never mints
152
+ * securities — use `createSecurity` first.
153
+ */
154
+ async createPortfolioBlock(
155
+ graphId: string,
156
+ body: CreatePortfolioBlockRequest
157
+ ): Promise<InvestorPortfolioBlock> {
134
158
  const envelope = await this.callOperation(
135
- 'Create portfolio',
136
- opCreatePortfolio({ path: { graph_id: graphId }, body })
159
+ 'Create portfolio block',
160
+ opCreatePortfolioBlock({ path: { graph_id: graphId }, body })
137
161
  )
138
- return rawToInvestorPortfolio(envelope.result as unknown as RawPortfolioResponse)
162
+ return rawToPortfolioBlock(envelope.result as unknown as RawPortfolioBlockResponse)
139
163
  }
140
164
 
141
- /** Update a portfolio's metadata. Only provided fields are applied. */
142
- async updatePortfolio(
165
+ /**
166
+ * Patch portfolio fields and apply position deltas (`add`, `update`,
167
+ * `dispose`) atomically. All deltas roll back together on failure.
168
+ * Pass only the fields/deltas you want changed — anything omitted
169
+ * is left untouched.
170
+ */
171
+ async updatePortfolioBlock(
143
172
  graphId: string,
144
173
  portfolioId: string,
145
- updates: Omit<UpdatePortfolioOperation, 'portfolio_id'>
146
- ): Promise<InvestorPortfolio> {
174
+ updates: {
175
+ portfolio?: PortfolioBlockPortfolioPatch
176
+ positions?: PortfolioBlockPositions
177
+ }
178
+ ): Promise<InvestorPortfolioBlock> {
179
+ const body: UpdatePortfolioBlockOperation = {
180
+ portfolio_id: portfolioId,
181
+ ...updates,
182
+ }
147
183
  const envelope = await this.callOperation(
148
- 'Update portfolio',
149
- opUpdatePortfolio({
150
- path: { graph_id: graphId },
151
- body: {
152
- ...updates,
153
- portfolio_id: portfolioId,
154
- } as UpdatePortfolioOperation,
155
- })
184
+ 'Update portfolio block',
185
+ opUpdatePortfolioBlock({ path: { graph_id: graphId }, body })
156
186
  )
157
- return rawToInvestorPortfolio(envelope.result as unknown as RawPortfolioResponse)
187
+ return rawToPortfolioBlock(envelope.result as unknown as RawPortfolioBlockResponse)
158
188
  }
159
189
 
160
190
  /**
161
- * Delete a portfolio. Fails with 409 if the portfolio still has active
162
- * positions dispose those first.
191
+ * Cascade-delete the portfolio plus all of its positions. When the
192
+ * portfolio still has active positions, the request must include
193
+ * `confirmActivePositions: true` — safety belt to prevent accidental
194
+ * cascade. Disposed-only portfolios delete without the flag.
163
195
  */
164
- async deletePortfolio(graphId: string, portfolioId: string): Promise<{ deleted: boolean }> {
196
+ async deletePortfolioBlock(
197
+ graphId: string,
198
+ portfolioId: string,
199
+ options?: { confirmActivePositions?: boolean }
200
+ ): Promise<{ deleted: boolean }> {
201
+ const body: DeletePortfolioBlockOperation = {
202
+ portfolio_id: portfolioId,
203
+ confirm_active_positions: options?.confirmActivePositions ?? false,
204
+ }
165
205
  const envelope = await this.callOperation(
166
- 'Delete portfolio',
167
- opDeletePortfolio({
168
- path: { graph_id: graphId },
169
- body: { portfolio_id: portfolioId },
170
- })
206
+ 'Delete portfolio block',
207
+ opDeletePortfolioBlock({ path: { graph_id: graphId }, body })
171
208
  )
172
209
  return (envelope.result ?? { deleted: true }) as { deleted: boolean }
173
210
  }
@@ -254,7 +291,7 @@ export class InvestorClient {
254
291
  return (envelope.result ?? { deleted: true }) as { deleted: boolean }
255
292
  }
256
293
 
257
- // ── Positions ───────────────────────────────────────────────────────
294
+ // ── Positions (reads only — writes folded into Portfolio Block) ─────
258
295
 
259
296
  /** List positions with pagination and filters. */
260
297
  async listPositions(
@@ -293,46 +330,6 @@ export class InvestorClient {
293
330
  )
294
331
  }
295
332
 
296
- /** Create a new position. */
297
- async createPosition(graphId: string, body: CreatePositionRequest): Promise<InvestorPosition> {
298
- const envelope = await this.callOperation(
299
- 'Create position',
300
- opCreatePosition({ path: { graph_id: graphId }, body })
301
- )
302
- return rawToInvestorPosition(envelope.result as unknown as RawPositionResponse)
303
- }
304
-
305
- /** Update a position. Only provided fields are applied. */
306
- async updatePosition(
307
- graphId: string,
308
- positionId: string,
309
- updates: Omit<UpdatePositionOperation, 'position_id'>
310
- ): Promise<InvestorPosition> {
311
- const envelope = await this.callOperation(
312
- 'Update position',
313
- opUpdatePosition({
314
- path: { graph_id: graphId },
315
- body: {
316
- ...updates,
317
- position_id: positionId,
318
- } as UpdatePositionOperation,
319
- })
320
- )
321
- return rawToInvestorPosition(envelope.result as unknown as RawPositionResponse)
322
- }
323
-
324
- /** Delete (dispose) a position. */
325
- async deletePosition(graphId: string, positionId: string): Promise<{ deleted: boolean }> {
326
- const envelope = await this.callOperation(
327
- 'Delete position',
328
- opDeletePosition({
329
- path: { graph_id: graphId },
330
- body: { position_id: positionId },
331
- })
332
- )
333
- return (envelope.result ?? { deleted: true }) as { deleted: boolean }
334
- }
335
-
336
333
  // ── Holdings (aggregation) ─────────────────────────────────────────
337
334
 
338
335
  /**
@@ -390,27 +387,13 @@ export class InvestorClient {
390
387
 
391
388
  // ── Module-private conversion helpers ─────────────────────────────────
392
389
  //
393
- // The RoboInvestor write operations (create/update portfolio, security,
394
- // position) return Pydantic-serialized envelopes where `result` is in
395
- // snake_case. The GraphQL-derived `InvestorPortfolio` / `InvestorSecurity`
396
- // / `InvestorPosition` types are in camelCase (matching the read path).
397
- //
398
- // These helpers are the single place where we bridge the two naming
399
- // conventions on the write path, mirroring the `rawFiscalCalendarToCamel`
400
- // / `rawToClosingEntry` pattern in `LedgerClient`. Keeping the unsafe
401
- // `as unknown as` cast localized to these helpers means a backend schema
402
- // drift surfaces here rather than silently at every call site.
403
-
404
- interface RawPortfolioResponse {
405
- id: string
406
- name: string
407
- description: string | null
408
- strategy: string | null
409
- inception_date: string | null
410
- base_currency: string
411
- created_at: string
412
- updated_at: string
413
- }
390
+ // The RoboInvestor write operations return Pydantic-serialized envelopes
391
+ // where `result` is in snake_case. The GraphQL-derived `Investor*` types
392
+ // are in camelCase (matching the read path). These helpers are the single
393
+ // place where we bridge the two naming conventions on the write path,
394
+ // mirroring the equivalent helpers in `LedgerClient`. Keeping the unsafe
395
+ // `as unknown as` cast localized here means a backend schema drift surfaces
396
+ // in one place rather than silently at every call site.
414
397
 
415
398
  interface RawSecurityResponse {
416
399
  id: string
@@ -428,40 +411,50 @@ interface RawSecurityResponse {
428
411
  updated_at: string
429
412
  }
430
413
 
431
- interface RawPositionResponse {
414
+ interface RawEntityLite {
415
+ id: string
416
+ name: string
417
+ source_graph_id: string | null
418
+ }
419
+
420
+ interface RawSecurityLite {
421
+ id: string
422
+ name: string
423
+ security_type: string
424
+ security_subtype: string | null
425
+ is_active: boolean
426
+ issuer: RawEntityLite | null
427
+ source_graph_id: string | null
428
+ }
429
+
430
+ interface RawPositionBlock {
432
431
  id: string
433
- portfolio_id: string
434
- security_id: string
435
- security_name: string | null
436
- entity_name: string | null
437
432
  quantity: number
438
433
  quantity_type: string
439
- cost_basis: number
440
434
  cost_basis_dollars: number
441
- currency: string
442
- current_value: number | null
443
435
  current_value_dollars: number | null
444
436
  valuation_date: string | null
445
437
  valuation_source: string | null
446
438
  acquisition_date: string | null
447
- disposition_date: string | null
448
439
  status: string
449
440
  notes: string | null
450
- created_at: string
451
- updated_at: string
441
+ security: RawSecurityLite
452
442
  }
453
443
 
454
- function rawToInvestorPortfolio(raw: RawPortfolioResponse): InvestorPortfolio {
455
- return {
456
- id: raw.id,
457
- name: raw.name,
458
- description: raw.description,
459
- strategy: raw.strategy,
460
- inceptionDate: raw.inception_date,
461
- baseCurrency: raw.base_currency,
462
- createdAt: raw.created_at,
463
- updatedAt: raw.updated_at,
464
- }
444
+ interface RawPortfolioBlockResponse {
445
+ id: string
446
+ name: string
447
+ description: string | null
448
+ strategy: string | null
449
+ inception_date: string | null
450
+ base_currency: string
451
+ owner: RawEntityLite | null
452
+ positions: RawPositionBlock[]
453
+ total_cost_basis_dollars: number
454
+ total_current_value_dollars: number | null
455
+ active_position_count: number
456
+ created_at: string
457
+ updated_at: string
465
458
  }
466
459
 
467
460
  function rawToInvestorSecurity(raw: RawSecurityResponse): InvestorSecurity {
@@ -482,26 +475,56 @@ function rawToInvestorSecurity(raw: RawSecurityResponse): InvestorSecurity {
482
475
  }
483
476
  }
484
477
 
485
- function rawToInvestorPosition(raw: RawPositionResponse): InvestorPosition {
478
+ function rawToEntityLite(raw: RawEntityLite | null): InvestorEntityLite {
479
+ if (raw === null) return null
480
+ return {
481
+ id: raw.id,
482
+ name: raw.name,
483
+ sourceGraphId: raw.source_graph_id,
484
+ }
485
+ }
486
+
487
+ function rawToSecurityLite(raw: RawSecurityLite): InvestorSecurityLite {
488
+ return {
489
+ id: raw.id,
490
+ name: raw.name,
491
+ securityType: raw.security_type,
492
+ securitySubtype: raw.security_subtype,
493
+ isActive: raw.is_active,
494
+ issuer: rawToEntityLite(raw.issuer),
495
+ sourceGraphId: raw.source_graph_id,
496
+ }
497
+ }
498
+
499
+ function rawToPositionBlock(raw: RawPositionBlock): InvestorPositionBlock {
486
500
  return {
487
501
  id: raw.id,
488
- portfolioId: raw.portfolio_id,
489
- securityId: raw.security_id,
490
- securityName: raw.security_name,
491
- entityName: raw.entity_name,
492
502
  quantity: raw.quantity,
493
503
  quantityType: raw.quantity_type,
494
- costBasis: raw.cost_basis,
495
504
  costBasisDollars: raw.cost_basis_dollars,
496
- currency: raw.currency,
497
- currentValue: raw.current_value,
498
505
  currentValueDollars: raw.current_value_dollars,
499
506
  valuationDate: raw.valuation_date,
500
507
  valuationSource: raw.valuation_source,
501
508
  acquisitionDate: raw.acquisition_date,
502
- dispositionDate: raw.disposition_date,
503
509
  status: raw.status,
504
510
  notes: raw.notes,
511
+ security: rawToSecurityLite(raw.security),
512
+ }
513
+ }
514
+
515
+ function rawToPortfolioBlock(raw: RawPortfolioBlockResponse): InvestorPortfolioBlock {
516
+ return {
517
+ id: raw.id,
518
+ name: raw.name,
519
+ description: raw.description,
520
+ strategy: raw.strategy,
521
+ inceptionDate: raw.inception_date,
522
+ baseCurrency: raw.base_currency,
523
+ owner: rawToEntityLite(raw.owner),
524
+ positions: raw.positions.map(rawToPositionBlock),
525
+ totalCostBasisDollars: raw.total_cost_basis_dollars,
526
+ totalCurrentValueDollars: raw.total_current_value_dollars,
527
+ activePositionCount: raw.active_position_count,
505
528
  createdAt: raw.created_at,
506
529
  updatedAt: raw.updated_at,
507
530
  }