@kernhq/module-inventory 0.1.2 → 0.3.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 (213) hide show
  1. package/README.md +134 -9
  2. package/dist/contract/capabilities.d.ts +49 -0
  3. package/dist/contract/capabilities.d.ts.map +1 -0
  4. package/dist/contract/capabilities.js +94 -0
  5. package/dist/contract/capabilities.js.map +1 -0
  6. package/dist/contract/events.d.ts +76 -0
  7. package/dist/contract/events.d.ts.map +1 -0
  8. package/dist/contract/events.js +62 -0
  9. package/dist/contract/events.js.map +1 -0
  10. package/dist/contract/index.d.ts +16 -0
  11. package/dist/contract/index.d.ts.map +1 -0
  12. package/dist/contract/index.js +16 -0
  13. package/dist/contract/index.js.map +1 -0
  14. package/dist/contract/models.d.ts +468 -0
  15. package/dist/contract/models.d.ts.map +1 -0
  16. package/dist/contract/models.js +320 -0
  17. package/dist/contract/models.js.map +1 -0
  18. package/dist/contract/notifications.d.ts +24 -0
  19. package/dist/contract/notifications.d.ts.map +1 -0
  20. package/dist/contract/notifications.js +72 -0
  21. package/dist/contract/notifications.js.map +1 -0
  22. package/dist/contract/permissions.d.ts +72 -0
  23. package/dist/contract/permissions.d.ts.map +1 -0
  24. package/dist/contract/permissions.js +79 -0
  25. package/dist/contract/permissions.js.map +1 -0
  26. package/dist/contract/router.d.ts +1378 -0
  27. package/dist/contract/router.d.ts.map +1 -0
  28. package/dist/contract/router.js +266 -0
  29. package/dist/contract/router.js.map +1 -0
  30. package/dist/contract/settings.d.ts +20 -0
  31. package/dist/contract/settings.d.ts.map +1 -0
  32. package/dist/contract/settings.js +49 -0
  33. package/dist/contract/settings.js.map +1 -0
  34. package/dist/server/index.d.ts +8 -1
  35. package/dist/server/index.d.ts.map +1 -1
  36. package/dist/server/index.js +288 -8
  37. package/dist/server/index.js.map +1 -1
  38. package/dist/server/jobs.d.ts +48 -0
  39. package/dist/server/jobs.d.ts.map +1 -0
  40. package/dist/server/jobs.js +358 -0
  41. package/dist/server/jobs.js.map +1 -0
  42. package/dist/server/router.d.ts +1789 -0
  43. package/dist/server/router.d.ts.map +1 -0
  44. package/dist/server/router.js +439 -0
  45. package/dist/server/router.js.map +1 -0
  46. package/dist/server/schema.d.ts +156 -9
  47. package/dist/server/schema.d.ts.map +1 -1
  48. package/dist/server/schema.js +183 -11
  49. package/dist/server/schema.js.map +1 -1
  50. package/dist/server/services/assets.d.ts +139 -0
  51. package/dist/server/services/assets.d.ts.map +1 -0
  52. package/dist/server/services/assets.js +429 -0
  53. package/dist/server/services/assets.js.map +1 -0
  54. package/dist/server/services/attachments.d.ts +80 -0
  55. package/dist/server/services/attachments.d.ts.map +1 -0
  56. package/dist/server/services/attachments.js +182 -0
  57. package/dist/server/services/attachments.js.map +1 -0
  58. package/dist/server/services/audience.d.ts +15 -0
  59. package/dist/server/services/audience.d.ts.map +1 -0
  60. package/dist/server/services/audience.js +64 -0
  61. package/dist/server/services/audience.js.map +1 -0
  62. package/dist/server/services/categories.d.ts +57 -0
  63. package/dist/server/services/categories.d.ts.map +1 -0
  64. package/dist/server/services/categories.js +124 -0
  65. package/dist/server/services/categories.js.map +1 -0
  66. package/dist/server/services/cursor.d.ts +68 -0
  67. package/dist/server/services/cursor.d.ts.map +1 -0
  68. package/dist/server/services/cursor.js +39 -0
  69. package/dist/server/services/cursor.js.map +1 -0
  70. package/dist/server/services/custody.d.ts +175 -0
  71. package/dist/server/services/custody.d.ts.map +1 -0
  72. package/dist/server/services/custody.js +367 -0
  73. package/dist/server/services/custody.js.map +1 -0
  74. package/dist/server/services/db-errors.d.ts +7 -0
  75. package/dist/server/services/db-errors.d.ts.map +1 -0
  76. package/dist/server/services/db-errors.js +32 -0
  77. package/dist/server/services/db-errors.js.map +1 -0
  78. package/dist/server/services/index.d.ts +26 -0
  79. package/dist/server/services/index.d.ts.map +1 -0
  80. package/dist/server/services/index.js +39 -0
  81. package/dist/server/services/index.js.map +1 -0
  82. package/dist/server/services/members.d.ts +27 -0
  83. package/dist/server/services/members.d.ts.map +1 -0
  84. package/dist/server/services/members.js +39 -0
  85. package/dist/server/services/members.js.map +1 -0
  86. package/dist/server/services/notify.d.ts +105 -0
  87. package/dist/server/services/notify.d.ts.map +1 -0
  88. package/dist/server/services/notify.js +147 -0
  89. package/dist/server/services/notify.js.map +1 -0
  90. package/dist/server/services/offboarding.d.ts +70 -0
  91. package/dist/server/services/offboarding.d.ts.map +1 -0
  92. package/dist/server/services/offboarding.js +116 -0
  93. package/dist/server/services/offboarding.js.map +1 -0
  94. package/dist/server/services/repairs.d.ts +204 -0
  95. package/dist/server/services/repairs.d.ts.map +1 -0
  96. package/dist/server/services/repairs.js +476 -0
  97. package/dist/server/services/repairs.js.map +1 -0
  98. package/dist/server/services/search.d.ts +85 -0
  99. package/dist/server/services/search.d.ts.map +1 -0
  100. package/dist/server/services/search.js +142 -0
  101. package/dist/server/services/search.js.map +1 -0
  102. package/dist/server/services/stats.d.ts +42 -0
  103. package/dist/server/services/stats.d.ts.map +1 -0
  104. package/dist/server/services/stats.js +80 -0
  105. package/dist/server/services/stats.js.map +1 -0
  106. package/dist/server/services/status.d.ts +102 -0
  107. package/dist/server/services/status.d.ts.map +1 -0
  108. package/dist/server/services/status.js +71 -0
  109. package/dist/server/services/status.js.map +1 -0
  110. package/migrations/0000_init.sql +12 -3
  111. package/migrations/0001_rls.sql +24 -0
  112. package/migrations/0002_custody_and_categories.sql +23 -0
  113. package/migrations/0003_repairs.sql +23 -0
  114. package/migrations/0004_platform_surfaces.sql +51 -0
  115. package/migrations/0005_repair_dates.sql +35 -0
  116. package/migrations/0006_workspace_registry_read.sql +50 -0
  117. package/migrations/0007_history_sequence.sql +83 -0
  118. package/migrations/meta/0000_snapshot.json +40 -13
  119. package/migrations/meta/0002_snapshot.json +1054 -0
  120. package/migrations/meta/0003_snapshot.json +1070 -0
  121. package/migrations/meta/0004_snapshot.json +1130 -0
  122. package/migrations/meta/0005_snapshot.json +1135 -0
  123. package/migrations/meta/_journal.json +44 -2
  124. package/package.json +5 -4
  125. package/src/client/api-instance.ts +27 -2
  126. package/src/client/api.ts +1 -1
  127. package/src/client/bidi.test.ts +148 -0
  128. package/src/client/bidi.ts +85 -0
  129. package/src/client/components/AssetDetailPanel.svelte +614 -0
  130. package/src/client/components/AssetFormDialog.svelte +191 -59
  131. package/src/client/components/AssetPhoto.svelte +178 -0
  132. package/src/client/components/AttachmentsSection.svelte +327 -0
  133. package/src/client/components/CustodyDialog.svelte +201 -0
  134. package/src/client/components/RepairDialog.svelte +271 -0
  135. package/src/client/components/RepairsSection.svelte +318 -0
  136. package/src/client/components/Timeline.svelte +347 -0
  137. package/src/client/components/TimelineText.svelte +124 -0
  138. package/src/client/core-api.ts +71 -0
  139. package/src/client/custody.test.ts +31 -0
  140. package/src/client/custody.ts +34 -0
  141. package/src/client/errors.test.ts +365 -0
  142. package/src/client/errors.ts +201 -0
  143. package/src/client/i18n.ts +11 -166
  144. package/src/client/index.ts +8 -1
  145. package/src/client/links.test.ts +74 -0
  146. package/src/client/links.ts +44 -0
  147. package/src/client/members.test.ts +132 -0
  148. package/src/client/members.ts +116 -0
  149. package/src/client/messages.test.ts +296 -0
  150. package/src/client/messages.ts +1424 -0
  151. package/src/client/mock.test.ts +555 -0
  152. package/src/client/mock.ts +1261 -52
  153. package/src/client/module.ts +76 -2
  154. package/src/client/pages/AssetsPage.svelte +638 -145
  155. package/src/client/permissions.ts +8 -1
  156. package/src/client/price.test.ts +254 -0
  157. package/src/client/price.ts +279 -0
  158. package/src/client/query.test.ts +58 -0
  159. package/src/client/query.ts +51 -2
  160. package/src/client/repairs.test.ts +38 -0
  161. package/src/client/repairs.ts +38 -0
  162. package/src/client/settings/CategoriesSettings.svelte +421 -0
  163. package/src/client/settings/GeneralSettings.svelte +403 -0
  164. package/src/client/status.ts +29 -0
  165. package/src/client/timeline.test.ts +175 -0
  166. package/src/client/timeline.ts +206 -0
  167. package/src/client/widgets/OverviewWidget.svelte +140 -26
  168. package/src/client/widgets/RepairsWidget.svelte +124 -0
  169. package/src/contract/capabilities.ts +99 -0
  170. package/src/contract/events.ts +83 -0
  171. package/src/contract/index.ts +16 -0
  172. package/src/contract/models.ts +360 -0
  173. package/src/contract/notifications.ts +73 -0
  174. package/src/contract/permissions.ts +79 -0
  175. package/src/contract/router.ts +300 -0
  176. package/src/contract/settings.ts +50 -0
  177. package/src/module.test.ts +330 -7
  178. package/src/server/index.ts +318 -8
  179. package/src/server/inventory.int.test.ts +4374 -0
  180. package/src/server/jobs.ts +444 -0
  181. package/src/server/migrations.test.ts +251 -0
  182. package/src/server/router.ts +574 -0
  183. package/src/server/schema.ts +184 -10
  184. package/src/server/services/assets.ts +528 -0
  185. package/src/server/services/attachments.ts +215 -0
  186. package/src/server/services/audience.ts +77 -0
  187. package/src/server/services/categories.ts +136 -0
  188. package/src/server/services/cursor.ts +104 -0
  189. package/src/server/services/custody.ts +471 -0
  190. package/src/server/services/db-errors.ts +42 -0
  191. package/src/server/services/index.ts +56 -0
  192. package/src/server/services/members.ts +54 -0
  193. package/src/server/services/notify.ts +196 -0
  194. package/src/server/services/offboarding.ts +150 -0
  195. package/src/server/services/repairs.ts +567 -0
  196. package/src/server/services/search.ts +166 -0
  197. package/src/server/services/stats.ts +88 -0
  198. package/src/server/services/status.test.ts +34 -0
  199. package/src/server/services/status.ts +143 -0
  200. package/tsconfig.base.json +22 -0
  201. package/tsconfig.client.json +1 -1
  202. package/tsconfig.json +1 -1
  203. package/vitest.config.ts +18 -3
  204. package/dist/contract.d.ts +0 -387
  205. package/dist/contract.d.ts.map +0 -1
  206. package/dist/contract.js +0 -119
  207. package/dist/contract.js.map +0 -1
  208. package/dist/server/_impl.d.ts +0 -427
  209. package/dist/server/_impl.d.ts.map +0 -1
  210. package/dist/server/_impl.js +0 -204
  211. package/dist/server/_impl.js.map +0 -1
  212. package/src/contract.ts +0 -143
  213. package/src/server/_impl.ts +0 -275
