Refactor: Standardize naming conventions in ae_core module to snake_case and remove deprecated functions.

This commit is contained in:
Scott Idem
2025-11-13 15:11:25 -05:00
parent d4ead978f9
commit fb634268c1
4 changed files with 24 additions and 174 deletions

View File

@@ -187,115 +187,7 @@ export async function delete_ae_obj_id(
/**
* @deprecated This function is deprecated. Use module-specific `process_ae_obj__*_props` functions instead.
* This generic processor with dynamic field detection is an older implementation.
* Module-specific processors now handle ID aliasing, content processing, and computed properties more effectively.
*/
export async function process_ae_obj__props({
obj_li,
log_lvl = 0,
}: {
obj_li: any[];
log_lvl?: number;
}) {
if (log_lvl) {
console.log(`*** process_ae_obj__props() ***`, obj_li);
}
if (!obj_li || obj_li.length === 0) {
return [];
}
const processed = obj_li.map(obj => {
// Extract fields with fallbacks and detect existence
const group = obj.group || '';
const priority = obj.priority !== undefined ? obj.priority : 0;
const sort = obj.sort !== undefined ? obj.sort : 0;
const created_on = obj.created_on || '';
const updated_on = obj.updated_on || '';
const name = obj.name || '';
// const description = obj.description || '';
// // Handle special case with description that has markdown
// // Remove the most common zerowidth characters from the start of the file
// const description_cleaned: string = obj.description.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,"");
// const description_md_html: null|string = marked.parse(description_cleaned ?? '') ?? null;
// // Add processed description
// obj.description_md_html = description_md_html;
// Handle special case with content that has markdown
// Remove the most common zerowidth characters from the start of the file
const has_content = obj.content !== undefined && obj.content !== null;
if (has_content) {
const content_cleaned: string = obj.content.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,"");
const content_md_html: null|string = marked.parse(content_cleaned ?? '') ?? null;
// Add processed content
obj.content_md_html = content_md_html;
}
// Check which fields actually exist for this object
const has_group = obj.group !== undefined && obj.group !== null;
const has_priority = obj.priority !== undefined && obj.priority !== null;
const has_sort = obj.sort !== undefined && obj.sort !== null;
const has_created = obj.created_on !== undefined && obj.created_on !== null;
const has_updated = obj.updated_on !== undefined && obj.updated_on !== null;
const has_name = obj.name !== undefined && obj.name !== null;
// Generate standardized tmp_sort fields with proper zero-padding
const sort_str = sort?.toString().padStart(3, '0') ?? '000';
// Build dynamic sorting combinations based on what exists
let tmp_sort_1 = '';
let tmp_sort_2 = '';
let tmp_sort_3 = '';
// Build based on existing fields
if (has_group && has_priority && has_sort) {
tmp_sort_1 = `${group}_${priority}_${sort_str}`;
tmp_sort_2 = `${group}_${priority}_${sort_str}_${name || ''}`;
tmp_sort_3 = `${priority}_${sort_str}_${group}`;
} else if (has_priority && has_sort) {
tmp_sort_1 = `${priority}_${sort_str}`;
tmp_sort_2 = `${priority}_${sort_str}_${name || ''}`;
tmp_sort_3 = `${priority}_${sort_str}`;
} else if (has_sort) {
tmp_sort_1 = `${sort_str}`;
tmp_sort_2 = `${sort_str}_${name || ''}`;
tmp_sort_3 = `${sort_str}`;
} else {
// Fallback for minimal data
tmp_sort_1 = `${created_on || ''}`;
tmp_sort_2 = `${created_on || ''}_${name || ''}`;
tmp_sort_3 = `${updated_on || ''}`;
}
// Add time-based sorting for better ordering
if (has_created && has_updated) {
tmp_sort_1 += `_${created_on}_${updated_on}`;
tmp_sort_2 += `_${created_on}_${updated_on}`;
tmp_sort_3 += `_${updated_on}`;
} else if (has_created) {
tmp_sort_1 += `_${created_on}`;
tmp_sort_2 += `_${created_on}`;
tmp_sort_3 += `_${created_on}`;
}
// Return processed object with standardized fields
return {
...obj,
tmp_sort_1,
tmp_sort_2,
tmp_sort_3
};
});
if (log_lvl) {
console.log(`Processed objects:`, processed);
}
return processed;
}
// Additional Modules that might be needed for reloads
@@ -315,49 +207,7 @@ import { load_ae_obj_id__post_comment } from "$lib/ae_posts/ae_posts__post_comme
import { load_ae_obj_id__person } from "$lib/ae_core/core__person";
/**
* @deprecated Use update_ae_obj_id_crud_v2 instead. This is the older version.
*/
export async function update_ae_obj_id_crud(
{
api_cfg,
object_type,
object_id,
field_name,
new_field_value,
log_lvl = 0
}: {
api_cfg: any,
object_type: string,
object_id: string,
field_name: string,
new_field_value: any,
log_lvl: number
}) {
try {
const results = await api.update_ae_obj_id_crud({
api_cfg: api_cfg,
obj_type: object_type,
obj_id: object_id,
field_name: field_name,
field_value: new_field_value,
key: api_cfg.api_crud_super_key,
log_lvl: log_lvl
});
if (results) {
console.log(`Patched - Field Name: ${field_name} with new Field Value: ${new_field_value}`);
return true;
} else {
console.log(`Not Patched - Field Name: ${field_name} with new Field Value: ${new_field_value}; Account ID: ${api_cfg.account_id}`);
return false;
}
} catch (error) {
console.log('Something went wrong patching the record.', error);
return false;
}
}
export async function update_ae_obj_id_crud_v2(

View File

@@ -7,8 +7,8 @@ import type { Dexie, Table } from 'dexie';
* @param log_lvl The logging level.
* @returns The found ID, or undefined if no ID could be found.
*/
function findObjectId(obj: any, table_name: string, log_lvl: number): string | number | undefined {
const potentialKeys = ['id', `${table_name}_id`, `${table_name}_id_random`];
function find_object_id(obj: any, table_name: string, log_lvl: number): string | number | undefined {
const potential_keys = ['id', `${table_name}_id`, `${table_name}_id_random`];
for (const key of potentialKeys) {
if (obj[key]) {
@@ -67,12 +67,12 @@ export async function db_save_ae_obj_li__ae_obj<T extends Record<string, any>>({
const db_table: Table<T> = db_instance.table(table_name);
if (!db_table) {
const errorMsg = `Table not found in Dexie instance: ${table_name}`;
const error_msg = `Table not found in Dexie instance: ${table_name}`;
console.error(errorMsg);
throw new Error(errorMsg);
}
const dataToSave = obj_li
const data_to_save = obj_li
.map((obj) => {
const record: Partial<T> = {};
@@ -82,7 +82,7 @@ export async function db_save_ae_obj_li__ae_obj<T extends Record<string, any>>({
}
// Ensure the primary key is included, attempting to find it from various legacy keys.
const id = findObjectId(obj, table_name, log_lvl);
const id = find_object_id(obj, table_name, log_lvl);
if (id === undefined) {
return null; // Skip objects without a valid ID.
@@ -93,7 +93,7 @@ export async function db_save_ae_obj_li__ae_obj<T extends Record<string, any>>({
})
.filter(Boolean) as T[];
if (dataToSave.length === 0) {
if (data_to_save.length === 0) {
if (log_lvl > 0) {
console.warn('All objects were skipped, likely due to missing IDs.');
}
@@ -102,7 +102,7 @@ export async function db_save_ae_obj_li__ae_obj<T extends Record<string, any>>({
try {
// bulkPut efficiently handles both inserts and updates.
const keys = await db_table.bulkPut(dataToSave);
const keys = await db_table.bulkPut(data_to_save);
if (log_lvl > 0) {
console.log(`Successfully saved ${keys.length} objects to "${table_name}".`);
}

View File

@@ -687,13 +687,13 @@ async function _process_generic_props<T extends Record<string, any>>({
// The API often returns fields like 'person_id_random', which need to be aliased to 'person_id'.
for (const key in processed_obj) {
if (key.endsWith('_random')) {
const newKey = key.slice(0, -7); // Remove '_random' suffix
const new_key = key.slice(0, -7); // Remove '_random' suffix
processed_obj[newKey] = processed_obj[key];
}
}
// Ensure 'id' is set from '[obj_type]_id_random'
const randomIdKey = `${obj_type}_id_random`;
if (processed_obj[randomIdKey]) {
const random_id_key = `${obj_type}_id_random`;
if (processed_obj[random_id_key]) {
processed_obj.id = processed_obj[randomIdKey];
}

View File

@@ -164,26 +164,26 @@ export async function js_generate_qr_code(qr_type, params = {}) {
console.log(`*** js_generate_qr_code() *** qr_type=${qr_type}`, params);
let qrData = null;
let qr_data = null;
// --- 1. Replicate Data Formatting Logic ---
switch (qr_type) {
case 'mecard':
// Format: MECARD:N:name;EMAIL:email;ADR:address;;
// Note: The original Python code had adr, but we'll use n and email as a minimum.
qrData = `MECARD:N:${n};EMAIL:${email};;`;
qr_data = `MECARD:N:${n};EMAIL:${email};;`;
// You can enhance this with other MeCard fields if needed.
break;
case 'vcard':
// Format: BEGIN:VCARD...END:VCARD
qrData = `BEGIN:VCARD\nVERSION:3.0\nN:${n}\nFN:${fn}\nORG:${org}\nEMAIL:${email}\n`;
qr_data = `BEGIN:VCARD\nVERSION:3.0\nN:${n}\nFN:${fn}\nORG:${org}\nEMAIL:${email}\n`;
if (url) { qrData += `URL:${url}\n`; }
if (tel) { qrData += `TEL:${tel}\n`; }
if (url) { qr_data += `URL:${url}\n`; }
if (tel) { qr_data += `TEL:${tel}\n`; }
if (adr_loc) {
// ADR format: TYPE=postal:Po box;Ext;Street;Locality;Region;Postal code;Country
qrData += `ADR:${adr_poa};${adr_ext};${adr_str};${adr_loc};${adr_reg};${adr_postal};${adr_country}\n`;
qr_data += `ADR:${adr_poa};${adr_ext};${adr_str};${adr_loc};${adr_reg};${adr_postal};${adr_country}\n`;
}
qrData += 'END:VCARD';
break;
@@ -191,32 +191,32 @@ export async function js_generate_qr_code(qr_type, params = {}) {
case 'obj':
// Custom format: OBJ:ot:obj_type,oi:obj_id
if (!obj_type || !obj_id) throw new Error('Missing obj_type or obj_id for type "obj".');
qrData = `OBJ:ot:${obj_type},oi:${obj_id}`;
qr_data = `OBJ:ot:${obj_type},oi:${obj_id}`;
break;
case 'kv':
// Custom format: KV:k:"key",v:"val"
if (!key || !val) throw new Error('Missing key or val for type "kv".');
qrData = `KV:k:"${key}",v:"${val}"`;
qr_data = `KV:k:"${key}",v:"${val}"`;
break;
case 'js':
// Assumes 'js' is a stringified JSON object
if (!js) throw new Error('Missing js string for type "js".');
qrData = `JS:${js}`;
qr_data = `JS:${js}`;
break;
case 'str':
// Raw string data
if (!str) throw new Error('Missing raw string data for type "str".');
qrData = str;
qr_data = str;
break;
default:
throw new Error(`Unknown QR type: ${qr_type}`);
}
if (!qrData) {
if (!qr_data) {
throw new Error('Failed to create QR code data string.');
}
@@ -225,14 +225,14 @@ export async function js_generate_qr_code(qr_type, params = {}) {
// Options match the Python 'qrcode' library defaults closely:
// error_correction = qrcode.constants.ERROR_CORRECT_M
// box_size = 10, border = 1
const dataUrl = await QRCode.toDataURL(qrData, {
const data_url = await QRCode.toDataURL(qr_data, {
errorCorrectionLevel: 'M',
margin: 1, // Corresponds to border
scale: 10, // Corresponds to box_size
type: 'image/png',
});
console.log('Generated QR code data URL:', dataUrl);
return dataUrl;
console.log('Generated QR code data URL:', data_url);
return data_url;
} catch (error) {
console.error('Error generating QR code:', error);
throw new Error('Could not generate QR code image.');