mirror of
https://github.com/hcengineering/platform.git
synced 2026-10-01 05:55:09 +02:00
1686 fix (#1690)
Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
@@ -14,8 +14,38 @@
|
||||
//
|
||||
|
||||
import contact, { Channel } from '@anticrm/contact'
|
||||
import gmail from '@anticrm/gmail'
|
||||
import { Class, Doc, DocumentQuery, FindOptions, FindResult, Hierarchy, Ref } from '@anticrm/core'
|
||||
import core, {
|
||||
AttachedDoc,
|
||||
Class,
|
||||
Doc,
|
||||
DocumentQuery,
|
||||
FindOptions,
|
||||
FindResult,
|
||||
Hierarchy,
|
||||
Ref,
|
||||
Tx,
|
||||
TxCollectionCUD,
|
||||
TxCreateDoc,
|
||||
TxProcessor
|
||||
} from '@anticrm/core'
|
||||
import gmail, { Message } from '@anticrm/gmail'
|
||||
import { TriggerControl } from '@anticrm/server-core'
|
||||
|
||||
const extractTx = (tx: Tx): Tx => {
|
||||
if (tx._class === core.class.TxCollectionCUD) {
|
||||
const ctx = tx as TxCollectionCUD<Doc, AttachedDoc>
|
||||
if (ctx.tx._class === core.class.TxCreateDoc) {
|
||||
const create = ctx.tx as TxCreateDoc<AttachedDoc>
|
||||
create.attributes.attachedTo = ctx.objectId
|
||||
create.attributes.attachedToClass = ctx.objectClass
|
||||
create.attributes.collection = ctx.collection
|
||||
return create
|
||||
}
|
||||
return ctx.tx
|
||||
}
|
||||
|
||||
return tx
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
@@ -33,13 +63,45 @@ export async function FindMessages (
|
||||
if (channel.provider !== contact.channelProvider.Email) {
|
||||
return []
|
||||
}
|
||||
const messages = await findAll(gmail.class.Message, { attachedTo: doc._id })
|
||||
const newMessages = await findAll(gmail.class.NewMessage, { attachedTo: doc._id })
|
||||
const messages = await findAll(gmail.class.Message, { attachedTo: channel._id })
|
||||
const newMessages = await findAll(gmail.class.NewMessage, { attachedTo: channel._id })
|
||||
return [...messages, ...newMessages]
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function OnMessageCreate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const actualTx = extractTx(tx)
|
||||
if (actualTx._class !== core.class.TxCreateDoc) {
|
||||
return []
|
||||
}
|
||||
|
||||
const createTx = tx as TxCreateDoc<Message>
|
||||
|
||||
if (!control.hierarchy.isDerived(createTx.objectClass, gmail.class.Message)) {
|
||||
return []
|
||||
}
|
||||
const message = TxProcessor.createDoc2Doc<Message>(createTx)
|
||||
|
||||
const channel = (await control.findAll(contact.class.Channel, { _id: message.attachedTo }, { limit: 1 }))[0]
|
||||
if (channel === undefined) {
|
||||
return []
|
||||
}
|
||||
if (channel.lastMessage === undefined || channel.lastMessage < message.sendOn) {
|
||||
const tx = control.txFactory.createTxUpdateDoc(channel._class, channel.space, channel._id, {
|
||||
lastMessage: message.sendOn
|
||||
})
|
||||
return [tx]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||
export default async () => ({
|
||||
trigger: {
|
||||
OnMessageCreate
|
||||
},
|
||||
function: {
|
||||
FindMessages
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user