2023-11-24 13:59:33 +02:00
|
|
|
import { createApiToken } from '@documenso/lib/server-only/public-api/create-api-token';
|
2023-11-27 16:29:24 +02:00
|
|
|
import { deleteTokenById } from '@documenso/lib/server-only/public-api/delete-api-token-by-id';
|
2023-11-27 12:50:21 +02:00
|
|
|
import { getUserTokens } from '@documenso/lib/server-only/public-api/get-all-user-tokens';
|
2023-11-24 13:59:33 +02:00
|
|
|
import { getApiTokenById } from '@documenso/lib/server-only/public-api/get-api-token-by-id';
|
|
|
|
|
|
|
|
|
|
import { authenticatedProcedure, router } from '../trpc';
|
|
|
|
|
import {
|
|
|
|
|
ZCreateTokenMutationSchema,
|
|
|
|
|
ZDeleteTokenByIdMutationSchema,
|
|
|
|
|
ZGetApiTokenByIdQuerySchema,
|
|
|
|
|
} from './schema';
|
|
|
|
|
|
|
|
|
|
export const apiTokenRouter = router({
|
2023-11-27 12:50:21 +02:00
|
|
|
getTokens: authenticatedProcedure.query(async ({ ctx }) => {
|
2024-12-06 16:01:24 +09:00
|
|
|
return await getUserTokens({ userId: ctx.user.id });
|
2023-11-27 12:50:21 +02:00
|
|
|
}),
|
2023-12-08 13:28:34 +00:00
|
|
|
|
2023-11-24 13:59:33 +02:00
|
|
|
getTokenById: authenticatedProcedure
|
|
|
|
|
.input(ZGetApiTokenByIdQuerySchema)
|
|
|
|
|
.query(async ({ input, ctx }) => {
|
2024-12-06 16:01:24 +09:00
|
|
|
const { id } = input;
|
2023-11-24 13:59:33 +02:00
|
|
|
|
2024-12-06 16:01:24 +09:00
|
|
|
return await getApiTokenById({
|
|
|
|
|
id,
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
});
|
2023-11-24 13:59:33 +02:00
|
|
|
}),
|
2023-12-08 13:28:34 +00:00
|
|
|
|
2023-11-24 13:59:33 +02:00
|
|
|
createToken: authenticatedProcedure
|
|
|
|
|
.input(ZCreateTokenMutationSchema)
|
|
|
|
|
.mutation(async ({ input, ctx }) => {
|
2024-12-06 16:01:24 +09:00
|
|
|
const { tokenName, teamId, expirationDate } = input;
|
2024-02-09 11:32:54 +02:00
|
|
|
|
2024-12-06 16:01:24 +09:00
|
|
|
return await createApiToken({
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
teamId,
|
|
|
|
|
tokenName,
|
|
|
|
|
expiresIn: expirationDate,
|
|
|
|
|
});
|
2023-11-24 13:59:33 +02:00
|
|
|
}),
|
2023-12-08 13:28:34 +00:00
|
|
|
|
2023-11-24 13:59:33 +02:00
|
|
|
deleteTokenById: authenticatedProcedure
|
|
|
|
|
.input(ZDeleteTokenByIdMutationSchema)
|
|
|
|
|
.mutation(async ({ input, ctx }) => {
|
2024-12-06 16:01:24 +09:00
|
|
|
const { id, teamId } = input;
|
2023-11-24 13:59:33 +02:00
|
|
|
|
2024-12-06 16:01:24 +09:00
|
|
|
return await deleteTokenById({
|
|
|
|
|
id,
|
|
|
|
|
teamId,
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
});
|
2023-11-24 13:59:33 +02:00
|
|
|
}),
|
|
|
|
|
});
|