@@ -0,0 +1,300 @@
1
+ import { baseContract, PageInput, page } from '@kernhq/contracts'
2
+ import { z } from 'zod'
3
+ import {
4
+ Asset,
5
+ AssetCreateInput,
6
+ AssetHistoryEntry,
7
+ AssetPatchInput,
8
+ AssetSort,
9
+ AssetStatus,
10
+ Attachment,
11
+ Category,
12
+ CategoryInput,
13
+ CustodyPeriod,
14
+ CustodyResult,
15
+ InventoryStats,
16
+ RepairInput,
17
+ RepairListItem,
18
+ RepairPatchInput,
19
+ RepairResult,
20
+ ws,
21
+ } from './models.js'
22
+
23
+ const t = ['inventory'] as const
24
+
25
+ /** A handover note is optional everywhere and shaped the same everywhere. */
26
+ const custodyNote = z.string().max(500).nullish()
27
+
28
+ export const inventoryContract = {
29
+ assets: {
30
+ list: baseContract
31
+ .route({ method: 'GET', path: '/assets', tags: t })
32
+ .input(
33
+ ws.extend({
34
+ ...PageInput.shape,
35
+ q: z.string().max(200).optional(),
36
+ categoryId: z.uuid().optional(),
37
+ status: AssetStatus.optional(),
38
+ custodianUserId: z.uuid().optional(),
39
+ /**
40
+ * Archived rows are excluded unless asked for. The page used to filter them in the
41
+ * browser, which is wrong the moment there is more than one page of them: the first
42
+ * twenty rows come back, half are dropped, and the list looks short rather than paged.
43
+ */
44
+ archived: z.boolean().default(false),
45
+ sort: AssetSort.default('recent'),
46
+ }),
47
+ )
48
+ .output(page(Asset)),
49
+ get: baseContract
50
+ .route({ method: 'GET', path: '/assets/{assetId}', tags: t })
51
+ .input(ws.extend({ assetId: z.uuid() }))
52
+ .output(Asset),
53
+ create: baseContract
54
+ .route({ method: 'POST', path: '/assets', tags: t })
55
+ .input(ws.extend(AssetCreateInput.shape))
56
+ .output(Asset),
57
+ update: baseContract
58
+ .route({ method: 'PATCH', path: '/assets/{assetId}', tags: t })
59
+ .input(ws.extend({ assetId: z.uuid(), ...AssetPatchInput.shape }))
60
+ .output(Asset),
61
+ archive: baseContract
62
+ .route({ method: 'POST', path: '/assets/{assetId}/archive', tags: t })
63
+ .input(ws.extend({ assetId: z.uuid(), archived: z.boolean().default(true) }))
64
+ .output(Asset),
65
+ /**
66
+ * The asset's own timeline, newest first.
67
+ *
68
+ * Paged with the same keyset discipline `assets.list` uses and the same cursor codec, because a
69
+ * second bookmark format is a second set of the three bugs the first one had. The bookmark is a
70
+ * row id; the ordering is by id, which for a uuidv7 is the clock and, unlike `created_at`, is
71
+ * unique — two entries written in one transaction (a create and its first custody row) share a
72
+ * timestamp and a page boundary between them would repeat or drop one.
73
+ *
74
+ * Reads on `inventory.asset.view`. See `permissions.ts` for why custody is not gated separately.
75
+ */
76
+ history: baseContract
77
+ .route({ method: 'GET', path: '/assets/{assetId}/history', tags: t })
78
+ .input(ws.extend({ assetId: z.uuid(), ...PageInput.shape }))
79
+ .output(page(AssetHistoryEntry)),
80
+ },
81
+
82
+ /**
83
+ * Who is holding what.
84
+ *
85
+ * Four verbs rather than one, because "hand it to somebody" and "hand it on to somebody else" are
86
+ * different questions with different failure modes: assigning something already out is a mistake
87
+ * worth refusing, and transferring something nobody holds is a different mistake. One procedure
88
+ * taking `userId | null` would answer both by silently doing whatever the row happened to allow.
89
+ */
90
+ custody: {
91
+ /** Hand a free item to a member. Refuses if somebody already has it — that is `transfer`. */
92
+ assign: baseContract
93
+ .route({ method: 'POST', path: '/assets/{assetId}/custody', tags: t })
94
+ .input(ws.extend({ assetId: z.uuid(), userId: z.uuid(), note: custodyNote }))
95
+ .output(CustodyResult),
96
+ /** Take it back. Closes the open period and puts the asset back in stock. */
97
+ return: baseContract
98
+ .route({ method: 'POST', path: '/assets/{assetId}/custody/return', tags: t })
99
+ .input(ws.extend({ assetId: z.uuid(), note: custodyNote }))
100
+ .output(CustodyResult),
101
+ /**
102
+ * Hand it straight on: one transaction, not a return followed by an assign.
103
+ *
104
+ * Two calls would leave the asset `in_stock` with no custodian in between — visible to anybody
105
+ * reading the list at that moment, and permanently visible in the timeline as a return that
106
+ * nobody performed.
107
+ */
108
+ transfer: baseContract
109
+ .route({ method: 'POST', path: '/assets/{assetId}/custody/transfer', tags: t })
110
+ .input(ws.extend({ assetId: z.uuid(), userId: z.uuid(), note: custodyNote }))
111
+ .output(CustodyResult),
112
+ /**
113
+ * Every period for one asset, newest first — "who had this laptop before me".
114
+ *
115
+ * An array rather than a page, like HR's `employment.history`: the rows are bounded by how many
116
+ * times one item changed hands, which is tens over its life. `limit` caps it rather than
117
+ * paging, so the answer is never unbounded and the caller never has a cursor to keep.
118
+ */
119
+ history: baseContract
120
+ .route({ method: 'GET', path: '/assets/{assetId}/custody', tags: t })
121
+ .input(ws.extend({ assetId: z.uuid(), limit: z.number().int().min(1).max(200).default(100) }))
122
+ .output(z.array(CustodyPeriod)),
123
+ /**
124
+ * What one person is holding right now.
125
+ *
126
+ * Answered from `assets.custodian_user_id` — denormalised inside the same transaction that
127
+ * writes the period, and indexed — rather than from an open-period join, so the offboarding
128
+ * question ("what does Ada still have?") is one indexed read.
129
+ */
130
+ byUser: baseContract
131
+ .route({ method: 'GET', path: '/custody/by-user/{userId}', tags: t })
132
+ .input(ws.extend({ userId: z.uuid(), ...PageInput.shape }))
133
+ .output(page(Asset)),
134
+ },
135
+
136
+ /**
137
+ * How a workspace groups what it owns.
138
+ *
139
+ * Not paged: a workspace has tens of categories, and every one of them has to be in the picker
140
+ * anyway. A cursor here would be a page boundary in a dropdown.
141
+ */
142
+ categories: {
143
+ list: baseContract
144
+ .route({ method: 'GET', path: '/categories', tags: t })
145
+ .input(ws.extend({ archived: z.boolean().default(false) }))
146
+ .output(z.array(Category)),
147
+ create: baseContract
148
+ .route({ method: 'POST', path: '/categories', tags: t })
149
+ .input(ws.extend(CategoryInput.shape))
150
+ .output(Category),
151
+ update: baseContract
152
+ .route({ method: 'PATCH', path: '/categories/{categoryId}', tags: t })
153
+ .input(ws.extend({ categoryId: z.uuid(), ...CategoryInput.partial().shape }))
154
+ .output(Category),
155
+ /**
156
+ * Archive, not delete — and the same procedure restores.
157
+ *
158
+ * `assets.category_id` carries no foreign key, so a delete would leave every asset filed under
159
+ * it pointing at nothing: a blank column, and a timeline entry that recorded the move losing
160
+ * the name it recorded. Archiving is reversible and destroys nothing.
161
+ */
162
+ archive: baseContract
163
+ .route({ method: 'POST', path: '/categories/{categoryId}/archive', tags: t })
164
+ .input(ws.extend({ categoryId: z.uuid(), archived: z.boolean().default(true) }))
165
+ .output(Category),
166
+ },
167
+
168
+ /**
169
+ * What went away to be fixed.
170
+ *
171
+ * **Behind the `repairs` capability**, which is this module's first switchable one — so every
172
+ * procedure here answers **404** rather than 403 in a workspace that has it off. 403 would say
173
+ * "this exists and you may not have it", which is false for a company that does not record
174
+ * repairs, and it would contradict a panel that has already hidden the tab.
175
+ *
176
+ * Writing takes `inventory.repair.manage`; reading rides `inventory.asset.view`, for the reason
177
+ * custody does — "where is the projector" is the question the register exists to answer.
178
+ */
179
+ repairs: {
180
+ /**
181
+ * One asset's repairs, or the whole workspace's — the same procedure, because they are the same
182
+ * query with one filter and a second one would be a second thing to keep in step.
183
+ *
184
+ * `open: true` is the "what is away right now" question a dashboard card asks, and it is a
185
+ * filter rather than a procedure of its own for the same reason.
186
+ *
187
+ * Paged, unlike `custody.history`: one asset's repairs are bounded by how often it breaks, but
188
+ * a workspace's are not. Ordered newest-logged first, by row id — a uuidv7 already carries the
189
+ * clock and is unique, where `sent_on` is a date two repairs logged the same day share.
190
+ */
191
+ list: baseContract
192
+ .route({ method: 'GET', path: '/repairs', tags: t })
193
+ .input(
194
+ ws.extend({
195
+ ...PageInput.shape,
196
+ assetId: z.uuid().optional(),
197
+ /** `true` for still away, `false` for finished, absent for both. */
198
+ open: z.boolean().optional(),
199
+ }),
200
+ )
201
+ .output(page(RepairListItem)),
202
+ /**
203
+ * Send it away. Refuses when the item is already at a repairer — one open repair per asset, and
204
+ * `inventory_repairs_one_open_uq` is what makes that true in the database rather than merely
205
+ * likely in the service.
206
+ *
207
+ * `sentOn` is optional and defaults to today **on the server**: a browser clock is not a fact
208
+ * this module is willing to record, and the same reasoning already keeps asset tags server-side.
209
+ */
210
+ create: baseContract
211
+ .route({ method: 'POST', path: '/assets/{assetId}/repairs', tags: t })
212
+ .input(ws.extend({ assetId: z.uuid(), ...RepairInput.shape }))
213
+ .output(RepairResult),
214
+ /**
215
+ * Correct what was recorded — a vendor, a cost that arrived with the invoice a week later.
216
+ *
217
+ * Deliberately cannot set `returnedOn`: that one column decides whether the asset reads as
218
+ * `under_repair`, so exactly one procedure moves it and the derived status has one door rather
219
+ * than two.
220
+ */
221
+ update: baseContract
222
+ .route({ method: 'PATCH', path: '/repairs/{repairId}', tags: t })
223
+ .input(ws.extend({ repairId: z.uuid(), ...RepairPatchInput.shape }))
224
+ .output(RepairResult),
225
+ /**
226
+ * It came back. Closes the repair and puts the asset back to `assigned` if somebody still holds
227
+ * it, or `in_stock` if nobody does — never blindly to `in_stock`, which would quietly release
228
+ * whoever is answerable for it.
229
+ *
230
+ * Takes the cost, because that is when the invoice usually arrives.
231
+ */
232
+ complete: baseContract
233
+ .route({ method: 'POST', path: '/repairs/{repairId}/complete', tags: t })
234
+ .input(
235
+ ws.extend({
236
+ repairId: z.uuid(),
237
+ returnedOn: z.iso.date().optional(),
238
+ costMinor: z.number().int().min(0).nullish(),
239
+ currency: z.string().length(3).nullish(),
240
+ }),
241
+ )
242
+ .output(RepairResult),
243
+ },
244
+
245
+ /**
246
+ * Receipts, warranties, manuals — and the asset's photo, which is an `Asset` field rather than one
247
+ * of these.
248
+ *
249
+ * **Behind the `attachments` capability**, so these answer 404 in a workspace that has it off.
250
+ *
251
+ * **A module does not upload.** The browser sends the bytes to core's file service and hands this
252
+ * module the id core gave it; `add` records that this asset has that file. Nothing here streams,
253
+ * signs or stores anything, and `remove` detaches rather than deleting core's file — the same
254
+ * bytes may be attached elsewhere, and a module has no standing to destroy another module's row.
255
+ */
256
+ attachments: {
257
+ /**
258
+ * Every file on one asset, its repairs' included, each carrying its own `repairId`.
259
+ *
260
+ * Not paged and not filtered by repair: one asset's files are bounded and entirely loaded, so
261
+ * the panel groups them in the browser. That is the one case where filtering client-side is
262
+ * right, and it is the opposite of what `assets.list` may do.
263
+ */
264
+ list: baseContract
265
+ .route({ method: 'GET', path: '/assets/{assetId}/attachments', tags: t })
266
+ .input(ws.extend({ assetId: z.uuid() }))
267
+ .output(z.array(Attachment)),
268
+ /** Attach files core already holds. `repairId` files them under one repair instead of the asset. */
269
+ add: baseContract
270
+ .route({ method: 'POST', path: '/assets/{assetId}/attachments', tags: t })
271
+ .input(
272
+ ws.extend({
273
+ assetId: z.uuid(),
274
+ fileIds: z.array(z.uuid()).min(1).max(20),
275
+ repairId: z.uuid().nullish(),
276
+ }),
277
+ )
278
+ .output(z.array(Attachment)),
279
+ /** Answers with the id it detached, so a client can drop exactly that row without re-reading. */
280
+ remove: baseContract
281
+ .route({ method: 'DELETE', path: '/attachments/{attachmentId}', tags: t })
282
+ .input(ws.extend({ attachmentId: z.uuid() }))
283
+ .output(z.object({ id: z.uuid() })),
284
+ },
285
+
286
+ /**
287
+ * The register in numbers.
288
+ *
289
+ * One procedure rather than a `total` on `assets.list`, because they answer different questions:
290
+ * a list's total describes the filter you asked for, and this describes the workspace. The assets
291
+ * page needs both — "showing 50 of 214" is two numbers from two places.
292
+ *
293
+ * Not behind a capability: it counts assets, which is `core`. `outForRepair` comes back null
294
+ * rather than 0 where `repairs` is off — see `InventoryStats`.
295
+ */
296
+ stats: {
297
+ summary: baseContract.route({ method: 'GET', path: '/stats', tags: t }).input(ws).output(InventoryStats),
298
+ },
299
+ }
300
+ export type InventoryContract = typeof inventoryContract
@@ -0,0 +1,50 @@
1
+ import { z } from 'zod'
2
+
3
+ /**
4
+ * Workspace-level settings for Inventory.
5
+ *
6
+ * Deliberately small. Nearly everything an administrator configures belongs to a category, a
7
+ * location or a field definition, because those are the things that differ between two kinds of
8
+ * item in the same company. What is left is genuinely workspace-wide.
9
+ *
10
+ * Note what is *not* here: the capability switches. Those live under a reserved `$capabilities` key
11
+ * the platform owns, so turning one off cannot collide with a settings field and cannot be dropped
12
+ * by a settings round-trip.
13
+ */
14
+ export const InventorySettings = z.object({
15
+ /**
16
+ * What an asset tag looks like. `INV-` and 4 gives `INV-0042`.
17
+ *
18
+ * People read these off a sticker and say them out loud, so a workspace that already labels its
19
+ * laptops `LT-` should not have to keep two numbering systems in its head. The counter itself is a
20
+ * row in `mod_inventory.counters`, not a setting — a number an administrator can edit is a number
21
+ * that produces a duplicate tag.
22
+ */
23
+ assetCodePrefix: z.string().max(8).default('INV-'),
24
+ assetCodePad: z.number().int().min(1).max(10).default(4),
25
+ /**
26
+ * How far ahead of a warranty running out somebody is told.
27
+ *
28
+ * This spent a release describing a sweep that did not exist — no job, no subscription, nothing
29
+ * reading the number — and was taken out for it. It is back because `warranty-sweep` reads it
30
+ * every morning: a live asset whose `warranty_until` falls inside this many days earns exactly
31
+ * one notice, marked on the row so the next morning does not send it again.
32
+ *
33
+ * A month by default, which is about how long it takes to decide whether to extend a warranty or
34
+ * budget for a replacement. Capped at a year: further out than that is not a notice, it is a
35
+ * report.
36
+ */
37
+ warrantyNoticeDays: z.number().int().min(1).max(365).default(30),
38
+ /**
39
+ * How long an item may be at a repairer before somebody is asked to chase it.
40
+ *
41
+ * Read by `repair-overdue`, which tells the person who logged the repair — and whoever is still
42
+ * holding the item — once, and then leaves them alone. Two weeks by default: long enough that an
43
+ * ordinary screen replacement never trips it, short enough that a laptop nobody chased does.
44
+ *
45
+ * A workspace with the `repairs` capability off never meets this: the sweep asks first, and a
46
+ * setting whose feature is switched off changes nothing rather than firing quietly.
47
+ */
48
+ repairOverdueDays: z.number().int().min(1).max(365).default(14),
49
+ })
50
+ export type InventorySettings = z.infer<typeof InventorySettings>