async function createOnboardingConsent(
onboardingId: string, // From registration email verification
consents: Array<{ type: string; status: 'granted' | 'denied' }>,
userIp: string,
userAgent: string
) {
const response = await fetch('https://api.baanx.com/v2/consent/onboarding', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-client-key': process.env.BAANX_CLIENT_KEY!,
'x-secret-key': process.env.BAANX_SECRET_KEY!
},
body: JSON.stringify({
onboardingId, // Use ID from registration, don't generate new one
tenantId: 'tenant_baanx_prod',
policyType: 'US',
consents: consents.map(c => ({
consentType: c.type,
consentStatus: c.status
})),
metadata: {
ipAddress: userIp,
userAgent: userAgent,
timestamp: new Date().toISOString(),
clientId: 'web-app-v1.2.0'
}
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(`Consent creation failed: ${error.details.join(', ')}`);
}
const { consentSetId } = await response.json();
return consentSetId;
}
// Get onboardingId from registration flow (Step 2)
const { onboardingId } = await verifyEmailCode(email, verificationCode);
// Create consent during registration (Step 4 - before address submission)
const consentSetId = await createOnboardingConsent(
onboardingId, // From registration
[
{ type: 'eSignAct', status: 'granted' },
{ type: 'termsAndPrivacy', status: 'granted' },
{ type: 'marketingNotifications', status: 'granted' },
{ type: 'smsNotifications', status: 'denied' },
{ type: 'emailNotifications', status: 'granted' }
],
'192.168.1.1',
'Mozilla/5.0...'
);
console.log(`Consent set created: ${consentSetId}`);