Remove calendar trigger (#3838)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2023-10-13 22:15:56 +06:00 committed by GitHub
parent 5e247c5d96
commit 1d50d7020c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 119 deletions

View File

@ -41,14 +41,6 @@ export function createModel (builder: Builder): void {
}
})
builder.createDoc(serverCore.class.Trigger, core.space.Model, {
trigger: serverCalendar.trigger.OnEvent,
txMatch: {
_class: core.class.TxCollectionCUD,
objectClass: calendar.class.Event
}
})
builder.mixin<Class<Doc>, ObjectDDParticipant>(
core.class.Doc,
core.class.Class,

View File

@ -13,7 +13,8 @@
// limitations under the License.
//
import calendar, { Calendar, Event, ReccuringEvent } from '@hcengineering/calendar'
import calendar, { Calendar, Event } from '@hcengineering/calendar'
import { PersonAccount } from '@hcengineering/contact'
import core, {
Class,
Doc,
@ -23,16 +24,12 @@ import core, {
Hierarchy,
Ref,
Tx,
TxCUD,
TxCreateDoc,
TxProcessor,
TxRemoveDoc,
TxUpdateDoc
TxProcessor
} from '@hcengineering/core'
import { getResource } from '@hcengineering/platform'
import { TriggerControl } from '@hcengineering/server-core'
import { getHTMLPresenter, getTextPresenter } from '@hcengineering/server-notification-resources'
import contact, { PersonAccount } from '@hcengineering/contact'
/**
* @public
@ -104,109 +101,6 @@ export async function OnPersonAccountCreate (tx: Tx, control: TriggerControl): P
return [res]
}
async function onEventCreate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const ctx = TxProcessor.extractTx(tx) as TxCreateDoc<Event>
const ev = TxProcessor.createDoc2Doc(ctx)
const res: Tx[] = []
for (const participant of ev.participants) {
const acc = (await control.modelDb.findAll(contact.class.PersonAccount, { person: participant }))[0]
if (acc === undefined) continue
if (acc._id === ev.createdBy ?? ev.modifiedBy) continue
const { _id, _class, space, modifiedBy, modifiedOn, ...data } = ev
const innerTx = control.txFactory.createTxCreateDoc(_class, `${acc._id}_calendar` as Ref<Calendar>, {
...data,
access: 'reader'
})
res.push(
control.txFactory.createTxCollectionCUD(ev.attachedToClass, ev.attachedTo, ev.space, ev.collection, innerTx)
)
}
return res
}
async function onEventUpdate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const ctx = TxProcessor.extractTx(tx) as TxUpdateDoc<Event>
const ev = (await control.findAll(calendar.class.Event, { _id: ctx.objectId }))[0]
const res: Tx[] = []
if (ev !== undefined) {
const events = await control.findAll(calendar.class.Event, { eventId: ev.eventId, _id: { $ne: ev._id } })
for (const event of events) {
const innerTx = control.txFactory.createTxUpdateDoc(event._class, event.space, event._id, ctx.operations)
res.push(
control.txFactory.createTxCollectionCUD(
event.attachedToClass,
event.attachedTo,
event.space,
event.collection,
innerTx
)
)
}
}
return res
}
async function onEventRemove (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const ctx = TxProcessor.extractTx(tx) as TxRemoveDoc<Event>
const event = control.removedMap.get(ctx.objectId)
const res: Tx[] = []
if (event !== undefined) {
const events = await control.findAll(calendar.class.Event, { eventId: (event as Event).eventId })
for (const event of events) {
const innerTx = control.txFactory.createTxRemoveDoc(event._class, event.space, event._id)
res.push(
control.txFactory.createTxCollectionCUD(
event.attachedToClass,
event.attachedTo,
event.space,
event.collection,
innerTx
)
)
}
if (event._class === calendar.class.ReccuringEvent) {
const childs = await control.findAll(calendar.class.ReccuringInstance, {
recurringEventId: (event as ReccuringEvent).eventId
})
for (const child of childs) {
const innerTx = control.txFactory.createTxRemoveDoc(child._class, child.space, child._id)
res.push(
control.txFactory.createTxCollectionCUD(
child.attachedToClass,
child.attachedTo,
child.space,
child.collection,
innerTx
)
)
}
}
}
return res
}
/**
* @public
*/
export async function OnEvent (tx: Tx, control: TriggerControl): Promise<Tx[]> {
if (tx.space === core.space.DerivedTx) return []
const ctx = TxProcessor.extractTx(tx) as TxCUD<Event>
if (!control.hierarchy.isDerived(ctx.objectClass, calendar.class.Event)) return []
switch (ctx._class) {
case core.class.TxCreateDoc:
return await onEventCreate(ctx, control)
case core.class.TxUpdateDoc:
return await onEventUpdate(ctx, control)
case core.class.TxRemoveDoc:
return await onEventRemove(ctx, control)
}
return []
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default async () => ({
function: {
@ -215,7 +109,6 @@ export default async () => ({
FindReminders
},
trigger: {
OnEvent,
OnPersonAccountCreate
}
})

View File

@ -45,7 +45,6 @@ export default plugin(serverCalendarId, {
>
},
trigger: {
OnEvent: '' as Resource<TriggerFunc>,
OnPersonAccountCreate: '' as Resource<TriggerFunc>
}
})