import { json } from 'remix' ; import { getErrorMessage } from '~/utils/misc' ; import { deleteConvertKitCache } from '~/utils/user-info.server' ; import * as ck from './convertkit.server' ; import type { Errors , Fields , ActionData } from './types' ; function getErrorForFirstName ( name ) { if ( ! name ) return `Name is required` ; if ( name . length > 60 ) return `Name is too long` ; return null ; } function getErrorForEmail ( email ) { if ( ! email ) return `Email is required` ; if ( ! /^.+@.+\..+$/ . test ( email ) ) return `That's not an email` ; return null ; } function getErrorForConvertKitTagId ( tagId , form ) { if ( ! form . get ( 'convertKitFormId' ) && ! tagId ) { return `convertKitTagId is required if convertKitFormId is not specified` ; } if ( ! tagId ) return null ; if ( tagId . length < 2 ) return `Convert Kit Tag ID is incorrect` ; return null ; } function getErrorForConvertKitFormId ( formId , form ) { if ( ! form . get ( 'convertKitTagId' ) && ! formId ) { return `convertKitFormId is required if convertKitTagId is not specified` ; } if ( ! formId ) return null ; if ( formId . length < 2 ) return `Convert Kit Form ID is incorrect` ; return null ; } function getErrorForFormId ( value ) { if ( ! value ) return `Form ID is required` ; return null ; } async function handleConvertKitFormSubmission ( request ) { const requestText = await request . text ( ) ; const form = new URLSearchParams ( requestText ) ; const fields = { formId : form . get ( 'formId' ) ?? '' , firstName : form . get ( 'firstName' ) ?? '' , email : form . get ( 'email' ) ?? '' , convertKitTagId : form . get ( 'convertKitTagId' ) ?? '' , convertKitFormId : form . get ( 'convertKitFormId' ) ?? '' , } ; const errors = { generalError : null , formId : getErrorForFormId ( fields . formId ) , firstName : getErrorForFirstName ( fields . firstName ) , email : getErrorForEmail ( fields . email ) , convertKitTagId : getErrorForConvertKitTagId ( fields . convertKitTagId , form ) , convertKitFormId : getErrorForConvertKitFormId ( fields . convertKitFormId , form ) , } ; let data ; if ( Object . values ( errors ) . some ( err => err !== null ) ) { data = { status : 'error' , errors } ; return json ( data , 400 ) ; } try { let subscriberId = null ; if ( fields . convertKitFormId ) { const subscriber = await ck . addSubscriberToForm ( fields ) ; subscriberId = subscriber . id ; } if ( fields . convertKitTagId ) { const subscriber = await ck . addTagToSubscriber ( fields ) ; subscriberId = subscriber . id ; } if ( subscriberId ) { await deleteConvertKitCache ( subscriberId ) . catch ( ( ) => { } ) ; } } catch ( error ) { errors . generalError = getErrorMessage ( error ) ; data = { status : 'error' , errors } ; return json ( data , 500 ) ; } data = { status : 'success' } ; return json ( data ) ; } export { handleConvertKitFormSubmission } ;