Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

3.3 KB
Raw Blame
import {
  Controller,
  Post,
  Get,
  Delete,
  Param,
  Body,
  UseGuards,
  BadRequestException,
  HttpCode,
  Ip,
  Headers,
} from '@nestjs/common'
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'

import { AuthenticationGuard } from '../auth/guards/authentication.guard'
import { Auth } from '../auth/decorators'
import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'
import { GSCService } from './gsc.service'
import { ProjectService } from './project.service'
import { trackCustom } from '../common/analytics'
import { getIPFromHeaders } from '../common/utils'

@ApiTags('Project - Google Search Console')
@UseGuards(AuthenticationGuard)
@Controller({ path: 'project/gsc', version: '1' })
export class GSCController {
  constructor(
    private readonly gscService: GSCService,
    private readonly projectService: ProjectService,
  ) {}

  @Post('process-token')
  @Auth()
  async processGSCToken(
    @Body() body: { code: string; state: string },
    @CurrentUserId() uid: string,
    @Headers() headers: Record<string, string>,
    @Ip() requestIp: string,
  ) {
    const ip = getIPFromHeaders(headers) || requestIp || ''
    const { code, state } = body

    if (!code || !state) {
      throw new BadRequestException('Invalid GSC token parameters')
    }

    const { pid } = await this.gscService.handleOAuthCallback(uid, code, state)

    await trackCustom(ip, headers['user-agent'], {
      ev: 'GSC_CONNECTED',
    })

    return { pid }
  }

  @ApiBearerAuth()
  @Post(':pid/connect')
  @Auth()
  async connect(@Param('pid') pid: string, @CurrentUserId() uid: string) {
    const project = await this.projectService.getRedisProject(pid)
    this.projectService.allowedToManage(project, uid)
    return this.gscService.generateConnectURL(uid, pid)
  }

  @ApiBearerAuth()
  @Get(':pid/status')
  @Auth()
  async status(@Param('pid') pid: string, @CurrentUserId() uid: string) {
    const project = await this.projectService.getRedisProject(pid)
    this.projectService.allowedToManage(project, uid)
    return this.gscService.getStatus(pid)
  }

  @ApiBearerAuth()
  @Get(':pid/properties')
  @Auth()
  async properties(@Param('pid') pid: string, @CurrentUserId() uid: string) {
    const project = await this.projectService.getRedisProject(pid)
    this.projectService.allowedToManage(project, uid)
    return this.gscService.listSites(pid)
  }

  @ApiBearerAuth()
  @Post(':pid/property')
  @Auth()
  async setProperty(
    @Param('pid') pid: string,
    @CurrentUserId() uid: string,
    @Body() body: { propertyUri: string },
  ) {
    const project = await this.projectService.getRedisProject(pid)
    this.projectService.allowedToManage(project, uid)
    await this.gscService.setProperty(pid, body?.propertyUri)
    return {}
  }

  @ApiBearerAuth()
  @Delete(':pid/disconnect')
  @Auth()
  @HttpCode(204)
  async disconnect(
    @Param('pid') pid: string,
    @CurrentUserId() uid: string,
    @Headers() headers: Record<string, string>,
    @Ip() requestIp: string,
  ) {
    const ip = getIPFromHeaders(headers) || requestIp || ''

    const project = await this.projectService.getRedisProject(pid)
    this.projectService.allowedToManage(project, uid)
    await this.gscService.disconnect(pid)

    await trackCustom(ip, headers['user-agent'], {
      ev: 'GSC_DISCONNECTED',
    })
  }
}