2
0

♻️ Update import contact to brevo script

This commit is contained in:
Baptiste Arnaud
2023-10-18 09:33:32 +02:00
parent eba52a5397
commit be9daee63e
2 changed files with 33 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
import { PrismaClient } from '@typebot.io/prisma' import { PrismaClient } from '@typebot.io/prisma'
import { promptAndSetEnvironment } from './utils' import { promptAndSetEnvironment } from './utils'
import got, { HTTPError } from 'got' import got, { HTTPError } from 'got'
import { readFileSync } from 'fs'
const importContactToBrevo = async () => { const importContactToBrevo = async () => {
await promptAndSetEnvironment() await promptAndSetEnvironment()
@@ -14,25 +13,44 @@ const importContactToBrevo = async () => {
console.log(e.params) console.log(e.params)
console.log(e.duration, 'ms') console.log(e.duration, 'ms')
}) })
const users = JSON.parse(readFileSync('users.json').toString()) as {
email: string const workspaces = await prisma.workspace.findMany({
name: string where: {
}[] plan: { in: ['STARTER', 'PRO'] },
console.log('Inserting users', users.length) 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 { try {
await got.post('https://api.brevo.com/v3/contacts/import', { await got.post('https://api.brevo.com/v3/contacts/import', {
headers: { headers: {
'api-key': process.env.BREVO_API_KEY, 'api-key': process.env.BREVO_API_KEY,
}, },
json: { json: {
listIds: [16], listIds: [17],
updateExistingContacts: true, updateExistingContacts: true,
jsonBody: users.map((user) => ({ jsonBody: workspaces
email: user.email, .flatMap((workspace) => workspace.members.map((m) => m.user.email))
attributes: { .map((email) => ({
FIRSTNAME: user.name ? user.name.split(' ')[0] : undefined, email,
}, })),
})),
}, },
}) })
} catch (err) { } catch (err) {

View File

@@ -16,7 +16,8 @@
"inspectUser": "tsx inspectUser.ts", "inspectUser": "tsx inspectUser.ts",
"checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts", "checkSubscriptionsStatus": "tsx checkSubscriptionsStatus.ts",
"createChatsPrices": "tsx createChatsPrices.ts", "createChatsPrices": "tsx createChatsPrices.ts",
"migrateSubscriptionsToUsageBased": "tsx migrateSubscriptionsToUsageBased.ts" "migrateSubscriptionsToUsageBased": "tsx migrateSubscriptionsToUsageBased.ts",
"importContactToBrevo": "tsx importContactToBrevo.ts"
}, },
"devDependencies": { "devDependencies": {
"@typebot.io/emails": "workspace:*", "@typebot.io/emails": "workspace:*",