Barazo AppView backend barazo.forum
at main 52 lines 1.5 kB view raw
1import { eq, and } from 'drizzle-orm' 2import { 3 communityOnboardingFields, 4 userOnboardingResponses, 5} from '../db/schema/onboarding-fields.js' 6import type { Database } from '../db/index.js' 7 8export interface OnboardingCheckResult { 9 complete: boolean 10 missingFields: { id: string; label: string; fieldType: string }[] 11} 12 13/** 14 * Check whether a user has completed all mandatory onboarding fields 15 * for a community. Returns complete=true if no mandatory fields are 16 * configured or all have responses. 17 * 18 * All fields (platform and admin) live in the database -- no virtual 19 * field injection needed. 20 */ 21export async function checkOnboardingComplete( 22 db: Database, 23 did: string, 24 communityDid: string 25): Promise<OnboardingCheckResult> { 26 const fields = await db 27 .select() 28 .from(communityOnboardingFields) 29 .where( 30 and( 31 eq(communityOnboardingFields.communityDid, communityDid), 32 eq(communityOnboardingFields.isMandatory, true) 33 ) 34 ) 35 36 const responses = await db 37 .select() 38 .from(userOnboardingResponses) 39 .where( 40 and( 41 eq(userOnboardingResponses.did, did), 42 eq(userOnboardingResponses.communityDid, communityDid) 43 ) 44 ) 45 46 const answeredFieldIds = new Set(responses.map((r) => r.fieldId)) 47 const missingFields = fields 48 .filter((f) => !answeredFieldIds.has(f.id)) 49 .map((f) => ({ id: f.id, label: f.label, fieldType: f.fieldType })) 50 51 return { complete: missingFields.length === 0, missingFields } 52}