Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.pnp.js
.yarn/install-state.gz
.pnpm-store
.cursor

# testing
/coverage
Expand Down
10 changes: 2 additions & 8 deletions amplify/functions/cancelPlan/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtim
import { env } from '$amplify/env/hookPlan'
import { type Schema } from '../../data/resource'

// Configurar Amplify para acceso a datos
const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(env)
Amplify.configure(resourceConfig, libraryOptions)

// Inicializar el cliente para DynamoDB (Amplify Data)
const clientSchema = generateClient<Schema>()

export const handler = async (event: any) => {
Expand Down Expand Up @@ -50,22 +48,18 @@ export const handler = async (event: any) => {
}
)
} catch (error: any) {
// Si se recibe error, verificamos si es por intentar modificar una preaprobación ya cancelada.
if (
error.response &&
error.response.data &&
typeof error.response.data.message === 'string' &&
error.response.data.message.toLowerCase().includes('cancelled preapproval')
) {
// Tratamos el error como si fuera exitoso.
response = error.response // Usamos el objeto de error.response para continuar.
response = error.response
} else {
throw error // Para otros errores, relanzamos.
throw error
}
}

// Si la respuesta no tiene status 200, pero ya fue capturado el error específico,
// podemos continuar.
const data = response.data
if (response.status !== 200 && response.status !== 400) {
return {
Expand Down
74 changes: 74 additions & 0 deletions amplify/functions/storeImages/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { env } from '$amplify/env/storeImages'
import { S3Config } from '../types/types'

export class ConfigService {
private static instance: ConfigService
private config: S3Config

private constructor() {
this.config = this.initializeConfig()
this.validateConfig()
}

public static getInstance(): ConfigService {
if (!ConfigService.instance) {
ConfigService.instance = new ConfigService()
}
return ConfigService.instance
}

private initializeConfig(): S3Config {
const bucketName = env.BUCKET_NAME || ''
const awsRegion = env.AWS_REGION_BUCKET || 'us-east-2'

let cloudFrontDomainBase = ''
if (
env.APP_ENV === 'production' &&
env.CLOUDFRONT_DOMAIN_NAME &&
env.CLOUDFRONT_DOMAIN_NAME.trim() !== ''
) {
cloudFrontDomainBase = env.CLOUDFRONT_DOMAIN_NAME.trim()
}

return {
bucketName,
awsRegion,
cloudFrontDomainBase,
}
}

private validateConfig(): void {
if (!this.config.bucketName) {
console.error(
'Error: BUCKET_NAME is not defined in the environment variables of the storeImages function.'
)
throw new Error('BUCKET_NAME is required')
}

// Advertencia si no hay región y no se usa CloudFront
if (!this.config.awsRegion && !this.config.cloudFrontDomainBase) {
console.warn(
"Warning: AWS_REGION_BUCKET is not defined. S3 URLs may default to 'us-east-2' if CloudFront is not used or not configured."
)
}

// Advertencia específica para producción sin CloudFront
if (env.APP_ENV === 'production' && !this.config.cloudFrontDomainBase) {
console.warn(
'Warning: APP_ENV is "production" but CLOUDFRONT_DOMAIN_NAME is not set. Image URLs will use S3 direct links.'
)
}
}

public getConfig(): S3Config {
return { ...this.config }
}

public generateImageUrl(s3Key: string): string {
if (this.config.cloudFrontDomainBase) {
return `https://${this.config.cloudFrontDomainBase}/${s3Key}`
}

return `https://${this.config.bucketName}.s3.${this.config.awsRegion}.amazonaws.com/${s3Key}`
}
}
Loading