Skip to content

Commit

Permalink
gcal service
Browse files Browse the repository at this point in the history
  • Loading branch information
ThyMinimalDev committed Jan 5, 2024
1 parent c5ed90c commit 0ec9055
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
36 changes: 11 additions & 25 deletions apps/api/v2/src/ee/gcal/gcal.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppsRepository } from "@/modules/apps/apps.repository";
import { GcalService } from "@/modules/apps/services/gcal.service";
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator";
import { AccessTokenGuard } from "@/modules/auth/guards/access-token/access-token.guard";
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
Expand All @@ -11,12 +12,12 @@ import {
HttpCode,
HttpStatus,
Logger,
NotFoundException,
Query,
Redirect,
Req,
UnauthorizedException,
UseGuards,
Headers,
} from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { Request } from "express";
Expand All @@ -43,27 +44,21 @@ export class GcalController {
private readonly credentialRepository: CredentialsRepository,
private readonly tokensRepository: TokensRepository,
private readonly selectedCalendarsRepository: SelectedCalendarsRepository,
private readonly config: ConfigService
private readonly config: ConfigService,
private readonly gcalService: GcalService
) {}

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

@Get("/oauth/auth-url")
@HttpCode(HttpStatus.OK)
@UseGuards(AccessTokenGuard)
async redirect(@Req() req: Request): Promise<ApiResponse<{ authUrl: string }>> {
const app = await this.appRepository.getAppBySlug("google-calendar");

if (!app) {
throw new NotFoundException();
}

const { client_id, client_secret } = z
.object({ client_id: z.string(), client_secret: z.string() })
.parse(app.keys);

const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, this.redirectUri);
const accessToken = req.get("Authorization")?.replace("Bearer ", "");
async redirect(
@Headers("Authorization") authorization: string,
@Req() req: Request
): Promise<ApiResponse<{ authUrl: string }>> {
const oAuth2Client = await this.gcalService.getOAuthClient(this.redirectUri);
const accessToken = authorization.replace("Bearer ", "");
const origin = req.get("origin") ?? req.get("host");
const authUrl = oAuth2Client.generateAuthUrl({
access_type: "offline",
Expand Down Expand Up @@ -97,16 +92,7 @@ export class GcalController {
throw new UnauthorizedException("Invalid Access token.");
}

const app = await this.appRepository.getAppBySlug("google-calendar");

if (!app) {
throw new NotFoundException();
}

const { client_id, client_secret } = z
.object({ client_id: z.string(), client_secret: z.string() })
.parse(app.keys);
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, this.redirectUri);
const oAuth2Client = await this.gcalService.getOAuthClient(this.redirectUri);
const token = await oAuth2Client.getToken(parsedCode);
const key = token.res?.data;
const credential = await this.credentialRepository.createAppCredential(
Expand Down
3 changes: 2 additions & 1 deletion apps/api/v2/src/ee/gcal/gcal.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GcalController } from "@/ee/gcal/gcal.controller";
import { AppsRepository } from "@/modules/apps/apps.repository";
import { GcalService } from "@/modules/apps/services/gcal.service";
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
import { OAuthClientModule } from "@/modules/oauth-clients/oauth-client.module";
import { PrismaModule } from "@/modules/prisma/prisma.module";
Expand All @@ -10,7 +11,7 @@ import { ConfigService } from "@nestjs/config";

@Module({
imports: [PrismaModule, TokensModule, OAuthClientModule],
providers: [AppsRepository, ConfigService, CredentialsRepository, SelectedCalendarsRepository],
providers: [AppsRepository, ConfigService, CredentialsRepository, SelectedCalendarsRepository, GcalService],
controllers: [GcalController],
})
export class GcalModule {}
27 changes: 27 additions & 0 deletions apps/api/v2/src/modules/apps/services/gcal.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { AppsRepository } from "@/modules/apps/apps.repository";
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
import { google } from "googleapis";
import { z } from "zod";

@Injectable()
export class GcalService {
private logger = new Logger("GcalService");

constructor(private readonly appsRepository: AppsRepository) {}

async getOAuthClient(redirectUri: string) {
this.logger.log("Getting Google Calendar OAuth Client");
const app = await this.appsRepository.getAppBySlug("google-calendar");

if (!app) {
throw new NotFoundException();
}

const { client_id, client_secret } = z
.object({ client_id: z.string(), client_secret: z.string() })
.parse(app.keys);

const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirectUri);
return oAuth2Client;
}
}

0 comments on commit 0ec9055

Please sign in to comment.