platform/services/github/pod-github/src/notifications.ts
Andrey Sobolev ddecae80dd
Move services to public (#6156)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2024-07-28 14:55:43 +07:00

49 lines
1.7 KiB
TypeScript

import { Account, Doc, Ref, TxOperations } from '@hcengineering/core'
import notification, { DocNotifyContext } from '@hcengineering/notification'
import { IntlString } from '@hcengineering/platform'
import github from '@hcengineering/github'
export async function createNotification (
client: TxOperations,
forDoc: Doc,
data: { user: Ref<Account>, message: IntlString, props: Record<string, any> }
): Promise<void> {
let docNotifyContext = await client.findOne(notification.class.DocNotifyContext, { attachedTo: forDoc._id })
if (docNotifyContext?._id === undefined) {
const docNotifyContextId = await client.createDoc(notification.class.DocNotifyContext, forDoc.space, {
attachedTo: forDoc._id,
attachedToClass: forDoc._class,
hidden: false,
user: data.user,
isPinned: false
})
docNotifyContext = await client.findOne(notification.class.DocNotifyContext, { _id: docNotifyContextId })
}
// Check if we had already same notification send, and just unmark it viewed.
const existing = await client.findOne(notification.class.CommonInboxNotification, {
user: data.user,
message: data.message,
props: data.props
})
if (existing !== undefined) {
await client.update(docNotifyContext as DocNotifyContext, {
lastUpdateTimestamp: Date.now()
})
await client.update(existing, {
isViewed: false
})
} else {
await client.createDoc(notification.class.CommonInboxNotification, forDoc.space, {
user: data.user,
icon: github.icon.Github,
message: data.message,
props: data.props,
isViewed: false,
docNotifyContext: docNotifyContext?._id as Ref<DocNotifyContext>
})
}
}