Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import {
KnowledgeConfiguration,
KnowledgeLibrary,
KnowledgeCategory,
KnowledgeArticle,
KnowledgeArticleVersion,
WikiSpace,
WikiPage,
KnowledgeSOP,
KnowledgePolicy,
ReadAcknowledgement
} from './schemas';
import { EventBusService } from '../../platform/events/event-bus.service';
import { AiGatewayService } from '../../platform/ai/services/ai-gateway.service';
@Injectable()
export class KnowledgeService {
constructor(
@InjectModel(KnowledgeConfiguration.name)
private readonly configModel: Model<KnowledgeConfiguration>,
@InjectModel(KnowledgeLibrary.name)
private readonly libraryModel: Model<KnowledgeLibrary>,
@InjectModel(KnowledgeCategory.name)
private readonly categoryModel: Model<KnowledgeCategory>,
@InjectModel(KnowledgeArticle.name)
private readonly articleModel: Model<KnowledgeArticle>,
@InjectModel(KnowledgeArticleVersion.name)
private readonly versionModel: Model<KnowledgeArticleVersion>,
@InjectModel(WikiSpace.name)
private readonly spaceModel: Model<WikiSpace>,
@InjectModel(WikiPage.name)
private readonly pageModel: Model<WikiPage>,
@InjectModel(KnowledgeSOP.name)
private readonly sopModel: Model<KnowledgeSOP>,
@InjectModel(KnowledgePolicy.name)
private readonly policyModel: Model<KnowledgePolicy>,
@InjectModel(ReadAcknowledgement.name)
private readonly acknowledgementModel: Model<ReadAcknowledgement>,
private readonly eventBus: EventBusService,
private readonly aiGateway: AiGatewayService
) {}
// 1. Config management
async getConfiguration(tenantId: string): Promise<KnowledgeConfiguration> {
const tenantObjId = new Types.ObjectId(tenantId);
let config = await this.configModel.findOne({ tenantId: tenantObjId }).exec();
Eif (!config) {
config = await this.configModel.create({ tenantId: tenantObjId });
}
return config;
}
// 2. Libraries taxonomy
async createLibrary(tenantId: string, data: any): Promise<KnowledgeLibrary> {
return this.libraryModel.create({
tenantId: new Types.ObjectId(tenantId),
...data
});
}
async createCategory(tenantId: string, data: any): Promise<KnowledgeCategory> {
return this.categoryModel.create({
tenantId: new Types.ObjectId(tenantId),
...data
});
}
// 3. Article management
async createArticle(tenantId: string, ownerId: string, data: any): Promise<KnowledgeArticle> {
const tenantObjId = new Types.ObjectId(tenantId);
const slug = (data.title || '').toLowerCase().replace(/[^a-z0-9]+/g, '-');
const article = await this.articleModel.create({
tenantId: tenantObjId,
ownerId: new Types.ObjectId(ownerId),
slug,
...data
});
await this.eventBus.publish(
'knowledge.article.created.v1',
{
articleId: article._id.toString(),
title: article.title,
slug: article.slug
},
tenantId
);
return article;
}
// 4. Version Control - Immutable Published Versions
async publishArticleVersion(
tenantId: string,
articleId: string,
changeLog: string,
authorId: string
): Promise<KnowledgeArticleVersion> {
const tenantObjId = new Types.ObjectId(tenantId);
const articleObjId = new Types.ObjectId(articleId);
const article = await this.articleModel.findOne({ _id: articleObjId, tenantId: tenantObjId }).exec();
Iif (!article) throw new NotFoundException('Article not found');
// Immutable check: if already deprecated/archived, do not allow edits
if (article.status === 'archived' || article.status === 'deprecated') {
throw new BadRequestException('Archived or deprecated articles cannot be modified or published');
}
// Increment minor version for draft, major for official publishing
if (article.status !== 'published') {
article.majorVersion += 1;
article.minorVersion = 0;
article.status = 'published';
} else E{
article.minorVersion += 1;
}
await article.save();
const version = await this.versionModel.create({
tenantId: tenantObjId,
articleId: articleObjId,
title: article.title,
content: article.content,
majorVersion: article.majorVersion,
minorVersion: article.minorVersion,
changeLog,
createdBy: new Types.ObjectId(authorId)
});
await this.eventBus.publish(
'knowledge.article.published.v1',
{
articleId: article._id.toString(),
versionId: version._id.toString(),
majorVersion: version.majorVersion,
minorVersion: version.minorVersion
},
tenantId
);
return version;
}
// Rollback Article
async rollbackArticle(tenantId: string, articleId: string, versionId: string): Promise<KnowledgeArticle> {
const tenantObjId = new Types.ObjectId(tenantId);
const articleObjId = new Types.ObjectId(articleId);
const versionObjId = new Types.ObjectId(versionId);
const version = await this.versionModel.findOne({ _id: versionObjId, articleId: articleObjId, tenantId: tenantObjId }).exec();
Iif (!version) throw new NotFoundException('Article version not found');
const article = await this.articleModel.findOne({ _id: articleObjId, tenantId: tenantObjId }).exec();
Iif (!article) throw new NotFoundException('Article not found');
article.title = version.title;
article.content = version.content;
article.minorVersion += 1; // track revision update
await article.save();
await this.eventBus.publish(
'knowledge.article.updated.v1',
{
articleId: article._id.toString(),
action: 'rollback',
rollbackToVersion: `${version.majorVersion}.${version.minorVersion}`
},
tenantId
);
return article;
}
// 5. SOP & Expiry
async createSOP(tenantId: string, data: any): Promise<KnowledgeSOP> {
const tenantObjId = new Types.ObjectId(tenantId);
return this.sopModel.create({
tenantId: tenantObjId,
...data
});
}
async supersedeSOP(tenantId: string, oldSopId: string, newSopId: string): Promise<void> {
const tenantObjId = new Types.ObjectId(tenantId);
const oldSopObjId = new Types.ObjectId(oldSopId);
const newSopObjId = new Types.ObjectId(newSopId);
await this.sopModel.findOneAndUpdate(
{ _id: oldSopObjId, tenantId: tenantObjId },
{ status: 'superseded', supersededBySopId: newSopObjId }
).exec();
}
// 6. Policy & Mandatory Read Acknowledgements
async createPolicy(tenantId: string, data: any): Promise<KnowledgePolicy> {
const tenantObjId = new Types.ObjectId(tenantId);
return this.policyModel.create({
tenantId: tenantObjId,
...data
});
}
async assignReadAcknowledgement(
tenantId: string,
documentType: 'Policy' | 'SOP',
documentId: string,
employeeId: string
): Promise<ReadAcknowledgement> {
const tenantObjId = new Types.ObjectId(tenantId);
const docObjId = new Types.ObjectId(documentId);
const empObjId = new Types.ObjectId(employeeId);
let ack = await this.acknowledgementModel.findOne({
tenantId: tenantObjId,
employeeId: empObjId,
documentId: docObjId
}).exec();
Eif (!ack) {
ack = await this.acknowledgementModel.create({
tenantId: tenantObjId,
employeeId: empObjId,
documentType,
documentId: docObjId,
status: 'pending'
});
await this.eventBus.publish(
'knowledge.review.required.v1',
{
acknowledgementId: ack._id.toString(),
employeeId: employeeId,
documentType,
documentId
},
tenantId
);
}
return ack;
}
async acknowledgeDocument(
tenantId: string,
employeeId: string,
documentId: string,
status: 'accepted' | 'rejected'
): Promise<ReadAcknowledgement> {
const tenantObjId = new Types.ObjectId(tenantId);
const empObjId = new Types.ObjectId(employeeId);
const docObjId = new Types.ObjectId(documentId);
const ack = await this.acknowledgementModel.findOne({
tenantId: tenantObjId,
employeeId: empObjId,
documentId: docObjId
}).exec();
Iif (!ack) throw new NotFoundException('Acknowledgement assignment not found');
ack.status = status;
ack.acknowledgedAt = new Date();
await ack.save();
const eventName = ack.documentType === 'Policy' ? 'knowledge.policy.accepted.v1' : 'knowledge.sop.accepted.v1';
await this.eventBus.publish(
eventName,
{
acknowledgementId: ack._id.toString(),
employeeId,
documentId
},
tenantId
);
return ack;
}
// 7. AI Document Intelligence
async summarizeArticleAI(tenantId: string, articleId: string): Promise<string> {
const tenantObjId = new Types.ObjectId(tenantId);
const article = await this.articleModel.findOne({ _id: new Types.ObjectId(articleId), tenantId: tenantObjId }).exec();
Iif (!article) throw new NotFoundException('Article not found');
const prompt = `Provide a concise 3-sentence summary of the following document content: \n\n${article.content}`;
// Reuse PII-secure AI Gateway capability routing
const summary = await this.aiGateway.generateTextWithRouting(tenantId, 'summarization', 'knowledge_summary_template', {
content: prompt
});
article.aiSummary = summary;
await article.save();
await this.eventBus.publish(
'knowledge.ai.summary.generated.v1',
{
articleId,
summary
},
tenantId
);
return summary;
}
// 8. Secure Expiring Content Media provider
async getSecureContentUrl(tenantId: string, filename: string): Promise<string> {
// Generate secure expiring signature path locally (never expose raw VPS file paths)
const expires = Date.now() + 15 * 60 * 1000; // 15 mins expiry
return `https://vps-storage.bevision.internal/files/${tenantId}/${filename}?expires=${expires}&signature=sha256_mock_sig`;
}
// 9. Analytics Engine
async getAnalyticsMetrics(tenantId: string): Promise<any> {
const tenantObjId = new Types.ObjectId(tenantId);
const totalArticles = await this.articleModel.countDocuments({ tenantId: tenantObjId }).exec();
const pendingReads = await this.acknowledgementModel.countDocuments({ tenantId: tenantObjId, status: 'pending' }).exec();
const acceptedReads = await this.acknowledgementModel.countDocuments({ tenantId: tenantObjId, status: 'accepted' }).exec();
return {
totalArticles,
pendingReads,
acceptedReads,
complianceRatePercentage: totalArticles > 0 ? Math.round((acceptedReads / (pendingReads + acceptedReads || 1)) * 100) : 100
};
}
}
|