-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathevent.controller.ts
More file actions
166 lines (143 loc) · 4.91 KB
/
event.controller.ts
File metadata and controls
166 lines (143 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { ObjectId } from "mongodb";
import { SessionRequest } from "supertokens-node/framework/express";
import { ID_OPTIMISTIC_PREFIX } from "@core/constants/core.constants";
import { Status } from "@core/errors/status.codes";
import { Logger } from "@core/logger/winston.logger";
import {
CompassCoreEventSchema,
CompassEvent,
CompassEventStatus,
Params_DeleteMany,
Payload_Order,
RecurringEventUpdateScope,
Schema_Event,
} from "@core/types/event.types";
import { Res_Promise, SReqBody } from "@backend/common/types/express.types";
import eventService from "@backend/event/services/event.service";
import { CompassSyncProcessor } from "@backend/sync/services/sync/compass.sync.processor";
const logger = Logger("app.event.controllers.event.controller");
class EventController {
private async processEvents(_events: CompassEvent[]) {
const events = _events.map((e) => ({
...e,
payload: CompassCoreEventSchema.parse({
...e.payload,
_id:
e.payload._id?.replace(`${ID_OPTIMISTIC_PREFIX}-`, "") ??
new ObjectId().toString(),
}),
})) as CompassEvent[];
await CompassSyncProcessor.processEvents(events);
}
create = async (
req: SReqBody<CompassEvent["payload"] | CompassEvent["payload"][]>,
res: Res_Promise,
) => {
try {
const { body } = req;
const user = req.session?.getUserId() as string;
// Handle both single object and array cases
const events = Array.isArray(body) ? body : [body];
console.log(events);
await this.processEvents(
events.map((e) => ({
payload: { ...e, user },
status: CompassEventStatus.CONFIRMED,
applyTo: RecurringEventUpdateScope.THIS_EVENT,
})) as CompassEvent[],
);
res.status(Status.NO_CONTENT).send();
} catch (e) {
logger.error(e);
res.status(Status.BAD_REQUEST).send();
}
};
delete = async (req: SessionRequest, res: Res_Promise) => {
try {
const { query } = req;
const user = req.session?.getUserId() as string;
const _id = req.params["id"] as string;
const event = await eventService.readById(user, _id);
const applyTo = query["applyTo"] ?? RecurringEventUpdateScope.THIS_EVENT;
await this.processEvents([
{
payload: event as CompassEvent["payload"],
status: CompassEventStatus.CANCELLED,
applyTo: applyTo as RecurringEventUpdateScope.THIS_EVENT,
},
]);
res.status(Status.NO_CONTENT).send();
} catch (e) {
logger.error(e);
res.status(Status.BAD_REQUEST).send();
}
};
deleteAllByUser = async (req: SessionRequest, res: Res_Promise) => {
const userToRemove = req.params["userId"] as string;
try {
const deleteAllRes = await eventService.deleteAllByUser(userToRemove);
res.promise(deleteAllRes);
} catch (e) {
res.promise(Promise.reject(e));
}
};
deleteMany = async (req: SReqBody<Params_DeleteMany>, res: Res_Promise) => {
const userId = req.session?.getUserId() as string;
try {
const deleteResponse = await eventService.deleteMany(userId, req.body);
res.promise(deleteResponse);
} catch (e) {
res.promise(Promise.reject(e));
}
};
readById = async (req: SessionRequest, res: Res_Promise) => {
const userId = req.session?.getUserId() as string;
const eventId = req.params["id"] as string;
try {
const response = await eventService.readById(userId, eventId);
res.promise(response);
} catch (e) {
res.promise(Promise.reject(e));
}
};
readAll = async (req: SessionRequest, res: Res_Promise) => {
const userId = req.session?.getUserId() as string;
try {
const usersEvents = await eventService.readAll(userId, req.query);
res.promise(usersEvents);
} catch (e) {
res.promise(Promise.reject(e));
}
};
reorder = async (req: SReqBody<Payload_Order[]>, res: Res_Promise) => {
try {
const userId = req.session?.getUserId() as string;
const newOrder = req.body;
const result = await eventService.reorder(userId, newOrder);
res.promise(result);
} catch (e) {
res.promise(Promise.reject(e));
}
};
update = async (req: SReqBody<Schema_Event>, res: Res_Promise) => {
try {
const { body, query, params, session } = req;
const user = session?.getUserId() as string;
const _id = params["id"] as string;
const payload = { ...body, user, _id } as CompassEvent["payload"];
const applyTo = query["applyTo"] as RecurringEventUpdateScope.THIS_EVENT;
await this.processEvents([
{
payload,
status: CompassEventStatus.CONFIRMED,
applyTo: applyTo ?? RecurringEventUpdateScope.THIS_EVENT,
},
]);
res.status(Status.NO_CONTENT).send();
} catch (e) {
logger.error(e);
res.status(Status.BAD_REQUEST).send();
}
};
}
export default new EventController();