generated from staart/api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
157 lines (146 loc) · 4.62 KB
/
index.ts
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
import {
Controller,
Get,
Post,
Middleware,
Request,
Response,
} from "@staart/server";
import { joiValidate, Joi } from "@staart/validate";
import { AddressObject } from "mailparser";
import { authHandler, validator } from "../../_staart/helpers/middleware";
import { classifyTokens } from "../../ara/services/classify";
import { smartTokensFromText } from "../../ara/services/tokenize";
import { parseEmail } from "../../ara/services/parse";
import { organizationUsernameToId } from "../../_staart/helpers/utils";
import {
trackOutgoingEmail,
getPublicMeetingDetails,
trackAnalyticsEvent,
} from "../../ara/rest";
import { ApiKeyResponse } from "../../_staart/helpers/jwt";
import { confirmMeetingForGuest } from "../../ara/services/crud/confirm-meeting";
import { safeRedirect } from "../../_staart/helpers/utils";
import { googleCalendarClient } from "../../ara/services/calendar-connection";
import { BASE_URL } from "../../config";
import { stringify } from "querystring";
import { getFullToken } from "../../ara/services/short-token";
export class ApiController {
@Post("classify")
@Middleware(authHandler)
async classify(req: Request) {
const text: string[] = req.body.text;
joiValidate({ text: Joi.array().required() }, { text });
return classifyTokens(text, () => {});
}
@Post("parse-email")
@Middleware(authHandler)
async parseEmail(req: Request) {
const text: string = req.body.text;
joiValidate({ text: Joi.array().required() }, { text });
return parseEmail(text);
}
@Post("smart-tokenize")
@Middleware(authHandler)
async smartTokenize(req: Request) {
const text: string = req.body.text;
const from: AddressObject = req.body.from;
joiValidate(
{ text: Joi.string().required(), from: Joi.object() },
{ text, from }
);
return smartTokensFromText(text, from);
}
@Post("perform-action")
@Middleware(authHandler)
async performAction(req: Request, res: Response) {
const token: ApiKeyResponse = res.locals.token;
const text: string = req.body.text;
const organizationId = token.organizationId;
joiValidate(
{
text: Joi.string().required(),
organizationId: Joi.string().required(),
},
{ text, organizationId }
);
return {};
// return performAction(organizationId, req.body, text, () => {});
}
@Get("read-receipt")
@Middleware(validator({ token: Joi.string() }, "query"))
async readReceiptEmail(req: Request, res: Response) {
const token = req.query.token as string;
trackOutgoingEmail(token)
.then(() => {})
.catch(() => {});
res.writeHead(200, { "Content-Type": "image/gif" });
res.end(
Buffer.from("R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=", "base64"),
"binary"
);
}
@Get("meeting-page/:username/:id")
@Middleware(
validator(
{ username: Joi.string().required(), id: Joi.string().required() },
"params"
)
)
@Middleware(validator({ jwt: Joi.string().required() }, "query"))
async getMeetingDetails(req: Request) {
return getPublicMeetingDetails(
req.params.username,
req.params.id,
req.query.jwt
);
}
@Post("confirm-meeting/:organizaionId/:meetingId")
@Middleware(
validator(
{
token: Joi.string().required(),
guestName: Joi.string().required(),
guestEmail: Joi.string().required(),
guestTimezone: Joi.string().required(),
duration: Joi.number().required(),
selectedDatetime: Joi.any().required(),
},
"body"
)
)
async confirmMeeting(req: Request) {
const id = await organizationUsernameToId(req.params.organizaionId);
return confirmMeetingForGuest(id, req.params.meetingId, req.body);
}
@Post("track/:index")
async track(req: Request, res: Response) {
const index = req.params.index;
const data = req.body;
joiValidate(
{
index: Joi.string().required(),
data: Joi.object().required(),
},
{ index, data }
);
trackAnalyticsEvent(res.locals, index, data)
.then(() => {})
.catch(() => {});
return { queued: true };
}
@Get("calendar-connection/google")
async getOAuthUrlGoogleCalendar(req: Request, res: Response) {
safeRedirect(req, res, googleCalendarClient.client.code.getUri());
}
@Get("calendar-connection/google/callback")
async getOAuthCallbackGoogleCalendar(req: Request, res: Response) {
return googleCalendarClient.callback(
`${BASE_URL}/auth${req.path}?${stringify(req.query)}`
);
}
@Get("full-token/:token")
async getFullToken(req: Request, res: Response) {
return getFullToken(req.params.token ?? "");
}
}