Barazo AppView backend
barazo.forum
1import {
2 pgTable,
3 pgPolicy,
4 text,
5 boolean,
6 integer,
7 timestamp,
8 jsonb,
9 primaryKey,
10 index,
11} from 'drizzle-orm/pg-core'
12import { sql } from 'drizzle-orm'
13import { appRole } from './roles.js'
14
15export const communityOnboardingFields = pgTable(
16 'community_onboarding_fields',
17 {
18 id: text('id')
19 .primaryKey()
20 .$defaultFn(() => crypto.randomUUID()),
21 communityDid: text('community_did').notNull(),
22 fieldType: text('field_type', {
23 enum: [
24 'age_confirmation',
25 'tos_acceptance',
26 'newsletter_email',
27 'custom_text',
28 'custom_select',
29 'custom_checkbox',
30 ],
31 }).notNull(),
32 label: text('label').notNull(),
33 description: text('description'),
34 isMandatory: boolean('is_mandatory').notNull().default(true),
35 sortOrder: integer('sort_order').notNull().default(0),
36 source: text('source', {
37 enum: ['platform', 'admin'],
38 })
39 .notNull()
40 .default('admin'),
41 config: jsonb('config').$type<Record<string, unknown>>(),
42 createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
43 updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(),
44 },
45 (table) => [
46 index('onboarding_fields_community_idx').on(table.communityDid),
47 pgPolicy('tenant_isolation', {
48 as: 'permissive',
49 to: appRole,
50 for: 'all',
51 using: sql`community_did = current_setting('app.current_community_did', true)`,
52 withCheck: sql`community_did = current_setting('app.current_community_did', true)`,
53 }),
54 ]
55).enableRLS()
56
57export const userOnboardingResponses = pgTable(
58 'user_onboarding_responses',
59 {
60 did: text('did').notNull(),
61 communityDid: text('community_did').notNull(),
62 fieldId: text('field_id').notNull(),
63 response: jsonb('response').$type<unknown>().notNull(),
64 completedAt: timestamp('completed_at', { withTimezone: true }).notNull().defaultNow(),
65 },
66 (table) => [
67 primaryKey({ columns: [table.did, table.communityDid, table.fieldId] }),
68 index('onboarding_responses_did_community_idx').on(table.did, table.communityDid),
69 pgPolicy('tenant_isolation', {
70 as: 'permissive',
71 to: appRole,
72 for: 'all',
73 using: sql`community_did = current_setting('app.current_community_did', true)`,
74 withCheck: sql`community_did = current_setting('app.current_community_did', true)`,
75 }),
76 ]
77).enableRLS()