|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import {randomUUID} from 'node:crypto'; |
| 6 | +import * as vscode from 'vscode'; |
| 7 | + |
| 8 | +/** A single query the user has saved for reuse from the Query Builder. */ |
| 9 | +export interface CipSavedQuery { |
| 10 | + id: string; |
| 11 | + name: string; |
| 12 | + sql: string; |
| 13 | + description?: string; |
| 14 | + /** Tenant the query was authored against. Used to scope visibility per realm. */ |
| 15 | + tenantId: string; |
| 16 | + createdAt: number; |
| 17 | + updatedAt: number; |
| 18 | +} |
| 19 | + |
| 20 | +/** Persistence key inside `vscode.Memento` (workspaceState). */ |
| 21 | +const STORE_KEY = 'b2c-dx.cipAnalytics.savedQueries'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Workspace-scoped saved-query store for the Query Builder. Mirrors the shape of |
| 25 | + * {@link CipConnectionService}: in-memory cache + persisted Memento + onDidChange event. |
| 26 | + * |
| 27 | + * Queries carry the tenant they were authored against so the UI can foreground |
| 28 | + * the active tenant's queries and dim cross-tenant ones — handy when the same |
| 29 | + * workspace switches between e.g. `zzat_prd` and `bjmp_prd`. |
| 30 | + */ |
| 31 | +export class CipQueryLibraryService implements vscode.Disposable { |
| 32 | + private queries: CipSavedQuery[]; |
| 33 | + private readonly _onDidChange = new vscode.EventEmitter<CipSavedQuery[]>(); |
| 34 | + readonly onDidChange = this._onDidChange.event; |
| 35 | + |
| 36 | + constructor(private readonly workspaceState: vscode.Memento) { |
| 37 | + const stored = this.workspaceState.get<CipSavedQuery[]>(STORE_KEY); |
| 38 | + this.queries = Array.isArray(stored) ? stored.filter(this.isValid) : []; |
| 39 | + } |
| 40 | + |
| 41 | + /** All saved queries across all tenants, newest-updated first. */ |
| 42 | + list(): CipSavedQuery[] { |
| 43 | + return [...this.queries].sort((a, b) => b.updatedAt - a.updatedAt); |
| 44 | + } |
| 45 | + |
| 46 | + /** Saved queries scoped to a tenant, newest-updated first. */ |
| 47 | + listForTenant(tenantId: string): CipSavedQuery[] { |
| 48 | + return this.list().filter((q) => q.tenantId === tenantId); |
| 49 | + } |
| 50 | + |
| 51 | + get(id: string): CipSavedQuery | undefined { |
| 52 | + return this.queries.find((q) => q.id === id); |
| 53 | + } |
| 54 | + |
| 55 | + async save(input: {name: string; sql: string; description?: string; tenantId: string}): Promise<CipSavedQuery> { |
| 56 | + const now = Date.now(); |
| 57 | + const entry: CipSavedQuery = { |
| 58 | + id: randomUUID(), |
| 59 | + name: input.name.trim(), |
| 60 | + sql: input.sql, |
| 61 | + description: input.description?.trim() || undefined, |
| 62 | + tenantId: input.tenantId, |
| 63 | + createdAt: now, |
| 64 | + updatedAt: now, |
| 65 | + }; |
| 66 | + this.queries = [entry, ...this.queries]; |
| 67 | + await this.persist(); |
| 68 | + return entry; |
| 69 | + } |
| 70 | + |
| 71 | + /** Update name / description / sql on an existing entry. Bumps `updatedAt`. */ |
| 72 | + async update( |
| 73 | + id: string, |
| 74 | + patch: Partial<Pick<CipSavedQuery, 'name' | 'sql' | 'description'>>, |
| 75 | + ): Promise<CipSavedQuery | undefined> { |
| 76 | + const idx = this.queries.findIndex((q) => q.id === id); |
| 77 | + if (idx < 0) return undefined; |
| 78 | + const prev = this.queries[idx]; |
| 79 | + const next: CipSavedQuery = { |
| 80 | + ...prev, |
| 81 | + ...(patch.name !== undefined ? {name: patch.name.trim()} : {}), |
| 82 | + ...(patch.sql !== undefined ? {sql: patch.sql} : {}), |
| 83 | + ...(patch.description !== undefined ? {description: patch.description.trim() || undefined} : {}), |
| 84 | + updatedAt: Date.now(), |
| 85 | + }; |
| 86 | + this.queries = [...this.queries.slice(0, idx), next, ...this.queries.slice(idx + 1)]; |
| 87 | + await this.persist(); |
| 88 | + return next; |
| 89 | + } |
| 90 | + |
| 91 | + async delete(id: string): Promise<void> { |
| 92 | + const before = this.queries.length; |
| 93 | + this.queries = this.queries.filter((q) => q.id !== id); |
| 94 | + if (this.queries.length !== before) { |
| 95 | + await this.persist(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + dispose(): void { |
| 100 | + this._onDidChange.dispose(); |
| 101 | + } |
| 102 | + |
| 103 | + private async persist(): Promise<void> { |
| 104 | + await this.workspaceState.update(STORE_KEY, this.queries); |
| 105 | + this._onDidChange.fire(this.list()); |
| 106 | + } |
| 107 | + |
| 108 | + /** Defensive validator — discards malformed entries from older versions of the schema. */ |
| 109 | + private isValid = (q: unknown): q is CipSavedQuery => { |
| 110 | + if (!q || typeof q !== 'object') return false; |
| 111 | + const v = q as Record<string, unknown>; |
| 112 | + return ( |
| 113 | + typeof v.id === 'string' && |
| 114 | + typeof v.name === 'string' && |
| 115 | + typeof v.sql === 'string' && |
| 116 | + typeof v.tenantId === 'string' && |
| 117 | + typeof v.createdAt === 'number' && |
| 118 | + typeof v.updatedAt === 'number' |
| 119 | + ); |
| 120 | + }; |
| 121 | +} |
0 commit comments