@xuda.io/account_module 1.2.2283 → 1.2.2285

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/index.mjs CHANGED
@@ -67,6 +67,59 @@ const logs_msa = await import(`${module_path}/logs_module/index_msa.mjs`);
67
67
  const ai_msa = await import(`${module_path}/ai_module/index_msa.mjs`);
68
68
  const notification_msa = await import(`${module_path}/notification_module/index_msa.mjs`);
69
69
 
70
+ // ── Account main-project auto-naming ─────────────────────────────────────────
71
+ // The account's main project (is_account_project) is named after its owner:
72
+ // "John's Project" (personal) / "Acme Inc's Project" (business). It's set at
73
+ // creation (misc_module) and re-synced here whenever the account name changes.
74
+ // The name is display-only — everything functional references the project by id,
75
+ // and it's hidden from listings via is_account_project — so renaming is safe.
76
+ const ACCOUNT_PROJECT_LEGACY_RX = /^Account\s+\S+\s+main project$/i;
77
+
78
+ // Owner-derived project name, or null when the account has no name yet.
79
+ export const account_project_name = function (account_info) {
80
+ const ai = account_info || {};
81
+ let base = String((ai.account_type === 'business' ? ai.business_name : ai.first_name) || '').trim();
82
+ if (!base) return null;
83
+ base = base.charAt(0).toUpperCase() + base.slice(1); // capitalize the first letter for display ("john" → "John")
84
+ return `${base}'s Project`; // possessive: "John's", "James's" — always append 's
85
+ };
86
+
87
+ // True when the project's current name is one WE generated (legacy sentinel, the
88
+ // pre-name fallback, or the name derived from ref_info) — so a sync/backfill may
89
+ // replace it. A name the user set themselves is NOT auto → never clobbered.
90
+ export const is_auto_account_project_name = function (name, ref_info) {
91
+ const n = String(name || '').trim();
92
+ if (!n) return true;
93
+ if (ACCOUNT_PROJECT_LEGACY_RX.test(n)) return true;
94
+ if (n === 'My Project') return true; // creation fallback before the name is known
95
+ const derived = account_project_name(ref_info);
96
+ return !!derived && n === derived;
97
+ };
98
+
99
+ // Re-sync the account main project's name to the owner's CURRENT name. Only renames
100
+ // when the project still carries an auto-generated name (relative to ref_info = the
101
+ // name info BEFORE this change); a user-customized name is left untouched. Best-effort.
102
+ export const _sync_account_project_name = async function (account_obj, ref_info) {
103
+ try {
104
+ const proj_id = account_obj?.account_project_id;
105
+ if (!proj_id) return;
106
+ const new_name = account_project_name(account_obj.account_info);
107
+ if (!new_name) return;
108
+ const ret = await db_module.get_couch_doc('xuda_master', proj_id);
109
+ const proj = ret?.data;
110
+ if (!proj?._id || proj.app_name === new_name) return;
111
+ if (!is_auto_account_project_name(proj.app_name, ref_info)) return; // user-customized → keep
112
+ proj.app_name = new_name;
113
+ if (!proj.app_general_prop) proj.app_general_prop = {};
114
+ proj.app_general_prop.app_name = new_name;
115
+ proj.last_updated_ts = Date.now();
116
+ await db_module.save_app_obj(proj, proj_id);
117
+ console.log(`[account_project_name] ${account_obj._id}: renamed account project → "${new_name}"`);
118
+ } catch (err) {
119
+ console.warn(`[account_project_name] sync failed for ${account_obj?._id}:`, err?.message || err);
120
+ }
121
+ };
122
+
70
123
  export const update_account_info = async function (req, job_id, headers) {
71
124
  const { uid } = req;
72
125
  const data = req;
@@ -92,6 +145,13 @@ export const update_account_info = async function (req, job_id, headers) {
92
145
  const ret = await db_module.get_couch_doc('xuda_accounts', uid);
93
146
  var account_obj = ret.data;
94
147
  account_obj.ts = Date.now();
148
+ // Owner's name BEFORE this update — lets the project-name sync (after save) tell an
149
+ // auto-generated project name (safe to rename) from a user-customized one (keep).
150
+ const _old_name_info = {
151
+ first_name: account_obj.account_info?.first_name,
152
+ business_name: account_obj.account_info?.business_name,
153
+ account_type: account_obj.account_info?.account_type,
154
+ };
95
155
  var change = '';
96
156
  var account_info_changes_arr = [];
97
157
  var error = {};
@@ -231,6 +291,13 @@ export const update_account_info = async function (req, job_id, headers) {
231
291
 
232
292
  const save_ret = await db_module.save_couch_doc('xuda_accounts', account_obj);
233
293
 
294
+ // Keep the account main project's name in sync with the owner's name
295
+ // ("John's Project"). Only when a name field actually changed, and only if the
296
+ // project still has an auto-generated name (a user-customized one is left alone).
297
+ if (['first_name', 'business_name', 'account_type'].some((k) => account_info_changes_arr.includes(k))) {
298
+ await _sync_account_project_name(account_obj, _old_name_info);
299
+ }
300
+
234
301
  if (account_obj.account_info?.profile_picture) {
235
302
  if (!account_obj.account_info?.profile_avatar && account_obj.account_info.profile_avatar_stat !== 2) {
236
303
  debugger;
package/index_ms.mjs CHANGED
@@ -17,6 +17,18 @@
17
17
 
18
18
 
19
19
 
20
+ export const account_project_name = async function (...args) {
21
+ return await broker.send_to_queue("account_project_name", ...args);
22
+ };
23
+
24
+ export const is_auto_account_project_name = async function (...args) {
25
+ return await broker.send_to_queue("is_auto_account_project_name", ...args);
26
+ };
27
+
28
+ export const _sync_account_project_name = async function (...args) {
29
+ return await broker.send_to_queue("_sync_account_project_name", ...args);
30
+ };
31
+
20
32
  export const update_account_info = async function (...args) {
21
33
  return await broker.send_to_queue("update_account_info", ...args);
22
34
  };
package/index_msa.mjs CHANGED
@@ -17,6 +17,18 @@
17
17
 
18
18
 
19
19
 
20
+ export const account_project_name = function (...args) {
21
+ broker.send_to_queue_async("account_project_name", ...args);
22
+ };
23
+
24
+ export const is_auto_account_project_name = function (...args) {
25
+ broker.send_to_queue_async("is_auto_account_project_name", ...args);
26
+ };
27
+
28
+ export const _sync_account_project_name = function (...args) {
29
+ broker.send_to_queue_async("_sync_account_project_name", ...args);
30
+ };
31
+
20
32
  export const update_account_info = function (...args) {
21
33
  broker.send_to_queue_async("update_account_info", ...args);
22
34
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/account_module",
3
- "version": "1.2.2283",
3
+ "version": "1.2.2285",
4
4
  "description": "Xuda Account Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {