@typescript-eda/domain
Version:
Core domain primitives for event-driven architecture
285 lines • 8.35 kB
JavaScript
"use strict";
// Copyright 2025-today Semantest Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SemanTestContract = void 0;
/**
* @fileoverview SemanTestContract entity representing automation contracts
* @author Semantest Team
* @module domain/semantic-automation/semantest-contract
*/
const entity_1 = require("../entity");
const semantest_capability_entity_1 = require("./semantest-capability.entity");
/**
* SemanTestContract entity representing automation contracts
* Extends Entity from typescript-eda-domain for consistent domain modeling
*/
class SemanTestContract extends entity_1.Entity {
constructor(props) {
super(props);
}
/**
* Factory method to create a new contract
*/
static create(id, version, domain, title, capabilities, options) {
const now = new Date();
return new SemanTestContract({
id,
version,
domain,
title,
description: options?.description,
capabilities,
workflows: options?.workflows,
metadata: options?.metadata,
createdAt: now,
updatedAt: now
});
}
/**
* Get contract ID
*/
getId() {
return this.get('id');
}
/**
* Get contract version
*/
getVersion() {
return this.get('version');
}
/**
* Get target domain
*/
getDomain() {
return this.get('domain');
}
/**
* Get contract title
*/
getTitle() {
return this.get('title');
}
/**
* Get contract description
*/
getDescription() {
return this.get('description');
}
/**
* Get all capabilities
*/
getCapabilities() {
return this.get('capabilities');
}
/**
* Get specific capability by name
*/
getCapability(name) {
return this.get('capabilities')[name];
}
/**
* Get capability names
*/
getCapabilityNames() {
return Object.keys(this.get('capabilities'));
}
/**
* Check if contract has capability
*/
hasCapability(name) {
return name in this.get('capabilities');
}
/**
* Get workflows
*/
getWorkflows() {
return this.get('workflows');
}
/**
* Get specific workflow by name
*/
getWorkflow(name) {
const workflows = this.get('workflows');
return workflows ? workflows[name] : undefined;
}
/**
* Get contract metadata
*/
getMetadata() {
return this.get('metadata');
}
/**
* Get creation timestamp
*/
getCreatedAt() {
return this.get('createdAt');
}
/**
* Get last update timestamp
*/
getUpdatedAt() {
return this.get('updatedAt');
}
/**
* Add or update capability
*/
withCapability(name, capability) {
const capabilities = { ...this.get('capabilities') };
capabilities[name] = capability;
return new SemanTestContract({
...this.props,
capabilities,
updatedAt: new Date()
});
}
/**
* Remove capability
*/
withoutCapability(name) {
const capabilities = { ...this.get('capabilities') };
delete capabilities[name];
return new SemanTestContract({
...this.props,
capabilities,
updatedAt: new Date()
});
}
/**
* Update contract metadata
*/
withMetadata(metadata) {
return new SemanTestContract({
...this.props,
metadata,
updatedAt: new Date()
});
}
/**
* Add workflow
*/
withWorkflow(name, workflow) {
const workflows = { ...this.get('workflows') };
workflows[name] = workflow;
return new SemanTestContract({
...this.props,
workflows,
updatedAt: new Date()
});
}
/**
* Validate contract structure and capabilities
*/
validate() {
const errors = [];
const warnings = [];
// Validate required fields
if (!this.getId()) {
errors.push('Contract ID is required');
}
if (!this.getVersion()) {
errors.push('Contract version is required');
}
if (!this.getDomain()) {
errors.push('Contract domain is required');
}
if (!this.getTitle()) {
errors.push('Contract title is required');
}
// Validate capabilities
const capabilities = this.getCapabilities();
if (Object.keys(capabilities).length === 0) {
warnings.push('Contract has no capabilities defined');
}
// Validate each capability
for (const [name, capability] of Object.entries(capabilities)) {
try {
const capabilityValidation = capability.validate();
if (!capabilityValidation.valid) {
errors.push(`Capability '${name}': ${capabilityValidation.errors.join(', ')}`);
}
if (capabilityValidation.warnings.length > 0) {
warnings.push(`Capability '${name}': ${capabilityValidation.warnings.join(', ')}`);
}
}
catch (error) {
errors.push(`Capability '${name}': ${error instanceof Error ? error.message : String(error)}`);
}
}
// Validate workflows
const workflows = this.getWorkflows();
if (workflows) {
for (const [name, workflow] of Object.entries(workflows)) {
for (const step of workflow.steps) {
if (!this.hasCapability(step.capability)) {
errors.push(`Workflow '${name}' references unknown capability '${step.capability}'`);
}
}
}
}
return {
valid: errors.length === 0,
errors,
warnings,
timestamp: new Date()
};
}
/**
* Convert contract to JSON for serialization
*/
toJSON() {
const capabilities = {};
for (const [name, capability] of Object.entries(this.get('capabilities'))) {
capabilities[name] = capability.toJSON();
}
return {
id: this.getId(),
version: this.getVersion(),
domain: this.getDomain(),
title: this.getTitle(),
description: this.getDescription(),
capabilities,
workflows: this.getWorkflows(),
metadata: this.getMetadata(),
createdAt: this.getCreatedAt().toISOString(),
updatedAt: this.getUpdatedAt().toISOString()
};
}
/**
* Create contract from JSON
*/
static fromJSON(json) {
// Convert capabilities from JSON
const capabilities = {};
if (json.capabilities) {
for (const [name, capabilityJson] of Object.entries(json.capabilities)) {
capabilities[name] = semantest_capability_entity_1.SemanTestCapability.fromJSON(capabilityJson);
}
}
return new SemanTestContract({
id: json.id,
version: json.version,
domain: json.domain,
title: json.title,
description: json.description,
capabilities,
workflows: json.workflows,
metadata: json.metadata,
createdAt: new Date(json.createdAt),
updatedAt: new Date(json.updatedAt)
});
}
}
exports.SemanTestContract = SemanTestContract;
//# sourceMappingURL=semantest-contract.entity.js.map