Phase 4 Stage 1: backend foundation (additive endpoints + schema)

Schema (idempotent migration):
- payment_sessions.is_free_trial -> is_first_session_discount (data copied)
- payment_sessions.mode TEXT NOT NULL DEFAULT 'chat' CHECK (chat|call)
- chat_sessions.topics TEXT[] for ESP picks (info-only)

New endpoints:
- GET /api/client/onboarding-state (drives verif sheet + S6 paywall gate)
- GET /api/client/chat-pricing (rewrite: chat+call groups + first-session
  discount block, per-customer eligibility)
- GET /api/shared/auth-providers (env-probed; replaces ENABLE_SOCIAL_AUTH
  build flag — frontend cutover lands in stage 2)
- GET /api/client/support-handles (Tanya Admin handles, CC-config-driven)

session_warning WS event fires once at 180s remaining.

app_config seeds (mock pricing tiers, first-session discount, support
handles, payment method order, end-session 2-step toggle).

CC SettingsPage: 3 new sections (first-session discount, pricing tiers
JSON editors, support handles).

15/15 Vitest passing. chat_sessions.is_free_trial also renamed for
consistency (plan only specified payment_sessions; pairing.service.js
read both).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-10 15:56:28 +08:00
parent 4ada7c991a
commit d33d4419ea
24 changed files with 1347 additions and 162 deletions

View File

@@ -35,33 +35,113 @@ export const setMaxCustomersPerMitra = async (value) => {
return { max_customers_per_mitra: value }
}
// --- Phase 3 config ---
// --- Phase 4: First-session discount (replaces Phase 3 free-trial config) ---
export const getFreeTrialConfig = async () => {
const [enabledRow] = await sql`SELECT value FROM app_config WHERE key = 'free_trial_enabled'`
const [durationRow] = await sql`SELECT value FROM app_config WHERE key = 'free_trial_duration_minutes'`
export const getFirstSessionDiscountConfig = async () => {
const rows = await sql`
SELECT key, value FROM app_config
WHERE key IN (
'first_session_discount_enabled',
'first_session_discount_actual_price_idr',
'first_session_discount_gimmick_price_idr',
'first_session_discount_duration_minutes',
'first_session_discount_modes'
)
`
const byKey = Object.fromEntries(rows.map((r) => [r.key, r.value?.value]))
return {
enabled: enabledRow?.value?.value ?? false,
duration_minutes: durationRow?.value?.value ?? 5,
enabled: byKey.first_session_discount_enabled ?? true,
actual_price_idr: byKey.first_session_discount_actual_price_idr ?? 2000,
gimmick_price_idr: byKey.first_session_discount_gimmick_price_idr ?? 12000,
duration_minutes: byKey.first_session_discount_duration_minutes ?? 12,
modes: byKey.first_session_discount_modes ?? ['chat'],
}
}
export const setFirstSessionDiscountConfig = async (patch) => {
const map = {
enabled: 'first_session_discount_enabled',
actual_price_idr: 'first_session_discount_actual_price_idr',
gimmick_price_idr: 'first_session_discount_gimmick_price_idr',
duration_minutes: 'first_session_discount_duration_minutes',
modes: 'first_session_discount_modes',
}
for (const [field, key] of Object.entries(map)) {
if (patch[field] === undefined) continue
await sql`
INSERT INTO app_config (key, value, updated_at)
VALUES (${key}, ${sql.json({ value: patch[field] })}, NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
`
}
return getFirstSessionDiscountConfig()
}
// Back-compat shim — CC settings page still calls /internal/config/free-trial.
// Phase 4 routes will be added; until the CC UI is migrated this maps to the new keys.
export const getFreeTrialConfig = async () => {
const cfg = await getFirstSessionDiscountConfig()
return {
enabled: cfg.enabled,
duration_minutes: cfg.duration_minutes,
}
}
export const setFreeTrialConfig = async ({ enabled, duration_minutes }) => {
if (enabled !== undefined) {
await sql`
INSERT INTO app_config (key, value, updated_at)
VALUES ('free_trial_enabled', ${sql.json({ value: enabled })}, NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
`
return setFirstSessionDiscountConfig({
...(enabled !== undefined ? { enabled } : {}),
...(duration_minutes !== undefined ? { duration_minutes } : {}),
})
}
// --- Phase 4: Support handles ---
export const getSupportHandles = async () => {
const [row] = await sql`SELECT value FROM app_config WHERE key = 'support_handles_json'`
// Stored shape: { wa: {...}, telegram: {...} }. Fall back to a safe empty payload
// so the client renders an empty Tanya Admin sheet rather than crashing.
return row?.value ?? {
wa: { label: 'WhatsApp', deeplink: '' },
telegram: { label: 'Telegram', deeplink: '' },
}
if (duration_minutes !== undefined) {
await sql`
INSERT INTO app_config (key, value, updated_at)
VALUES ('free_trial_duration_minutes', ${sql.json({ value: duration_minutes })}, NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
`
}
export const setSupportHandles = async ({ wa, telegram }) => {
const current = await getSupportHandles()
const next = {
wa: { ...current.wa, ...(wa || {}) },
telegram: { ...current.telegram, ...(telegram || {}) },
}
return getFreeTrialConfig()
await sql`
INSERT INTO app_config (key, value, updated_at)
VALUES ('support_handles_json', ${sql.json(next)}, NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
`
return next
}
// --- Phase 4: Pricing tier groups ---
export const getPricingTierGroups = async () => {
const rows = await sql`
SELECT key, value FROM app_config
WHERE key IN ('pricing_chat_tiers_json', 'pricing_call_tiers_json')
`
const byKey = Object.fromEntries(rows.map((r) => [r.key, r.value]))
return {
chat: byKey.pricing_chat_tiers_json?.tiers ?? [],
call: byKey.pricing_call_tiers_json?.tiers ?? [],
}
}
export const setPricingTierGroup = async (mode, tiers) => {
const key = mode === 'call' ? 'pricing_call_tiers_json' : 'pricing_chat_tiers_json'
await sql`
INSERT INTO app_config (key, value, updated_at)
VALUES (${key}, ${sql.json({ tiers })}, NOW())
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW()
`
return getPricingTierGroups()
}
export const getExtensionTimeoutConfig = async () => {