@things-factory/ai-dashboard 10.1.277 → 10.1.279
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 +22 -8
- package/client/bootstrap.ts +0 -25
- package/client/components/card-dialogs.ts +0 -445
- package/client/components/card-library.ts +0 -285
- package/client/components/dashboard-card.ts +0 -411
- package/client/components/proposal-preview.ts +0 -95
- package/client/index.ts +0 -5
- package/client/pages/my-dashboard-page.ts +0 -373
- package/client/route.ts +0 -8
- package/client/tsconfig.json +0 -13
- package/client/utils/card-api.ts +0 -99
- package/client/utils/card-words.ts +0 -91
- package/client/utils/examples.ts +0 -15
- package/dist-client/bootstrap.js.map +0 -1
- package/dist-client/components/card-dialogs.js.map +0 -1
- package/dist-client/components/card-library.js.map +0 -1
- package/dist-client/components/dashboard-card.js.map +0 -1
- package/dist-client/components/proposal-preview.js.map +0 -1
- package/dist-client/index.js.map +0 -1
- package/dist-client/pages/my-dashboard-page.js.map +0 -1
- package/dist-client/route.js.map +0 -1
- package/dist-client/tsconfig.tsbuildinfo +0 -1
- package/dist-client/utils/card-api.js.map +0 -1
- package/dist-client/utils/card-words.js.map +0 -1
- package/dist-client/utils/examples.js.map +0 -1
- package/dist-server/index.js.map +0 -1
- package/dist-server/role-tasks.js.map +0 -1
- package/dist-server/service/card/card-access.js.map +0 -1
- package/dist-server/service/card/card-definition.js.map +0 -1
- package/dist-server/service/card/card-names.js.map +0 -1
- package/dist-server/service/card/card-proposal.js.map +0 -1
- package/dist-server/service/card/card-resolver.js.map +0 -1
- package/dist-server/service/card/card-run.js.map +0 -1
- package/dist-server/service/card/card-tool.js.map +0 -1
- package/dist-server/service/card/card-types.js.map +0 -1
- package/dist-server/service/card/dashboard-card.js.map +0 -1
- package/dist-server/service/card/long-text.js.map +0 -1
- package/dist-server/service/index.js.map +0 -1
- package/dist-server/tsconfig.tsbuildinfo +0 -1
- package/server/index.ts +0 -16
- package/server/role-tasks.ts +0 -12
- package/server/service/card/card-access.test.ts +0 -173
- package/server/service/card/card-access.ts +0 -24
- package/server/service/card/card-definition.ts +0 -468
- package/server/service/card/card-intent.test.ts +0 -285
- package/server/service/card/card-names.ts +0 -12
- package/server/service/card/card-proposal.ts +0 -46
- package/server/service/card/card-resolver.ts +0 -440
- package/server/service/card/card-run.test.ts +0 -237
- package/server/service/card/card-run.ts +0 -302
- package/server/service/card/card-tool.ts +0 -368
- package/server/service/card/card-types.ts +0 -145
- package/server/service/card/card-words.test.ts +0 -72
- package/server/service/card/dashboard-card.ts +0 -303
- package/server/service/card/long-text.ts +0 -25
- package/server/service/index.ts +0 -16
- package/test/card-menu.test.ts +0 -50
- package/test/translations.test.ts +0 -42
- package/test/words-not-codes.test.ts +0 -24
- package/tsconfig.json +0 -9
|
@@ -1,440 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The doors of the dashboard cards (ADR-0160 decisions 6-10) - one's own cards and those shared with one, one's own
|
|
3
|
-
* dashboard, a card's versions and shares (its owner's), running a card, and saving a proposal a conversation made.
|
|
4
|
-
*
|
|
5
|
-
* Every door asks who may use the card again (card-access.ts) - a share withdrawn closes at once. Running a card asks
|
|
6
|
-
* the data's own door as the viewer and reads with the viewer's request (card-run.ts): the card gives the definition,
|
|
7
|
-
* never the data.
|
|
8
|
-
*/
|
|
9
|
-
import { Arg, Ctx, Directive, Int, Mutation, Query, Resolver } from 'type-graphql'
|
|
10
|
-
import { GraphQLJSON } from 'graphql-scalars'
|
|
11
|
-
import { In, IsNull, Not } from 'typeorm'
|
|
12
|
-
|
|
13
|
-
import { getRepository } from '@things-factory/shell'
|
|
14
|
-
import { User } from '@things-factory/auth-base'
|
|
15
|
-
import { audienceMemberOf, audienceTargetRefusal, departmentTreeOf, domainRolesOf } from '@things-factory/organization'
|
|
16
|
-
import {
|
|
17
|
-
assertMayWriteSession,
|
|
18
|
-
ChatMessage,
|
|
19
|
-
ChatSession,
|
|
20
|
-
detailReadOf,
|
|
21
|
-
isSessionParticipant,
|
|
22
|
-
mayReadSession,
|
|
23
|
-
readTargetOf,
|
|
24
|
-
rootFieldOf,
|
|
25
|
-
runAsCaller,
|
|
26
|
-
toolAccessFor
|
|
27
|
-
} from '@things-factory/ai-assistant'
|
|
28
|
-
|
|
29
|
-
import { cardAccessOf, type CardAccess, type CardViewer } from './card-access.js'
|
|
30
|
-
import { CARD_SCHEMA_VERSION, checkDefinition, sourceOf, todayIn, type CardDefinition } from './card-definition.js'
|
|
31
|
-
import { CARD_NAME_MAX } from './card-names.js'
|
|
32
|
-
import { cardIdOf, findCardProposal } from './card-proposal.js'
|
|
33
|
-
import { runCard, type CardRunResult } from './card-run.js'
|
|
34
|
-
import { DashboardCard, DashboardCardPlace, DashboardCardShare, DashboardCardShareScope, DashboardCardVersion } from './dashboard-card.js'
|
|
35
|
-
import { DashboardCardItem, DashboardCardShareInput, DashboardCardVersionItem, DashboardPlaceInput, DashboardPlaceItem, DashboardShareChoice, DashboardShareOptions } from './card-types.js'
|
|
36
|
-
|
|
37
|
-
const QUERY = '@privilege(category: "ai-dashboard", privilege: "query", domainOwnerGranted: true)'
|
|
38
|
-
const MUTATION = '@privilege(category: "ai-dashboard", privilege: "mutation", domainOwnerGranted: true)'
|
|
39
|
-
|
|
40
|
-
const MAX_WIDTH = 4
|
|
41
|
-
const MAX_HEIGHT = 2
|
|
42
|
-
|
|
43
|
-
/** The name as typed, or refused with the bound - a longer name was cut without a word (inline help audit 2026-10-30). */
|
|
44
|
-
function cardNameOf(name: unknown, context: ResolverContext): string {
|
|
45
|
-
const given = String(name ?? '').trim()
|
|
46
|
-
if (given.length > CARD_NAME_MAX) refuse(context, 'name-too-long', 'a card name takes up to {max} characters; this one has {length}.', { max: CARD_NAME_MAX, length: given.length })
|
|
47
|
-
return given
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** A refusal said in the reader's words, with a code the screen may read. */
|
|
51
|
-
function refuse(context: ResolverContext, code: string, fallback: string, options: Record<string, unknown> = {}): never {
|
|
52
|
-
const message = context.t ? String(context.t(`ai-dashboard.refusal.${code}`, { defaultValue: fallback, ...options })) : fallback
|
|
53
|
-
const error: any = new Error(message)
|
|
54
|
-
/* The issue code is for the people who keep the application - the sentence a person reads does not carry it. */
|
|
55
|
-
error.extensions = { code: `ai-dashboard.${code}`, ...(options.issue ? { issue: options.issue } : {}) }
|
|
56
|
-
throw error
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* The viewer of a request as an audience sees them (organization, ADR-0163 decision 2), and the departments of their own
|
|
61
|
-
* active records - the ones they may share with. Asked once per request.
|
|
62
|
-
*/
|
|
63
|
-
async function viewerOf(context: ResolverContext): Promise<{ viewer: CardViewer; departments: { id: string; name: string }[] }> {
|
|
64
|
-
const state: any = context.state
|
|
65
|
-
if (state.__aiDashboardViewer) return state.__aiDashboardViewer
|
|
66
|
-
const { ownDepartments, ...viewer } = await audienceMemberOf(state.domain, state.user ?? {})
|
|
67
|
-
const answer = { viewer, departments: ownDepartments }
|
|
68
|
-
state.__aiDashboardViewer = answer
|
|
69
|
-
return answer
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const sharesOf = async (cardIds: string[], tx?: any) =>
|
|
73
|
-
cardIds.length ? await getRepository(DashboardCardShare, tx).find({ where: { card: { id: In(cardIds) } } as any }) : []
|
|
74
|
-
|
|
75
|
-
/** A card the viewer may use, and how - refused otherwise (decision 7). */
|
|
76
|
-
async function usableCard(given: string, context: ResolverContext, tx?: any): Promise<{ card: DashboardCard; access: CardAccess }> {
|
|
77
|
-
/* A card's uuid or its key - anything else is no card, and is not handed to a database that refuses it as no uuid. */
|
|
78
|
-
const id = cardIdOf(given)
|
|
79
|
-
if (!id) refuse(context, 'unknown-card', 'there is no such card.')
|
|
80
|
-
const card = await getRepository(DashboardCard, tx).findOne({ where: { id, domain: { id: context.state.domain.id } } as any })
|
|
81
|
-
if (!card) refuse(context, 'unknown-card', 'there is no such card.')
|
|
82
|
-
const { viewer } = await viewerOf(context)
|
|
83
|
-
const access = cardAccessOf(card, viewer, await sharesOf([card.id], tx))
|
|
84
|
-
if (!access) refuse(context, 'card-not-shared', 'this card is not shared with you.')
|
|
85
|
-
return { card, access }
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async function ownCard(id: string, context: ResolverContext, tx?: any): Promise<DashboardCard> {
|
|
89
|
-
const { card, access } = await usableCard(id, context, tx)
|
|
90
|
-
if (access !== 'owner') refuse(context, 'not-your-card', 'only the one who made this card changes it - copy it to make your own.')
|
|
91
|
-
return card
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function namesOf(userIds: string[]): Promise<Map<string, string>> {
|
|
95
|
-
const ids = [...new Set(userIds.filter(Boolean))]
|
|
96
|
-
if (!ids.length) return new Map()
|
|
97
|
-
const users = await getRepository(User).find({ where: { id: In(ids) } as any })
|
|
98
|
-
return new Map(users.map(one => [one.id, one.name || one.email || one.id]))
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** The cards as the viewer sees them - each with its access, whether placed, and its shares for its owner. */
|
|
102
|
-
async function itemsOf(cards: DashboardCard[], context: ResolverContext): Promise<DashboardCardItem[]> {
|
|
103
|
-
const { viewer } = await viewerOf(context)
|
|
104
|
-
const shares = await sharesOf(cards.map(one => one.id))
|
|
105
|
-
const placed = cards.length
|
|
106
|
-
? new Set((await getRepository(DashboardCardPlace).find({ where: { domain: { id: viewer.domainId }, user: { id: viewer.userId }, card: { id: In(cards.map(one => one.id)) } } as any })).map(one => one.cardId))
|
|
107
|
-
: new Set<string>()
|
|
108
|
-
const names = await namesOf(cards.map(one => one.ownerId))
|
|
109
|
-
return cards.flatMap(card => {
|
|
110
|
-
const mine = shares.filter(one => one.cardId === card.id)
|
|
111
|
-
const access = cardAccessOf(card, viewer, mine)
|
|
112
|
-
if (!access) return []
|
|
113
|
-
return [
|
|
114
|
-
{
|
|
115
|
-
id: card.id,
|
|
116
|
-
name: card.name,
|
|
117
|
-
description: card.description,
|
|
118
|
-
version: card.version,
|
|
119
|
-
definition: card.definition,
|
|
120
|
-
access,
|
|
121
|
-
ownerName: names.get(card.ownerId),
|
|
122
|
-
copiedFromId: card.copiedFromId,
|
|
123
|
-
placed: placed.has(card.id),
|
|
124
|
-
...(access === 'owner' ? { shares: mine.map(one => ({ scope: one.scope, targetId: one.targetId, targetName: one.targetName })) } : {}),
|
|
125
|
-
updatedAt: card.updatedAt
|
|
126
|
-
}
|
|
127
|
-
]
|
|
128
|
-
})
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/** Runs a definition as the viewer - the data's door and the data's API, the viewer's (decision 6). */
|
|
132
|
-
export async function runAsViewer(definition: CardDefinition, context: ResolverContext): Promise<CardRunResult> {
|
|
133
|
-
return await runCard(definition, {
|
|
134
|
-
readTargetOf,
|
|
135
|
-
detailReadOf,
|
|
136
|
-
passesDoor: door => toolAccessFor(context)(door),
|
|
137
|
-
run: runAsCaller(context),
|
|
138
|
-
today: todayIn(context.state.domain?.timezone),
|
|
139
|
-
timeZone: context.state.domain?.timezone,
|
|
140
|
-
words: (key, fallback) => {
|
|
141
|
-
if (!key || typeof context.t !== 'function') return fallback
|
|
142
|
-
const said = context.t(key)
|
|
143
|
-
return said && said !== key ? String(said) : fallback
|
|
144
|
-
}
|
|
145
|
-
})
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** A definition fit to keep - its source known and every name its own. */
|
|
149
|
-
function keepable(definition: any, context: ResolverContext): CardDefinition {
|
|
150
|
-
const { source, issue } = sourceOf(definition?.source, readTargetOf, detailReadOf)
|
|
151
|
-
if (!source) refuse(context, 'definition-unfit', 'the card reads nothing this application declares.', { issue: issue?.code })
|
|
152
|
-
const issues = checkDefinition(definition, source)
|
|
153
|
-
if (issues.length) refuse(context, 'definition-unfit', 'the card definition does not fit its source.', { issue: issues[0].code })
|
|
154
|
-
return definition
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async function placeAtEnd(cardId: string, context: ResolverContext, tx: any): Promise<DashboardCardPlace> {
|
|
158
|
-
const { domain, user } = context.state
|
|
159
|
-
const repository = getRepository(DashboardCardPlace, tx)
|
|
160
|
-
const existing = await repository.findOne({ where: { domain: { id: domain.id }, user: { id: user.id }, card: { id: cardId } } as any })
|
|
161
|
-
if (existing) return existing
|
|
162
|
-
const last = await repository.findOne({ where: { domain: { id: domain.id }, user: { id: user.id } } as any, order: { order: 'DESC' } as any })
|
|
163
|
-
return await repository.save({ domain, user, card: { id: cardId }, order: (last?.order ?? -1) + 1, width: 2, height: 1, creator: user, updater: user } as any)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
@Resolver()
|
|
167
|
-
export class DashboardCardResolver {
|
|
168
|
-
@Directive(QUERY)
|
|
169
|
-
@Query(returns => [DashboardPlaceItem], { description: "The cards on the viewer's dashboard, in order - only those the viewer may still use." })
|
|
170
|
-
async myDashboard(@Ctx() context: ResolverContext): Promise<DashboardPlaceItem[]> {
|
|
171
|
-
const { domain, user } = context.state
|
|
172
|
-
const places = await getRepository(DashboardCardPlace).find({ where: { domain: { id: domain.id }, user: { id: user.id } } as any, order: { order: 'ASC' } as any })
|
|
173
|
-
const cards = places.length ? await getRepository(DashboardCard).find({ where: { id: In(places.map(one => one.cardId)), domain: { id: domain.id } } as any }) : []
|
|
174
|
-
const items = new Map((await itemsOf(cards, context)).map(one => [one.id, one]))
|
|
175
|
-
return places.flatMap(place => (items.has(place.cardId) ? [{ id: place.id, order: place.order, width: place.width, height: place.height, card: items.get(place.cardId)! }] : []))
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
@Directive(QUERY)
|
|
179
|
-
@Query(returns => [DashboardCardItem], { description: "The viewer's card library - 'mine', the cards they made, or 'shared', those shared with them." })
|
|
180
|
-
async dashboardCards(@Arg('which', { description: "'mine' or 'shared'." }) which: string, @Ctx() context: ResolverContext): Promise<DashboardCardItem[]> {
|
|
181
|
-
const { viewer } = await viewerOf(context)
|
|
182
|
-
if (which === 'mine') {
|
|
183
|
-
const cards = await getRepository(DashboardCard).find({ where: { domain: { id: viewer.domainId }, owner: { id: viewer.userId } } as any, order: { updatedAt: 'DESC' } as any })
|
|
184
|
-
return await itemsOf(cards, context)
|
|
185
|
-
}
|
|
186
|
-
const shares = await getRepository(DashboardCardShare).find({
|
|
187
|
-
where: [
|
|
188
|
-
{ domain: { id: viewer.domainId }, scope: DashboardCardShareScope.USER, targetId: viewer.userId },
|
|
189
|
-
{ domain: { id: viewer.domainId }, scope: DashboardCardShareScope.DOMAIN, targetId: viewer.domainId },
|
|
190
|
-
...(viewer.departmentIds.length ? [{ domain: { id: viewer.domainId }, scope: DashboardCardShareScope.DEPARTMENT, targetId: In([...viewer.departmentIds]) }] : []),
|
|
191
|
-
...(viewer.roleIds?.length ? [{ domain: { id: viewer.domainId }, scope: DashboardCardShareScope.ROLE, targetId: In([...viewer.roleIds]) }] : [])
|
|
192
|
-
] as any
|
|
193
|
-
})
|
|
194
|
-
const ids = [...new Set(shares.map(one => one.cardId))]
|
|
195
|
-
const cards = ids.length ? await getRepository(DashboardCard).find({ where: { id: In(ids), domain: { id: viewer.domainId }, owner: { id: Not(viewer.userId) } } as any, order: { updatedAt: 'DESC' } as any }) : []
|
|
196
|
-
return await itemsOf(cards, context)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
@Directive(QUERY)
|
|
200
|
-
@Query(returns => GraphQLJSON, { description: 'Draws one card as the viewer - its block, how much of the source it was drawn from, or why it cannot be drawn. No model is asked.' })
|
|
201
|
-
async dashboardCardRun(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<CardRunResult> {
|
|
202
|
-
const { card } = await usableCard(id, context)
|
|
203
|
-
return await runAsViewer(card.definition as CardDefinition, context)
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
@Directive(QUERY)
|
|
207
|
-
@Query(returns => [DashboardCardVersionItem], { description: "A card's versions, newest first - its owner only." })
|
|
208
|
-
async dashboardCardVersions(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<DashboardCardVersionItem[]> {
|
|
209
|
-
const card = await ownCard(id, context)
|
|
210
|
-
const versions = await getRepository(DashboardCardVersion).find({ where: { card: { id: card.id } } as any, order: { version: 'DESC' } as any })
|
|
211
|
-
const names = await namesOf(versions.map(one => one.creatorId))
|
|
212
|
-
return versions.map(one => ({ version: one.version, name: one.name, definition: one.definition, creatorName: names.get(one.creatorId), createdAt: one.createdAt }))
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
@Directive(QUERY)
|
|
216
|
-
@Query(returns => DashboardShareOptions, { description: 'What the viewer may share a card with - the domain, their own departments, and whether people can be looked up.' })
|
|
217
|
-
async dashboardShareOptions(@Ctx() context: ResolverContext): Promise<DashboardShareOptions> {
|
|
218
|
-
const { departments } = await viewerOf(context)
|
|
219
|
-
const domain: any = context.state.domain
|
|
220
|
-
return {
|
|
221
|
-
domainId: domain.id,
|
|
222
|
-
domainName: domain.name ?? domain.subdomain ?? domain.id,
|
|
223
|
-
departments,
|
|
224
|
-
peopleAvailable: await toolAccessFor(context)('Query.employees'),
|
|
225
|
-
roles: (await domainRolesOf(context.state.domain)).map(one => ({ id: one.id, name: one.name, ...(one.description ? { note: one.description } : {}) }))
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
@Directive(QUERY)
|
|
230
|
-
@Query(returns => [DashboardShareChoice], { description: "People to share a card with, found by name through the organization's own list as the viewer." })
|
|
231
|
-
async dashboardSharePeople(@Arg('text') text: string, @Ctx() context: ResolverContext): Promise<DashboardShareChoice[]> {
|
|
232
|
-
const words = (text ?? '').trim()
|
|
233
|
-
if (!words) return []
|
|
234
|
-
const answer = await runAsCaller(context)(
|
|
235
|
-
'query ($filters: [Filter!], $pagination: Pagination) { employees(filters: $filters, pagination: $pagination) { items { name department { name } user { id } } } }',
|
|
236
|
-
{ filters: [{ name: 'active', operator: 'eq', value: true }, { name: 'name', operator: 'i_like', value: words }], pagination: { page: 1, limit: 20 } }
|
|
237
|
-
)
|
|
238
|
-
if (answer.errors?.length) refuse(context, 'people-unavailable', 'people cannot be looked up here.')
|
|
239
|
-
return (answer.data?.employees?.items ?? [])
|
|
240
|
-
.filter((one: any) => one?.user?.id && one.user.id !== context.state.user.id)
|
|
241
|
-
.map((one: any) => ({ id: one.user.id, name: one.name, note: one.department?.name }))
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
@Directive(QUERY)
|
|
245
|
-
@Query(returns => GraphQLJSON, {
|
|
246
|
-
description: "Draws a card a conversation proposed, as the one looking at the conversation - the proposal keeps the definition, never its values."
|
|
247
|
-
})
|
|
248
|
-
async dashboardCardProposalPreview(@Arg('sessionId') sessionId: string, @Arg('proposalKey') proposalKey: string, @Ctx() context: ResolverContext): Promise<CardRunResult> {
|
|
249
|
-
const { domain, user } = context.state
|
|
250
|
-
const session = await getRepository(ChatSession).findOneBy({ id: sessionId, domain: { id: domain.id } } as any)
|
|
251
|
-
if (!session) refuse(context, 'unknown-conversation', 'there is no such conversation.')
|
|
252
|
-
const participant = await isSessionParticipant(domain.id, session.id, user?.id)
|
|
253
|
-
if (!mayReadSession(session.visibility, participant)) refuse(context, 'conversation-closed', 'this conversation is not open to you.')
|
|
254
|
-
const { proposal } = findCardProposal(await proposalMessages(session.id), proposalKey)
|
|
255
|
-
if (!proposal) refuse(context, 'unknown-proposal', 'there is no such card in this conversation.')
|
|
256
|
-
return await runAsViewer(proposal.card.definition, context)
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
@Directive(MUTATION)
|
|
260
|
-
@Directive('@transaction')
|
|
261
|
-
@Mutation(returns => GraphQLJSON, {
|
|
262
|
-
description:
|
|
263
|
-
"Saves a card a conversation proposed and places it on the viewer's dashboard - the stored proposal's definition, never one the screen sends. A change of a saved card is its owner's, from the version it was read at. Pressing it again answers the same card."
|
|
264
|
-
})
|
|
265
|
-
async saveDashboardCardProposal(@Arg('sessionId') sessionId: string, @Arg('proposalKey') proposalKey: string, @Ctx() context: ResolverContext): Promise<{ cardId: string; version: number; placeId: string; again?: boolean }> {
|
|
266
|
-
const { domain, user, tx } = context.state as any
|
|
267
|
-
const session = await getRepository(ChatSession, tx).findOneBy({ id: sessionId, domain: { id: domain.id } } as any)
|
|
268
|
-
if (!session) refuse(context, 'unknown-conversation', 'there is no such conversation.')
|
|
269
|
-
await assertMayWriteSession({
|
|
270
|
-
session,
|
|
271
|
-
userId: user?.id,
|
|
272
|
-
isParticipant: () => isSessionParticipant(domain.id, session.id, user?.id),
|
|
273
|
-
t: (key, options) => (context.t ? context.t(key, options) : String(options?.defaultValue ?? key))
|
|
274
|
-
})
|
|
275
|
-
|
|
276
|
-
/* Saved already - the same card, whoever's request it is now asked by. */
|
|
277
|
-
const saved = await getRepository(DashboardCardVersion, tx).findOne({ where: { domain: { id: domain.id }, proposalSessionId: session.id, proposalKey } as any })
|
|
278
|
-
if (saved) {
|
|
279
|
-
const card = await ownCard(saved.cardId, context, tx)
|
|
280
|
-
const place = await placeAtEnd(card.id, context, tx)
|
|
281
|
-
return { cardId: card.id, version: saved.version, placeId: place.id, again: true }
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const { proposal, superseded, notACard } = findCardProposal(await proposalMessages(session.id, tx), proposalKey)
|
|
285
|
-
if (notACard) refuse(context, 'not-a-card', 'this proposal is not a dashboard card.')
|
|
286
|
-
if (!proposal) refuse(context, 'unknown-proposal', 'there is no such card in this conversation.')
|
|
287
|
-
if (superseded) refuse(context, 'superseded', 'a newer card replaced this one in the conversation - save that one.')
|
|
288
|
-
const definition = keepable(proposal.card.definition, context)
|
|
289
|
-
/* Keeping a card one cannot read draws nothing - the data's door is the saver's too. */
|
|
290
|
-
const { source } = sourceOf(definition.source, readTargetOf, detailReadOf)
|
|
291
|
-
if (!(await toolAccessFor(context)(rootFieldOf(source!.query, 'query')))) refuse(context, 'data-closed', 'you may not read the data this card draws.')
|
|
292
|
-
const name = cardNameOf(proposal.card.name, context) || String(context.t ? context.t('ai-dashboard.text.untitled-card') : 'Untitled card')
|
|
293
|
-
const description = proposal.card.description ? String(proposal.card.description).slice(0, 300) : null
|
|
294
|
-
|
|
295
|
-
let card: DashboardCard
|
|
296
|
-
let version: number
|
|
297
|
-
if (proposal.card.cardId) {
|
|
298
|
-
card = await ownCard(proposal.card.cardId, context, tx)
|
|
299
|
-
if (proposal.card.baseVersion !== card.version) refuse(context, 'stale-version', 'the card changed since this proposal (now version {version}) - ask for the change again.', { version: card.version })
|
|
300
|
-
version = card.version + 1
|
|
301
|
-
await getRepository(DashboardCard, tx).save({ ...card, name, description, version, definitionJson: JSON.stringify(definition), updater: user } as any)
|
|
302
|
-
} else {
|
|
303
|
-
version = 1
|
|
304
|
-
card = await getRepository(DashboardCard, tx).save({ domain, name, description, owner: user, version, definitionJson: JSON.stringify(definition), creator: user, updater: user } as any)
|
|
305
|
-
}
|
|
306
|
-
await getRepository(DashboardCardVersion, tx).save({ domain, card: { id: card.id }, version, name, definitionJson: JSON.stringify(definition), proposalSessionId: session.id, proposalKey, creator: user, updater: user } as any)
|
|
307
|
-
const place = await placeAtEnd(card.id, context, tx)
|
|
308
|
-
return { cardId: card.id, version, placeId: place.id }
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
@Directive(MUTATION)
|
|
312
|
-
@Directive('@transaction')
|
|
313
|
-
@Mutation(returns => DashboardCardItem, { description: "Renames one's own card - a new version." })
|
|
314
|
-
async renameDashboardCard(@Arg('id') id: string, @Arg('name') name: string, @Ctx() context: ResolverContext): Promise<DashboardCardItem> {
|
|
315
|
-
const { domain, user, tx } = context.state as any
|
|
316
|
-
const card = await ownCard(id, context, tx)
|
|
317
|
-
const given = cardNameOf(name, context)
|
|
318
|
-
if (!given) refuse(context, 'no-name', 'a card needs a name.')
|
|
319
|
-
const version = card.version + 1
|
|
320
|
-
await getRepository(DashboardCard, tx).save({ ...card, name: given, version, updater: user } as any)
|
|
321
|
-
await getRepository(DashboardCardVersion, tx).save({ domain, card: { id: card.id }, version, name: given, definitionJson: card.definitionJson, creator: user, updater: user } as any)
|
|
322
|
-
return (await itemsOf([await getRepository(DashboardCard, tx).findOneBy({ id: card.id })], context))[0]
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
@Directive(MUTATION)
|
|
326
|
-
@Directive('@transaction')
|
|
327
|
-
@Mutation(returns => DashboardCardItem, { description: "Brings back an earlier version of one's own card - its definition and name become a new version." })
|
|
328
|
-
async restoreDashboardCardVersion(@Arg('id') id: string, @Arg('version', type => Int) version: number, @Ctx() context: ResolverContext): Promise<DashboardCardItem> {
|
|
329
|
-
const { domain, user, tx } = context.state as any
|
|
330
|
-
const card = await ownCard(id, context, tx)
|
|
331
|
-
const earlier = await getRepository(DashboardCardVersion, tx).findOne({ where: { card: { id: card.id }, version } as any })
|
|
332
|
-
if (!earlier) refuse(context, 'unknown-version', 'there is no such version.')
|
|
333
|
-
keepable(earlier.definition, context)
|
|
334
|
-
const next = card.version + 1
|
|
335
|
-
await getRepository(DashboardCard, tx).save({ ...card, name: earlier.name, definitionJson: earlier.definitionJson, version: next, updater: user } as any)
|
|
336
|
-
await getRepository(DashboardCardVersion, tx).save({ domain, card: { id: card.id }, version: next, name: earlier.name, definitionJson: earlier.definitionJson, creator: user, updater: user } as any)
|
|
337
|
-
return (await itemsOf([await getRepository(DashboardCard, tx).findOneBy({ id: card.id })], context))[0]
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
@Directive(MUTATION)
|
|
341
|
-
@Directive('@transaction')
|
|
342
|
-
@Mutation(returns => DashboardCardItem, { description: 'Copies a card one may use into a card of one\'s own, placed on one\'s dashboard.' })
|
|
343
|
-
async copyDashboardCard(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<DashboardCardItem> {
|
|
344
|
-
const { domain, user, tx } = context.state as any
|
|
345
|
-
const { card } = await usableCard(id, context, tx)
|
|
346
|
-
const copy = await getRepository(DashboardCard, tx).save({ domain, name: card.name, description: card.description, owner: user, version: 1, definitionJson: card.definitionJson, copiedFromId: card.id, creator: user, updater: user } as any)
|
|
347
|
-
await getRepository(DashboardCardVersion, tx).save({ domain, card: { id: copy.id }, version: 1, name: copy.name, definitionJson: copy.definitionJson, creator: user, updater: user } as any)
|
|
348
|
-
await placeAtEnd(copy.id, context, tx)
|
|
349
|
-
return (await itemsOf([copy], context))[0]
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
@Directive(MUTATION)
|
|
353
|
-
@Directive('@transaction')
|
|
354
|
-
@Mutation(returns => Boolean, { description: "Deletes one's own card - its versions, shares and every place it was put." })
|
|
355
|
-
async deleteDashboardCard(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<boolean> {
|
|
356
|
-
const { tx } = context.state as any
|
|
357
|
-
const card = await ownCard(id, context, tx)
|
|
358
|
-
for (const entity of [DashboardCardPlace, DashboardCardShare, DashboardCardVersion] as any[]) {
|
|
359
|
-
const rows = await getRepository(entity, tx).find({ where: { card: { id: card.id } } as any })
|
|
360
|
-
if (rows.length) await getRepository(entity, tx).softRemove(rows)
|
|
361
|
-
}
|
|
362
|
-
await getRepository(DashboardCard, tx).softRemove(card)
|
|
363
|
-
return true
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
@Directive(MUTATION)
|
|
367
|
-
@Directive('@transaction')
|
|
368
|
-
@Mutation(returns => DashboardCardItem, { description: "Sets who one's own card is shared with - the whole list; none keeps it to oneself. A share gives the definition, never the data." })
|
|
369
|
-
async shareDashboardCard(@Arg('id') id: string, @Arg('shares', type => [DashboardCardShareInput]) shares: DashboardCardShareInput[], @Ctx() context: ResolverContext): Promise<DashboardCardItem> {
|
|
370
|
-
const { domain, user, tx } = context.state as any
|
|
371
|
-
const card = await ownCard(id, context, tx)
|
|
372
|
-
const roleIds = (shares ?? []).some(one => one.scope === DashboardCardShareScope.ROLE) ? (await domainRolesOf(domain, undefined, tx)).map(one => one.id) : []
|
|
373
|
-
const departmentIds = (shares ?? []).some(one => one.scope === DashboardCardShareScope.DEPARTMENT) ? [...(await departmentTreeOf(domain, tx)).keys()] : []
|
|
374
|
-
const wanted = new Map<string, DashboardCardShareInput>()
|
|
375
|
-
for (const share of shares ?? []) {
|
|
376
|
-
const refusal = audienceTargetRefusal(share, { domainId: domain.id, departmentIds, roleIds })
|
|
377
|
-
if (refusal) refuse(context, `share-${refusal}`, 'this share cannot be given ({code}).', { code: refusal })
|
|
378
|
-
if (share.scope === DashboardCardShareScope.USER && share.targetId === user.id) continue
|
|
379
|
-
wanted.set(`${share.scope}:${share.targetId}`, share)
|
|
380
|
-
}
|
|
381
|
-
const repository = getRepository(DashboardCardShare, tx)
|
|
382
|
-
const current = await repository.find({ where: { card: { id: card.id } } as any })
|
|
383
|
-
const gone = current.filter(one => !wanted.has(`${one.scope}:${one.targetId}`))
|
|
384
|
-
if (gone.length) await repository.softRemove(gone)
|
|
385
|
-
for (const [key, share] of wanted) {
|
|
386
|
-
if (current.some(one => `${one.scope}:${one.targetId}` === key)) continue
|
|
387
|
-
await repository.save({ domain, card: { id: card.id }, scope: share.scope, targetId: share.targetId, targetName: share.targetName?.slice(0, 100), creator: user, updater: user } as any)
|
|
388
|
-
}
|
|
389
|
-
return (await itemsOf([card], context))[0]
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
@Directive(MUTATION)
|
|
393
|
-
@Directive('@transaction')
|
|
394
|
-
@Mutation(returns => DashboardPlaceItem, { description: "Places a card one may use at the end of one's dashboard." })
|
|
395
|
-
async placeDashboardCard(@Arg('cardId') cardId: string, @Ctx() context: ResolverContext): Promise<DashboardPlaceItem> {
|
|
396
|
-
const { tx } = context.state as any
|
|
397
|
-
const { card } = await usableCard(cardId, context, tx)
|
|
398
|
-
const place = await placeAtEnd(card.id, context, tx)
|
|
399
|
-
return { id: place.id, order: place.order, width: place.width, height: place.height, card: (await itemsOf([card], context))[0] }
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
@Directive(MUTATION)
|
|
403
|
-
@Directive('@transaction')
|
|
404
|
-
@Mutation(returns => Boolean, { description: "Takes a card off one's dashboard - the card stays in the library." })
|
|
405
|
-
async removeDashboardPlace(@Arg('id') id: string, @Ctx() context: ResolverContext): Promise<boolean> {
|
|
406
|
-
const { domain, user, tx } = context.state as any
|
|
407
|
-
const place = await getRepository(DashboardCardPlace, tx).findOne({ where: { id, domain: { id: domain.id }, user: { id: user.id } } as any })
|
|
408
|
-
if (!place) refuse(context, 'unknown-place', 'that card is not on your dashboard.')
|
|
409
|
-
await getRepository(DashboardCardPlace, tx).softRemove(place)
|
|
410
|
-
return true
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
@Directive(MUTATION)
|
|
414
|
-
@Directive('@transaction')
|
|
415
|
-
@Mutation(returns => Boolean, { description: "Sets the order and size of the cards on one's dashboard." })
|
|
416
|
-
async arrangeDashboard(@Arg('places', type => [DashboardPlaceInput]) places: DashboardPlaceInput[], @Ctx() context: ResolverContext): Promise<boolean> {
|
|
417
|
-
const { domain, user, tx } = context.state as any
|
|
418
|
-
const repository = getRepository(DashboardCardPlace, tx)
|
|
419
|
-
const mine = await repository.find({ where: { domain: { id: domain.id }, user: { id: user.id } } as any })
|
|
420
|
-
for (const given of places ?? []) {
|
|
421
|
-
const place = mine.find(one => one.id === given.id)
|
|
422
|
-
if (!place) refuse(context, 'unknown-place', 'that card is not on your dashboard.')
|
|
423
|
-
if (!(given.width >= 1 && given.width <= MAX_WIDTH && given.height >= 1 && given.height <= MAX_HEIGHT)) refuse(context, 'size-out-of-range', 'a card spans 1 to 4 columns and 1 to 2 rows.')
|
|
424
|
-
await repository.save({ ...place, order: given.order, width: given.width, height: given.height, updater: user } as any)
|
|
425
|
-
}
|
|
426
|
-
return true
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
/** The conversation's messages that carry proposals, oldest first. */
|
|
431
|
-
async function proposalMessages(sessionId: string, tx?: any): Promise<{ proposals: any[] }[]> {
|
|
432
|
-
const carrying = await getRepository(ChatMessage, tx).find({
|
|
433
|
-
select: ['id', 'proposals', 'createdAt'] as any,
|
|
434
|
-
where: { session: { id: sessionId } as any, proposals: Not(IsNull()) } as any,
|
|
435
|
-
order: { createdAt: 'ASC' } as any
|
|
436
|
-
})
|
|
437
|
-
return carrying.map((message: any) => ({ proposals: message.proposalsJson ?? [] }))
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
export { CARD_SCHEMA_VERSION }
|