feat(admin): consentement aux annonces et désabonnement sans connexion
Les comptes déjà créés ne sont pas inscrits. Le choix est facultatif à l’inscription et dans les paramètres. Un lien public demande confirmation avant d’arrêter les actualités, sans toucher aux messages de compte.
This commit is contained in:
29
memento-note/app/actions/marketing-preference.ts
Normal file
29
memento-note/app/actions/marketing-preference.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
'use server'
|
||||
|
||||
import { auth } from '@/auth'
|
||||
import { marketingConsentText } from '@/lib/marketing/consent-copy'
|
||||
import { getMarketingPreferenceView, setMarketingOptIn } from '@/lib/marketing/preference'
|
||||
import { marketingRequestMeta } from '@/lib/marketing/request-meta'
|
||||
|
||||
export async function getMyMarketingPreference() {
|
||||
const session = await auth()
|
||||
const userId = (session?.user as { id?: string } | undefined)?.id
|
||||
if (!userId) return null
|
||||
return getMarketingPreferenceView(userId)
|
||||
}
|
||||
|
||||
export async function updateMyMarketingPreference(optedIn: boolean, language?: string) {
|
||||
const session = await auth()
|
||||
const userId = (session?.user as { id?: string } | undefined)?.id
|
||||
if (!userId) return { ok: false as const, error: 'auth' as const }
|
||||
const meta = await marketingRequestMeta()
|
||||
const result = await setMarketingOptIn({
|
||||
userId,
|
||||
optedIn,
|
||||
source: 'settings',
|
||||
language: language || null,
|
||||
meta,
|
||||
})
|
||||
if (!result.ok) return { ok: false as const, error: result.error }
|
||||
return { ok: true as const, legalText: optedIn ? marketingConsentText(language) : null }
|
||||
}
|
||||
30
memento-note/app/actions/marketing-unsubscribe.ts
Normal file
30
memento-note/app/actions/marketing-unsubscribe.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
'use server'
|
||||
|
||||
import { unsubscribeByUserId } from '@/lib/marketing/preference'
|
||||
import { marketingRequestMeta } from '@/lib/marketing/request-meta'
|
||||
import { verifyUnsubscribeToken } from '@/lib/marketing/unsubscribe-token'
|
||||
|
||||
export async function previewUnsubscribeToken(token: string | null): Promise<{
|
||||
valid: boolean
|
||||
}> {
|
||||
if (!token) return { valid: false }
|
||||
const payload = verifyUnsubscribeToken(token)
|
||||
return { valid: Boolean(payload) }
|
||||
}
|
||||
|
||||
export async function confirmMarketingUnsubscribe(token: string): Promise<{
|
||||
ok: boolean
|
||||
already?: boolean
|
||||
invalid?: boolean
|
||||
}> {
|
||||
const payload = verifyUnsubscribeToken(token)
|
||||
if (!payload) return { ok: false, invalid: true }
|
||||
const meta = await marketingRequestMeta()
|
||||
const result = await unsubscribeByUserId({
|
||||
userId: payload.userId,
|
||||
source: 'unsubscribe',
|
||||
meta,
|
||||
})
|
||||
if (!result.ok) return { ok: false, invalid: true }
|
||||
return { ok: true, already: result.already }
|
||||
}
|
||||
@@ -50,7 +50,7 @@ export async function register(prevState: string | undefined, formData: FormData
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
const role = isAdmin ? 'ADMIN' : 'USER';
|
||||
|
||||
await prisma.user.create({
|
||||
const created = await prisma.user.create({
|
||||
data: {
|
||||
email: normalizedEmail,
|
||||
password: hashedPassword,
|
||||
@@ -61,6 +61,25 @@ export async function register(prevState: string | undefined, formData: FormData
|
||||
},
|
||||
});
|
||||
|
||||
if (formData.get('marketingConsent') === '1') {
|
||||
try {
|
||||
const { setMarketingOptIn } = await import('@/lib/marketing/preference')
|
||||
const { marketingRequestMeta } = await import('@/lib/marketing/request-meta')
|
||||
const meta = await marketingRequestMeta()
|
||||
await setMarketingOptIn({
|
||||
userId: created.id,
|
||||
optedIn: true,
|
||||
source: 'register',
|
||||
language: typeof formData.get('marketingLanguage') === 'string'
|
||||
? String(formData.get('marketingLanguage'))
|
||||
: null,
|
||||
meta,
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('[register] marketing consent save failed:', err)
|
||||
}
|
||||
}
|
||||
|
||||
if (!isAdmin) {
|
||||
const mailResult = await sendVerificationEmail({
|
||||
email: normalizedEmail,
|
||||
|
||||
Reference in New Issue
Block a user