@memberjunction/templates-base-types 4.0.0 → 4.1.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
CHANGED
|
@@ -1,15 +1,45 @@
|
|
|
1
1
|
# @memberjunction/templates-base-types
|
|
2
2
|
|
|
3
|
-
Base types and
|
|
3
|
+
Base types and metadata caching engine for MemberJunction's templating system. Provides cached access to template metadata and can be used on both client and server.
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph TD
|
|
9
|
+
subgraph "@memberjunction/templates-base-types"
|
|
10
|
+
A[TemplateEngineBase] --> B[Templates Cache]
|
|
11
|
+
A --> C[Template Contents]
|
|
12
|
+
A --> D[Template Params]
|
|
13
|
+
A --> E[Content Types]
|
|
14
|
+
A --> F[Categories]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
subgraph "Loaded via Dataset"
|
|
18
|
+
G["Template_Metadata<br/>Dataset"]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
G --> A
|
|
22
|
+
|
|
23
|
+
H["@memberjunction/templates<br/>(Server Engine)"] --> A
|
|
24
|
+
|
|
25
|
+
style A fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
26
|
+
style B fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
27
|
+
style C fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
28
|
+
style D fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
29
|
+
style E fill:#7c5295,stroke:#563a6b,color:#fff
|
|
30
|
+
style F fill:#7c5295,stroke:#563a6b,color:#fff
|
|
31
|
+
style H fill:#b8762f,stroke:#8a5722,color:#fff
|
|
32
|
+
```
|
|
4
33
|
|
|
5
34
|
## Overview
|
|
6
35
|
|
|
7
|
-
This package
|
|
36
|
+
This package provides the foundational layer for MemberJunction's templating system:
|
|
8
37
|
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
12
|
-
- **Template
|
|
38
|
+
- **TemplateEngineBase**: Singleton engine that loads and caches all template metadata via the `Template_Metadata` dataset
|
|
39
|
+
- **TemplateRenderResult**: Standard result type for template rendering operations
|
|
40
|
+
- **Metadata Access**: Cached getters for templates, content types, categories, contents, and params
|
|
41
|
+
- **Template Lookup**: Convenience method for finding templates by name (case-insensitive)
|
|
42
|
+
- **Content Association**: Automatically associates template contents and params with their parent templates after loading
|
|
13
43
|
|
|
14
44
|
## Installation
|
|
15
45
|
|
|
@@ -17,72 +47,50 @@ This package serves as the foundation for the MemberJunction templating system,
|
|
|
17
47
|
npm install @memberjunction/templates-base-types
|
|
18
48
|
```
|
|
19
49
|
|
|
20
|
-
##
|
|
21
|
-
|
|
22
|
-
### TemplateEngineBase
|
|
50
|
+
## Usage
|
|
23
51
|
|
|
24
|
-
|
|
52
|
+
### Initializing the Engine
|
|
25
53
|
|
|
26
54
|
```typescript
|
|
27
55
|
import { TemplateEngineBase } from '@memberjunction/templates-base-types';
|
|
28
56
|
|
|
29
|
-
//
|
|
30
|
-
|
|
57
|
+
// Load template metadata
|
|
58
|
+
await TemplateEngineBase.Instance.Config(false, contextUser);
|
|
31
59
|
|
|
32
|
-
//
|
|
33
|
-
await
|
|
34
|
-
|
|
35
|
-
// Access templates
|
|
36
|
-
const templates = templateEngine.Templates;
|
|
37
|
-
|
|
38
|
-
// Find a specific template by name (case-insensitive)
|
|
39
|
-
const template = templateEngine.FindTemplate('Welcome Email');
|
|
60
|
+
// Force refresh
|
|
61
|
+
await TemplateEngineBase.Instance.Config(true, contextUser);
|
|
40
62
|
```
|
|
41
63
|
|
|
42
|
-
###
|
|
43
|
-
|
|
44
|
-
An enhanced version of the base `TemplateEntity` that includes associated content and parameters as properties, along with utility methods.
|
|
64
|
+
### Accessing Template Metadata
|
|
45
65
|
|
|
46
66
|
```typescript
|
|
47
|
-
|
|
67
|
+
const engine = TemplateEngineBase.Instance;
|
|
48
68
|
|
|
49
|
-
//
|
|
50
|
-
const
|
|
69
|
+
// All templates (with Content and Params pre-associated)
|
|
70
|
+
const templates = engine.Templates;
|
|
51
71
|
|
|
52
|
-
//
|
|
53
|
-
const
|
|
72
|
+
// Find a template by name
|
|
73
|
+
const emailTemplate = engine.FindTemplate('Welcome Email');
|
|
54
74
|
|
|
55
|
-
//
|
|
56
|
-
const
|
|
57
|
-
const
|
|
75
|
+
// Access template contents and params
|
|
76
|
+
const contents = emailTemplate.Content;
|
|
77
|
+
const params = emailTemplate.Params;
|
|
58
78
|
|
|
59
|
-
//
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
// Validate input data against template parameters
|
|
64
|
-
const validationResult = template.ValidateTemplateInput({
|
|
65
|
-
customerName: 'John Doe',
|
|
66
|
-
invoiceNumber: '12345'
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
if (!validationResult.Success) {
|
|
70
|
-
console.error('Validation errors:', validationResult.Errors);
|
|
71
|
-
}
|
|
79
|
+
// Other metadata
|
|
80
|
+
const contentTypes = engine.TemplateContentTypes;
|
|
81
|
+
const categories = engine.TemplateCategories;
|
|
72
82
|
```
|
|
73
83
|
|
|
74
84
|
### TemplateRenderResult
|
|
75
85
|
|
|
76
|
-
A simple class representing the result of a template rendering operation.
|
|
77
|
-
|
|
78
86
|
```typescript
|
|
79
87
|
import { TemplateRenderResult } from '@memberjunction/templates-base-types';
|
|
80
88
|
|
|
81
|
-
//
|
|
89
|
+
// Returned by rendering operations
|
|
82
90
|
const result: TemplateRenderResult = {
|
|
83
91
|
Success: true,
|
|
84
|
-
Output: '<html
|
|
85
|
-
Message: undefined
|
|
92
|
+
Output: '<html>...</html>',
|
|
93
|
+
Message: undefined // Only set on failure
|
|
86
94
|
};
|
|
87
95
|
```
|
|
88
96
|
|
|
@@ -90,156 +98,49 @@ const result: TemplateRenderResult = {
|
|
|
90
98
|
|
|
91
99
|
### TemplateEngineBase
|
|
92
100
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
- `static get Instance(): TemplateEngineBase` - Get the singleton instance
|
|
104
|
-
- `async Config(forceRefresh?: boolean, contextUser?: UserInfo, provider?: IMetadataProvider)` - Load template metadata
|
|
105
|
-
- `FindTemplate(templateName: string): TemplateEntityExtended` - Find a template by name (case-insensitive)
|
|
106
|
-
|
|
107
|
-
### TemplateEntityExtended
|
|
108
|
-
|
|
109
|
-
#### Properties
|
|
110
|
-
|
|
111
|
-
- `Content: TemplateContentEntity[]` - Associated content records
|
|
112
|
-
- `Params: TemplateParamEntity[]` - Associated parameter definitions
|
|
113
|
-
|
|
114
|
-
#### Methods
|
|
115
|
-
|
|
116
|
-
- `GetContentByType(type: string): TemplateContentEntity[]` - Get all content of a specific type
|
|
117
|
-
- `GetHighestPriorityContent(type?: string): TemplateContentEntity` - Get the highest priority content
|
|
118
|
-
- `ValidateTemplateInput(data: any): ValidationResult` - Validate input data against parameter requirements
|
|
101
|
+
| Member | Type | Description |
|
|
102
|
+
|--------|------|-------------|
|
|
103
|
+
| `Instance` | static getter | Singleton instance |
|
|
104
|
+
| `Config()` | method | Load/refresh template metadata |
|
|
105
|
+
| `Templates` | getter | All templates with associated contents and params |
|
|
106
|
+
| `TemplateContentTypes` | getter | Available content types |
|
|
107
|
+
| `TemplateCategories` | getter | Template categories |
|
|
108
|
+
| `TemplateContents` | getter | All template content records |
|
|
109
|
+
| `TemplateParams` | getter | All template parameter definitions |
|
|
110
|
+
| `FindTemplate()` | method | Find template by name (case-insensitive) |
|
|
119
111
|
|
|
120
112
|
### TemplateRenderResult
|
|
121
113
|
|
|
122
|
-
|
|
114
|
+
| Property | Type | Description |
|
|
115
|
+
|----------|------|-------------|
|
|
116
|
+
| `Success` | boolean | Whether rendering succeeded |
|
|
117
|
+
| `Output` | string | The rendered output |
|
|
118
|
+
| `Message` | string (optional) | Error message on failure |
|
|
123
119
|
|
|
124
|
-
|
|
125
|
-
- `Output: string` - The rendered output
|
|
126
|
-
- `Message?: string` - Optional message (typically for errors)
|
|
120
|
+
## Relationship to @memberjunction/templates
|
|
127
121
|
|
|
128
|
-
|
|
122
|
+
This package provides the **metadata caching layer** usable anywhere. The `@memberjunction/templates` package extends it with server-side Nunjucks rendering, custom extensions, and AI prompt integration.
|
|
129
123
|
|
|
130
|
-
|
|
124
|
+
```mermaid
|
|
125
|
+
graph LR
|
|
126
|
+
A["@memberjunction/templates-base-types<br/>(Metadata + Types)"] --> B["@memberjunction/templates<br/>(Server Rendering)"]
|
|
127
|
+
C[Client Code] --> A
|
|
128
|
+
D[Server Code] --> B
|
|
131
129
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const engine = TemplateEngineBase.Instance;
|
|
137
|
-
|
|
138
|
-
// Load metadata (only needed once)
|
|
139
|
-
await engine.Config();
|
|
140
|
-
|
|
141
|
-
// List all active templates
|
|
142
|
-
const activeTemplates = engine.Templates.filter(t => t.IsActive);
|
|
143
|
-
|
|
144
|
-
// Find templates by category
|
|
145
|
-
const emailTemplates = engine.Templates.filter(t =>
|
|
146
|
-
t.CategoryID === engine.TemplateCategories.find(c => c.Name === 'Email')?.ID
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
console.log(`Found ${activeTemplates.length} active templates`);
|
|
150
|
-
console.log(`Found ${emailTemplates.length} email templates`);
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Working with Template Content
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
import { TemplateEngineBase, TemplateEntityExtended } from '@memberjunction/templates-base-types';
|
|
158
|
-
|
|
159
|
-
async function getTemplateContent(templateName: string, contentType: string) {
|
|
160
|
-
const engine = TemplateEngineBase.Instance;
|
|
161
|
-
await engine.Config();
|
|
162
|
-
|
|
163
|
-
const template = engine.FindTemplate(templateName);
|
|
164
|
-
if (!template) {
|
|
165
|
-
throw new Error(`Template "${templateName}" not found`);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Get specific content type
|
|
169
|
-
const content = template.GetHighestPriorityContent(contentType);
|
|
170
|
-
if (!content) {
|
|
171
|
-
throw new Error(`No ${contentType} content found for template "${templateName}"`);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return content.TemplateText;
|
|
175
|
-
}
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
### Template Parameter Validation
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
import { TemplateEngineBase } from '@memberjunction/templates-base-types';
|
|
182
|
-
|
|
183
|
-
async function validateTemplateData(templateName: string, inputData: any) {
|
|
184
|
-
const engine = TemplateEngineBase.Instance;
|
|
185
|
-
await engine.Config();
|
|
186
|
-
|
|
187
|
-
const template = engine.FindTemplate(templateName);
|
|
188
|
-
if (!template) {
|
|
189
|
-
throw new Error(`Template "${templateName}" not found`);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const validation = template.ValidateTemplateInput(inputData);
|
|
193
|
-
|
|
194
|
-
if (!validation.Success) {
|
|
195
|
-
// Handle validation errors
|
|
196
|
-
validation.Errors.forEach(error => {
|
|
197
|
-
console.error(`Parameter ${error.Source}: ${error.Message}`);
|
|
198
|
-
});
|
|
199
|
-
throw new Error('Template validation failed');
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Data is valid, proceed with rendering
|
|
203
|
-
return true;
|
|
204
|
-
}
|
|
130
|
+
style A fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
131
|
+
style B fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
132
|
+
style C fill:#b8762f,stroke:#8a5722,color:#fff
|
|
133
|
+
style D fill:#b8762f,stroke:#8a5722,color:#fff
|
|
205
134
|
```
|
|
206
135
|
|
|
207
136
|
## Dependencies
|
|
208
137
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
## Integration with Other MJ Packages
|
|
216
|
-
|
|
217
|
-
This package serves as the foundation for:
|
|
218
|
-
|
|
219
|
-
- **`@memberjunction/templates-engine`** - The full template rendering engine that builds on these base types
|
|
220
|
-
- **Custom template extensions** - Third-party extensions can use these types for compatibility
|
|
221
|
-
|
|
222
|
-
The base types ensure consistent template handling across the entire MemberJunction ecosystem, whether templates are being managed, validated, or rendered.
|
|
223
|
-
|
|
224
|
-
## Build and Development
|
|
225
|
-
|
|
226
|
-
```bash
|
|
227
|
-
# Build the package
|
|
228
|
-
npm run build
|
|
229
|
-
|
|
230
|
-
# Watch for changes during development
|
|
231
|
-
npm run start
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
The package is built using TypeScript and outputs to the `dist` directory. Type definitions are automatically generated during the build process.
|
|
235
|
-
|
|
236
|
-
## Notes
|
|
237
|
-
|
|
238
|
-
- This package intentionally has minimal dependencies to ensure it can be used in various environments
|
|
239
|
-
- The `TemplateEngineBase` uses the singleton pattern to ensure consistent metadata access across an application
|
|
240
|
-
- Template metadata is loaded from the MemberJunction database using the dataset system
|
|
241
|
-
- The `@RegisterClass` decorator on `TemplateEntityExtended` ensures proper integration with the MJ entity system
|
|
138
|
+
| Package | Purpose |
|
|
139
|
+
|---------|---------|
|
|
140
|
+
| `@memberjunction/core` | BaseEngine, UserInfo, IMetadataProvider |
|
|
141
|
+
| `@memberjunction/core-entities` | Template entity types |
|
|
142
|
+
| `@memberjunction/global` | MJGlobal class factory |
|
|
242
143
|
|
|
243
144
|
## License
|
|
244
145
|
|
|
245
|
-
ISC
|
|
146
|
+
ISC
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/templates-base-types",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.1.0",
|
|
5
5
|
"description": "MemberJunction Templating Base Types for Client/Server Use",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
"typescript": "^5.9.3"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@memberjunction/core": "4.
|
|
23
|
-
"@memberjunction/core-entities": "4.
|
|
24
|
-
"@memberjunction/global": "4.
|
|
22
|
+
"@memberjunction/core": "4.1.0",
|
|
23
|
+
"@memberjunction/core-entities": "4.1.0",
|
|
24
|
+
"@memberjunction/global": "4.1.0"
|
|
25
25
|
},
|
|
26
26
|
"repository": {
|
|
27
27
|
"type": "git",
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { ValidationResult } from "@memberjunction/core";
|
|
2
|
-
import { TemplateContentEntity, TemplateEntity, TemplateParamEntity } from "@memberjunction/core-entities";
|
|
3
|
-
export declare class TemplateEntityExtended extends TemplateEntity {
|
|
4
|
-
private _Content;
|
|
5
|
-
get Content(): TemplateContentEntity[];
|
|
6
|
-
set Content(value: TemplateContentEntity[]);
|
|
7
|
-
private _Params;
|
|
8
|
-
get Params(): TemplateParamEntity[];
|
|
9
|
-
set Params(value: TemplateParamEntity[]);
|
|
10
|
-
/**
|
|
11
|
-
* Returns all content for a given type for the template
|
|
12
|
-
* @param type
|
|
13
|
-
* @returns
|
|
14
|
-
*/
|
|
15
|
-
GetContentByType(type: string): TemplateContentEntity[];
|
|
16
|
-
/**
|
|
17
|
-
* Returns the highest priority content for the template
|
|
18
|
-
* @param type If provided, returns the highest priority content of the specified type
|
|
19
|
-
* @returns
|
|
20
|
-
*/
|
|
21
|
-
GetHighestPriorityContent(type?: string): TemplateContentEntity;
|
|
22
|
-
/**
|
|
23
|
-
* Returns all parameters that apply to a specific template content.
|
|
24
|
-
* This includes both global parameters (where TemplateContentID is NULL)
|
|
25
|
-
* and content-specific parameters for the given contentId.
|
|
26
|
-
*
|
|
27
|
-
* @param contentId - The ID of the template content. If not provided, returns only global parameters.
|
|
28
|
-
* @returns Array of TemplateParamEntity objects that apply to the specified content
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* // Get all parameters for a specific content
|
|
32
|
-
* const params = template.GetParametersForContent('content-uuid');
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* // Get only global parameters (that apply to all contents)
|
|
36
|
-
* const globalParams = template.GetParametersForContent();
|
|
37
|
-
*/
|
|
38
|
-
GetParametersForContent(contentId?: string): TemplateParamEntity[];
|
|
39
|
-
/**
|
|
40
|
-
* This method is different from the Validate() method which validates the state of the Template itself.
|
|
41
|
-
* This method validates the data object provided meets the requirements for the template's parameter definitions.
|
|
42
|
-
*
|
|
43
|
-
* @param data - the data object to validate against the template's parameter definitions
|
|
44
|
-
* @param contentId - Optional: The ID of the template content to validate against.
|
|
45
|
-
* If provided, validates against parameters specific to that content.
|
|
46
|
-
* If not provided, validates against all parameters.
|
|
47
|
-
* @returns ValidationResult with success status and any validation errors
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* // Validate against all parameters
|
|
51
|
-
* const result = template.ValidateTemplateInput(inputData);
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* // Validate against parameters for a specific content
|
|
55
|
-
* const result = template.ValidateTemplateInput(inputData, 'content-uuid');
|
|
56
|
-
*/
|
|
57
|
-
ValidateTemplateInput(data: any, contentId?: string): ValidationResult;
|
|
58
|
-
}
|
|
59
|
-
export declare function LoadTemplateEntityExtended(): void;
|
|
60
|
-
//# sourceMappingURL=TemplateEntityExtended.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TemplateEntityExtended.d.ts","sourceRoot":"","sources":["../src/TemplateEntityExtended.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACvF,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AAG3G,qBACa,sBAAuB,SAAQ,cAAc;IACtD,OAAO,CAAC,QAAQ,CAA+B;IAC/C,IAAW,OAAO,IAAI,qBAAqB,EAAE,CAE5C;IACD,IAAW,OAAO,CAAC,KAAK,EAAE,qBAAqB,EAAE,EAEhD;IAED,OAAO,CAAC,OAAO,CAA6B;IAC5C,IAAW,MAAM,IAAI,mBAAmB,EAAE,CAEzC;IACD,IAAW,MAAM,CAAC,KAAK,EAAE,mBAAmB,EAAE,EAE7C;IAED;;;;OAIG;IACI,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,qBAAqB,EAAE;IAI9D;;;;OAIG;IACI,yBAAyB,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,qBAAqB;IAUtE;;;;;;;;;;;;;;;OAeG;IACI,uBAAuB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,mBAAmB,EAAE;IAazE;;;;;;;;;;;;;;;;;OAiBG;IACI,qBAAqB,CAAC,IAAI,EAAE,GAAG,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,gBAAgB;CA0BhF;AAED,wBAAgB,0BAA0B,SAEzC"}
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.LoadTemplateEntityExtended = exports.TemplateEntityExtended = void 0;
|
|
10
|
-
const core_1 = require("@memberjunction/core");
|
|
11
|
-
const core_entities_1 = require("@memberjunction/core-entities");
|
|
12
|
-
const global_1 = require("@memberjunction/global");
|
|
13
|
-
let TemplateEntityExtended = class TemplateEntityExtended extends core_entities_1.TemplateEntity {
|
|
14
|
-
constructor() {
|
|
15
|
-
super(...arguments);
|
|
16
|
-
this._Content = [];
|
|
17
|
-
this._Params = [];
|
|
18
|
-
}
|
|
19
|
-
get Content() {
|
|
20
|
-
return this._Content;
|
|
21
|
-
}
|
|
22
|
-
set Content(value) {
|
|
23
|
-
this._Content = value;
|
|
24
|
-
}
|
|
25
|
-
get Params() {
|
|
26
|
-
return this._Params;
|
|
27
|
-
}
|
|
28
|
-
set Params(value) {
|
|
29
|
-
this._Params = value;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Returns all content for a given type for the template
|
|
33
|
-
* @param type
|
|
34
|
-
* @returns
|
|
35
|
-
*/
|
|
36
|
-
GetContentByType(type) {
|
|
37
|
-
return this.Content.filter(c => c.Type.trim().toLowerCase() === type.trim().toLowerCase());
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Returns the highest priority content for the template
|
|
41
|
-
* @param type If provided, returns the highest priority content of the specified type
|
|
42
|
-
* @returns
|
|
43
|
-
*/
|
|
44
|
-
GetHighestPriorityContent(type) {
|
|
45
|
-
if (type) {
|
|
46
|
-
return this.Content.filter(c => c.Type.trim().toLowerCase() === type.trim().toLowerCase())
|
|
47
|
-
.sort((a, b) => a.Priority - b.Priority)[0];
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
return this.Content.sort((a, b) => a.Priority - b.Priority)[0];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Returns all parameters that apply to a specific template content.
|
|
55
|
-
* This includes both global parameters (where TemplateContentID is NULL)
|
|
56
|
-
* and content-specific parameters for the given contentId.
|
|
57
|
-
*
|
|
58
|
-
* @param contentId - The ID of the template content. If not provided, returns only global parameters.
|
|
59
|
-
* @returns Array of TemplateParamEntity objects that apply to the specified content
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* // Get all parameters for a specific content
|
|
63
|
-
* const params = template.GetParametersForContent('content-uuid');
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* // Get only global parameters (that apply to all contents)
|
|
67
|
-
* const globalParams = template.GetParametersForContent();
|
|
68
|
-
*/
|
|
69
|
-
GetParametersForContent(contentId) {
|
|
70
|
-
if (!contentId) {
|
|
71
|
-
// Return only global parameters (TemplateContentID is null)
|
|
72
|
-
return this.Params.filter(p => !p.TemplateContentID);
|
|
73
|
-
}
|
|
74
|
-
// Return both global parameters and content-specific parameters
|
|
75
|
-
return this.Params.filter(p => !p.TemplateContentID || // Global param (applies to all contents)
|
|
76
|
-
p.TemplateContentID === contentId // Content-specific param
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* This method is different from the Validate() method which validates the state of the Template itself.
|
|
81
|
-
* This method validates the data object provided meets the requirements for the template's parameter definitions.
|
|
82
|
-
*
|
|
83
|
-
* @param data - the data object to validate against the template's parameter definitions
|
|
84
|
-
* @param contentId - Optional: The ID of the template content to validate against.
|
|
85
|
-
* If provided, validates against parameters specific to that content.
|
|
86
|
-
* If not provided, validates against all parameters.
|
|
87
|
-
* @returns ValidationResult with success status and any validation errors
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* // Validate against all parameters
|
|
91
|
-
* const result = template.ValidateTemplateInput(inputData);
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* // Validate against parameters for a specific content
|
|
95
|
-
* const result = template.ValidateTemplateInput(inputData, 'content-uuid');
|
|
96
|
-
*/
|
|
97
|
-
ValidateTemplateInput(data, contentId) {
|
|
98
|
-
const result = new core_1.ValidationResult();
|
|
99
|
-
// Get the relevant parameters based on contentId
|
|
100
|
-
const paramsToValidate = contentId ?
|
|
101
|
-
this.GetParametersForContent(contentId) :
|
|
102
|
-
this.Params;
|
|
103
|
-
paramsToValidate.forEach((p) => {
|
|
104
|
-
if (p.IsRequired) {
|
|
105
|
-
if (!data ||
|
|
106
|
-
data[p.Name] === undefined ||
|
|
107
|
-
data[p.Name] === null ||
|
|
108
|
-
(typeof data[p.Name] === 'string' && data[p.Name].toString().trim() === ''))
|
|
109
|
-
result.Errors.push({
|
|
110
|
-
Source: p.Name,
|
|
111
|
-
Message: `Parameter ${p.Name} is required.`,
|
|
112
|
-
Value: data[p.Name],
|
|
113
|
-
Type: 'Failure'
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
// now set result's top level success flag based on the existence of ANY failure record within the errors collection
|
|
118
|
-
result.Success = result.Errors.some(e => e.Type === 'Failure') ? false : true;
|
|
119
|
-
return result;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
exports.TemplateEntityExtended = TemplateEntityExtended;
|
|
123
|
-
exports.TemplateEntityExtended = TemplateEntityExtended = __decorate([
|
|
124
|
-
(0, global_1.RegisterClass)(core_1.BaseEntity, 'Templates')
|
|
125
|
-
], TemplateEntityExtended);
|
|
126
|
-
function LoadTemplateEntityExtended() {
|
|
127
|
-
// this does nothing but prevents the class from being removed by the tree shaker
|
|
128
|
-
}
|
|
129
|
-
exports.LoadTemplateEntityExtended = LoadTemplateEntityExtended;
|
|
130
|
-
//# sourceMappingURL=TemplateEntityExtended.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TemplateEntityExtended.js","sourceRoot":"","sources":["../src/TemplateEntityExtended.ts"],"names":[],"mappings":";;;;;;;;;AAAA,+CAAuF;AACvF,iEAA2G;AAC3G,mDAAuD;AAGhD,IAAM,sBAAsB,GAA5B,MAAM,sBAAuB,SAAQ,8BAAc;IAAnD;;QACK,aAAQ,GAA4B,EAAE,CAAC;QAQvC,YAAO,GAA0B,EAAE,CAAC;IAyGhD,CAAC;IAhHG,IAAW,OAAO;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;IACD,IAA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