Skip to content

Commit

Permalink
✨ Add script template that reads CSV file and process things
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Oct 11, 2024
1 parent cda7f2d commit 5fb10d2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"updateUserEmail": "tsx src/updateUserEmail.ts",
"inspectChatSession": "tsx src/inspectChatSession.ts",
"deleteChatSession": "tsx src/deleteChatSession.ts",
"checkDependencies": "tsx src/checkDependencies.ts"
"checkDependencies": "tsx src/checkDependencies.ts",
"readCsvAndDoSomething": "SKIP_ENV_CHECK=true dotenv -e ./.env.production -- tsx src/readCsvAndDoSomething.ts"
},
"exports": {
"./*": "./src/*.ts"
Expand Down
96 changes: 96 additions & 0 deletions packages/scripts/src/readCsvAndDoSomething.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { readFileSync } from "fs";
import { IntegrationBlockType } from "@typebot.io/blocks-integrations/constants";
import { parseGroups } from "@typebot.io/groups/schemas";
import prisma from "@typebot.io/prisma";
import { Plan } from "@typebot.io/prisma/enum";
import { SingleBar } from "cli-progress";
import { parse } from "papaparse";

const main = async () => {
const fileContent = readFileSync("./src/files/topTwo.csv").toString();
const topTypebots = parse<{
id: string;
total_results: number;
}>(fileContent, { header: true });

console.log("processing", topTypebots.data.length, "typebots...");

console.log(topTypebots.data);

const bar = new SingleBar({});

bar.start(topTypebots.data.length, 0);

let i = 0;
const updatedGroupsList = [];
for (const { id } of topTypebots.data) {
bar.update(i++);

const publicTypebot = await prisma.publicTypebot.findUnique({
where: {
typebotId: id,
},
select: {
version: true,
groups: true,
typebot: {
select: {
workspace: {
select: {
plan: true,
},
},
},
},
},
});

if (!publicTypebot) continue;
if (publicTypebot.typebot.workspace.plan !== Plan.FREE) continue;

const groups = parseGroups(publicTypebot.groups, {
typebotVersion: publicTypebot.version,
});

let wasUpdated = false;
const updatedGroups = groups.map((group) => {
return {
...group,
blocks: group.blocks.map((block) => {
if (
block.type === IntegrationBlockType.EMAIL &&
block.options?.credentialsId === "default"
) {
wasUpdated = true;
return {
...block,
options: {
...block.options,
credentialsId: undefined,
},
};
}
return block;
}),
};
});

updatedGroupsList.push(updatedGroups);

if (wasUpdated) {
console.log("Updating...", id);
await prisma.publicTypebot.update({
where: {
typebotId: id,
},
data: {
groups: updatedGroups,
},
});
}
}

bar.stop();
};

main().then();

0 comments on commit 5fb10d2

Please sign in to comment.