mirror of https://github.com/buster-so/buster.git
move get asset types around
This commit is contained in:
parent
b2cf1725fc
commit
5e6eb76f5b
|
@ -20,24 +20,21 @@ const app = new Hono()
|
||||||
const user = c.get('busterUser');
|
const user = c.get('busterUser');
|
||||||
|
|
||||||
const userOrg = await getUserOrganizationId(user.id);
|
const userOrg = await getUserOrganizationId(user.id);
|
||||||
if (!userOrg) {
|
|
||||||
throw new HTTPException(403, { message: 'User is not associated with an organization' });
|
|
||||||
}
|
|
||||||
|
|
||||||
let title: string | null = null;
|
let title: string | null = null;
|
||||||
|
|
||||||
switch (assetType) {
|
switch (assetType) {
|
||||||
case 'chat':
|
case 'chat':
|
||||||
title = await getChatTitle({ assetId, organizationId: userOrg.organizationId });
|
title = await getChatTitle({ assetId, organizationId: userOrg?.organizationId });
|
||||||
break;
|
break;
|
||||||
case 'metric':
|
case 'metric':
|
||||||
title = await getMetricTitle({ assetId, organizationId: userOrg.organizationId });
|
title = await getMetricTitle({ assetId, organizationId: userOrg?.organizationId });
|
||||||
break;
|
break;
|
||||||
case 'collection':
|
case 'collection':
|
||||||
title = await getCollectionTitle({ assetId, organizationId: userOrg.organizationId });
|
title = await getCollectionTitle({ assetId, organizationId: userOrg?.organizationId });
|
||||||
break;
|
break;
|
||||||
case 'dashboard':
|
case 'dashboard':
|
||||||
title = await getDashboardTitle({ assetId, organizationId: userOrg.organizationId });
|
title = await getDashboardTitle({ assetId, organizationId: userOrg?.organizationId });
|
||||||
break;
|
break;
|
||||||
default: {
|
default: {
|
||||||
const _exhaustive: never = assetType;
|
const _exhaustive: never = assetType;
|
||||||
|
|
|
@ -6,27 +6,4 @@ export {
|
||||||
type GenerateAssetMessagesInput,
|
type GenerateAssetMessagesInput,
|
||||||
} from './assets';
|
} from './assets';
|
||||||
|
|
||||||
export {
|
|
||||||
getChatDashboardFiles,
|
|
||||||
type DashboardFileContext,
|
|
||||||
type DashboardFile,
|
|
||||||
} from './dashboards';
|
|
||||||
|
|
||||||
export {
|
|
||||||
getMetricTitle,
|
|
||||||
GetMetricTitleInputSchema,
|
|
||||||
type GetMetricTitleInput,
|
|
||||||
} from './get-metric-title';
|
|
||||||
|
|
||||||
export {
|
|
||||||
getCollectionTitle,
|
|
||||||
GetCollectionTitleInputSchema,
|
|
||||||
type GetCollectionTitleInput,
|
|
||||||
} from './get-collection-title';
|
|
||||||
|
|
||||||
export {
|
|
||||||
getDashboardTitle,
|
|
||||||
GetDashboardTitleInputSchema,
|
|
||||||
type GetDashboardTitleInput,
|
|
||||||
} from './get-dashboard-title';
|
|
||||||
export type { DatabaseAssetType } from './assets';
|
export type { DatabaseAssetType } from './assets';
|
||||||
|
|
|
@ -10,7 +10,8 @@ export const GetCollectionTitleInputSchema = z.object({
|
||||||
|
|
||||||
export type GetCollectionTitleInput = z.infer<typeof GetCollectionTitleInputSchema>;
|
export type GetCollectionTitleInput = z.infer<typeof GetCollectionTitleInputSchema>;
|
||||||
|
|
||||||
export async function getCollectionTitle(input: GetCollectionTitleInput): Promise<string | null> {
|
// Updated return type to remove null since we now throw an error instead
|
||||||
|
export async function getCollectionTitle(input: GetCollectionTitleInput): Promise<string> {
|
||||||
const validated = GetCollectionTitleInputSchema.parse(input);
|
const validated = GetCollectionTitleInputSchema.parse(input);
|
||||||
|
|
||||||
const [collection] = await db
|
const [collection] = await db
|
||||||
|
@ -22,12 +23,16 @@ export async function getCollectionTitle(input: GetCollectionTitleInput): Promis
|
||||||
.where(and(eq(collections.id, validated.assetId), isNull(collections.deletedAt)))
|
.where(and(eq(collections.id, validated.assetId), isNull(collections.deletedAt)))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
|
// Throw error instead of returning null
|
||||||
if (!collection) {
|
if (!collection) {
|
||||||
return null;
|
throw new Error(`Collection with ID ${validated.assetId} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Throw error for permission failure instead of returning null
|
||||||
if (collection.organizationId !== validated.organizationId) {
|
if (collection.organizationId !== validated.organizationId) {
|
||||||
return null;
|
throw new Error(
|
||||||
|
`Access denied: Collection with ID ${validated.assetId} does not belong to the specified organization`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return collection.name;
|
return collection.name;
|
|
@ -0,0 +1,5 @@
|
||||||
|
export {
|
||||||
|
getCollectionTitle,
|
||||||
|
GetCollectionTitleInputSchema,
|
||||||
|
type GetCollectionTitleInput,
|
||||||
|
} from './get-collection-title';
|
|
@ -10,7 +10,8 @@ export const GetDashboardTitleInputSchema = z.object({
|
||||||
|
|
||||||
export type GetDashboardTitleInput = z.infer<typeof GetDashboardTitleInputSchema>;
|
export type GetDashboardTitleInput = z.infer<typeof GetDashboardTitleInputSchema>;
|
||||||
|
|
||||||
export async function getDashboardTitle(input: GetDashboardTitleInput): Promise<string | null> {
|
// Updated return type to remove null since we now throw an error instead
|
||||||
|
export async function getDashboardTitle(input: GetDashboardTitleInput): Promise<string> {
|
||||||
const validated = GetDashboardTitleInputSchema.parse(input);
|
const validated = GetDashboardTitleInputSchema.parse(input);
|
||||||
|
|
||||||
const [dashboard] = await db
|
const [dashboard] = await db
|
||||||
|
@ -23,12 +24,16 @@ export async function getDashboardTitle(input: GetDashboardTitleInput): Promise<
|
||||||
.where(and(eq(dashboardFiles.id, validated.assetId), isNull(dashboardFiles.deletedAt)))
|
.where(and(eq(dashboardFiles.id, validated.assetId), isNull(dashboardFiles.deletedAt)))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
|
// Throw error instead of returning null
|
||||||
if (!dashboard) {
|
if (!dashboard) {
|
||||||
return null;
|
throw new Error(`Dashboard with ID ${validated.assetId} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Throw error for permission failure instead of returning null
|
||||||
if (!dashboard.publiclyAccessible && dashboard.organizationId !== validated.organizationId) {
|
if (!dashboard.publiclyAccessible && dashboard.organizationId !== validated.organizationId) {
|
||||||
return null;
|
throw new Error(
|
||||||
|
`Access denied: Dashboard with ID ${validated.assetId} is not publicly accessible and does not belong to the specified organization`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return dashboard.name;
|
return dashboard.name;
|
|
@ -0,0 +1,11 @@
|
||||||
|
export {
|
||||||
|
getChatDashboardFiles,
|
||||||
|
type DashboardFileContext,
|
||||||
|
type DashboardFile,
|
||||||
|
} from './dashboards';
|
||||||
|
|
||||||
|
export {
|
||||||
|
getDashboardTitle,
|
||||||
|
GetDashboardTitleInputSchema,
|
||||||
|
type GetDashboardTitleInput,
|
||||||
|
} from './get-dashboard-title';
|
|
@ -5,3 +5,6 @@ export * from './assets';
|
||||||
export * from './metadata';
|
export * from './metadata';
|
||||||
export * from './chats';
|
export * from './chats';
|
||||||
export * from './organizations';
|
export * from './organizations';
|
||||||
|
export * from './dashboards';
|
||||||
|
export * from './metrics';
|
||||||
|
export * from './collections';
|
||||||
|
|
|
@ -10,7 +10,8 @@ export const GetMetricTitleInputSchema = z.object({
|
||||||
|
|
||||||
export type GetMetricTitleInput = z.infer<typeof GetMetricTitleInputSchema>;
|
export type GetMetricTitleInput = z.infer<typeof GetMetricTitleInputSchema>;
|
||||||
|
|
||||||
export async function getMetricTitle(input: GetMetricTitleInput): Promise<string | null> {
|
// Updated return type to remove null since we now throw an error instead
|
||||||
|
export async function getMetricTitle(input: GetMetricTitleInput): Promise<string> {
|
||||||
const validated = GetMetricTitleInputSchema.parse(input);
|
const validated = GetMetricTitleInputSchema.parse(input);
|
||||||
|
|
||||||
const [metric] = await db
|
const [metric] = await db
|
||||||
|
@ -23,12 +24,16 @@ export async function getMetricTitle(input: GetMetricTitleInput): Promise<string
|
||||||
.where(and(eq(metricFiles.id, validated.assetId), isNull(metricFiles.deletedAt)))
|
.where(and(eq(metricFiles.id, validated.assetId), isNull(metricFiles.deletedAt)))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
|
// Throw error instead of returning null
|
||||||
if (!metric) {
|
if (!metric) {
|
||||||
return null;
|
throw new Error(`Metric with ID ${validated.assetId} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Throw error for permission failure instead of returning null
|
||||||
if (!metric.publiclyAccessible && metric.organizationId !== validated.organizationId) {
|
if (!metric.publiclyAccessible && metric.organizationId !== validated.organizationId) {
|
||||||
return null;
|
throw new Error(
|
||||||
|
`Access denied: Metric with ID ${validated.assetId} is not publicly accessible and does not belong to the specified organization`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return metric.name;
|
return metric.name;
|
|
@ -0,0 +1,5 @@
|
||||||
|
export {
|
||||||
|
getMetricTitle,
|
||||||
|
GetMetricTitleInputSchema,
|
||||||
|
type GetMetricTitleInput,
|
||||||
|
} from './get-metric-title';
|
|
@ -1,8 +1,9 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
import { AssetTypeSchema } from '../assets/asset-types.types';
|
||||||
|
|
||||||
export const GetTitleRequestSchema = z.object({
|
export const GetTitleRequestSchema = z.object({
|
||||||
assetId: z.string().uuid(),
|
assetId: z.string().uuid(),
|
||||||
assetType: z.enum(['chat', 'metric', 'collection', 'dashboard']),
|
assetType: AssetTypeSchema,
|
||||||
});
|
});
|
||||||
|
|
||||||
export type GetTitleRequest = z.infer<typeof GetTitleRequestSchema>;
|
export type GetTitleRequest = z.infer<typeof GetTitleRequestSchema>;
|
||||||
|
|
Loading…
Reference in New Issue