🧐 Update add users to brevo list script
This commit is contained in:
@ -1,65 +0,0 @@
|
|||||||
import { PrismaClient } from '@typebot.io/prisma'
|
|
||||||
import { promptAndSetEnvironment } from './utils'
|
|
||||||
import got, { HTTPError } from 'got'
|
|
||||||
|
|
||||||
const importContactToBrevo = async () => {
|
|
||||||
await promptAndSetEnvironment()
|
|
||||||
const prisma = new PrismaClient({
|
|
||||||
log: [{ emit: 'event', level: 'query' }, 'info', 'warn', 'error'],
|
|
||||||
})
|
|
||||||
|
|
||||||
prisma.$on('query', (e) => {
|
|
||||||
console.log(e.query)
|
|
||||||
console.log(e.params)
|
|
||||||
console.log(e.duration, 'ms')
|
|
||||||
})
|
|
||||||
|
|
||||||
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: [17],
|
|
||||||
updateExistingContacts: true,
|
|
||||||
jsonBody: workspaces
|
|
||||||
.flatMap((workspace) => workspace.members.map((m) => m.user.email))
|
|
||||||
.map((email) => ({
|
|
||||||
email,
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof HTTPError) {
|
|
||||||
console.log(err.response.body)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
console.log(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
importContactToBrevo()
|
|
70
packages/scripts/insertUsersInBrevoList.ts
Normal file
70
packages/scripts/insertUsersInBrevoList.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import { PrismaClient } from '@typebot.io/prisma'
|
||||||
|
import { promptAndSetEnvironment } from './utils'
|
||||||
|
import got, { HTTPError } from 'got'
|
||||||
|
import { confirm, text, isCancel } from '@clack/prompts'
|
||||||
|
import { writeFileSync } from 'fs'
|
||||||
|
|
||||||
|
const insertUsersInBrevoList = async () => {
|
||||||
|
await promptAndSetEnvironment('production')
|
||||||
|
|
||||||
|
const listId = await text({
|
||||||
|
message: 'List ID?',
|
||||||
|
})
|
||||||
|
if (!listId || isCancel(listId) || isNaN(listId as unknown as number))
|
||||||
|
process.exit()
|
||||||
|
|
||||||
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
|
const threeMonthsAgo = new Date()
|
||||||
|
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3)
|
||||||
|
|
||||||
|
const oneMonthAgo = new Date()
|
||||||
|
oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1)
|
||||||
|
|
||||||
|
const users = await prisma.user.findMany({
|
||||||
|
where: {
|
||||||
|
lastActivityAt: {
|
||||||
|
gte: threeMonthsAgo,
|
||||||
|
},
|
||||||
|
createdAt: {
|
||||||
|
lt: oneMonthAgo,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
email: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Inserting users', users.length)
|
||||||
|
|
||||||
|
writeFileSync('logs/users.json', JSON.stringify(users, null, 2))
|
||||||
|
|
||||||
|
const proceed = await confirm({ message: 'Proceed?' })
|
||||||
|
if (!proceed || typeof proceed !== 'boolean') {
|
||||||
|
console.log('Aborting')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await got.post('https://api.brevo.com/v3/contacts/import', {
|
||||||
|
headers: {
|
||||||
|
'api-key': process.env.BREVO_API_KEY,
|
||||||
|
},
|
||||||
|
json: {
|
||||||
|
listIds: [Number(listId)],
|
||||||
|
updateExistingContacts: true,
|
||||||
|
jsonBody: users.map((email) => ({
|
||||||
|
email,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof HTTPError) {
|
||||||
|
console.log(err.response.body)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
insertUsersInBrevoList()
|
@ -17,7 +17,7 @@
|
|||||||
"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",
|
"insertUsersInBrevoList": "tsx insertUsersInBrevoList.ts",
|
||||||
"getUsage": "tsx getUsage.ts",
|
"getUsage": "tsx getUsage.ts",
|
||||||
"suspendWorkspace": "tsx suspendWorkspace.ts",
|
"suspendWorkspace": "tsx suspendWorkspace.ts",
|
||||||
"destroyUser": "tsx destroyUser.ts",
|
"destroyUser": "tsx destroyUser.ts",
|
||||||
|
Reference in New Issue
Block a user