| 
1 | 1 | import { TRPCError } from "@trpc/server";  | 
2 | 2 | import prisma from "@typebot.io/prisma";  | 
 | 3 | +import { DbNull } from "@typebot.io/prisma/enum";  | 
3 | 4 | import { getTypebotAccessRight } from "@typebot.io/typebot/helpers/getTypebotAccessRight";  | 
4 | 5 | import { isReadTypebotForbidden } from "@typebot.io/typebot/helpers/isReadTypebotForbidden";  | 
5 | 6 | import { migrateTypebot } from "@typebot.io/typebot/migrations/migrateTypebot";  | 
6 |  | -import { typebotSchema } from "@typebot.io/typebot/schemas/typebot";  | 
 | 7 | +import {  | 
 | 8 | +  type Typebot,  | 
 | 9 | +  type TypebotV6,  | 
 | 10 | +  typebotSchema,  | 
 | 11 | +} from "@typebot.io/typebot/schemas/typebot";  | 
7 | 12 | import { z } from "@typebot.io/zod";  | 
8 | 13 | import { publicProcedure } from "@/helpers/server/trpc";  | 
9 | 14 | 
 
  | 
@@ -70,12 +75,47 @@ export const getTypebot = publicProcedure  | 
70 | 75 |         });  | 
71 | 76 | 
 
  | 
72 | 77 |       try {  | 
73 |  | -        const parsedTypebot = migrateToLatestVersion  | 
74 |  | -          ? await migrateTypebot(typebotSchema.parse(existingTypebot))  | 
75 |  | -          : typebotSchema.parse(existingTypebot);  | 
 | 78 | +        const { typebot, wasMigrated } = (  | 
 | 79 | +          migrateToLatestVersion  | 
 | 80 | +            ? await migrateTypebot(typebotSchema.parse(existingTypebot))  | 
 | 81 | +            : {  | 
 | 82 | +                typebot: typebotSchema.parse(existingTypebot),  | 
 | 83 | +                wasMigrated: false,  | 
 | 84 | +              }  | 
 | 85 | +        ) as  | 
 | 86 | +          | { typebot: TypebotV6; wasMigrated: true }  | 
 | 87 | +          | { typebot: Typebot; wasMigrated: false };  | 
 | 88 | + | 
 | 89 | +        if (wasMigrated)  | 
 | 90 | +          await prisma.typebot.update({  | 
 | 91 | +            where: {  | 
 | 92 | +              id: existingTypebot.id,  | 
 | 93 | +            },  | 
 | 94 | +            data: {  | 
 | 95 | +              version: typebot.version,  | 
 | 96 | +              name: typebot.name,  | 
 | 97 | +              icon: typebot.icon,  | 
 | 98 | +              selectedThemeTemplateId: typebot.selectedThemeTemplateId,  | 
 | 99 | +              events: typebot.events,  | 
 | 100 | +              groups: typebot.groups,  | 
 | 101 | +              theme: typebot.theme ? typebot.theme : undefined,  | 
 | 102 | +              settings: typebot.settings ? typebot.settings : undefined,  | 
 | 103 | +              folderId: typebot.folderId,  | 
 | 104 | +              variables: typebot.variables,  | 
 | 105 | +              edges: typebot.edges,  | 
 | 106 | +              resultsTablePreferences:  | 
 | 107 | +                typebot.resultsTablePreferences === null  | 
 | 108 | +                  ? DbNull  | 
 | 109 | +                  : typebot.resultsTablePreferences,  | 
 | 110 | +              publicId: typebot.publicId === null ? null : typebot.publicId,  | 
 | 111 | +              customDomain: typebot.customDomain,  | 
 | 112 | +              isClosed: typebot.isClosed,  | 
 | 113 | +              whatsAppCredentialsId: typebot.whatsAppCredentialsId ?? undefined,  | 
 | 114 | +            },  | 
 | 115 | +          });  | 
76 | 116 | 
 
  | 
77 | 117 |         return {  | 
78 |  | -          typebot: parsedTypebot,  | 
 | 118 | +          typebot,  | 
79 | 119 |           currentUserMode: getTypebotAccessRight(user, existingTypebot),  | 
80 | 120 |         };  | 
81 | 121 |       } catch (err) {  | 
 | 
0 commit comments