-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticipants-service.ts
More file actions
87 lines (71 loc) · 1.92 KB
/
participants-service.ts
File metadata and controls
87 lines (71 loc) · 1.92 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
import { eq } from "drizzle-orm";
import { Err, Ok, Result } from "ts-results";
import { uuid } from "../../../app/common";
import { db } from "../../../db";
import { participants } from "../../../db/schema";
import { Participant } from "../models/Participant";
export const findByUuid = async (
id: string,
): Promise<Result<Participant, Error>> => {
if (!uuid.isValid(id)) {
return Err(new Error("Invalid UUID"));
}
const result = await db
.select()
.from(participants)
.where(eq(participants.uuid, id));
if (result.length === 0) {
return Err(new Error("Participant not found"));
}
const participant = await convert(result[0]);
return Ok(participant);
};
export const findById = async (
id: number,
): Promise<Result<Participant, Error>> => {
const result = await db
.select()
.from(participants)
.where(eq(participants.id, id));
if (result.length === 0) {
return Err(new Error("Participant not found"));
}
const participant = await convert(result[0]);
return Ok(participant);
};
export const convert = async (result: any): Promise<Participant> => {
const participant = new Participant({
id: result.id,
uuid: result.uuid,
email: result.email,
});
return participant;
};
export const findByEmail = async (
email?: string,
): Promise<Result<Participant, Error>> => {
return Err(new Error("Not implemented yet"));
};
export const create = async (
email: string,
): Promise<Result<Participant, Error>> => {
const id = uuid.create();
// TODO: add a regex to validate email
try {
const result = await db
.insert(participants)
.values({
uuid: id,
email: email,
})
.returning();
const participant = new Participant({
id: result[0].id,
uuid: result[0].uuid,
email: result[0].email,
});
return Ok(participant);
} catch (e) {
return Err(new Error("Failed to create participant"));
}
};