hazo_notify 1.1.3 → 3.0.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.
Files changed (224) hide show
  1. package/README.md +239 -20
  2. package/dist/components/template_manager/category_dialog.d.ts +11 -0
  3. package/dist/components/template_manager/category_dialog.d.ts.map +1 -0
  4. package/dist/components/template_manager/category_dialog.js +41 -0
  5. package/dist/components/template_manager/category_dialog.js.map +1 -0
  6. package/dist/components/template_manager/category_tree.d.ts +13 -0
  7. package/dist/components/template_manager/category_tree.d.ts.map +1 -0
  8. package/dist/components/template_manager/category_tree.js +45 -0
  9. package/dist/components/template_manager/category_tree.js.map +1 -0
  10. package/dist/components/template_manager/index.d.ts +10 -0
  11. package/dist/components/template_manager/index.d.ts.map +1 -0
  12. package/dist/components/template_manager/index.js +8 -0
  13. package/dist/components/template_manager/index.js.map +1 -0
  14. package/dist/components/template_manager/preview_dialog.d.ts +11 -0
  15. package/dist/components/template_manager/preview_dialog.d.ts.map +1 -0
  16. package/dist/components/template_manager/preview_dialog.js +15 -0
  17. package/dist/components/template_manager/preview_dialog.js.map +1 -0
  18. package/dist/components/template_manager/template_dialog.d.ts +12 -0
  19. package/dist/components/template_manager/template_dialog.d.ts.map +1 -0
  20. package/dist/components/template_manager/template_dialog.js +46 -0
  21. package/dist/components/template_manager/template_dialog.js.map +1 -0
  22. package/dist/components/template_manager/template_editor.d.ts +12 -0
  23. package/dist/components/template_manager/template_editor.d.ts.map +1 -0
  24. package/dist/components/template_manager/template_editor.js +57 -0
  25. package/dist/components/template_manager/template_editor.js.map +1 -0
  26. package/dist/components/template_manager/template_globals_admin.d.ts +8 -0
  27. package/dist/components/template_manager/template_globals_admin.d.ts.map +1 -0
  28. package/dist/components/template_manager/template_globals_admin.js +27 -0
  29. package/dist/components/template_manager/template_globals_admin.js.map +1 -0
  30. package/dist/components/template_manager/template_manager_admin.d.ts +44 -0
  31. package/dist/components/template_manager/template_manager_admin.d.ts.map +1 -0
  32. package/dist/components/template_manager/template_manager_admin.js +272 -0
  33. package/dist/components/template_manager/template_manager_admin.js.map +1 -0
  34. package/dist/lib/emailer/emailer.d.ts.map +1 -0
  35. package/dist/lib/emailer/emailer.js.map +1 -0
  36. package/dist/lib/emailer/index.d.ts.map +1 -0
  37. package/dist/lib/emailer/index.js.map +1 -0
  38. package/dist/lib/emailer/providers/index.d.ts.map +1 -0
  39. package/dist/lib/emailer/providers/index.js.map +1 -0
  40. package/dist/lib/emailer/providers/pop3_provider.d.ts.map +1 -0
  41. package/dist/lib/emailer/providers/pop3_provider.js.map +1 -0
  42. package/dist/lib/emailer/providers/smtp_provider.d.ts.map +1 -0
  43. package/dist/lib/emailer/providers/smtp_provider.js.map +1 -0
  44. package/dist/lib/emailer/providers/zeptomail_provider.d.ts.map +1 -0
  45. package/dist/{emailer → lib/emailer}/providers/zeptomail_provider.js +9 -6
  46. package/dist/lib/emailer/providers/zeptomail_provider.js.map +1 -0
  47. package/dist/lib/emailer/types.d.ts.map +1 -0
  48. package/dist/lib/emailer/types.js.map +1 -0
  49. package/dist/lib/emailer/utils/constants.d.ts.map +1 -0
  50. package/dist/lib/emailer/utils/constants.js.map +1 -0
  51. package/dist/lib/emailer/utils/index.d.ts.map +1 -0
  52. package/dist/lib/emailer/utils/index.js.map +1 -0
  53. package/dist/lib/emailer/utils/logger.d.ts.map +1 -0
  54. package/dist/lib/emailer/utils/logger.js.map +1 -0
  55. package/dist/lib/emailer/utils/validation.d.ts.map +1 -0
  56. package/dist/lib/emailer/utils/validation.js.map +1 -0
  57. package/dist/lib/index.d.ts.map +1 -0
  58. package/dist/lib/index.js.map +1 -0
  59. package/dist/lib/template_manager/cache/scope_chain_cache.d.ts +26 -0
  60. package/dist/lib/template_manager/cache/scope_chain_cache.d.ts.map +1 -0
  61. package/dist/lib/template_manager/cache/scope_chain_cache.js +60 -0
  62. package/dist/lib/template_manager/cache/scope_chain_cache.js.map +1 -0
  63. package/dist/{template_manager → lib/template_manager}/config/config_loader.d.ts +16 -15
  64. package/dist/lib/template_manager/config/config_loader.d.ts.map +1 -0
  65. package/dist/lib/template_manager/config/config_loader.js +98 -0
  66. package/dist/lib/template_manager/config/config_loader.js.map +1 -0
  67. package/dist/{template_manager → lib/template_manager}/config/constants.d.ts +4 -54
  68. package/dist/lib/template_manager/config/constants.d.ts.map +1 -0
  69. package/dist/{template_manager → lib/template_manager}/config/constants.js +4 -84
  70. package/dist/lib/template_manager/config/constants.js.map +1 -0
  71. package/dist/lib/template_manager/config/index.d.ts.map +1 -0
  72. package/dist/lib/template_manager/config/index.js.map +1 -0
  73. package/dist/{template_manager → lib/template_manager}/db/category_repository.d.ts +22 -14
  74. package/dist/lib/template_manager/db/category_repository.d.ts.map +1 -0
  75. package/dist/{template_manager → lib/template_manager}/db/category_repository.js +127 -83
  76. package/dist/lib/template_manager/db/category_repository.js.map +1 -0
  77. package/dist/lib/template_manager/db/index.d.ts +6 -0
  78. package/dist/lib/template_manager/db/index.d.ts.map +1 -0
  79. package/dist/lib/template_manager/db/index.js +6 -0
  80. package/dist/lib/template_manager/db/index.js.map +1 -0
  81. package/dist/{template_manager → lib/template_manager}/db/template_repository.d.ts +25 -28
  82. package/dist/lib/template_manager/db/template_repository.d.ts.map +1 -0
  83. package/dist/lib/template_manager/db/template_repository.js +507 -0
  84. package/dist/lib/template_manager/db/template_repository.js.map +1 -0
  85. package/dist/lib/template_manager/engine/handlebars_engine.d.ts.map +1 -0
  86. package/dist/lib/template_manager/engine/handlebars_engine.js.map +1 -0
  87. package/dist/lib/template_manager/engine/index.d.ts.map +1 -0
  88. package/dist/lib/template_manager/engine/index.js.map +1 -0
  89. package/dist/{template_manager → lib/template_manager}/engine/variable_resolver.d.ts +13 -14
  90. package/dist/lib/template_manager/engine/variable_resolver.d.ts.map +1 -0
  91. package/dist/{template_manager → lib/template_manager}/engine/variable_resolver.js +25 -33
  92. package/dist/lib/template_manager/engine/variable_resolver.js.map +1 -0
  93. package/dist/lib/template_manager/index.d.ts +56 -0
  94. package/dist/lib/template_manager/index.d.ts.map +1 -0
  95. package/dist/lib/template_manager/index.js +68 -0
  96. package/dist/lib/template_manager/index.js.map +1 -0
  97. package/dist/lib/template_manager/init.d.ts +54 -0
  98. package/dist/lib/template_manager/init.d.ts.map +1 -0
  99. package/dist/lib/template_manager/init.js +85 -0
  100. package/dist/lib/template_manager/init.js.map +1 -0
  101. package/dist/lib/template_manager/registry.d.ts +23 -0
  102. package/dist/lib/template_manager/registry.d.ts.map +1 -0
  103. package/dist/lib/template_manager/registry.js +33 -0
  104. package/dist/lib/template_manager/registry.js.map +1 -0
  105. package/dist/lib/template_manager/seed/sync.d.ts +8 -0
  106. package/dist/lib/template_manager/seed/sync.d.ts.map +1 -0
  107. package/dist/lib/template_manager/seed/sync.js +77 -0
  108. package/dist/lib/template_manager/seed/sync.js.map +1 -0
  109. package/dist/{template_manager → lib/template_manager}/template_manager.d.ts +25 -27
  110. package/dist/lib/template_manager/template_manager.d.ts.map +1 -0
  111. package/dist/{template_manager → lib/template_manager}/template_manager.js +41 -55
  112. package/dist/lib/template_manager/template_manager.js.map +1 -0
  113. package/dist/{template_manager → lib/template_manager}/types.d.ts +32 -125
  114. package/dist/lib/template_manager/types.d.ts.map +1 -0
  115. package/dist/lib/template_manager/types.js +12 -0
  116. package/dist/lib/template_manager/types.js.map +1 -0
  117. package/dist/lib/template_manager/utils/index.d.ts.map +1 -0
  118. package/dist/lib/template_manager/utils/index.js.map +1 -0
  119. package/dist/lib/template_manager/utils/system_variables.d.ts.map +1 -0
  120. package/dist/lib/template_manager/utils/system_variables.js.map +1 -0
  121. package/dist/lib/template_manager/utils/validation.d.ts.map +1 -0
  122. package/dist/lib/template_manager/utils/validation.js.map +1 -0
  123. package/dist/lib/utils.d.ts +3 -0
  124. package/dist/lib/utils.d.ts.map +1 -0
  125. package/dist/lib/utils.js +6 -0
  126. package/dist/lib/utils.js.map +1 -0
  127. package/migrations/002_scope_migration.sql +93 -0
  128. package/package.json +57 -37
  129. package/dist/emailer/emailer.d.ts.map +0 -1
  130. package/dist/emailer/emailer.js.map +0 -1
  131. package/dist/emailer/index.d.ts.map +0 -1
  132. package/dist/emailer/index.js.map +0 -1
  133. package/dist/emailer/providers/index.d.ts.map +0 -1
  134. package/dist/emailer/providers/index.js.map +0 -1
  135. package/dist/emailer/providers/pop3_provider.d.ts.map +0 -1
  136. package/dist/emailer/providers/pop3_provider.js.map +0 -1
  137. package/dist/emailer/providers/smtp_provider.d.ts.map +0 -1
  138. package/dist/emailer/providers/smtp_provider.js.map +0 -1
  139. package/dist/emailer/providers/zeptomail_provider.d.ts.map +0 -1
  140. package/dist/emailer/providers/zeptomail_provider.js.map +0 -1
  141. package/dist/emailer/types.d.ts.map +0 -1
  142. package/dist/emailer/types.js.map +0 -1
  143. package/dist/emailer/utils/constants.d.ts.map +0 -1
  144. package/dist/emailer/utils/constants.js.map +0 -1
  145. package/dist/emailer/utils/index.d.ts.map +0 -1
  146. package/dist/emailer/utils/index.js.map +0 -1
  147. package/dist/emailer/utils/logger.d.ts.map +0 -1
  148. package/dist/emailer/utils/logger.js.map +0 -1
  149. package/dist/emailer/utils/validation.d.ts.map +0 -1
  150. package/dist/emailer/utils/validation.js.map +0 -1
  151. package/dist/index.d.ts.map +0 -1
  152. package/dist/index.js.map +0 -1
  153. package/dist/template_manager/config/config_loader.d.ts.map +0 -1
  154. package/dist/template_manager/config/config_loader.js +0 -154
  155. package/dist/template_manager/config/config_loader.js.map +0 -1
  156. package/dist/template_manager/config/constants.d.ts.map +0 -1
  157. package/dist/template_manager/config/constants.js.map +0 -1
  158. package/dist/template_manager/config/index.d.ts.map +0 -1
  159. package/dist/template_manager/config/index.js.map +0 -1
  160. package/dist/template_manager/db/category_repository.d.ts.map +0 -1
  161. package/dist/template_manager/db/category_repository.js.map +0 -1
  162. package/dist/template_manager/db/index.d.ts +0 -6
  163. package/dist/template_manager/db/index.d.ts.map +0 -1
  164. package/dist/template_manager/db/index.js +0 -6
  165. package/dist/template_manager/db/index.js.map +0 -1
  166. package/dist/template_manager/db/template_repository.d.ts.map +0 -1
  167. package/dist/template_manager/db/template_repository.js +0 -674
  168. package/dist/template_manager/db/template_repository.js.map +0 -1
  169. package/dist/template_manager/engine/handlebars_engine.d.ts.map +0 -1
  170. package/dist/template_manager/engine/handlebars_engine.js.map +0 -1
  171. package/dist/template_manager/engine/index.d.ts.map +0 -1
  172. package/dist/template_manager/engine/index.js.map +0 -1
  173. package/dist/template_manager/engine/variable_resolver.d.ts.map +0 -1
  174. package/dist/template_manager/engine/variable_resolver.js.map +0 -1
  175. package/dist/template_manager/index.d.ts +0 -32
  176. package/dist/template_manager/index.d.ts.map +0 -1
  177. package/dist/template_manager/index.js +0 -41
  178. package/dist/template_manager/index.js.map +0 -1
  179. package/dist/template_manager/template_manager.d.ts.map +0 -1
  180. package/dist/template_manager/template_manager.js.map +0 -1
  181. package/dist/template_manager/types.d.ts.map +0 -1
  182. package/dist/template_manager/types.js +0 -11
  183. package/dist/template_manager/types.js.map +0 -1
  184. package/dist/template_manager/utils/index.d.ts.map +0 -1
  185. package/dist/template_manager/utils/index.js.map +0 -1
  186. package/dist/template_manager/utils/system_variables.d.ts.map +0 -1
  187. package/dist/template_manager/utils/system_variables.js.map +0 -1
  188. package/dist/template_manager/utils/validation.d.ts.map +0 -1
  189. package/dist/template_manager/utils/validation.js.map +0 -1
  190. /package/dist/{emailer → lib/emailer}/emailer.d.ts +0 -0
  191. /package/dist/{emailer → lib/emailer}/emailer.js +0 -0
  192. /package/dist/{emailer → lib/emailer}/index.d.ts +0 -0
  193. /package/dist/{emailer → lib/emailer}/index.js +0 -0
  194. /package/dist/{emailer → lib/emailer}/providers/index.d.ts +0 -0
  195. /package/dist/{emailer → lib/emailer}/providers/index.js +0 -0
  196. /package/dist/{emailer → lib/emailer}/providers/pop3_provider.d.ts +0 -0
  197. /package/dist/{emailer → lib/emailer}/providers/pop3_provider.js +0 -0
  198. /package/dist/{emailer → lib/emailer}/providers/smtp_provider.d.ts +0 -0
  199. /package/dist/{emailer → lib/emailer}/providers/smtp_provider.js +0 -0
  200. /package/dist/{emailer → lib/emailer}/providers/zeptomail_provider.d.ts +0 -0
  201. /package/dist/{emailer → lib/emailer}/types.d.ts +0 -0
  202. /package/dist/{emailer → lib/emailer}/types.js +0 -0
  203. /package/dist/{emailer → lib/emailer}/utils/constants.d.ts +0 -0
  204. /package/dist/{emailer → lib/emailer}/utils/constants.js +0 -0
  205. /package/dist/{emailer → lib/emailer}/utils/index.d.ts +0 -0
  206. /package/dist/{emailer → lib/emailer}/utils/index.js +0 -0
  207. /package/dist/{emailer → lib/emailer}/utils/logger.d.ts +0 -0
  208. /package/dist/{emailer → lib/emailer}/utils/logger.js +0 -0
  209. /package/dist/{emailer → lib/emailer}/utils/validation.d.ts +0 -0
  210. /package/dist/{emailer → lib/emailer}/utils/validation.js +0 -0
  211. /package/dist/{index.d.ts → lib/index.d.ts} +0 -0
  212. /package/dist/{index.js → lib/index.js} +0 -0
  213. /package/dist/{template_manager → lib/template_manager}/config/index.d.ts +0 -0
  214. /package/dist/{template_manager → lib/template_manager}/config/index.js +0 -0
  215. /package/dist/{template_manager → lib/template_manager}/engine/handlebars_engine.d.ts +0 -0
  216. /package/dist/{template_manager → lib/template_manager}/engine/handlebars_engine.js +0 -0
  217. /package/dist/{template_manager → lib/template_manager}/engine/index.d.ts +0 -0
  218. /package/dist/{template_manager → lib/template_manager}/engine/index.js +0 -0
  219. /package/dist/{template_manager → lib/template_manager}/utils/index.d.ts +0 -0
  220. /package/dist/{template_manager → lib/template_manager}/utils/index.js +0 -0
  221. /package/dist/{template_manager → lib/template_manager}/utils/system_variables.d.ts +0 -0
  222. /package/dist/{template_manager → lib/template_manager}/utils/system_variables.js +0 -0
  223. /package/dist/{template_manager → lib/template_manager}/utils/validation.d.ts +0 -0
  224. /package/dist/{template_manager → lib/template_manager}/utils/validation.js +0 -0
