Skip to content

Commit

Permalink
♻️ Update import contact to brevo script
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Oct 23, 2023
1 parent eba52a5 commit be9daee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
44 changes: 31 additions & 13 deletions packages/scripts/importContactToBrevo.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down

0 comments on commit be9daee

Please sign in to comment.