-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
84 lines (68 loc) · 2.23 KB
/
service.ts
File metadata and controls
84 lines (68 loc) · 2.23 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
import { eventStore } from "./store.js";
import { SqlContext } from "../../db/db.js";
import { CreateEvent, EventId, EventsQueryFilter, UpdateEvent } from "./schema.js";
import { NotFoundError, UnauthorizedError } from "../../lib/errors.js";
import { UserRole, EventState, Nullable } from "@events.comp-soc.com/shared";
export const eventService = {
async getEvents({
db,
filters,
role,
}: {
db: SqlContext;
filters: EventsQueryFilter;
role: Nullable<UserRole>;
}) {
const isCommittee = role === UserRole.Committee;
const authorisedFilters = {
...filters,
state: isCommittee ? filters.state : EventState.Published,
};
return eventStore.get({ db, filters: authorisedFilters });
},
async getEventById({
db,
data,
role,
}: {
db: SqlContext;
data: EventId;
role: Nullable<UserRole>;
}) {
const { id } = data;
const event = await eventStore.findById({ db, data });
const isCommittee = role === UserRole.Committee;
if (!event || (!isCommittee && event.state === EventState.Draft)) {
throw new NotFoundError(`Event with ${id} not found`);
}
return event;
},
async createEvent({ db, data, role }: { db: SqlContext; data: CreateEvent; role: UserRole }) {
if (role !== UserRole.Committee) {
throw new UnauthorizedError("Only committee can create an event");
}
return eventStore.create({ db, data });
},
async updateEvent({ db, data, role }: { db: SqlContext; data: UpdateEvent; role: UserRole }) {
const { id } = data;
if (role !== UserRole.Committee) {
throw new UnauthorizedError("Only committee members can update events");
}
const updated = await eventStore.update({ db, data });
if (!updated) {
throw new NotFoundError(`Event with ${id} not found`);
}
return updated;
},
async deleteEvent({ db, data, role }: { db: SqlContext; data: EventId; role: UserRole }) {
const { id } = data;
if (role !== UserRole.Committee) {
throw new UnauthorizedError("Only committee can delete an event");
}
const deleted = await eventStore.delete({ db, data });
if (!deleted) {
throw new NotFoundError(`Event with ${id} not found`);
}
return deleted;
},
};