-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧐 Update add users to brevo list script
- Loading branch information
1 parent
272adc0
commit 9a36281
Showing
3 changed files
with
71 additions
and
66 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters