11import { ObjectId } from "mongodb" ;
22import { SessionRequest } from "supertokens-node/framework/express" ;
3+ import { ID_OPTIMISTIC_PREFIX } from "@core/constants/core.constants" ;
34import { Status } from "@core/errors/status.codes" ;
45import { Logger } from "@core/logger/winston.logger" ;
56import {
6- CompassCoreEvent ,
77 CompassCoreEventSchema ,
88 CompassEvent ,
99 CompassEventStatus ,
@@ -19,24 +19,17 @@ import { CompassSyncProcessor } from "@backend/sync/services/sync/compass.sync.p
1919const logger = Logger ( "app.event.controllers.event.controller" ) ;
2020
2121class EventController {
22- private async processEvent (
23- _payload : Omit < CompassEvent [ "payload" ] , "user" > ,
24- status ?: CompassEventStatus ,
25- applyTo : RecurringEventUpdateScope = RecurringEventUpdateScope . THIS_EVENT ,
26- ) {
27- const payload = CompassCoreEventSchema . parse ( _payload ) ;
28-
29- const event = {
30- status : status ?? CompassEventStatus . CONFIRMED ,
31- payload,
32- applyTo,
33- } as CompassEvent ;
34-
35- await CompassSyncProcessor . processEvents ( [ event ] ) ;
22+ private async processEvents ( _events : CompassEvent [ ] ) {
23+ const events = _events . map ( ( e ) => ( {
24+ ...e ,
25+ payload : CompassCoreEventSchema . parse ( e . payload ) ,
26+ } ) ) as CompassEvent [ ] ;
27+
28+ await CompassSyncProcessor . processEvents ( events ) ;
3629 }
3730
3831 create = async (
39- req : SReqBody < CompassCoreEvent | CompassCoreEvent [ ] > ,
32+ req : SReqBody < CompassEvent [ "payload" ] | CompassEvent [ "payload" ] [ ] > ,
4033 res : Res_Promise ,
4134 ) => {
4235 try {
@@ -46,19 +39,19 @@ class EventController {
4639 // Handle both single object and array cases
4740 const events = Array . isArray ( body ) ? body : [ body ] ;
4841
49- // Process each event
50- for ( const eventData of events ) {
51- const _id = new ObjectId ( ) . toString ( ) ;
52- const event = { ...eventData , _id , user } ;
53-
54- const safeEvent = CompassCoreEventSchema . parse ( event ) ;
55-
56- await this . processEvent (
57- safeEvent ,
58- CompassEventStatus . CONFIRMED ,
59- RecurringEventUpdateScope . THIS_EVENT ,
60- ) ;
61- }
42+ await this . processEvents (
43+ events . map ( ( e ) => ( {
44+ payload : {
45+ ...e ,
46+ _id :
47+ e . _id ?. replace ( ` ${ ID_OPTIMISTIC_PREFIX } ` , "" ) ??
48+ new ObjectId ( ) . toString ( ) ,
49+ user ,
50+ } ,
51+ status : CompassEventStatus . CONFIRMED ,
52+ applyTo : RecurringEventUpdateScope . THIS_EVENT ,
53+ } ) ) as CompassEvent [ ] ,
54+ ) ;
6255
6356 res . status ( Status . NO_CONTENT ) . send ( ) ;
6457 } catch ( e ) {
@@ -70,16 +63,19 @@ class EventController {
7063
7164 delete = async ( req : SessionRequest , res : Res_Promise ) => {
7265 try {
66+ const { query } = req ;
7367 const user = req . session ?. getUserId ( ) as string ;
7468 const _id = req . params [ "id" ] as string ;
7569 const event = await eventService . readById ( user , _id ) ;
70+ const applyTo = query [ "applyTo" ] ?? RecurringEventUpdateScope . THIS_EVENT ;
7671
77- await this . processEvent (
78- event as CompassEvent [ "payload" ] ,
79- CompassEventStatus . CANCELLED ,
80- ( req . query [ "applyTo" ] as RecurringEventUpdateScope ) ??
81- RecurringEventUpdateScope . THIS_EVENT ,
82- ) ;
72+ await this . processEvents ( [
73+ {
74+ payload : event as CompassEvent [ "payload" ] ,
75+ status : CompassEventStatus . CANCELLED ,
76+ applyTo : applyTo as RecurringEventUpdateScope . THIS_EVENT ,
77+ } ,
78+ ] ) ;
8379
8480 res . status ( Status . NO_CONTENT ) . send ( ) ;
8581 } catch ( e ) {
@@ -146,13 +142,16 @@ class EventController {
146142 const { body, query, params, session } = req ;
147143 const user = session ?. getUserId ( ) as string ;
148144 const _id = params [ "id" ] as string ;
149-
150- await this . processEvent (
151- { ...body , user, _id } as CompassEvent [ "payload" ] ,
152- CompassEventStatus . CONFIRMED ,
153- ( query [ "applyTo" ] as RecurringEventUpdateScope ) ??
154- RecurringEventUpdateScope . THIS_EVENT ,
155- ) ;
145+ const payload = { ...body , user, _id } as CompassEvent [ "payload" ] ;
146+ const applyTo = query [ "applyTo" ] as RecurringEventUpdateScope . THIS_EVENT ;
147+
148+ await this . processEvents ( [
149+ {
150+ payload,
151+ status : CompassEventStatus . CONFIRMED ,
152+ applyTo : applyTo ?? RecurringEventUpdateScope . THIS_EVENT ,
153+ } ,
154+ ] ) ;
156155
157156 res . status ( Status . NO_CONTENT ) . send ( ) ;
158157 } catch ( e ) {
0 commit comments