2
0

📈 Add convenient isFirstOfKind field in total results digest

This commit is contained in:
Baptiste Arnaud
2023-03-14 21:10:43 +01:00
parent 67cb4b4878
commit 25c367901f
2 changed files with 20 additions and 11 deletions

View File

@ -73,6 +73,7 @@ const newResultsCollectedEventSchema = typebotEvent.merge(
name: z.literal('New results collected'),
data: z.object({
total: z.number(),
isFirstOfKind: z.literal(true).optional(),
}),
})
)

View File

@ -52,31 +52,39 @@ export const sendTotalResultsDigest = async () => {
})
const resultsWithWorkspaces = results
.flatMap((result) => {
.flatMap((result, resultIndex) => {
const workspace = workspaces.find((workspace) =>
workspace.typebots.some((typebot) => typebot.id === result.typebotId)
)
if (!workspace) return
return workspace.members
.filter((member) => member.role !== WorkspaceRole.GUEST)
.map((member) => ({
.map((member, memberIndex) => ({
userId: member.userId,
workspaceId: workspace.id,
typebotId: result.typebotId,
totalResultsYesterday: result._count._all,
isFirstOfKind:
resultIndex === 0 && memberIndex === 0
? (true as const)
: undefined,
}))
})
.filter(isDefined)
const events = resultsWithWorkspaces.map((result) => ({
name: 'New results collected',
userId: result.userId,
workspaceId: result.workspaceId,
typebotId: result.typebotId,
data: {
total: result.totalResultsYesterday,
},
})) satisfies TelemetryEvent[]
const events = resultsWithWorkspaces.map(
(result) =>
({
name: 'New results collected',
userId: result.userId,
workspaceId: result.workspaceId,
typebotId: result.typebotId,
data: {
total: result.totalResultsYesterday,
isFirstOfKind: result.isFirstOfKind,
},
} satisfies TelemetryEvent)
)
await sendTelemetryEvents(events)
console.log(`Sent ${events.length} events.`)