2023-03-09 08:46:36 +01:00
|
|
|
import prisma from '@/lib/prisma'
|
2023-03-15 11:51:30 +01:00
|
|
|
import { authenticatedProcedure } from '@/helpers/server/trpc'
|
2023-03-09 08:46:36 +01:00
|
|
|
import { TRPCError } from '@trpc/server'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { stripeCredentialsSchema } from '@typebot.io/schemas/features/blocks/inputs/payment/schemas'
|
|
|
|
import { googleSheetsCredentialsSchema } from '@typebot.io/schemas/features/blocks/integrations/googleSheets/schemas'
|
|
|
|
import { openAICredentialsSchema } from '@typebot.io/schemas/features/blocks/integrations/openai'
|
|
|
|
import { smtpCredentialsSchema } from '@typebot.io/schemas/features/blocks/integrations/sendEmail'
|
2023-03-09 08:46:36 +01:00
|
|
|
import { z } from 'zod'
|
2023-08-17 15:11:50 +02:00
|
|
|
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden'
|
2023-03-09 08:46:36 +01:00
|
|
|
|
|
|
|
export const listCredentials = authenticatedProcedure
|
|
|
|
.meta({
|
|
|
|
openapi: {
|
|
|
|
method: 'GET',
|
|
|
|
path: '/credentials',
|
|
|
|
protect: true,
|
|
|
|
summary: 'List workspace credentials',
|
|
|
|
tags: ['Credentials'],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
workspaceId: z.string(),
|
|
|
|
type: stripeCredentialsSchema.shape.type
|
|
|
|
.or(smtpCredentialsSchema.shape.type)
|
|
|
|
.or(googleSheetsCredentialsSchema.shape.type)
|
|
|
|
.or(openAICredentialsSchema.shape.type),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.output(
|
|
|
|
z.object({
|
|
|
|
credentials: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ input: { workspaceId, type }, ctx: { user } }) => {
|
|
|
|
const workspace = await prisma.workspace.findFirst({
|
|
|
|
where: {
|
|
|
|
id: workspaceId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
2023-08-17 15:11:50 +02:00
|
|
|
members: true,
|
|
|
|
credentials: {
|
|
|
|
where: {
|
|
|
|
type,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
2023-03-09 08:46:36 +01:00
|
|
|
},
|
|
|
|
})
|
2023-08-17 15:11:50 +02:00
|
|
|
if (!workspace || (await isReadWorkspaceFobidden(workspace, user)))
|
|
|
|
throw new TRPCError({ code: 'NOT_FOUND', message: 'Workspace not found' })
|
|
|
|
|
|
|
|
return { credentials: workspace.credentials }
|
2023-03-09 08:46:36 +01:00
|
|
|
})
|