Skip to content

Commit 57ee145

Browse files
🐛 fix(calendar-event-transiton): calendar event to someday fix ba… (#974)
1 parent 0515260 commit 57ee145

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

packages/backend/src/event/controllers/event.controller.ts

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ObjectId } from "mongodb";
22
import { SessionRequest } from "supertokens-node/framework/express";
3+
import { ID_OPTIMISTIC_PREFIX } from "@core/constants/core.constants";
34
import { Status } from "@core/errors/status.codes";
45
import { Logger } from "@core/logger/winston.logger";
56
import {
6-
CompassCoreEvent,
77
CompassCoreEventSchema,
88
CompassEvent,
99
CompassEventStatus,
@@ -19,24 +19,17 @@ import { CompassSyncProcessor } from "@backend/sync/services/sync/compass.sync.p
1919
const logger = Logger("app.event.controllers.event.controller");
2020

2121
class 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) {

packages/core/src/constants/core.constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const SOMEDAY_WEEKLY_LIMIT = 9;
99
export const SOMEDAY_WEEK_LIMIT_MSG = `Sorry, you can only have ${SOMEDAY_WEEKLY_LIMIT} unscheduled events per week.`;
1010
export const SOMEDAY_MONTH_LIMIT_MSG = `Sorry, you can only have ${SOMEDAY_MONTHLY_LIMIT} unscheduled events per month.`;
1111
export const SYNC_DEBUG = "/api/sync/debug";
12+
export const ID_OPTIMISTIC_PREFIX = "optimistic";
1213

1314
export enum NodeEnv {
1415
Development = "development",

0 commit comments

Comments
 (0)