|
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + Injectable, |
| 4 | + InternalServerErrorException, |
| 5 | + Logger, |
| 6 | +} from '@nestjs/common'; |
| 7 | +import axios, { AxiosInstance } from 'axios'; |
| 8 | +import { |
| 9 | + DomainStatusResponseDto, |
| 10 | + DomainVerificationDto, |
| 11 | + GetDomainStatusDto, |
| 12 | +} from './dto/domain-status.dto'; |
| 13 | + |
| 14 | +interface VercelDomainVerification { |
| 15 | + type: string; |
| 16 | + domain: string; |
| 17 | + value: string; |
| 18 | + reason?: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface VercelDomainResponse { |
| 22 | + name: string; |
| 23 | + verified: boolean; |
| 24 | + verification?: VercelDomainVerification[]; |
| 25 | +} |
| 26 | + |
| 27 | +@Injectable() |
| 28 | +export class TrustPortalService { |
| 29 | + private readonly logger = new Logger(TrustPortalService.name); |
| 30 | + private readonly vercelApi: AxiosInstance; |
| 31 | + |
| 32 | + constructor() { |
| 33 | + const bearerToken = process.env.VERCEL_ACCESS_TOKEN; |
| 34 | + |
| 35 | + if (!bearerToken) { |
| 36 | + this.logger.warn('VERCEL_ACCESS_TOKEN is not set'); |
| 37 | + } |
| 38 | + |
| 39 | + // Initialize axios instance for Vercel API |
| 40 | + this.vercelApi = axios.create({ |
| 41 | + baseURL: 'https://api.vercel.com', |
| 42 | + headers: { |
| 43 | + Authorization: `Bearer ${bearerToken || ''}`, |
| 44 | + 'Content-Type': 'application/json', |
| 45 | + }, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + async getDomainStatus( |
| 50 | + dto: GetDomainStatusDto, |
| 51 | + ): Promise<DomainStatusResponseDto> { |
| 52 | + const { domain } = dto; |
| 53 | + |
| 54 | + if (!process.env.TRUST_PORTAL_PROJECT_ID) { |
| 55 | + throw new InternalServerErrorException( |
| 56 | + 'TRUST_PORTAL_PROJECT_ID is not configured', |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + if (!process.env.VERCEL_TEAM_ID) { |
| 61 | + throw new InternalServerErrorException( |
| 62 | + 'VERCEL_TEAM_ID is not configured', |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (!domain) { |
| 67 | + throw new BadRequestException('Domain is required'); |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + this.logger.log(`Fetching domain status for: ${domain}`); |
| 72 | + |
| 73 | + // Get domain information including verification status |
| 74 | + // Vercel API endpoint: GET /v9/projects/{projectId}/domains/{domain} |
| 75 | + const response = await this.vercelApi.get<VercelDomainResponse>( |
| 76 | + `/v9/projects/${process.env.TRUST_PORTAL_PROJECT_ID}/domains/${domain}`, |
| 77 | + { |
| 78 | + params: { |
| 79 | + teamId: process.env.VERCEL_TEAM_ID, |
| 80 | + }, |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + const domainInfo = response.data; |
| 85 | + |
| 86 | + const verification: DomainVerificationDto[] | undefined = |
| 87 | + domainInfo.verification?.map((v) => ({ |
| 88 | + type: v.type, |
| 89 | + domain: v.domain, |
| 90 | + value: v.value, |
| 91 | + reason: v.reason, |
| 92 | + })); |
| 93 | + |
| 94 | + return { |
| 95 | + domain: domainInfo.name, |
| 96 | + verified: domainInfo.verified ?? false, |
| 97 | + verification, |
| 98 | + }; |
| 99 | + } catch (error) { |
| 100 | + this.logger.error( |
| 101 | + `Failed to get domain status for ${domain}:`, |
| 102 | + error instanceof Error ? error.stack : error, |
| 103 | + ); |
| 104 | + |
| 105 | + // Handle axios errors with more detail |
| 106 | + if (axios.isAxiosError(error)) { |
| 107 | + const statusCode = error.response?.status; |
| 108 | + const message = error.response?.data?.error?.message || error.message; |
| 109 | + this.logger.error(`Vercel API error (${statusCode}): ${message}`); |
| 110 | + } |
| 111 | + |
| 112 | + throw new InternalServerErrorException( |
| 113 | + 'Failed to get domain status from Vercel', |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments