* esm * wip * wip * wip * wip * wip * wip * subscription notice * wip * wip * wip * fix envs * fix: update docker build * fix * esm/types * delete dashboard :D * add patches to dockerfiles * update packages + catalogs + ts * wip * remove native libs * ts * improvements * fix redirects and fetching session * try fix favicon * fixes * fix * order and resize reportds within a dashboard * improvements * wip * added userjot to dashboard * fix * add op * wip * different cache key * improve date picker * fix table * event details loading * redo onboarding completely * fix login * fix * fix * extend session, billing and improve bars * fix * reduce price on 10M
102 lines
2.3 KiB
TypeScript
102 lines
2.3 KiB
TypeScript
import crypto from 'node:crypto';
|
|
import { z } from 'zod';
|
|
|
|
import type { Prisma } from '@openpanel/db';
|
|
import { db } from '@openpanel/db';
|
|
|
|
import { hashPassword } from '@openpanel/common/server';
|
|
import { getClientAccess } from '../access';
|
|
import { TRPCAccessError } from '../errors';
|
|
import { createTRPCRouter, protectedProcedure } from '../trpc';
|
|
|
|
export const clientRouter = createTRPCRouter({
|
|
list: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
projectId: z.string(),
|
|
}),
|
|
)
|
|
.query(async ({ input }) => {
|
|
return db.client.findMany({
|
|
where: {
|
|
projectId: input.projectId,
|
|
},
|
|
});
|
|
}),
|
|
update: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
const access = await getClientAccess({
|
|
userId: ctx.session.userId,
|
|
clientId: input.id,
|
|
});
|
|
|
|
if (!access) {
|
|
throw TRPCAccessError('You do not have access to this client');
|
|
}
|
|
|
|
return db.client.update({
|
|
where: {
|
|
id: input.id,
|
|
},
|
|
data: {
|
|
name: input.name,
|
|
},
|
|
});
|
|
}),
|
|
create: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
name: z.string(),
|
|
projectId: z.string(),
|
|
organizationId: z.string(),
|
|
type: z.enum(['read', 'write', 'root']).optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input }) => {
|
|
const secret = `sec_${crypto.randomBytes(10).toString('hex')}`;
|
|
const data: Prisma.ClientCreateArgs['data'] = {
|
|
organizationId: input.organizationId,
|
|
projectId: input.projectId,
|
|
name: input.name,
|
|
type: input.type ?? 'write',
|
|
secret: await hashPassword(secret),
|
|
};
|
|
|
|
const client = await db.client.create({ data });
|
|
|
|
return {
|
|
...client,
|
|
secret,
|
|
};
|
|
}),
|
|
remove: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
id: z.string(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
const access = await getClientAccess({
|
|
userId: ctx.session.userId,
|
|
clientId: input.id,
|
|
});
|
|
|
|
if (!access) {
|
|
throw TRPCAccessError('You do not have access to this client');
|
|
}
|
|
|
|
await db.client.delete({
|
|
where: {
|
|
id: input.id,
|
|
},
|
|
});
|
|
return true;
|
|
}),
|
|
});
|