diff --git a/packages/scripts/importContactToBrevo.ts b/packages/scripts/importContactToBrevo.ts index eb9d30ae1..f669c16f2 100644 --- a/packages/scripts/importContactToBrevo.ts +++ b/packages/scripts/importContactToBrevo.ts @@ -1,7 +1,6 @@ import { PrismaClient } from '@typebot.io/prisma' import { promptAndSetEnvironment } from './utils' import got, { HTTPError } from 'got' -import { readFileSync } from 'fs' const importContactToBrevo = async () => { await promptAndSetEnvironment() @@ -14,25 +13,44 @@ const importContactToBrevo = async () => { console.log(e.params) console.log(e.duration, 'ms') }) - const users = JSON.parse(readFileSync('users.json').toString()) as { - email: string - name: string - }[] - console.log('Inserting users', users.length) + + const workspaces = await prisma.workspace.findMany({ + where: { + plan: { in: ['STARTER', 'PRO'] }, + stripeId: { not: null }, + }, + select: { + id: true, + members: { + where: { + role: 'ADMIN', + }, + select: { + user: { + select: { + email: true, + }, + }, + }, + }, + }, + }) + + console.log('Inserting users', workspaces.flatMap((w) => w.members).length) + try { await got.post('https://api.brevo.com/v3/contacts/import', { headers: { 'api-key': process.env.BREVO_API_KEY, }, json: { - listIds: [16], + listIds: [17], updateExistingContacts: true, - jsonBody: users.map((user) => ({ - email: user.email, - attributes: { - FIRSTNAME: user.name ? user.name.split(' ')[0] : undefined, - }, - })), + jsonBody: workspaces + .flatMap((workspace) => workspace.members.map((m) => m.user.email)) + .map((email) => ({ + email, + })), }, }) } catch (err) { diff --git a/packages/scripts/package.json b/packages/scripts/package.json index 6bbc210da..04f2c203f 100644 --- a/packages/scripts/package.json +++ b/packages/scripts/package.json @@ -16,7 +16,8 @@ "inspectUser": "tsx inspectUser.ts", "checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts", "createChatsPrices": "tsx createChatsPrices.ts", - "migrateSubscriptionsToUsageBased": "tsx migrateSubscriptionsToUsageBased.ts" + "migrateSubscriptionsToUsageBased": "tsx migrateSubscriptionsToUsageBased.ts", + "importContactToBrevo": "tsx importContactToBrevo.ts" }, "devDependencies": { "@typebot.io/emails": "workspace:*",