Files
speckle-server/packages/server/modules/activitystream/services/summary.ts
T
Gergő Jedlicska d1d5984e30 gergo/summaryEmails (#979)
* refactor(server emails): email transports module refactor to TypeScript

* refactor(docker-compose deps): move local email server to common dev compose file

* chore(server launch.json): add ts-node script running example

* chore(server deps): add nodemailer types package

* refactor(server activitystream): add strongly typed activity definitions

* feat(server activitystream): add activity repository

* feat(server info): add canonical url on the service level

* feat(server): add static file serving route to server core

* feat(server): add dependencies for periodical email digests

* feat(server activity stream): call the initialization step from the activity stream module

* feat(server activity digest): add WIP weekly email digest implementation

* feat(server digest email): smul upgrades and fixes to the email template and its contents

* just for Fabs to test

* chore(root package.json): remove deleted docker-compose references

* feat(frontend profile): add notification preferences panel

* feat(server digest emails): set prod ready cron tab and timespan

* refactor(server email digest): move templates into the email module

* refactor(server activity digests): refactor to use notifications infrastructure

* test(server activities): add tests and some refactor to activities and notification preferences

* refactor(notification preferences): fix minor issues

* test(server notification preferences test): fix describe nesting

* fix(server activities): add missing action types

* fix(server activities): fix errors after merging main

* test(server activity notifications): add test coverage for activity notifications service

* refactor(server activities): fixing tests and some cleanup

* feat(server cli): add summary notification command to cli

* chore(dev env db versions): upgrade local dev env versions

* chore(server deps): upgrade local dev db to pg 14

* fix(docker-compose): bind maildev to localhost

* process-scoped notifications test queues

* test(activity tests): add  sleep to fix flaky CI

* feat(activity digests): add demo date for digest trigger

* feat(activity digest): add UK timezone trigger date

Co-authored-by: Iain Sproat <68657+iainsproat@users.noreply.github.com>
Co-authored-by: Fabians <fabis94@live.com>
2022-09-09 12:46:57 +02:00

72 lines
1.9 KiB
TypeScript

import {
getActivity,
getActiveUserStreams,
UserStreams
} from '@/modules/activitystream/repositories'
import { StreamScopeActivity } from '@/modules/activitystream/helpers/types'
import {
NotificationPublisher,
NotificationType
} from '@/modules/notifications/helpers/types'
import { StreamRecord, UserRecord } from '@/modules/core/helpers/types'
import { getUser } from '@/modules/core/repositories/users'
import { getStream } from '@/modules/core/services/streams'
export type StreamActivitySummary = {
stream: StreamRecord | null
activity: StreamScopeActivity[]
}
export type ActivitySummary = {
user: UserRecord
streamActivities: StreamActivitySummary[]
}
export const createActivitySummary = async (
userId: string,
streamIds: string[],
start: Date,
end: Date
): Promise<ActivitySummary | null> => {
const streamActivities = (
await Promise.all(
streamIds.map(async (streamId) => {
return {
stream: (await getStream({ streamId, userId })) ?? null,
activity: await getActivity(streamId, start, end, null) //userId is null for now, to not filter out any activity
}
})
)
).filter((sa) => sa.activity.length)
const user = await getUser(userId)
if (!user) return null
return {
user,
streamActivities
}
}
export const sendActivityNotifications = async (
start: Date,
end: Date,
notificationPublisher: NotificationPublisher,
userActiveStreamsLookup: (
start: Date,
end: Date
) => Promise<UserStreams[]> = getActiveUserStreams
): Promise<void> => {
const activeUserStreams = await userActiveStreamsLookup(start, end)
await Promise.all(
activeUserStreams.map((userStreams) =>
notificationPublisher(NotificationType.ActivityDigest, {
targetUserId: userStreams.userId,
data: {
streamIds: userStreams.streamIds,
start,
end
}
})
)
)
}