Fix SSR errors, enhance Person activity views, and expand Core CRUD

- Resolved Svelte 5 / SvelteKit SSR errors by adding browser checks for window.postMessage and Dexie database operations
- Prevented side effects on global state during detail page preloading by refactoring people/[person_id]/+page.ts to use shallow copies
- Implemented full V3 CRUD support, detail pages, and editable_fields for Address and Contact modules
- Enhanced Event and Post search to support filtering by person_id, enabling real related data in the Person detail view
- Fixed missing onMount import in Person detail component
This commit is contained in:
Scott Idem
2026-01-06 19:20:27 -05:00
parent 6d0531e227
commit c0fc5052ab
14 changed files with 940 additions and 63 deletions

View File

@@ -145,20 +145,22 @@ export const post_object = async function post_object({
// Post a message to the window indicating the upload is complete
try {
window.postMessage(
{
type: 'api_post_json_form',
status: 'complete',
task_id: task_id,
endpoint: endpoint,
size_total: 0,
size_loaded: 0,
percent_completed: 100,
progress: 100,
rate: 0
},
'*'
);
if (typeof window !== 'undefined') {
window.postMessage(
{
type: 'api_post_json_form',
status: 'complete',
task_id: task_id,
endpoint: endpoint,
size_total: 0,
size_loaded: 0,
percent_completed: 100,
progress: 100,
rate: 0
},
'*'
);
}
} catch (error) {
console.error('Error posting message to window:', error);
}

View File

@@ -0,0 +1,11 @@
export const editable_fields__address = [
'city',
'state_province',
'country',
'enable',
'hide',
'priority',
'sort',
'group',
'notes'
];

View File

@@ -30,19 +30,72 @@ export interface Address {
updated_on?: null | Date;
}
// Updated 2026-01-06
export async function load_ae_obj_id__address({
api_cfg,
address_id,
view = 'default',
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
address_id: string;
view?: string;
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
ae_promises.load__address_obj = await api.get_ae_obj_v3({
api_cfg,
obj_type: 'address',
obj_id: address_id,
view,
params,
log_lvl
}).then(async (result) => {
if (result) {
if (try_cache) {
const processed = await process_ae_obj__address_props({ obj_li: [result], log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'address',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
return null;
});
return ae_promises.load__address_obj;
}
// Updated 2026-01-06
export async function load_ae_obj_li__address({
api_cfg,
for_obj_type,
for_obj_type = 'account',
for_obj_id,
enabled = 'enabled',
hidden = 'not_hidden',
view = 'default',
limit = 99,
offset = 0,
order_by_li = { city: 'ASC' },
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
for_obj_type?: string;
for_obj_id?: string;
for_obj_id: string;
enabled?: 'all' | 'enabled' | 'not_enabled';
hidden?: 'all' | 'hidden' | 'not_hidden';
view?: string;
limit?: number;
offset?: number;
order_by_li?: Record<string, 'ASC' | 'DESC'>;
try_cache?: boolean;
log_lvl?: number;
}) {
ae_promises.load__address_obj_li = await api.get_ae_obj_li_v3({
@@ -52,17 +105,23 @@ export async function load_ae_obj_li__address({
for_obj_id,
enabled,
hidden,
view,
limit,
offset,
order_by_li,
log_lvl
}).then(async (result) => {
if (result && Array.isArray(result)) {
const processed = await process_ae_obj__address_props({ obj_li: result, log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'address',
obj_li: processed,
properties_to_save: ['id', 'address_id', 'address_id_random', 'city', 'state_province', 'country', 'enable'],
log_lvl
});
if (try_cache) {
const processed = await process_ae_obj__address_props({ obj_li: result, log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'address',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
return [];
@@ -70,10 +129,186 @@ export async function load_ae_obj_li__address({
return ae_promises.load__address_obj_li;
}
export async function process_ae_obj__address_props({ obj_li, log_lvl = 0 }: { obj_li: any[], log_lvl?: number }) {
return obj_li.map(obj => {
const new_obj = { ...obj };
new_obj.id = obj.address_id_random;
return new_obj;
// Updated 2026-01-06
export async function create_ae_obj__address({
api_cfg,
account_id,
data_kv,
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
account_id: string;
data_kv: key_val;
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
const result = await api.create_ae_obj_v3({
api_cfg,
obj_type: 'address',
fields: {
account_id_random: account_id,
...data_kv
},
params,
log_lvl
});
if (result && try_cache) {
const processed = await process_ae_obj__address_props({ obj_li: [result], log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'address',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
// Updated 2026-01-06
export async function update_ae_obj__address({
api_cfg,
address_id,
data_kv,
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
address_id: string;
data_kv: key_val;
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
const result = await api.update_ae_obj_v3({
api_cfg,
obj_type: 'address',
obj_id: address_id,
fields: data_kv,
params,
log_lvl
});
if (result && try_cache) {
const processed = await process_ae_obj__address_props({ obj_li: [result], log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'address',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
// Updated 2026-01-06
export async function delete_ae_obj_id__address({
api_cfg,
address_id,
method = 'delete',
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
address_id: string;
method?: 'delete' | 'soft_delete' | 'disable' | 'hide';
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
const result = await api.delete_ae_obj_v3({
api_cfg,
obj_type: 'address',
obj_id: address_id,
method,
params,
log_lvl
});
if (try_cache) {
await db_core.address.delete(address_id);
}
return result;
}
export const properties_to_save = [
'id',
'address_id',
'address_id_random',
'account_id',
'account_id_random',
'for_type',
'for_id',
'for_id_random',
'city',
'state_province',
'country',
'enable',
'hide',
'priority',
'sort',
'group',
'notes',
'created_on',
'updated_on'
];
async function _process_generic_props<T extends Record<string, any>>({
obj_li,
obj_type,
log_lvl = 0,
specific_processor
}: {
obj_li: T[];
obj_type: string;
log_lvl?: number;
specific_processor?: (obj: T) => Promise<T> | T;
}): Promise<T[]> {
if (!obj_li || obj_li.length === 0) return [];
const processed_obj_li: T[] = [];
for (const original_obj of obj_li) {
let processed_obj = { ...original_obj };
for (const key in processed_obj) {
if (key.endsWith('_random')) {
const newKey = key.slice(0, -7);
(processed_obj as any)[newKey] = processed_obj[key];
}
}
const randomIdKey = `${obj_type}_id_random`;
if (processed_obj[randomIdKey]) {
(processed_obj as any).id = processed_obj[randomIdKey];
}
if (specific_processor) {
processed_obj = await Promise.resolve(specific_processor(processed_obj));
}
processed_obj_li.push(processed_obj as T);
}
return processed_obj_li;
}
export async function process_ae_obj__address_props({
obj_li,
log_lvl = 0
}: {
obj_li: any[];
log_lvl?: number;
}) {
return _process_generic_props({
obj_li,
obj_type: 'address',
log_lvl
});
}

View File

@@ -0,0 +1,11 @@
export const editable_fields__contact = [
'name',
'email',
'phone',
'enable',
'hide',
'priority',
'sort',
'group',
'notes'
];

View File

@@ -30,19 +30,72 @@ export interface Contact {
updated_on?: null | Date;
}
// Updated 2026-01-06
export async function load_ae_obj_id__contact({
api_cfg,
contact_id,
view = 'default',
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
contact_id: string;
view?: string;
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
ae_promises.load__contact_obj = await api.get_ae_obj_v3({
api_cfg,
obj_type: 'contact',
obj_id: contact_id,
view,
params,
log_lvl
}).then(async (result) => {
if (result) {
if (try_cache) {
const processed = await process_ae_obj__contact_props({ obj_li: [result], log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'contact',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
return null;
});
return ae_promises.load__contact_obj;
}
// Updated 2026-01-06
export async function load_ae_obj_li__contact({
api_cfg,
for_obj_type,
for_obj_type = 'account',
for_obj_id,
enabled = 'enabled',
hidden = 'not_hidden',
view = 'default',
limit = 99,
offset = 0,
order_by_li = { name: 'ASC' },
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
for_obj_type?: string;
for_obj_id?: string;
for_obj_id: string;
enabled?: 'all' | 'enabled' | 'not_enabled';
hidden?: 'all' | 'hidden' | 'not_hidden';
view?: string;
limit?: number;
offset?: number;
order_by_li?: Record<string, 'ASC' | 'DESC'>;
try_cache?: boolean;
log_lvl?: number;
}) {
ae_promises.load__contact_obj_li = await api.get_ae_obj_li_v3({
@@ -52,17 +105,23 @@ export async function load_ae_obj_li__contact({
for_obj_id,
enabled,
hidden,
view,
limit,
offset,
order_by_li,
log_lvl
}).then(async (result) => {
if (result && Array.isArray(result)) {
const processed = await process_ae_obj__contact_props({ obj_li: result, log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'contact',
obj_li: processed,
properties_to_save: ['id', 'contact_id', 'contact_id_random', 'name', 'email', 'phone', 'enable'],
log_lvl
});
if (try_cache) {
const processed = await process_ae_obj__contact_props({ obj_li: result, log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'contact',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
return [];
@@ -70,10 +129,186 @@ export async function load_ae_obj_li__contact({
return ae_promises.load__contact_obj_li;
}
export async function process_ae_obj__contact_props({ obj_li, log_lvl = 0 }: { obj_li: any[], log_lvl?: number }) {
return obj_li.map(obj => {
const new_obj = { ...obj };
new_obj.id = obj.contact_id_random;
return new_obj;
// Updated 2026-01-06
export async function create_ae_obj__contact({
api_cfg,
account_id,
data_kv,
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
account_id: string;
data_kv: key_val;
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
const result = await api.create_ae_obj_v3({
api_cfg,
obj_type: 'contact',
fields: {
account_id_random: account_id,
...data_kv
},
params,
log_lvl
});
if (result && try_cache) {
const processed = await process_ae_obj__contact_props({ obj_li: [result], log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'contact',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
// Updated 2026-01-06
export async function update_ae_obj__contact({
api_cfg,
contact_id,
data_kv,
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
contact_id: string;
data_kv: key_val;
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
const result = await api.update_ae_obj_v3({
api_cfg,
obj_type: 'contact',
obj_id: contact_id,
fields: data_kv,
params,
log_lvl
});
if (result && try_cache) {
const processed = await process_ae_obj__contact_props({ obj_li: [result], log_lvl });
await db_save_ae_obj_li__ae_obj({
db_instance: db_core,
table_name: 'contact',
obj_li: processed,
properties_to_save,
log_lvl
});
}
return result;
}
// Updated 2026-01-06
export async function delete_ae_obj_id__contact({
api_cfg,
contact_id,
method = 'delete',
params = {},
try_cache = true,
log_lvl = 0
}: {
api_cfg: any;
contact_id: string;
method?: 'delete' | 'soft_delete' | 'disable' | 'hide';
params?: key_val;
try_cache?: boolean;
log_lvl?: number;
}) {
const result = await api.delete_ae_obj_v3({
api_cfg,
obj_type: 'contact',
obj_id: contact_id,
method,
params,
log_lvl
});
if (try_cache) {
await db_core.contact.delete(contact_id);
}
return result;
}
export const properties_to_save = [
'id',
'contact_id',
'contact_id_random',
'account_id',
'account_id_random',
'for_type',
'for_id',
'for_id_random',
'name',
'email',
'phone',
'enable',
'hide',
'priority',
'sort',
'group',
'notes',
'created_on',
'updated_on'
];
async function _process_generic_props<T extends Record<string, any>>({
obj_li,
obj_type,
log_lvl = 0,
specific_processor
}: {
obj_li: T[];
obj_type: string;
log_lvl?: number;
specific_processor?: (obj: T) => Promise<T> | T;
}): Promise<T[]> {
if (!obj_li || obj_li.length === 0) return [];
const processed_obj_li: T[] = [];
for (const original_obj of obj_li) {
let processed_obj = { ...original_obj };
for (const key in processed_obj) {
if (key.endsWith('_random')) {
const newKey = key.slice(0, -7);
(processed_obj as any)[newKey] = processed_obj[key];
}
}
const randomIdKey = `${obj_type}_id_random`;
if (processed_obj[randomIdKey]) {
(processed_obj as any).id = processed_obj[randomIdKey];
}
if (specific_processor) {
processed_obj = await Promise.resolve(specific_processor(processed_obj));
}
processed_obj_li.push(processed_obj as T);
}
return processed_obj_li;
}
export async function process_ae_obj__contact_props({
obj_li,
log_lvl = 0
}: {
obj_li: any[];
log_lvl?: number;
}) {
return _process_generic_props({
obj_li,
obj_type: 'contact',
log_lvl
});
}

View File

@@ -1,4 +1,5 @@
import type { Dexie, Table } from 'dexie';
import { browser } from '$app/environment';
/**
* Extracts the primary key from an object using a prioritized list of possible key names.
@@ -61,6 +62,8 @@ export async function db_save_ae_obj_li__ae_obj<T extends Record<string, any>>({
properties_to_save: (keyof T)[];
log_lvl?: number;
}) {
if (!browser) return [];
if (log_lvl > 0) {
console.log(
`*** db_save_ae_obj_li__ae_obj: Attempting to save ${obj_li.length} objects to table "${table_name}" ***`

View File

@@ -321,6 +321,7 @@ export async function qry_ae_obj_li__event({
for_obj_type = 'account',
for_obj_id,
qry_str,
qry_person_id = null,
enabled = 'enabled',
hidden = 'not_hidden',
view = 'default',
@@ -333,6 +334,7 @@ export async function qry_ae_obj_li__event({
for_obj_type?: string;
for_obj_id: string;
qry_str?: string;
qry_person_id?: string | null;
enabled?: 'enabled' | 'all' | 'not_enabled';
hidden?: 'hidden' | 'all' | 'not_hidden';
view?: string;
@@ -344,6 +346,10 @@ export async function qry_ae_obj_li__event({
const search_query: any = { and: [] };
if (qry_str) search_query.q = qry_str;
if (qry_person_id) {
search_query.and.push({ field: 'external_person_id', op: 'eq', value: qry_person_id });
}
return await api.search_ae_obj_v3({
api_cfg,
obj_type: 'event',

View File

@@ -322,6 +322,7 @@ export async function qry__post({
api_cfg,
account_id,
qry_str,
qry_person_id = null,
enabled = 'enabled',
hidden = 'not_hidden',
view = 'default',
@@ -338,6 +339,7 @@ export async function qry__post({
api_cfg: any;
account_id: string;
qry_str?: string;
qry_person_id?: string | null;
enabled?: 'enabled' | 'all' | 'not_enabled' | undefined;
hidden?: 'hidden' | 'all' | 'not_hidden' | undefined;
view?: string;
@@ -356,6 +358,10 @@ export async function qry__post({
search_query.q = qry_str;
}
if (qry_person_id) {
search_query.and.push({ field: 'external_person_id', op: 'eq', value: qry_person_id });
}
ae_promises.load__post_obj_li = await api.search_ae_obj_v3({
api_cfg,
obj_type: 'post',