Files
speckle-server/packages/server/modules/notifications/graph/resolvers/userNotifications.ts
T
Daniel Gak Anagrov 3ca4a11ca3 feat(notifications): basic listener structure, notification record, delayed mechanism (#5432)
* feat: basic notification listener sturcuture

* feat: clean up generated gql

* chore: edited structure

* feat: added basic repo

* feat: ported comment email to job queue

* feat: ported stream access request accepted

* feat: added notification insertion

* fix: minor typings

* feat: delayed notifications

* updated types

* feat: fixed gql

* notifications are listed

* index on notifications

* feat: while loop skiping for update locked

* delayed notification for access request

* take into account user prefrences

* on comment view, notification is marked as read

* feat: added gql notifications

* feat: avoid raising errors

* fix: error added scopes

* fix: mr comments

* fix: cursor and service method

* feat: added stronger types to notifications and versioning logic

* minor: rows updated
2025-10-06 12:19:12 +01:00

99 lines
2.7 KiB
TypeScript

import { db } from '@/db/knex'
import type { Resolvers } from '@/modules/core/graph/generated/graphql'
import { parseNotificationToLatestVersion } from '@/modules/notifications/helpers/toLatestVersion'
import {
deleteUserNotificationsFactory,
getUserNotificationsCountFactory,
getUserNotificationsFactory,
updateUserNotificationFactory
} from '@/modules/notifications/repositories/userNotification'
import { asOperation } from '@/modules/shared/command'
import { chunk } from 'lodash-es'
const getUserNotifications = getUserNotificationsFactory({ db })
const deleteUserNotifications = deleteUserNotificationsFactory({ db })
const updateUserNotification = updateUserNotificationFactory({ db })
const getUserNotificationsCount = getUserNotificationsCountFactory({ db })
const resolvers: Resolvers = {
User: {
async notifications(parent, args) {
const [totalCount, { items, cursor }] = await Promise.all([
await getUserNotificationsCount({ userId: parent.id }),
await getUserNotifications({
userId: parent.id,
cursor: args.cursor || null,
limit: args.limit || null
})
])
return {
totalCount,
cursor,
items: items.map(parseNotificationToLatestVersion)
}
}
},
Mutation: {
notificationMutations: () => ({})
},
NotificationMutations: {
async bulkDelete(_parent, { ids }, context) {
await asOperation(
async () => {
await deleteUserNotifications({
userId: context.userId!,
ids
})
},
{
logger: context.log,
name: 'userNotificationPreferencesUpdate',
description: 'deleting user notifications'
}
)
return true
},
async bulkUpdate(_parent, args, context) {
await asOperation(
async () => {
const inputBatches = chunk(args.input, 10)
for (const batch of inputBatches) {
await Promise.all(
batch.map(({ id, read }) => {
let update = {}
if (read === false) {
update = {
read: false,
sendEmailAt: null
}
} else {
update = {
read: true
}
}
return updateUserNotification({
userId: context.userId!,
id,
update
})
})
)
}
},
{
logger: context.log,
name: 'userNotificationPreferencesUpdate',
description: 'marking user notifications as read'
}
)
return true
}
}
}
export default resolvers