Skip to content

Commit 0ec9055

Browse files
committed
gcal service
1 parent c5ed90c commit 0ec9055

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

apps/api/v2/src/ee/gcal/gcal.controller.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AppsRepository } from "@/modules/apps/apps.repository";
2+
import { GcalService } from "@/modules/apps/services/gcal.service";
23
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator";
34
import { AccessTokenGuard } from "@/modules/auth/guards/access-token/access-token.guard";
45
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
@@ -11,12 +12,12 @@ import {
1112
HttpCode,
1213
HttpStatus,
1314
Logger,
14-
NotFoundException,
1515
Query,
1616
Redirect,
1717
Req,
1818
UnauthorizedException,
1919
UseGuards,
20+
Headers,
2021
} from "@nestjs/common";
2122
import { ConfigService } from "@nestjs/config";
2223
import { Request } from "express";
@@ -43,27 +44,21 @@ export class GcalController {
4344
private readonly credentialRepository: CredentialsRepository,
4445
private readonly tokensRepository: TokensRepository,
4546
private readonly selectedCalendarsRepository: SelectedCalendarsRepository,
46-
private readonly config: ConfigService
47+
private readonly config: ConfigService,
48+
private readonly gcalService: GcalService
4749
) {}
4850

4951
private redirectUri = `${this.config.get("api.url")}/platform/gcal/oauth/save`;
5052

5153
@Get("/oauth/auth-url")
5254
@HttpCode(HttpStatus.OK)
5355
@UseGuards(AccessTokenGuard)
54-
async redirect(@Req() req: Request): Promise<ApiResponse<{ authUrl: string }>> {
55-
const app = await this.appRepository.getAppBySlug("google-calendar");
56-
57-
if (!app) {
58-
throw new NotFoundException();
59-
}
60-
61-
const { client_id, client_secret } = z
62-
.object({ client_id: z.string(), client_secret: z.string() })
63-
.parse(app.keys);
64-
65-
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, this.redirectUri);
66-
const accessToken = req.get("Authorization")?.replace("Bearer ", "");
56+
async redirect(
57+
@Headers("Authorization") authorization: string,
58+
@Req() req: Request
59+
): Promise<ApiResponse<{ authUrl: string }>> {
60+
const oAuth2Client = await this.gcalService.getOAuthClient(this.redirectUri);
61+
const accessToken = authorization.replace("Bearer ", "");
6762
const origin = req.get("origin") ?? req.get("host");
6863
const authUrl = oAuth2Client.generateAuthUrl({
6964
access_type: "offline",
@@ -97,16 +92,7 @@ export class GcalController {
9792
throw new UnauthorizedException("Invalid Access token.");
9893
}
9994

100-
const app = await this.appRepository.getAppBySlug("google-calendar");
101-
102-
if (!app) {
103-
throw new NotFoundException();
104-
}
105-
106-
const { client_id, client_secret } = z
107-
.object({ client_id: z.string(), client_secret: z.string() })
108-
.parse(app.keys);
109-
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, this.redirectUri);
95+
const oAuth2Client = await this.gcalService.getOAuthClient(this.redirectUri);
11096
const token = await oAuth2Client.getToken(parsedCode);
11197
const key = token.res?.data;
11298
const credential = await this.credentialRepository.createAppCredential(

apps/api/v2/src/ee/gcal/gcal.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GcalController } from "@/ee/gcal/gcal.controller";
22
import { AppsRepository } from "@/modules/apps/apps.repository";
3+
import { GcalService } from "@/modules/apps/services/gcal.service";
34
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
45
import { OAuthClientModule } from "@/modules/oauth-clients/oauth-client.module";
56
import { PrismaModule } from "@/modules/prisma/prisma.module";
@@ -10,7 +11,7 @@ import { ConfigService } from "@nestjs/config";
1011

1112
@Module({
1213
imports: [PrismaModule, TokensModule, OAuthClientModule],
13-
providers: [AppsRepository, ConfigService, CredentialsRepository, SelectedCalendarsRepository],
14+
providers: [AppsRepository, ConfigService, CredentialsRepository, SelectedCalendarsRepository, GcalService],
1415
controllers: [GcalController],
1516
})
1617
export class GcalModule {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { AppsRepository } from "@/modules/apps/apps.repository";
2+
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
3+
import { google } from "googleapis";
4+
import { z } from "zod";
5+
6+
@Injectable()
7+
export class GcalService {
8+
private logger = new Logger("GcalService");
9+
10+
constructor(private readonly appsRepository: AppsRepository) {}
11+
12+
async getOAuthClient(redirectUri: string) {
13+
this.logger.log("Getting Google Calendar OAuth Client");
14+
const app = await this.appsRepository.getAppBySlug("google-calendar");
15+
16+
if (!app) {
17+
throw new NotFoundException();
18+
}
19+
20+
const { client_id, client_secret } = z
21+
.object({ client_id: z.string(), client_secret: z.string() })
22+
.parse(app.keys);
23+
24+
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirectUri);
25+
return oAuth2Client;
26+
}
27+
}

0 commit comments

Comments
 (0)