mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-25 11:13:09 +00:00
- submitWhatsapp resolve Consent.key = 'whatsapp-inscricao' via requireConsentByKey e FALHA FECHADO com mensagem clara enquanto o documento nao existir - migration 20260725_170000 (data-only, idempotente, guardada): tagueia o documento vivo id 2 (key vazia/'') com a chave estavel; no-op se ja houver documento com a chave ou se id 2 tiver outra chave; NAO inventa texto juridico (politica Onda 0) - chave em lib/campaignConsentKeys + lease key nas fixtures; int spec pina fail-closed + caminho feliz (contact + subscription -> consent.id) - ATENCAO DEPLOY: revisar o SQL antes do push (aplica em producao no build da Vercel)
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
'use server'
|
|
|
|
import config from '@payload-config'
|
|
import { getPayload } from 'payload'
|
|
|
|
import { WHATSAPP_SUBSCRIPTION_CONSENT_KEY } from '@/lib/campaignConsentKeys'
|
|
import { WhatsAppFormInput, whatsAppFormSchema } from '@/lib/schemas/whatsapp-form'
|
|
import { requireConsentByKey } from '@/utilities/campaignConsent'
|
|
|
|
export const submitWhatsapp = async (input: WhatsAppFormInput) => {
|
|
const { comment, ...contactInput } = whatsAppFormSchema.parse(input)
|
|
const payload = await getPayload({ config })
|
|
|
|
// Fail-closed consent resolution by stable key (Pass 2 D3) — the flow
|
|
// refuses to record a subscription while the keyed Consent document is
|
|
// missing, same policy as the campaign flows.
|
|
const consent = await requireConsentByKey(
|
|
payload,
|
|
WHATSAPP_SUBSCRIPTION_CONSENT_KEY,
|
|
undefined,
|
|
'Consentimento da inscrição no WhatsApp ainda não configurado.',
|
|
)
|
|
|
|
const transactionID = await payload.db.beginTransaction()
|
|
|
|
if (!transactionID) {
|
|
throw new Error('failed to start transaction')
|
|
}
|
|
|
|
try {
|
|
const contact = await payload.create({
|
|
collection: 'contact',
|
|
data: contactInput,
|
|
req: { transactionID },
|
|
})
|
|
|
|
await payload.create({
|
|
collection: 'subscription',
|
|
data: {
|
|
contact: contact.id,
|
|
consent: consent.id,
|
|
comment,
|
|
},
|
|
req: { transactionID },
|
|
})
|
|
|
|
await payload.db.commitTransaction(transactionID)
|
|
|
|
return { ok: true }
|
|
} catch (error) {
|
|
await payload.db.rollbackTransaction(transactionID)
|
|
|
|
throw error
|
|
}
|
|
}
|