-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.ts
More file actions
55 lines (42 loc) · 2.03 KB
/
controller.ts
File metadata and controls
55 lines (42 loc) · 2.03 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
import { Context, TypedResponse } from "hono";
import { IQuickbooksService } from "./service";
import { RedirectEndpointParams } from "../../types/quickbooks";
import { ControllerResponse } from "../../utilities/response";
import Boom from "@hapi/boom";
import { validate } from "uuid";
export interface IQuickbooksController {
redirectToAuthorization(ctx: Context): ControllerResponse<TypedResponse<{ url: string }, 200>>;
generateSession(ctx: Context): Promise<Response>;
updateUnprocessedInvoices(ctx: Context): ControllerResponse<TypedResponse<{ success: true }, 200>>;
importQuickbooksData(ctx: Context): ControllerResponse<TypedResponse<{ success: true }, 201>>;
}
export class QuickbooksController implements IQuickbooksController {
constructor(private service: IQuickbooksService) {}
async redirectToAuthorization(ctx: Context) {
const userId = ctx.get("userId");
const { url } = await this.service.generateAuthUrl({ userId });
return ctx.json({ url }, 200);
}
async generateSession(ctx: Context) {
const params = RedirectEndpointParams.parse(ctx.req.query());
if ("error" in params) {
await this.service.consumeOAuthState({ state: params.state });
return ctx.json({ error: "Did not approve" }, 400);
}
await this.service.createQuickbooksSession(params);
return ctx.redirect(`${process.env.FRONTEND_URL || "http://localhost:3000"}`);
}
async updateUnprocessedInvoices(ctx: Context) {
const userId = ctx.get("userId");
await this.service.updateUnprocessedInvoices({ userId });
return ctx.json({ success: true }, 200);
}
async importQuickbooksData(ctx: Context) {
const userId = ctx.get("userId");
if (!userId || typeof userId !== "string" || userId.trim() === "" || !validate(userId)) {
throw Boom.badRequest("Invalid or missing userId");
}
await this.service.importQuickbooksData({ userId });
return ctx.json({ success: true }, 201);
}
}