@@ -1,18 +1,21 @@
1
1
  /**
2
- * Category repository for template manager
2
+ * Category repository for template manager (v2.0.0)
3
3
  *
4
- * CRUD operations for template categories using hazo_connect
4
+ * CRUD operations for template categories using hazo_connect.
5
+ * Uses scope_id (null = global) for hierarchical category resolution.
5
6
  */
6
- import { TABLE_TEMPLATE_CATEGORY, TABLE_TEMPLATES, ERROR_MESSAGES, } from '../config/constants.js';
7
+ import { TABLE_TEMPLATE_CATEGORY, ERROR_MESSAGES, } from '../config/constants.js';
7
8
  import { validate_category_name, validate_uuid } from '../utils/validation.js';
8
9
  /**
9
- * List all template categories for an organization
10
+ * List all template categories visible for a given scope (scoped rows + global rows).
11
+ * When scope_id is provided, scoped rows override global rows with the same
12
+ * template_category_name in the returned set.
10
13
  *
11
14
  * @param hazo_connect - Database connection instance
12
- * @param org_id - Organization ID
15
+ * @param scope_id - Scope to list categories for (null/undefined = global only)
13
16
  * @returns Response with array of categories
14
17
  */
15
- export async function list_categories(hazo_connect, org_id) {
18
+ export async function list_categories(hazo_connect, scope_id) {
16
19
  try {
17
20
  if (!hazo_connect) {
18
21
  return {
@@ -21,20 +24,40 @@ export async function list_categories(hazo_connect, org_id) {
21
24
  message: ERROR_MESSAGES.DB_CONNECTION_REQUIRED,
22
25
  };
23
26
  }
24
- if (!validate_uuid(org_id)) {
25
- return {
26
- success: false,
27
- error: 'Invalid organization ID',
28
- message: 'Invalid organization ID format',
29
- };
27
+ let categories;
28
+ if (scope_id) {
29
+ const scoped_q = hazo_connect
30
+ .query()
31
+ .from(TABLE_TEMPLATE_CATEGORY)
32
+ .select('*')
33
+ .where('scope_id', 'eq', scope_id);
34
+ const global_q = hazo_connect
35
+ .query()
36
+ .from(TABLE_TEMPLATE_CATEGORY)
37
+ .select('*')
38
+ .where('scope_id', 'is', null);
39
+ const [scoped_rows, global_rows] = await Promise.all([
40
+ hazo_connect.list(scoped_q),
41
+ hazo_connect.list(global_q),
42
+ ]);
43
+ // Merge: scoped rows override global rows with the same category name
44
+ const by_name = new Map();
45
+ for (const row of global_rows)
46
+ by_name.set(row.template_category_name, row);
47
+ for (const row of scoped_rows)
48
+ by_name.set(row.template_category_name, row); // scoped wins
49
+ categories = Array.from(by_name.values()).sort((a, b) => a.template_category_name.localeCompare(b.template_category_name));
50
+ }
51
+ else {
52
+ // Global-only
53
+ const q = hazo_connect
54
+ .query()
55
+ .from(TABLE_TEMPLATE_CATEGORY)
56
+ .select('*')
57
+ .where('scope_id', 'is', null)
58
+ .order('template_category_name', 'asc');
59
+ categories = await hazo_connect.list(q);
30
60
  }
31
- const query = hazo_connect
32
- .query()
33
- .from(TABLE_TEMPLATE_CATEGORY)
34
- .select('*')
35
- .where('org_id', 'eq', org_id)
36
- .order('template_category_name', 'asc');
37
- const categories = await hazo_connect.list(query);
38
61
  return {
39
62
  success: true,
40
63
  data: categories,
@@ -42,16 +65,16 @@ export async function list_categories(hazo_connect, org_id) {
42
65
  };
43
66
  }
44
67
  catch (error) {
45
- const error_message = error instanceof Error ? error.message : 'Unknown error occurred';
68
+ const msg = error instanceof Error ? error.message : 'Unknown error';
46
69
  return {
47
70
  success: false,
48
- error: error_message,
49
- message: `Failed to list categories: ${error_message}`,
71
+ error: msg,
72
+ message: `Failed to list categories: ${msg}`,
50
73
  };
51
74
  }
52
75
  }
53
76
  /**
54
- * Get a single template category by ID
77
+ * Get a single template category by ID.
55
78
  *
56
79
  * @param hazo_connect - Database connection instance
57
80
  * @param id - Category ID
@@ -88,16 +111,16 @@ export async function get_category(hazo_connect, id) {
88
111
  };
89
112
  }
90
113
  catch (error) {
91
- const error_message = error instanceof Error ? error.message : 'Unknown error occurred';
114
+ const msg = error instanceof Error ? error.message : 'Unknown error';
92
115
  return {
93
116
  success: false,
94
- error: error_message,
95
- message: `Failed to get category: ${error_message}`,
117
+ error: msg,
118
+ message: `Failed to get category: ${msg}`,
96
119
  };
97
120
  }
98
121
  }
99
122
  /**
100
- * Create a new template category
123
+ * Create a new template category.
101
124
  *
102
125
  * @param hazo_connect - Database connection instance
103
126
  * @param data - Category input data
@@ -119,23 +142,15 @@ export async function create_category(hazo_connect, data) {
119
142
  message: ERROR_MESSAGES.INVALID_CATEGORY_NAME,
120
143
  };
121
144
  }
122
- if (!validate_uuid(data.org_id)) {
145
+ if (data.scope_id != null && !validate_uuid(data.scope_id)) {
123
146
  return {
124
147
  success: false,
125
- error: 'Invalid organization ID',
126
- message: 'Invalid organization ID format',
127
- };
128
- }
129
- if (!validate_uuid(data.root_org_id)) {
130
- return {
131
- success: false,
132
- error: 'Invalid root organization ID',
133
- message: 'Invalid root organization ID format',
148
+ error: 'Invalid scope ID',
149
+ message: 'Invalid scope ID format',
134
150
  };
135
151
  }
136
152
  const category = await hazo_connect.insert(TABLE_TEMPLATE_CATEGORY, {
137
- org_id: data.org_id,
138
- root_org_id: data.root_org_id,
153
+ scope_id: data.scope_id,
139
154
  template_category_name: data.template_category_name.trim(),
140
155
  });
141
156
  return {
@@ -145,16 +160,16 @@ export async function create_category(hazo_connect, data) {
145
160
  };
146
161
  }
147
162
  catch (error) {
148
- const error_message = error instanceof Error ? error.message : 'Unknown error occurred';
163
+ const msg = error instanceof Error ? error.message : 'Unknown error';
149
164
  return {
150
165
  success: false,
151
- error: error_message,
152
- message: `Failed to create category: ${error_message}`,
166
+ error: msg,
167
+ message: `Failed to create category: ${msg}`,
153
168
  };
154
169
  }
155
170
  }
156
171
  /**
157
- * Update an existing template category
172
+ * Update an existing template category.
158
173
  *
159
174
  * @param hazo_connect - Database connection instance
160
175
  * @param id - Category ID
@@ -185,7 +200,6 @@ export async function update_category(hazo_connect, id, data) {
185
200
  message: ERROR_MESSAGES.INVALID_CATEGORY_NAME,
186
201
  };
187
202
  }
188
- // Check if category exists
189
203
  const existing = await hazo_connect.findById(TABLE_TEMPLATE_CATEGORY, id);
190
204
  if (!existing) {
191
205
  return {
@@ -198,7 +212,8 @@ export async function update_category(hazo_connect, id, data) {
198
212
  changed_at: new Date().toISOString(),
199
213
  };
200
214
  if (data.template_category_name !== undefined) {
201
- update_data.template_category_name = data.template_category_name.trim();
215
+ update_data.template_category_name =
216
+ data.template_category_name.trim();
202
217
  }
203
218
  const category = await hazo_connect.updateById(TABLE_TEMPLATE_CATEGORY, id, update_data);
204
219
  return {
@@ -208,16 +223,18 @@ export async function update_category(hazo_connect, id, data) {
208
223
  };
209
224
  }
210
225
  catch (error) {
211
- const error_message = error instanceof Error ? error.message : 'Unknown error occurred';
226
+ const msg = error instanceof Error ? error.message : 'Unknown error';
212
227
  return {
213
228
  success: false,
214
- error: error_message,
215
- message: `Failed to update category: ${error_message}`,
229
+ error: msg,
230
+ message: `Failed to update category: ${msg}`,
216
231
  };
217
232
  }
218
233
  }
219
234
  /**
220
- * Delete a template category and all its templates
235
+ * Delete a template category.
236
+ * If the category has templates and the FK is RESTRICT, the DB will reject
237
+ * the delete — the error propagates to the caller to handle.
221
238
  *
222
239
  * @param hazo_connect - Database connection instance
223
240
  * @param id - Category ID
@@ -239,7 +256,6 @@ export async function delete_category(hazo_connect, id) {
239
256
  message: 'Invalid category ID format',
240
257
  };
241
258
  }
242
- // Check if category exists
243
259
  const existing = await hazo_connect.findById(TABLE_TEMPLATE_CATEGORY, id);
244
260
  if (!existing) {
245
261
  return {
@@ -248,43 +264,34 @@ export async function delete_category(hazo_connect, id) {
248
264
  message: ERROR_MESSAGES.CATEGORY_NOT_FOUND,
249
265
  };
250
266
  }
251
- // First delete all templates in this category
252
- // Note: This could be handled by DB cascade, but we do it explicitly
253
- const templates_query = hazo_connect
254
- .query()
255
- .from(TABLE_TEMPLATES)
256
- .select('id')
257
- .where('template_category_id', 'eq', id);
258
- const templates = await hazo_connect.list(templates_query);
259
- for (const template of templates) {
260
- await hazo_connect.deleteById(TABLE_TEMPLATES, template.id);
261
- }
262
- // Delete the category
263
267
  await hazo_connect.deleteById(TABLE_TEMPLATE_CATEGORY, id);
264
268
  return {
265
269
  success: true,
266
270
  data: true,
267
- message: `Category deleted along with ${templates.length} templates`,
271
+ message: 'Category deleted successfully',
268
272
  };
269
273
  }
270
274
  catch (error) {
271
- const error_message = error instanceof Error ? error.message : 'Unknown error occurred';
275
+ const msg = error instanceof Error ? error.message : 'Unknown error';
272
276
  return {
273
277
  success: false,
274
- error: error_message,
275
- message: `Failed to delete category: ${error_message}`,
278
+ error: msg,
279
+ message: `Failed to delete category: ${msg}`,
276
280
  };
277
281
  }
278
282
  }
279
283
  /**
280
- * Find a category by name within an organization
284
+ * Find a category by name using hierarchical scope resolution.
285
+ *
286
+ * scope_chain: [scope_id, ...ancestors] — chain[0] is deepest (most preferred).
287
+ * Empty array → global-only lookup.
281
288
  *
282
289
  * @param hazo_connect - Database connection instance
283
- * @param org_id - Organization ID
284
- * @param name - Category name
285
- * @returns Response with category if found
290
+ * @param scope_chain - Ordered scope chain, deepest first
291
+ * @param name - Category name to look up
292
+ * @returns Response with best-matching category or null if not found
286
293
  */
287
- export async function find_category_by_name(hazo_connect, org_id, name) {
294
+ export async function find_category_by_name_in_chain(hazo_connect, scope_chain, name) {
288
295
  try {
289
296
  if (!hazo_connect) {
290
297
  return {
@@ -293,25 +300,62 @@ export async function find_category_by_name(hazo_connect, org_id, name) {
293
300
  message: ERROR_MESSAGES.DB_CONNECTION_REQUIRED,
294
301
  };
295
302
  }
296
- const query = hazo_connect
297
- .query()
298
- .from(TABLE_TEMPLATE_CATEGORY)
299
- .select('*')
300
- .where('org_id', 'eq', org_id)
301
- .where('template_category_name', 'eq', name.trim());
302
- const categories = await hazo_connect.list(query);
303
+ let candidates = [];
304
+ if (scope_chain.length > 0) {
305
+ const scoped_q = hazo_connect
306
+ .query()
307
+ .from(TABLE_TEMPLATE_CATEGORY)
308
+ .select('*')
309
+ .where('template_category_name', 'eq', name.trim())
310
+ .whereIn('scope_id', scope_chain);
311
+ const global_q = hazo_connect
312
+ .query()
313
+ .from(TABLE_TEMPLATE_CATEGORY)
314
+ .select('*')
315
+ .where('template_category_name', 'eq', name.trim())
316
+ .where('scope_id', 'is', null);
317
+ const [scoped, global] = await Promise.all([
318
+ hazo_connect.list(scoped_q),
319
+ hazo_connect.list(global_q),
320
+ ]);
321
+ candidates = [...scoped, ...global];
322
+ }
323
+ else {
324
+ const q = hazo_connect
325
+ .query()
326
+ .from(TABLE_TEMPLATE_CATEGORY)
327
+ .select('*')
328
+ .where('template_category_name', 'eq', name.trim())
329
+ .where('scope_id', 'is', null);
330
+ candidates = await hazo_connect.list(q);
331
+ }
332
+ if (candidates.length === 0) {
333
+ return {
334
+ success: true,
335
+ data: null,
336
+ message: 'Category not found',
337
+ };
338
+ }
339
+ // Sort: deepest scope first, global last
340
+ candidates.sort((a, b) => {
341
+ const ai = a.scope_id !== null ? scope_chain.indexOf(a.scope_id) : -1;
342
+ const bi = b.scope_id !== null ? scope_chain.indexOf(b.scope_id) : -1;
343
+ const a_rank = a.scope_id === null ? Infinity : ai === -1 ? Infinity : ai;
344
+ const b_rank = b.scope_id === null ? Infinity : bi === -1 ? Infinity : bi;
345
+ return a_rank - b_rank;
346
+ });
303
347
  return {
304
348
  success: true,
305
- data: categories.length > 0 ? categories[0] : null,
306
- message: categories.length > 0 ? 'Category found' : 'Category not found',
349
+ data: candidates[0],
350
+ message: 'Category found',
307
351
  };
308
352
  }
309
353
  catch (error) {
310
- const error_message = error instanceof Error ? error.message : 'Unknown error occurred';
354
+ const msg = error instanceof Error ? error.message : 'Unknown error';
311
355
  return {
312
356
  success: false,
313
- error: error_message,
314
- message: `Failed to find category: ${error_message}`,
357
+ error: msg,
358
+ message: `Failed to find category: ${msg}`,
315
359
  };
316
360
  }
317
361
  }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category_repository.js","sourceRoot":"","sources":["../../../../src/lib/template_manager/db/category_repository.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AASH,OAAO,EACL,uBAAuB,EACvB,cAAc,GACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE/E;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,YAAiC,EACjC,QAAwB;IAExB,IAAI,C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AAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC;QACrE,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,GAAG;YACV,OAAO,EAAE,4BAA4B,GAAG,EAAE;SAC3C,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Database barrel export for template manager (v2.0.0)
3
+ */
4
+ export { list_categories, get_category, create_category, update_category, delete_category, find_category_by_name_in_chain, } from './category_repository.js';
5
+ export { list_templates, list_templates_by_category, get_template, get_template_by_name, create_template, update_template, delete_template, } from './template_repository.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/lib/template_manager/db/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,eAAe,EACf,YAAY,EACZ,eAAe,EACf,eAAe,EACf,eAAe,EACf,8BAA8B,GAC/B,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,cAAc,EACd,0BAA0B,EAC1B,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,eAAe,EACf,eAAe,GAChB,MAAM,0BAA0B,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Database barrel export for template manager (v2.0.0)
3
+ */
4
+ export { list_categories, get_category, create_category, update_category, delete_category, find_category_by_name_in_chain, } from './category_repository.js';
5
+ export { list_templates, list_templates_by_category, get_template, get_template_by_name, create_template, update_template, delete_template, } from './template_repository.js';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/lib/template_manager/db/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,eAAe,EACf,YAAY,EACZ,eAAe,EACf,eAAe,EACf,eAAe,EACf,8BAA8B,GAC/B,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,cAAc,EACd,0BAA0B,EAC1B,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,eAAe,EACf,eAAe,GAChB,MAAM,0BAA0B,CAAC"}
@@ -1,27 +1,30 @@
1
1
  /**
2
- * Template repository for template manager
2
+ * Template repository for template manager (v2.0.0)
3
3
  *
4
- * CRUD operations for email templates using hazo_connect
4
+ * CRUD operations for email templates using hazo_connect.
5
+ * Uses scope_id (null = global) for hierarchical template resolution.
5
6
  */
6
7
  import type { HazoConnectInstance, EmailTemplate, EmailTemplateInput, EmailTemplateUpdate, TemplateOperationResponse } from '../types.js';
7
8
  /**
8
- * List all email templates for an organization
9
+ * List all templates visible for a given scope (scoped rows + global rows).
10
+ * When scope_id is provided, scoped rows override global rows with the same
11
+ * template_name in the returned set.
9
12
  *
10
13
  * @param hazo_connect - Database connection instance
11
- * @param org_id - Organization ID
14
+ * @param scope_id - Scope to list templates for (null/undefined = global only)
12
15
  * @returns Response with array of templates
13
16
  */
14
- export declare function list_templates(hazo_connect: HazoConnectInstance, org_id: string): Promise<TemplateOperationResponse<EmailTemplate[]>>;
17
+ export declare function list_templates(hazo_connect: HazoConnectInstance, scope_id?: string | null): Promise<TemplateOperationResponse<EmailTemplate[]>>;
15
18
  /**
16
- * List templates by category
19
+ * List templates by category ID.
17
20
  *
18
21
  * @param hazo_connect - Database connection instance
19
22
  * @param category_id - Category ID
20
23
  * @returns Response with array of templates
21
24
  */
22
- export declare function list_templates_by_category(hazo_connect: HazoConnectInstance, category_id: string): Promise<TemplateOperationResponse<EmailTemplate[]>>;
25
+ export declare function list_templates_by_category(hazo_connect: HazoConnectInstance, category_id: string, scope_id?: string | null): Promise<TemplateOperationResponse<EmailTemplate[]>>;
23
26
  /**
24
- * Get a single email template by ID
27
+ * Get a single email template by ID.
25
28
  *
26
29
  * @param hazo_connect - Database connection instance
27
30
  * @param id - Template ID
@@ -29,16 +32,20 @@ export declare function list_templates_by_category(hazo_connect: HazoConnectInst
29
32
  */
30
33
  export declare function get_template(hazo_connect: HazoConnectInstance, id: string): Promise<TemplateOperationResponse<EmailTemplate>>;
31
34
  /**
32
- * Get a template by name within an organization
35
+ * Get a template by name using hierarchical scope resolution.
36
+ *
37
+ * scope_chain: [scope_id, ...ancestors] — chain[0] is deepest (most preferred).
38
+ * Empty array → global-only lookup.
39
+ * Prefers the deepest-scope match; falls back to global (scope_id IS NULL).
33
40
  *
34
41
  * @param hazo_connect - Database connection instance
35
- * @param org_id - Organization ID
36
- * @param template_name - Template name
37
- * @returns Response with template if found
42
+ * @param scope_chain - Ordered scope chain, deepest first
43
+ * @param template_name - Template name to look up
44
+ * @returns Response with the best-matching template
38
45
  */
39
- export declare function get_template_by_name(hazo_connect: HazoConnectInstance, org_id: string, template_name: string): Promise<TemplateOperationResponse<EmailTemplate>>;
46
+ export declare function get_template_by_name(hazo_connect: HazoConnectInstance, scope_chain: string[], template_name: string): Promise<TemplateOperationResponse<EmailTemplate>>;
40
47
  /**
41
- * Create a new email template
48
+ * Create a new email template.
42
49
  *
43
50
  * @param hazo_connect - Database connection instance
44
51
  * @param data - Template input data
@@ -46,31 +53,21 @@ export declare function get_template_by_name(hazo_connect: HazoConnectInstance,
46
53
  */
47
54
  export declare function create_template(hazo_connect: HazoConnectInstance, data: EmailTemplateInput): Promise<TemplateOperationResponse<EmailTemplate>>;
48
55
  /**
49
- * Update an existing email template
56
+ * Update an existing email template.
50
57
  *
51
58
  * @param hazo_connect - Database connection instance
52
59
  * @param id - Template ID
53
- * @param data - Update data
60
+ * @param data - Fields to update
54
61
  * @returns Response with updated template
55
62
  */
56
63
  export declare function update_template(hazo_connect: HazoConnectInstance, id: string, data: EmailTemplateUpdate): Promise<TemplateOperationResponse<EmailTemplate>>;
57
64
  /**
58
- * Delete an email template
65
+ * Delete a template by ID.
66
+ * Permission enforcement (manifest protection) is done at the API layer.
59
67
  *
60
68
  * @param hazo_connect - Database connection instance
61
69
  * @param id - Template ID
62
70
  * @returns Response with deletion result
63
71
  */
64
72
  export declare function delete_template(hazo_connect: HazoConnectInstance, id: string): Promise<TemplateOperationResponse<boolean>>;
65
- /**
66
- * Seed default templates for an organization
67
- *
68
- * Creates a "System" category and populates it with default templates
69
- *
70
- * @param hazo_connect - Database connection instance
71
- * @param org_id - Organization ID
72
- * @param root_org_id - Root organization ID
73
- * @returns Response with seeded templates
74
- */
75
- export declare function seed_default_templates(hazo_connect: HazoConnectInstance, org_id: string, root_org_id: string): Promise<TemplateOperationResponse<EmailTemplate[]>>;
76
73
  //# sourceMappingURL=template_repository.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"template_repository.d.ts","sourceRoot":"","sources":["../../../../src/lib/template_manager/db/template_repository.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,aAAa,EACb,kBAAkB,EAClB,mBAAmB,EACnB,yBAAyB,EAC1B,MAAM,aAAa,CAAC;AAarB;;;;;;;;GAQG;AACH,wBAAsB,cAAc,CAClC,YAAY,EAAE,mBAAmB,EACjC,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,GACvB,OAAO,CAAC,yBAAyB,CAAC,aAAa,EAAE,CAAC,CAAC,CA8DrD;AAED;;;;;;GAMG;AACH,wBAAsB,0BAA0B,CAC9C,YAAY,EAAE,mBAAmB,EACjC,WAAW,EAAE,MAAM,EACnB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,GACvB,OAAO,CAAC,yBAAyB,CAAC,aAAa,EAAE,CAAC,CAAC,CAoErD;AAED;;;;;;GAMG;AACH,wBAAsB,YAAY,CAChC,YAAY,EAAE,mBAAmB,EACjC,EAAE,EAAE,MAAM,GACT,OAAO,CAAC,yBAAyB,CAAC,aAAa,CAAC,CAAC,CA4CnD;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,oBAAoB,CACxC,YAAY,EAAE,mBAAmB,EACjC,WAAW,EAAE,MAAM,EAAE,EACrB,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,yBAAyB,CAAC,a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