1+ 'use server'
12import axios from 'axios'
2- import type { NextRequest } from 'next/server'
33import type z from 'zod'
4- import type { Level2Keys } from '@/lib/helpers/typescriptTyping'
4+ // import type { Level2Keys } from '@/lib/helpers/typescriptTyping'
55import { subscribeSchema } from '@/lib/zodSchemas/zodSchemas'
6- import { validateFormRequest } from '../../forms/validateFormRequest'
7-
8- export type SubscribeFormParameters = z . infer < typeof subscribeSchema >
6+ import { validateFormRequest } from '../api/forms/validateFormRequest'
97
108const MAKE_SUBSCRIBER_API_BASE_URL = process . env . MAKE_SUBSCRIBER_API_BASE_URL
119const MAKE_API_KEY = process . env . MAKE_API_KEY || ''
1210const SUBSCRIBER_LIST_ID_EN = process . env . MAKE_SUBSCRIBER_LIST_ID_EN
1311const SUBSCRIBER_LIST_ID_NO = process . env . MAKE_SUBSCRIBER_LIST_ID_NO
1412const MAKE_API_USER = process . env . MAKE_API_USERID || ''
1513
16- export const newsletterCategoryMap = {
14+ const newsletterCategoryMap = {
1715 no : {
1816 generalNews : 'generelle nyheter' ,
1917 Company : 'generelle nyheter' ,
@@ -35,7 +33,7 @@ export const newsletterCategoryMap = {
3533}
3634
3735export type newsletterCategoryLocale = keyof typeof newsletterCategoryMap
38- export type newsletterCategoryKeys = Level2Keys < typeof newsletterCategoryMap >
36+ // type newsletterCategoryKeys = Level2Keys<typeof newsletterCategoryMap>
3937
4038const subscriberApi = axios . create ( {
4139 baseURL : MAKE_SUBSCRIBER_API_BASE_URL ,
@@ -45,81 +43,80 @@ const subscriberApi = axios.create({
4543 } ,
4644} )
4745
48- /**
49- * Subscribe a user using subscriber_list_id and tags
50- */
51- export const signUp = async (
52- data : SubscribeFormParameters ,
53- languageCode : newsletterCategoryLocale ,
54- ) => {
55- const { email, categories } = data
56- console . log ( 'categories' , categories )
46+ type SubscribeProps = {
47+ locale : newsletterCategoryLocale
48+ frcCaptchaSolution : any
49+ formData : z . infer < typeof subscribeSchema >
50+ }
51+
52+ export async function subscribe ( {
53+ locale,
54+ frcCaptchaSolution,
55+ formData,
56+ } : SubscribeProps ) {
57+ const captchaResult = await validateFormRequest (
58+ frcCaptchaSolution ,
59+ 'subscription form' ,
60+ )
61+ if ( captchaResult . status !== 200 ) {
62+ return {
63+ status : false ,
64+ message : captchaResult . message ,
65+ }
66+ }
67+
68+ const validatedData = subscribeSchema . safeParse ( formData )
69+
70+ if ( ! validatedData . success ) {
71+ return { status : false , errors : validatedData . error }
72+ }
73+
74+ const { email, categories } = formData
75+
5776 try {
5877 const tags = categories ?. map (
59- category => newsletterCategoryMap [ languageCode ] [ category ] ,
78+ category => newsletterCategoryMap [ locale ] [ category ] ,
6079 )
80+ //Leave for testing period
6181 console . log ( 'newsletter tags' , tags )
6282
6383 const response = await subscriberApi . post (
6484 `/subscribers?subscriber_list_id=${
65- languageCode === 'no' ? SUBSCRIBER_LIST_ID_NO : SUBSCRIBER_LIST_ID_EN
85+ locale === 'no' ? SUBSCRIBER_LIST_ID_NO : SUBSCRIBER_LIST_ID_EN
6686 } &tag=merge`,
6787 {
6888 email,
6989 tags,
7090 } ,
7191 )
72- console . log ( 'Response from Make subscriber api' , response . statusText )
92+ //Leave for testing period
93+ console . log ( 'Response from Make subscriber api' , response )
7394
74- return response . status === 200
95+ if ( response ?. status === 200 ) {
96+ return {
97+ status : true ,
98+ message : 'Successfully subscribed' ,
99+ }
100+ }
101+ //Leave for testing period
102+ console . log (
103+ 'Error occured while newsletter subscription' ,
104+ response ?. statusText ,
105+ )
106+ return {
107+ status : false ,
108+ message : 'Error during subscription' ,
109+ }
75110 } catch ( error : any ) {
76111 console . error ( '❌ Error in signUp:' , {
77112 message : error . message ,
78113 responseData : error . response ?. data ,
79114 responseStatus : error . response ?. status ,
80115 requestHeaders : error . config ?. headers ,
81116 } )
82- return false
117+ return {
118+ status : false ,
119+ message : 'Error during subscription' ,
120+ }
83121 }
84122}
85-
86- export async function POST ( req : NextRequest ) {
87- const body = await req . json ( )
88- const { frcCaptchaSolution, languageCode, data } = body
89-
90- const captchaResult = await validateFormRequest (
91- frcCaptchaSolution ,
92- 'subscription form' ,
93- )
94- if ( captchaResult . status !== 200 ) {
95- return Response . json (
96- { msg : captchaResult . message } ,
97- { status : captchaResult . status } ,
98- )
99- }
100-
101- const validatedData = subscribeSchema . safeParse ( data )
102-
103- if ( ! validatedData . success ) {
104- return Response . json ( { msg : 'Invalid data' } , { status : 400 } )
105- }
106-
107- await signUp ( data , languageCode )
108- . then ( isSuccessful => {
109- if ( ! isSuccessful ) {
110- console . log ( 'Signup was not successfull while newsletter subscription' )
111- return Response . json (
112- { msg : 'Error during subscription' } ,
113- { status : 500 } ,
114- )
115- }
116- return Response . json ( { msg : 'Successfully subscribed' } , { status : 200 } )
117- } )
118- . catch ( error => {
119- console . log ( 'Error occured while newsletter subscription' , error )
120- return Response . json (
121- { msg : 'Error during subscription' } ,
122- { status : 500 } ,
123- )
124- } )
125- }
0 commit comments