|
| 1 | +import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; |
| 2 | + |
| 3 | +@Injectable() |
| 4 | +export class AirtableService { |
| 5 | + private readonly BASE_ID = 'appsibjo37dhUSTQp'; |
| 6 | + private readonly YSWS_TABLE_ID = 'tblZEEoz2V2kFJHIk'; |
| 7 | + private readonly AIRTABLE_API_KEY = process.env.USER_SERVICE_AIRTABLE_API_KEY; |
| 8 | + |
| 9 | + async createYSWSSubmission(data: { |
| 10 | + user: { |
| 11 | + firstName: string; |
| 12 | + lastName: string; |
| 13 | + email: string; |
| 14 | + birthday: Date; |
| 15 | + addressLine1: string; |
| 16 | + addressLine2?: string; |
| 17 | + city: string; |
| 18 | + state: string; |
| 19 | + country: string; |
| 20 | + zipCode: string; |
| 21 | + }; |
| 22 | + project: { |
| 23 | + projectTitle: string; |
| 24 | + description: string; |
| 25 | + playableUrl: string; |
| 26 | + repoUrl: string; |
| 27 | + screenshotUrl: string; |
| 28 | + nowHackatimeHours: number; |
| 29 | + }; |
| 30 | + submission: { |
| 31 | + description: string; |
| 32 | + playableUrl: string; |
| 33 | + repoUrl: string; |
| 34 | + screenshotUrl: string; |
| 35 | + }; |
| 36 | + }): Promise<{ recordId: string }> { |
| 37 | + if (!this.AIRTABLE_API_KEY) { |
| 38 | + throw new HttpException( |
| 39 | + 'Airtable API key not configured', |
| 40 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + const fields = { |
| 46 | + 'First Name': data.user.firstName, |
| 47 | + 'Last Name': data.user.lastName, |
| 48 | + 'Email': data.user.email, |
| 49 | + 'Birthday': data.user.birthday.toISOString().split('T')[0], |
| 50 | + 'Address (Line 1)': data.user.addressLine1, |
| 51 | + 'Address (Line 2)': data.user.addressLine2 || '', |
| 52 | + 'City': data.user.city, |
| 53 | + 'State / Province': data.user.state, |
| 54 | + 'Country': data.user.country, |
| 55 | + 'ZIP / Postal Code': data.user.zipCode, |
| 56 | + 'Code URL': data.project.repoUrl, |
| 57 | + 'Playable URL': data.project.playableUrl, |
| 58 | + 'Description': data.project.description, |
| 59 | + 'Screenshot': [ |
| 60 | + { |
| 61 | + url: data.project.screenshotUrl, |
| 62 | + filename: `screenshot-${Date.now()}.png` |
| 63 | + } |
| 64 | + ], |
| 65 | + 'Optional - Override Hours Spent': data.project.nowHackatimeHours, |
| 66 | + 'Automation - First Submitted At': new Date().toISOString(), |
| 67 | + 'Automation - Submit to Unified YSWS': true, |
| 68 | + }; |
| 69 | + |
| 70 | + const response = await fetch( |
| 71 | + `https://api.airtable.com/v0/${this.BASE_ID}/${this.YSWS_TABLE_ID}`, |
| 72 | + { |
| 73 | + method: 'POST', |
| 74 | + headers: { |
| 75 | + Authorization: `Bearer ${this.AIRTABLE_API_KEY}`, |
| 76 | + 'Content-Type': 'application/json', |
| 77 | + }, |
| 78 | + body: JSON.stringify({ |
| 79 | + records: [ |
| 80 | + { |
| 81 | + fields, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }), |
| 85 | + }, |
| 86 | + ); |
| 87 | + |
| 88 | + if (!response.ok) { |
| 89 | + const errorData = await response.json(); |
| 90 | + console.error('Airtable API error:', errorData); |
| 91 | + throw new HttpException( |
| 92 | + 'Failed to create YSWS submission record', |
| 93 | + response.status || HttpStatus.BAD_REQUEST, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + const result = await response.json(); |
| 98 | + return { recordId: result.records[0].id }; |
| 99 | + } catch (error) { |
| 100 | + console.error('Error creating YSWS submission:', error); |
| 101 | + if (error instanceof HttpException) { |
| 102 | + throw error; |
| 103 | + } |
| 104 | + throw new HttpException( |
| 105 | + 'Internal server error', |
| 106 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + async updateYSWSSubmission(recordId: string, data: { |
| 112 | + approvedHours?: number; |
| 113 | + hoursJustification?: string; |
| 114 | + status?: string; |
| 115 | + }): Promise<void> { |
| 116 | + if (!this.AIRTABLE_API_KEY) { |
| 117 | + throw new HttpException( |
| 118 | + 'Airtable API key not configured', |
| 119 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + try { |
| 124 | + const fields: any = {}; |
| 125 | + |
| 126 | + if (data.approvedHours !== undefined) { |
| 127 | + fields['Optional - Override Hours Spent'] = data.approvedHours; |
| 128 | + } |
| 129 | + |
| 130 | + if (data.hoursJustification !== undefined) { |
| 131 | + fields['Optional - Override Hours Spent Justification'] = data.hoursJustification; |
| 132 | + } |
| 133 | + |
| 134 | + const response = await fetch( |
| 135 | + `https://api.airtable.com/v0/${this.BASE_ID}/${this.YSWS_TABLE_ID}/${recordId}`, |
| 136 | + { |
| 137 | + method: 'PATCH', |
| 138 | + headers: { |
| 139 | + Authorization: `Bearer ${this.AIRTABLE_API_KEY}`, |
| 140 | + 'Content-Type': 'application/json', |
| 141 | + }, |
| 142 | + body: JSON.stringify({ |
| 143 | + fields, |
| 144 | + }), |
| 145 | + }, |
| 146 | + ); |
| 147 | + |
| 148 | + if (!response.ok) { |
| 149 | + const errorData = await response.json(); |
| 150 | + console.error('Airtable update error:', errorData); |
| 151 | + throw new HttpException( |
| 152 | + 'Failed to update YSWS submission record', |
| 153 | + response.status || HttpStatus.BAD_REQUEST, |
| 154 | + ); |
| 155 | + } |
| 156 | + } catch (error) { |
| 157 | + console.error('Error updating YSWS submission:', error); |
| 158 | + if (error instanceof HttpException) { |
| 159 | + throw error; |
| 160 | + } |
| 161 | + throw new HttpException( |
| 162 | + 'Internal server error', |
| 163 | + HttpStatus.INTERNAL_SERVER_ERROR, |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments