hazo_auth 10.23.3 → 10.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/cli-src/lib/auth/auth_types.ts +6 -0
- package/cli-src/lib/auth/hazo_get_auth.server.ts +3 -0
- package/cli-src/lib/location_fields_config.server.ts +42 -0
- package/cli-src/lib/my_settings_config.server.ts +6 -0
- package/cli-src/lib/register_config.server.ts +16 -0
- package/cli-src/lib/services/group_service.ts +380 -0
- package/cli-src/lib/services/index.ts +1 -0
- package/cli-src/lib/services/location_fields_helper.ts +30 -0
- package/cli-src/lib/services/registration_service.ts +21 -0
- package/cli-src/lib/services/user_update_service.ts +7 -0
- package/dist/components/layouts/my_settings/hooks/use_my_settings.d.ts +8 -0
- package/dist/components/layouts/my_settings/hooks/use_my_settings.d.ts.map +1 -1
- package/dist/components/layouts/my_settings/hooks/use_my_settings.js +37 -0
- package/dist/components/layouts/my_settings/index.d.ts +6 -1
- package/dist/components/layouts/my_settings/index.d.ts.map +1 -1
- package/dist/components/layouts/my_settings/index.js +3 -3
- package/dist/components/layouts/register/hooks/use_register_form.d.ts +15 -1
- package/dist/components/layouts/register/hooks/use_register_form.d.ts.map +1 -1
- package/dist/components/layouts/register/hooks/use_register_form.js +69 -4
- package/dist/components/layouts/register/index.d.ts +7 -1
- package/dist/components/layouts/register/index.d.ts.map +1 -1
- package/dist/components/layouts/register/index.js +24 -3
- package/dist/components/layouts/shared/hooks/use_auth_status.d.ts +3 -0
- package/dist/components/layouts/shared/hooks/use_auth_status.d.ts.map +1 -1
- package/dist/components/layouts/shared/hooks/use_auth_status.js +5 -2
- package/dist/lib/auth/auth_types.d.ts +3 -0
- package/dist/lib/auth/auth_types.d.ts.map +1 -1
- package/dist/lib/auth/hazo_get_auth.server.d.ts.map +1 -1
- package/dist/lib/auth/hazo_get_auth.server.js +3 -0
- package/dist/lib/location_fields_config.server.d.ts +22 -0
- package/dist/lib/location_fields_config.server.d.ts.map +1 -0
- package/dist/lib/location_fields_config.server.js +31 -0
- package/dist/lib/my_settings_config.server.d.ts +4 -0
- package/dist/lib/my_settings_config.server.d.ts.map +1 -1
- package/dist/lib/my_settings_config.server.js +3 -0
- package/dist/lib/register_config.server.d.ts +10 -0
- package/dist/lib/register_config.server.d.ts.map +1 -1
- package/dist/lib/register_config.server.js +7 -0
- package/dist/lib/services/group_service.d.ts +66 -0
- package/dist/lib/services/group_service.d.ts.map +1 -0
- package/dist/lib/services/group_service.js +266 -0
- package/dist/lib/services/index.d.ts +1 -0
- package/dist/lib/services/index.d.ts.map +1 -1
- package/dist/lib/services/index.js +1 -0
- package/dist/lib/services/location_fields_helper.d.ts +13 -0
- package/dist/lib/services/location_fields_helper.d.ts.map +1 -0
- package/dist/lib/services/location_fields_helper.js +24 -0
- package/dist/lib/services/registration_service.d.ts +9 -0
- package/dist/lib/services/registration_service.d.ts.map +1 -1
- package/dist/lib/services/registration_service.js +11 -0
- package/dist/lib/services/user_update_service.d.ts +5 -0
- package/dist/lib/services/user_update_service.d.ts.map +1 -1
- package/dist/lib/services/user_update_service.js +2 -0
- package/dist/locales/en.json +9 -0
- package/dist/page_components/my_settings.d.ts +9 -1
- package/dist/page_components/my_settings.d.ts.map +1 -1
- package/dist/page_components/my_settings.js +8 -2
- package/dist/server/routes/me.d.ts.map +1 -1
- package/dist/server/routes/me.js +5 -1
- package/dist/server/routes/register.d.ts.map +1 -1
- package/dist/server/routes/register.js +46 -0
- package/dist/server/routes/update_user.d.ts.map +1 -1
- package/dist/server/routes/update_user.js +24 -1
- package/dist/server/routes/user_management_users.d.ts +1 -1
- package/dist/server_pages/my_settings.d.ts.map +1 -1
- package/dist/server_pages/my_settings.js +1 -1
- package/dist/server_pages/register.d.ts.map +1 -1
- package/dist/server_pages/register.js +1 -1
- package/dist/server_pages/register_client_wrapper.d.ts +4 -2
- package/dist/server_pages/register_client_wrapper.d.ts.map +1 -1
- package/dist/server_pages/register_client_wrapper.js +2 -2
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
A reusable authentication UI component package powered by Next.js, TailwindCSS, and shadcn. It integrates `hazo_config` for configuration management and `hazo_connect` for data access, enabling future components to stay aligned with platform conventions.
|
|
4
4
|
|
|
5
|
+
### What's New in v10.24.0
|
|
6
|
+
|
|
7
|
+
- **Groups + join codes** (ADR-0005) — new `hazo_groups` table (a named collection within a scope,
|
|
8
|
+
e.g. a class) and `hazo_group_memberships` table, migration `024_groups_and_join_codes.sql`.
|
|
9
|
+
`group_service.ts` exports `create_group()` (generates a unique 6-character join code, excluding
|
|
10
|
+
ambiguous characters 0/O, 1/I/l), `join_by_code()` (case-insensitive, idempotent, reactivates a
|
|
11
|
+
previously-removed membership), `get_members()` (paginated), and `get_user_groups()`.
|
|
12
|
+
|
|
5
13
|
### What's New in v10.22.0
|
|
6
14
|
|
|
7
15
|
- **Signup lifecycle hooks** — `register_auth_hooks({ on_user_created })` (from `hazo_auth/server-lib`) lets your app react to new-user creation (welcome emails, lifecycle sequences, analytics) without hazo_auth depending on your code. Fires best-effort on email registration, Google/Facebook OAuth, and OTP auto-register, passing `{ user_id, email, name?, auth_provider }`. See [Phase 8.2 of SETUP_CHECKLIST.md](SETUP_CHECKLIST.md#phase-82-signup-lifecycle-hooks-setup-optional).
|
|
@@ -19,6 +19,12 @@ export type HazoAuthUser = {
|
|
|
19
19
|
// User's explicit locale preference (BCP 47 tag, e.g. "en", "pt-BR"). NULL means
|
|
20
20
|
// no preference set — callers fall back to detection (Accept-Language, app default).
|
|
21
21
|
preferred_locale: string | null;
|
|
22
|
+
// Indexed location columns (migration 025) — null unless the consuming app has
|
|
23
|
+
// enabled the corresponding [hazo_auth__custom_fields] toggle AND the user has
|
|
24
|
+
// set a value. See location_fields_config.server.ts.
|
|
25
|
+
country: string | null;
|
|
26
|
+
state_province: string | null;
|
|
27
|
+
suburb: string | null;
|
|
22
28
|
};
|
|
23
29
|
|
|
24
30
|
/**
|
|
@@ -207,6 +207,9 @@ async function fetch_user_data_from_db(user_id: string): Promise<{
|
|
|
207
207
|
app_user_data: parse_app_user_data(user_db.app_user_data),
|
|
208
208
|
legal_acceptance: parse_legal_acceptance(user_db.legal_acceptance),
|
|
209
209
|
preferred_locale: (user_db.preferred_locale as string | null | undefined) || null,
|
|
210
|
+
country: (user_db.country as string | null | undefined) || null,
|
|
211
|
+
state_province: (user_db.state_province as string | null | undefined) || null,
|
|
212
|
+
suburb: (user_db.suburb as string | null | undefined) || null,
|
|
210
213
|
};
|
|
211
214
|
|
|
212
215
|
// v5.x: Fetch user's roles from hazo_user_scopes (scope-based role assignments)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// file_description: server-only helper to read location field toggles from hazo_auth_config.ini
|
|
2
|
+
// section: server-only-guard
|
|
3
|
+
import "server-only";
|
|
4
|
+
|
|
5
|
+
// section: imports
|
|
6
|
+
import { get_config_boolean } from "./config/config_loader.server.js";
|
|
7
|
+
|
|
8
|
+
// section: constants
|
|
9
|
+
const SECTION_NAME = "hazo_auth__custom_fields";
|
|
10
|
+
|
|
11
|
+
// section: types
|
|
12
|
+
export type LocationFieldsConfig = {
|
|
13
|
+
country: boolean;
|
|
14
|
+
state_province: boolean;
|
|
15
|
+
suburb: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// section: helpers
|
|
19
|
+
/**
|
|
20
|
+
* Reads the indexed location field toggles (country / state_province / suburb)
|
|
21
|
+
* from `[hazo_auth__custom_fields]` in hazo_auth_config.ini. All default to
|
|
22
|
+
* `false` — registration is unchanged for apps that don't opt in. These three
|
|
23
|
+
* columns are indexed (migration 025) for cohort benchmarking (GROUP BY
|
|
24
|
+
* country, state_province); arbitrary free-form fields go through the
|
|
25
|
+
* existing app_user_data schema (`get_app_user_data_schema()`) instead.
|
|
26
|
+
* @returns Which location fields are enabled
|
|
27
|
+
*/
|
|
28
|
+
export function get_location_fields_config(): LocationFieldsConfig {
|
|
29
|
+
return {
|
|
30
|
+
country: get_config_boolean(SECTION_NAME, "location_country", false),
|
|
31
|
+
state_province: get_config_boolean(SECTION_NAME, "location_state_province", false),
|
|
32
|
+
suburb: get_config_boolean(SECTION_NAME, "location_suburb", false),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* True if at least one location field is enabled — lets a consumer skip
|
|
38
|
+
* rendering an empty "Location" section entirely.
|
|
39
|
+
*/
|
|
40
|
+
export function is_any_location_field_enabled(config: LocationFieldsConfig): boolean {
|
|
41
|
+
return config.country || config.state_province || config.suburb;
|
|
42
|
+
}
|
|
@@ -10,6 +10,7 @@ import { get_profile_picture_config } from "./profile_picture_config.server.js";
|
|
|
10
10
|
import { get_messages_config } from "./messages_config.server.js";
|
|
11
11
|
import { get_ui_sizes_config } from "./ui_sizes_config.server.js";
|
|
12
12
|
import { get_file_types_config } from "./file_types_config.server.js";
|
|
13
|
+
import { get_location_fields_config, type LocationFieldsConfig } from "./location_fields_config.server.js";
|
|
13
14
|
|
|
14
15
|
// section: types
|
|
15
16
|
export type MySettingsConfig = {
|
|
@@ -18,6 +19,9 @@ export type MySettingsConfig = {
|
|
|
18
19
|
show_email_field: boolean;
|
|
19
20
|
show_password_field: boolean;
|
|
20
21
|
};
|
|
22
|
+
/** Which indexed location fields to show as an editable "Location" card —
|
|
23
|
+
* see [hazo_auth__custom_fields] in hazo_auth_config.ini. */
|
|
24
|
+
locationFields: LocationFieldsConfig;
|
|
21
25
|
passwordRequirements: {
|
|
22
26
|
minimum_length: number;
|
|
23
27
|
require_uppercase: boolean;
|
|
@@ -120,9 +124,11 @@ export function get_my_settings_config(): MySettingsConfig {
|
|
|
120
124
|
const loginButtonLabel = get_config_value_optional(section, "login_button_label");
|
|
121
125
|
const loginPath = get_config_value(section, "login_path", "/hazo_auth/login");
|
|
122
126
|
const enableAccountDeletion = get_config_boolean(section, "enable_account_deletion", false);
|
|
127
|
+
const locationFields = get_location_fields_config();
|
|
123
128
|
|
|
124
129
|
return {
|
|
125
130
|
userFields,
|
|
131
|
+
locationFields,
|
|
126
132
|
passwordRequirements,
|
|
127
133
|
profilePicture,
|
|
128
134
|
heading,
|
|
@@ -13,6 +13,8 @@ import { get_password_requirements_config } from "./password_requirements_config
|
|
|
13
13
|
import { get_already_logged_in_config } from "./already_logged_in_config.server.js";
|
|
14
14
|
import { get_user_fields_config } from "./user_fields_config.server.js";
|
|
15
15
|
import { get_oauth_config } from "./oauth_config.server.js";
|
|
16
|
+
import { get_location_fields_config, type LocationFieldsConfig } from "./location_fields_config.server.js";
|
|
17
|
+
import { get_app_user_data_schema, type AppUserDataSchema } from "./app_user_data_config.server.js";
|
|
16
18
|
|
|
17
19
|
// Default image path - consuming apps should either:
|
|
18
20
|
// 1. Configure their own image_src in hazo_auth_config.ini
|
|
@@ -49,6 +51,14 @@ export type RegisterConfig = {
|
|
|
49
51
|
enable_facebook_oauth: boolean;
|
|
50
52
|
facebook_button_text: string | undefined;
|
|
51
53
|
};
|
|
54
|
+
/** Which indexed location fields to render on the registration form — see
|
|
55
|
+
* [hazo_auth__custom_fields] in hazo_auth_config.ini. */
|
|
56
|
+
locationFields: LocationFieldsConfig;
|
|
57
|
+
/** Arbitrary custom fields to render on the registration form, reusing the
|
|
58
|
+
* EXISTING [hazo_auth__app_user_data] JSON Schema config (ticket 09 does NOT
|
|
59
|
+
* introduce a second, parallel custom-field config format). Null when
|
|
60
|
+
* schema-based app_user_data editing isn't enabled. */
|
|
61
|
+
customFieldsSchema: AppUserDataSchema | null;
|
|
52
62
|
};
|
|
53
63
|
|
|
54
64
|
// section: helpers
|
|
@@ -106,6 +116,10 @@ export function get_register_config(): RegisterConfig {
|
|
|
106
116
|
// Get OAuth configuration (shared with login)
|
|
107
117
|
const oauthConfig = get_oauth_config();
|
|
108
118
|
|
|
119
|
+
// Location + arbitrary custom fields (ticket 09)
|
|
120
|
+
const locationFields = get_location_fields_config();
|
|
121
|
+
const customFieldsSchema = get_app_user_data_schema();
|
|
122
|
+
|
|
109
123
|
// For the register page, the Google button text is undefined unless the
|
|
110
124
|
// consumer explicitly overrides it — RegisterLayout falls back to
|
|
111
125
|
// `t("register.oauth.google_button_default")` ("Sign up with Google").
|
|
@@ -135,6 +149,8 @@ export function get_register_config(): RegisterConfig {
|
|
|
135
149
|
enable_facebook_oauth: oauthConfig.enable_facebook_oauth,
|
|
136
150
|
facebook_button_text: oauthConfig.facebook_button_text,
|
|
137
151
|
},
|
|
152
|
+
locationFields,
|
|
153
|
+
customFieldsSchema,
|
|
138
154
|
};
|
|
139
155
|
}
|
|
140
156
|
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
// file_description: service for groups (named collections within a scope, e.g. a
|
|
2
|
+
// class) and join-code based self-service membership, using hazo_connect
|
|
3
|
+
// section: imports
|
|
4
|
+
import type { HazoConnectAdapter } from "hazo_connect";
|
|
5
|
+
import { createCrudService } from "hazo_connect/server";
|
|
6
|
+
import { randomInt } from "crypto";
|
|
7
|
+
import { create_app_logger } from "../app_logger.js";
|
|
8
|
+
import { sanitize_error_for_user } from "../utils/error_sanitizer.js";
|
|
9
|
+
import { get_scope_by_id } from "./scope_service.js";
|
|
10
|
+
|
|
11
|
+
// section: constants
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* CRUD service options for hazo_group_memberships table
|
|
15
|
+
* This table uses a composite primary key (user_id, group_id) and no 'id' column
|
|
16
|
+
*/
|
|
17
|
+
const GROUP_MEMBERSHIPS_CRUD_OPTIONS = {
|
|
18
|
+
primaryKeys: ["user_id", "group_id"],
|
|
19
|
+
autoId: false as const,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Uppercase alphanumeric, excluding ambiguous characters (0/O, 1/I/l).
|
|
23
|
+
const JOIN_CODE_ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
24
|
+
const JOIN_CODE_LENGTH = 6;
|
|
25
|
+
const MAX_JOIN_CODE_ATTEMPTS = 10;
|
|
26
|
+
const DEFAULT_MEMBERS_PAGE_SIZE = 50;
|
|
27
|
+
|
|
28
|
+
// section: types
|
|
29
|
+
|
|
30
|
+
export type GroupStatus = "ACTIVE" | "ARCHIVED";
|
|
31
|
+
export type GroupMembershipStatus = "ACTIVE" | "REMOVED";
|
|
32
|
+
|
|
33
|
+
export type GroupRecord = {
|
|
34
|
+
id: string;
|
|
35
|
+
scope_id: string;
|
|
36
|
+
name: string;
|
|
37
|
+
join_code: string;
|
|
38
|
+
created_by: string | null;
|
|
39
|
+
metadata: Record<string, unknown> | null;
|
|
40
|
+
status: GroupStatus;
|
|
41
|
+
created_at: string;
|
|
42
|
+
changed_at: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type GroupMembershipRecord = {
|
|
46
|
+
user_id: string;
|
|
47
|
+
group_id: string;
|
|
48
|
+
role_id: string | null;
|
|
49
|
+
joined_at: string;
|
|
50
|
+
status: GroupMembershipStatus;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type GroupServiceResult = {
|
|
54
|
+
success: boolean;
|
|
55
|
+
group?: GroupRecord;
|
|
56
|
+
groups?: GroupRecord[];
|
|
57
|
+
membership?: GroupMembershipRecord;
|
|
58
|
+
members?: GroupMembershipRecord[];
|
|
59
|
+
total?: number;
|
|
60
|
+
error?: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type CreateGroupData = {
|
|
64
|
+
scope_id: string;
|
|
65
|
+
name: string;
|
|
66
|
+
created_by?: string;
|
|
67
|
+
metadata?: Record<string, unknown>;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type JoinByCodeData = {
|
|
71
|
+
user_id: string;
|
|
72
|
+
join_code: string;
|
|
73
|
+
role_id?: string;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type GetMembersOptions = {
|
|
77
|
+
limit?: number;
|
|
78
|
+
offset?: number;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// section: helpers
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Generates a 6-character join code from JOIN_CODE_ALPHABET.
|
|
85
|
+
* Excludes 0/O, 1/I/l so codes are easy to read aloud and transcribe.
|
|
86
|
+
*/
|
|
87
|
+
function generate_join_code(): string {
|
|
88
|
+
let code = "";
|
|
89
|
+
for (let i = 0; i < JOIN_CODE_LENGTH; i++) {
|
|
90
|
+
code += JOIN_CODE_ALPHABET[randomInt(JOIN_CODE_ALPHABET.length)];
|
|
91
|
+
}
|
|
92
|
+
return code;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Parses the metadata TEXT column back into an object, defaulting to null on bad JSON. */
|
|
96
|
+
function parse_metadata(raw: unknown): Record<string, unknown> | null {
|
|
97
|
+
if (typeof raw !== "string" || raw.length === 0) return null;
|
|
98
|
+
try {
|
|
99
|
+
return JSON.parse(raw);
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function to_group_record(row: Record<string, unknown>): GroupRecord {
|
|
106
|
+
return {
|
|
107
|
+
...(row as unknown as GroupRecord),
|
|
108
|
+
metadata: parse_metadata(row.metadata),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// section: main_service
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Creates a new group within a scope, generating a unique join code.
|
|
116
|
+
* Uniqueness is checked proactively (and enforced at the DB level by a
|
|
117
|
+
* UNIQUE constraint on hazo_groups.join_code).
|
|
118
|
+
*/
|
|
119
|
+
export async function create_group(
|
|
120
|
+
adapter: HazoConnectAdapter,
|
|
121
|
+
data: CreateGroupData,
|
|
122
|
+
): Promise<GroupServiceResult> {
|
|
123
|
+
try {
|
|
124
|
+
const group_service = createCrudService(adapter, "hazo_groups");
|
|
125
|
+
const now = new Date().toISOString();
|
|
126
|
+
|
|
127
|
+
const scope_result = await get_scope_by_id(adapter, data.scope_id);
|
|
128
|
+
if (!scope_result.success) {
|
|
129
|
+
return { success: false, error: "Scope not found" };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Proactively avoid known collisions, then insert. If a race slips a
|
|
133
|
+
// duplicate past the proactive check, the DB's UNIQUE constraint on
|
|
134
|
+
// join_code rejects the insert and we regenerate and retry — the
|
|
135
|
+
// proactive check alone can't close that race.
|
|
136
|
+
let last_error: unknown;
|
|
137
|
+
for (let attempt = 0; attempt < MAX_JOIN_CODE_ATTEMPTS; attempt++) {
|
|
138
|
+
const candidate = generate_join_code();
|
|
139
|
+
const existing = await group_service.findBy({ join_code: candidate });
|
|
140
|
+
if (Array.isArray(existing) && existing.length > 0) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const inserted = await group_service.insert({
|
|
146
|
+
scope_id: data.scope_id,
|
|
147
|
+
name: data.name,
|
|
148
|
+
join_code: candidate,
|
|
149
|
+
created_by: data.created_by ?? null,
|
|
150
|
+
metadata: data.metadata ? JSON.stringify(data.metadata) : null,
|
|
151
|
+
status: "ACTIVE",
|
|
152
|
+
created_at: now,
|
|
153
|
+
changed_at: now,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (!Array.isArray(inserted) || inserted.length === 0) {
|
|
157
|
+
return { success: false, error: "Failed to create group" };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { success: true, group: to_group_record(inserted[0]) };
|
|
161
|
+
} catch (insert_error) {
|
|
162
|
+
const message = insert_error instanceof Error ? insert_error.message : String(insert_error);
|
|
163
|
+
if (!/unique/i.test(message)) {
|
|
164
|
+
throw insert_error;
|
|
165
|
+
}
|
|
166
|
+
// join_code collided at the DB level (race with another create_group
|
|
167
|
+
// call) — regenerate and retry.
|
|
168
|
+
last_error = insert_error;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
throw last_error ?? new Error("Failed to generate a unique join code");
|
|
173
|
+
} catch (error) {
|
|
174
|
+
const logger = create_app_logger();
|
|
175
|
+
const error_message = sanitize_error_for_user(error, {
|
|
176
|
+
logToConsole: true,
|
|
177
|
+
logToLogger: true,
|
|
178
|
+
logger,
|
|
179
|
+
context: {
|
|
180
|
+
filename: "group_service.ts",
|
|
181
|
+
line_number: 0,
|
|
182
|
+
operation: "create_group",
|
|
183
|
+
scope_id: data.scope_id,
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
return { success: false, error: error_message };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Joins a user to a group by join code. Idempotent: an existing ACTIVE
|
|
192
|
+
* membership is returned as-is rather than erroring; a previously REMOVED
|
|
193
|
+
* membership is reactivated.
|
|
194
|
+
*/
|
|
195
|
+
export async function join_by_code(
|
|
196
|
+
adapter: HazoConnectAdapter,
|
|
197
|
+
data: JoinByCodeData,
|
|
198
|
+
): Promise<GroupServiceResult> {
|
|
199
|
+
try {
|
|
200
|
+
const group_service = createCrudService(adapter, "hazo_groups");
|
|
201
|
+
const membership_service = createCrudService(
|
|
202
|
+
adapter,
|
|
203
|
+
"hazo_group_memberships",
|
|
204
|
+
GROUP_MEMBERSHIPS_CRUD_OPTIONS,
|
|
205
|
+
);
|
|
206
|
+
const now = new Date().toISOString();
|
|
207
|
+
|
|
208
|
+
const normalized_code = data.join_code.trim().toUpperCase();
|
|
209
|
+
const groups = await group_service.findBy({ join_code: normalized_code });
|
|
210
|
+
if (!Array.isArray(groups) || groups.length === 0) {
|
|
211
|
+
return { success: false, error: "Invalid join code" };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const group = to_group_record(groups[0]);
|
|
215
|
+
if (group.status !== "ACTIVE") {
|
|
216
|
+
return { success: false, error: "This group is no longer accepting members" };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const existing = await membership_service.findBy({
|
|
220
|
+
user_id: data.user_id,
|
|
221
|
+
group_id: group.id,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
if (Array.isArray(existing) && existing.length > 0) {
|
|
225
|
+
const existing_membership = existing[0] as GroupMembershipRecord;
|
|
226
|
+
if (existing_membership.status === "ACTIVE") {
|
|
227
|
+
return { success: true, group, membership: existing_membership };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Reactivate a previously-removed membership.
|
|
231
|
+
const reactivated = await membership_service.upsert(
|
|
232
|
+
{
|
|
233
|
+
user_id: data.user_id,
|
|
234
|
+
group_id: group.id,
|
|
235
|
+
role_id: data.role_id ?? existing_membership.role_id ?? null,
|
|
236
|
+
joined_at: now,
|
|
237
|
+
status: "ACTIVE",
|
|
238
|
+
},
|
|
239
|
+
["user_id", "group_id"],
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
success: true,
|
|
244
|
+
group,
|
|
245
|
+
membership: reactivated[0] as GroupMembershipRecord,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const inserted = await membership_service.insert({
|
|
250
|
+
user_id: data.user_id,
|
|
251
|
+
group_id: group.id,
|
|
252
|
+
role_id: data.role_id ?? null,
|
|
253
|
+
joined_at: now,
|
|
254
|
+
status: "ACTIVE",
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
if (!Array.isArray(inserted) || inserted.length === 0) {
|
|
258
|
+
return { success: false, error: "Failed to join group" };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return { success: true, group, membership: inserted[0] as GroupMembershipRecord };
|
|
262
|
+
} catch (error) {
|
|
263
|
+
const logger = create_app_logger();
|
|
264
|
+
const error_message = sanitize_error_for_user(error, {
|
|
265
|
+
logToConsole: true,
|
|
266
|
+
logToLogger: true,
|
|
267
|
+
logger,
|
|
268
|
+
context: {
|
|
269
|
+
filename: "group_service.ts",
|
|
270
|
+
line_number: 0,
|
|
271
|
+
operation: "join_by_code",
|
|
272
|
+
user_id: data.user_id,
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
return { success: false, error: error_message };
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Gets a paginated page of a group's active members.
|
|
281
|
+
*/
|
|
282
|
+
export async function get_members(
|
|
283
|
+
adapter: HazoConnectAdapter,
|
|
284
|
+
group_id: string,
|
|
285
|
+
options: GetMembersOptions = {},
|
|
286
|
+
): Promise<GroupServiceResult> {
|
|
287
|
+
try {
|
|
288
|
+
const membership_service = createCrudService(
|
|
289
|
+
adapter,
|
|
290
|
+
"hazo_group_memberships",
|
|
291
|
+
GROUP_MEMBERSHIPS_CRUD_OPTIONS,
|
|
292
|
+
);
|
|
293
|
+
const limit = options.limit ?? DEFAULT_MEMBERS_PAGE_SIZE;
|
|
294
|
+
const offset = options.offset ?? 0;
|
|
295
|
+
|
|
296
|
+
const members = await membership_service.list((qb) =>
|
|
297
|
+
qb
|
|
298
|
+
.where("group_id", "eq", group_id)
|
|
299
|
+
.where("status", "eq", "ACTIVE")
|
|
300
|
+
.order("joined_at", "asc")
|
|
301
|
+
.limit(limit)
|
|
302
|
+
.offset(offset),
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
const total = await membership_service.findBy({
|
|
306
|
+
group_id,
|
|
307
|
+
status: "ACTIVE",
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
success: true,
|
|
312
|
+
members: members as GroupMembershipRecord[],
|
|
313
|
+
total: Array.isArray(total) ? total.length : 0,
|
|
314
|
+
};
|
|
315
|
+
} catch (error) {
|
|
316
|
+
const logger = create_app_logger();
|
|
317
|
+
const error_message = sanitize_error_for_user(error, {
|
|
318
|
+
logToConsole: true,
|
|
319
|
+
logToLogger: true,
|
|
320
|
+
logger,
|
|
321
|
+
context: {
|
|
322
|
+
filename: "group_service.ts",
|
|
323
|
+
line_number: 0,
|
|
324
|
+
operation: "get_members",
|
|
325
|
+
group_id,
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
return { success: false, error: error_message };
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Gets every group a user has an active membership in.
|
|
334
|
+
*/
|
|
335
|
+
export async function get_user_groups(
|
|
336
|
+
adapter: HazoConnectAdapter,
|
|
337
|
+
user_id: string,
|
|
338
|
+
): Promise<GroupServiceResult> {
|
|
339
|
+
try {
|
|
340
|
+
const membership_service = createCrudService(
|
|
341
|
+
adapter,
|
|
342
|
+
"hazo_group_memberships",
|
|
343
|
+
GROUP_MEMBERSHIPS_CRUD_OPTIONS,
|
|
344
|
+
);
|
|
345
|
+
const group_service = createCrudService(adapter, "hazo_groups");
|
|
346
|
+
|
|
347
|
+
const memberships = await membership_service.findBy({
|
|
348
|
+
user_id,
|
|
349
|
+
status: "ACTIVE",
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
if (!Array.isArray(memberships) || memberships.length === 0) {
|
|
353
|
+
return { success: true, groups: [] };
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const groups: GroupRecord[] = [];
|
|
357
|
+
for (const membership of memberships as GroupMembershipRecord[]) {
|
|
358
|
+
const found = await group_service.findBy({ id: membership.group_id });
|
|
359
|
+
if (Array.isArray(found) && found.length > 0) {
|
|
360
|
+
groups.push(to_group_record(found[0]));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return { success: true, groups };
|
|
365
|
+
} catch (error) {
|
|
366
|
+
const logger = create_app_logger();
|
|
367
|
+
const error_message = sanitize_error_for_user(error, {
|
|
368
|
+
logToConsole: true,
|
|
369
|
+
logToLogger: true,
|
|
370
|
+
logger,
|
|
371
|
+
context: {
|
|
372
|
+
filename: "group_service.ts",
|
|
373
|
+
line_number: 0,
|
|
374
|
+
operation: "get_user_groups",
|
|
375
|
+
user_id,
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
return { success: false, error: error_message };
|
|
379
|
+
}
|
|
380
|
+
}
|
|
@@ -18,6 +18,7 @@ export * from "./firm_service.js";
|
|
|
18
18
|
export * from "./post_verification_service.js";
|
|
19
19
|
export * from "./scope_service.js";
|
|
20
20
|
export * from "./user_scope_service.js";
|
|
21
|
+
export * from "./group_service.js";
|
|
21
22
|
export * from "./oauth_service.js";
|
|
22
23
|
export * from "./branding_service.js";
|
|
23
24
|
export * from "./google_token_service.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// file_description: pure helper shared by registration_service and user_update_service —
|
|
2
|
+
// picks and trims the three indexed location columns (country / state_province / suburb)
|
|
3
|
+
// from caller-supplied data, dropping anything undefined or blank after trimming.
|
|
4
|
+
|
|
5
|
+
// section: types
|
|
6
|
+
export type LocationFieldsInput = {
|
|
7
|
+
country?: string;
|
|
8
|
+
state_province?: string;
|
|
9
|
+
suburb?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// section: helpers
|
|
13
|
+
/**
|
|
14
|
+
* Returns only the location fields that were actually provided and non-blank,
|
|
15
|
+
* trimmed — ready to spread straight into an `insert`/`updateById` data object.
|
|
16
|
+
* @param location - Raw location input (e.g. from a registration or settings PATCH body)
|
|
17
|
+
* @returns A sparse object with only the fields worth persisting
|
|
18
|
+
*/
|
|
19
|
+
export function build_location_fields(location: LocationFieldsInput | undefined): Record<string, string> {
|
|
20
|
+
if (!location) return {};
|
|
21
|
+
|
|
22
|
+
const result: Record<string, string> = {};
|
|
23
|
+
for (const key of ["country", "state_province", "suburb"] as const) {
|
|
24
|
+
const value = location[key];
|
|
25
|
+
if (value === undefined) continue;
|
|
26
|
+
const trimmed = value.trim();
|
|
27
|
+
if (trimmed) result[key] = trimmed;
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
@@ -18,6 +18,8 @@ import {
|
|
|
18
18
|
} from "../user_types_config.server.js";
|
|
19
19
|
import { write_legal_acceptance } from "../legal/legal_docs_service.js";
|
|
20
20
|
import { fire_auth_hook } from "../auth_hooks.js";
|
|
21
|
+
import { build_location_fields, type LocationFieldsInput } from "./location_fields_helper.js";
|
|
22
|
+
import { update_app_user_data } from "./app_user_data_service.js";
|
|
21
23
|
|
|
22
24
|
// section: types
|
|
23
25
|
export type RegistrationData = {
|
|
@@ -28,6 +30,14 @@ export type RegistrationData = {
|
|
|
28
30
|
legal_accepted?: Record<string, { hash: string }>;
|
|
29
31
|
ip?: string | null;
|
|
30
32
|
user_agent?: string | null;
|
|
33
|
+
/** Indexed location columns — only persisted for fields enabled via
|
|
34
|
+
* `[hazo_auth__custom_fields]` (the route handler is responsible for
|
|
35
|
+
* stripping anything not enabled before calling here; see register.ts). */
|
|
36
|
+
location?: LocationFieldsInput;
|
|
37
|
+
/** Arbitrary custom fields (ticket 09's `custom_field_*` config), merged into the
|
|
38
|
+
* existing `app_user_data` JSONB column via `update_app_user_data()` — no new
|
|
39
|
+
* column or config parser for these; see register_config.server.ts. */
|
|
40
|
+
custom_fields?: Record<string, unknown>;
|
|
31
41
|
};
|
|
32
42
|
|
|
33
43
|
export type RegistrationResult = {
|
|
@@ -98,6 +108,10 @@ export async function register_user(
|
|
|
98
108
|
}
|
|
99
109
|
}
|
|
100
110
|
|
|
111
|
+
// Indexed location columns (country / state_province / suburb) — additive,
|
|
112
|
+
// no-op when `data.location` is omitted (every pre-ticket-09 caller).
|
|
113
|
+
Object.assign(insert_data, build_location_fields(data.location));
|
|
114
|
+
|
|
101
115
|
// Set default profile picture if enabled
|
|
102
116
|
const profile_picture_config = get_profile_picture_config();
|
|
103
117
|
if (profile_picture_config.user_photo_default) {
|
|
@@ -127,6 +141,13 @@ export async function register_user(
|
|
|
127
141
|
};
|
|
128
142
|
}
|
|
129
143
|
|
|
144
|
+
// Arbitrary custom fields (JSON Schema-driven, [hazo_auth__app_user_data]) —
|
|
145
|
+
// merge mode is a no-op concern here (brand new user, nothing to merge over),
|
|
146
|
+
// kept consistent with update_app_user_data()'s only mode of operation elsewhere.
|
|
147
|
+
if (data.custom_fields && Object.keys(data.custom_fields).length > 0) {
|
|
148
|
+
await update_app_user_data(adapter, user_id, data.custom_fields, { merge: true });
|
|
149
|
+
}
|
|
150
|
+
|
|
130
151
|
void fire_auth_hook("on_user_created", {
|
|
131
152
|
user_id,
|
|
132
153
|
email,
|
|
@@ -6,6 +6,7 @@ import { map_ui_source_to_db, type ProfilePictureSourceUI } from "./profile_pict
|
|
|
6
6
|
import { create_app_logger } from "../app_logger.js";
|
|
7
7
|
import { sanitize_error_for_user } from "../utils/error_sanitizer.js";
|
|
8
8
|
import { get_filename, get_line_number } from "../utils/api_route_helpers.js";
|
|
9
|
+
import { build_location_fields, type LocationFieldsInput } from "./location_fields_helper.js";
|
|
9
10
|
|
|
10
11
|
// section: types
|
|
11
12
|
export type UserUpdateData = {
|
|
@@ -17,6 +18,10 @@ export type UserUpdateData = {
|
|
|
17
18
|
// Undefined means "leave unchanged" — the caller is expected to have already validated
|
|
18
19
|
// the value (see src/server/routes/update_user.ts).
|
|
19
20
|
preferred_locale?: string | null;
|
|
21
|
+
/** Indexed location columns (My Settings' "Location" card). Only non-blank,
|
|
22
|
+
* provided fields are persisted — omitted or blank fields are left unchanged
|
|
23
|
+
* (see build_location_fields; it has no "clear" semantics). */
|
|
24
|
+
location?: LocationFieldsInput;
|
|
20
25
|
};
|
|
21
26
|
|
|
22
27
|
export type UserUpdateResult = {
|
|
@@ -115,6 +120,8 @@ export async function update_user_profile(
|
|
|
115
120
|
update_data.preferred_locale = data.preferred_locale;
|
|
116
121
|
}
|
|
117
122
|
|
|
123
|
+
Object.assign(update_data, build_location_fields(data.location));
|
|
124
|
+
|
|
118
125
|
// If email changed, set email_verified to false
|
|
119
126
|
if (email_changed) {
|
|
120
127
|
update_data.email_verified = false;
|
|
@@ -19,6 +19,9 @@ export type UseMySettingsResult = {
|
|
|
19
19
|
profilePictureUrl?: string;
|
|
20
20
|
profileSource?: "upload" | "library" | "gravatar" | "custom";
|
|
21
21
|
preferredLocale?: string | null;
|
|
22
|
+
country?: string | null;
|
|
23
|
+
stateProvince?: string | null;
|
|
24
|
+
suburb?: string | null;
|
|
22
25
|
lastLogon?: string;
|
|
23
26
|
loading: boolean;
|
|
24
27
|
authProviders?: string;
|
|
@@ -37,6 +40,11 @@ export type UseMySettingsResult = {
|
|
|
37
40
|
handleNameSave: (value: string) => Promise<void>;
|
|
38
41
|
handleEmailSave: (value: string) => Promise<void>;
|
|
39
42
|
handlePreferredLocaleSave: (localeCode: string) => Promise<void>;
|
|
43
|
+
handleLocationSave: (location: {
|
|
44
|
+
country?: string;
|
|
45
|
+
state_province?: string;
|
|
46
|
+
suburb?: string;
|
|
47
|
+
}) => Promise<void>;
|
|
40
48
|
refreshUserData: () => Promise<void>;
|
|
41
49
|
};
|
|
42
50
|
export type UseMySettingsParams = {
|