2
0

👷 Improve getUsage accuracy in check cron job

This commit is contained in:
Baptiste Arnaud
2023-10-17 08:53:09 +02:00
parent 6b0c263f88
commit 1cc4ccfcfa
5 changed files with 219 additions and 183 deletions

View File

@ -1,37 +0,0 @@
import { PrismaClient } from '@typebot.io/prisma'
export const getUsage =
(prisma: PrismaClient) => async (workspaceId: string) => {
const now = new Date()
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
const firstDayOfNextMonth = new Date(
now.getFullYear(),
now.getMonth() + 1,
1
)
const typebots = await prisma.typebot.findMany({
where: {
workspace: {
id: workspaceId,
},
},
select: { id: true },
})
const [totalChatsUsed] = await Promise.all([
prisma.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,
},
},
}),
])
return {
totalChatsUsed,
}
}