tianji/src/server/trpc/routers/telemetry.ts

84 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-02-17 16:47:22 +00:00
import { z } from 'zod';
import {
OpenApiMetaInfo,
router,
workspaceOwnerProcedure,
workspaceProcedure,
} from '../trpc';
import { OPENAPI_TAG } from '../../utils/const';
import { prisma } from '../../model/_client';
import { TelemetryModelSchema } from '../../prisma/zod';
import { OpenApiMeta } from 'trpc-openapi';
export const telemetryRouter = router({
all: workspaceProcedure
.meta(
buildTelemetryOpenapi({
method: 'GET',
path: '/all',
})
)
.output(z.array(TelemetryModelSchema))
.query(async ({ input }) => {
const { workspaceId } = input;
const res = await prisma.telemetry.findMany({
where: {
workspaceId,
},
orderBy: {
updatedAt: 'desc',
},
});
return res;
}),
upsert: workspaceOwnerProcedure
.meta(
buildTelemetryOpenapi({
method: 'POST',
path: '/upsert',
})
)
.input(
z.object({
telemetryId: z.string().optional(),
name: z.string(),
})
)
.output(TelemetryModelSchema)
.mutation(async ({ input }) => {
const { workspaceId, telemetryId, name } = input;
if (telemetryId) {
return prisma.telemetry.update({
where: {
id: telemetryId,
workspaceId,
},
data: {
name,
},
});
} else {
return prisma.telemetry.create({
data: {
workspaceId,
name,
},
});
}
}),
});
function buildTelemetryOpenapi(meta: OpenApiMetaInfo): OpenApiMeta {
return {
openapi: {
tags: [OPENAPI_TAG.TELEMETRY],
protect: true,
...meta,
path: `/workspace/{workspaceId}${meta.path}`,
},
};
}