⚗️ Implement chat API
This commit is contained in:
@ -19,7 +19,6 @@ export const getAuthenticatedUser = async (
|
||||
const authenticateByToken = async (
|
||||
apiToken: string
|
||||
): Promise<User | undefined> => {
|
||||
console.log(window)
|
||||
if (typeof window !== 'undefined') return
|
||||
return (await prisma.user.findFirst({
|
||||
where: { apiTokens: { some: { token: apiToken } } },
|
||||
|
@ -30,6 +30,7 @@ export const parseNewTypebot = ({
|
||||
| 'icon'
|
||||
| 'isArchived'
|
||||
| 'isClosed'
|
||||
| 'resultsTablePreferences'
|
||||
> => {
|
||||
const startGroupId = cuid()
|
||||
const startBlockId = cuid()
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { duplicateWebhookQueries } from '@/features/blocks/integrations/webhook'
|
||||
import cuid from 'cuid'
|
||||
import { Plan } from 'db'
|
||||
import { Plan, Prisma } from 'db'
|
||||
import {
|
||||
ChoiceInputBlock,
|
||||
ConditionBlock,
|
||||
@ -38,7 +38,10 @@ export const importTypebotQuery = async (typebot: Typebot, userPlan: Plan) => {
|
||||
const duplicateTypebot = (
|
||||
typebot: Typebot,
|
||||
userPlan: Plan
|
||||
): { typebot: Typebot; webhookIdsMapping: Map<string, string> } => {
|
||||
): {
|
||||
typebot: Omit<Prisma.TypebotUncheckedCreateInput, 'id'> & { id: string }
|
||||
webhookIdsMapping: Map<string, string>
|
||||
} => {
|
||||
const groupIdsMapping = generateOldNewIdsMapping(typebot.groups)
|
||||
const edgeIdsMapping = generateOldNewIdsMapping(typebot.edges)
|
||||
const webhookIdsMapping = generateOldNewIdsMapping(
|
||||
@ -119,8 +122,8 @@ const duplicateTypebot = (
|
||||
general: { ...typebot.settings.general, isBrandingEnabled: true },
|
||||
}
|
||||
: typebot.settings,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
resultsTablePreferences: typebot.resultsTablePreferences ?? undefined,
|
||||
},
|
||||
webhookIdsMapping,
|
||||
|
@ -11,7 +11,7 @@ enum ActionType {
|
||||
Flush = 'FLUSH',
|
||||
}
|
||||
|
||||
export interface Actions<T extends { updatedAt: string } | undefined> {
|
||||
export interface Actions<T extends { updatedAt: Date } | undefined> {
|
||||
set: (
|
||||
newPresent: T | ((current: T) => T),
|
||||
options?: { updateDate: boolean }
|
||||
@ -24,13 +24,13 @@ export interface Actions<T extends { updatedAt: string } | undefined> {
|
||||
presentRef: React.MutableRefObject<T>
|
||||
}
|
||||
|
||||
interface Action<T extends { updatedAt: string } | undefined> {
|
||||
interface Action<T extends { updatedAt: Date } | undefined> {
|
||||
type: ActionType
|
||||
newPresent?: T
|
||||
updateDate?: boolean
|
||||
}
|
||||
|
||||
export interface State<T extends { updatedAt: string } | undefined> {
|
||||
export interface State<T extends { updatedAt: Date } | undefined> {
|
||||
past: T[]
|
||||
present: T
|
||||
future: T[]
|
||||
@ -42,7 +42,7 @@ const initialState = {
|
||||
future: [],
|
||||
}
|
||||
|
||||
const reducer = <T extends { updatedAt: string } | undefined>(
|
||||
const reducer = <T extends { updatedAt: Date } | undefined>(
|
||||
state: State<T>,
|
||||
action: Action<T>
|
||||
) => {
|
||||
@ -112,7 +112,7 @@ const reducer = <T extends { updatedAt: string } | undefined>(
|
||||
}
|
||||
}
|
||||
|
||||
const useUndo = <T extends { updatedAt: string } | undefined>(
|
||||
const useUndo = <T extends { updatedAt: Date } | undefined>(
|
||||
initialPresent: T
|
||||
): [State<T>, Actions<T>] => {
|
||||
const [state, dispatch] = useReducer(reducer, {
|
||||
|
@ -26,6 +26,7 @@ export const parsePublicTypebotToTypebot = (
|
||||
workspaceId: existingTypebot.workspaceId,
|
||||
isArchived: existingTypebot.isArchived,
|
||||
isClosed: existingTypebot.isClosed,
|
||||
resultsTablePreferences: existingTypebot.resultsTablePreferences,
|
||||
})
|
||||
|
||||
export const parseTypebotToPublicTypebot = (
|
||||
@ -38,8 +39,8 @@ export const parseTypebotToPublicTypebot = (
|
||||
settings: typebot.settings,
|
||||
theme: typebot.theme,
|
||||
variables: typebot.variables,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
|
||||
export const checkIfTypebotsAreEqual = (typebotA: Typebot, typebotB: Typebot) =>
|
||||
|
@ -57,7 +57,7 @@ export const ResultsTableContainer = () => {
|
||||
|
||||
{typebot && (
|
||||
<SubmissionsTable
|
||||
preferences={typebot.resultsTablePreferences}
|
||||
preferences={typebot.resultsTablePreferences ?? undefined}
|
||||
resultHeader={resultHeader}
|
||||
data={tableData}
|
||||
onScrollToBottom={fetchNextPage}
|
||||
|
@ -94,11 +94,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
data:
|
||||
'groups' in data
|
||||
? data
|
||||
: (parseNewTypebot({
|
||||
: parseNewTypebot({
|
||||
ownerAvatarUrl: user.image,
|
||||
isBrandingEnabled: workspace.plan === Plan.FREE,
|
||||
...data,
|
||||
}) as Prisma.TypebotUncheckedCreateInput),
|
||||
}),
|
||||
})
|
||||
return res.send(typebot)
|
||||
}
|
||||
|
@ -1,8 +1,15 @@
|
||||
import { createContext } from '@/utils/server/context'
|
||||
import { appRouter } from '@/utils/server/routers/v1/_app'
|
||||
import { captureException } from '@sentry/nextjs'
|
||||
import { createOpenApiNextHandler } from 'trpc-openapi'
|
||||
|
||||
export default createOpenApiNextHandler({
|
||||
router: appRouter,
|
||||
createContext,
|
||||
onError({ error }) {
|
||||
if (error.code === 'INTERNAL_SERVER_ERROR') {
|
||||
captureException(error)
|
||||
console.error('Something went wrong', error)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
@ -10,6 +10,6 @@ const openApiDocument = generateOpenApiDocument(appRouter, {
|
||||
})
|
||||
|
||||
writeFileSync(
|
||||
'./openapi/builder.json',
|
||||
'./openapi/builder/_spec_.json',
|
||||
JSON.stringify(openApiDocument, null, 2)
|
||||
)
|
||||
|
Reference in New Issue
Block a user