UNPKG

@ackplus/nest-dynamic-templates

Version:

Dynamic template management for NestJS applications

226 lines 8.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateLayoutService = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const typeorm_1 = require("@nestjs/typeorm"); const typeorm_2 = require("typeorm"); const template_layout_entity_1 = require("../entities/template-layout.entity"); const template_engine_registry_1 = require("./template-engine.registry"); let TemplateLayoutService = class TemplateLayoutService { constructor(layoutRepository, engineRegistry) { this.layoutRepository = layoutRepository; this.engineRegistry = engineRegistry; } async render(renderDto) { const { name, scope, scopeId, locale, context } = renderDto; const templateLayout = await this.findTemplateLayout(name, scope, scopeId, locale); if (!templateLayout) { throw new common_1.NotFoundException(`Template not found: ${name}`); } const templateEngine = this.engineRegistry.getTemplateEngine(templateLayout.engine); let content = await templateEngine.render(templateLayout.content, context); if (content) { content = await this.renderEngine(templateLayout.engine, content, context); } if (templateLayout.language) { content = await this.renderLanguage(templateLayout.language, content, context); } return { content }; } async renderContent(input) { const { content, language, engine, context } = input; let renderContent = content; if (content) { renderContent = await this.renderEngine(engine, content, context); } if (language) { renderContent = await this.renderLanguage(language, renderContent, context); } return renderContent; } async renderLanguage(language, content, context) { const languageEngine = this.engineRegistry.getLanguageEngine(language); return languageEngine.render(content, context); } async renderEngine(engine, content, context) { const templateEngine = this.engineRegistry.getTemplateEngine(engine); return templateEngine.render(content, context); } async getTemplateLayouts(filter = {}) { const { scope, scopeId, type, locale, excludeNames = [], } = filter; const where = {}; if (type) where.type = type; if (locale) where.locale = locale; if (excludeNames.length > 0) where.name = (0, typeorm_2.Not)((0, typeorm_2.In)(excludeNames)); const systemTemplates = await this.layoutRepository.find({ where: { ...where, scope: 'system', scopeId: (0, typeorm_2.IsNull)(), }, }); if (scope === 'system') { return systemTemplates; } const templates = await this.layoutRepository.find({ where: { ...where, scope: (0, typeorm_2.Equal)(scope), scopeId: (0, typeorm_2.Equal)(scopeId), }, order: { createdAt: 'DESC', }, }); const templateMap = new Map(); for (const template of systemTemplates) { const key = `${template.type}/${template.name}/${template.locale}`; templateMap.set(key, template); } for (const template of templates) { const key = `${template.type}/${template.name}/${template.locale}`; templateMap.set(key, template); } return Array.from(templateMap.values()); } async getTemplateLayoutById(id) { return this.layoutRepository.findOne({ where: { id }, }); } async findTemplateLayout(name, scope, scopeId, locale) { const locales = (locale ? [locale, 'en'] : ['en']).filter(Boolean); for (const currentLocale of locales) { const template = await this.layoutRepository.findOne({ where: { name, scope, scopeId: scope === 'system' ? (0, typeorm_2.IsNull)() : (0, typeorm_2.Equal)(scopeId), locale: currentLocale, } }); if (template) { return template; } } if (scope !== 'system') { for (const currentLocale of locales) { const template = await this.layoutRepository.findOne({ where: { name, scope: 'system', scopeId: (0, typeorm_2.IsNull)(), locale: currentLocale, } }); if (template) { return template; } } } return null; } async createTemplateLayout(data) { if (data.scope !== 'system') { throw new common_1.ForbiddenException('Only system templates can be created directly'); } const existingTemplate = await this.layoutRepository.findOne({ where: { name: data.name, scope: 'system', scopeId: (0, typeorm_2.IsNull)(), locale: data.locale, }, }); if (existingTemplate) { throw new common_1.ConflictException(`System template already exists`); } const template = this.layoutRepository.create({ ...data, scopeId: null, }); return this.layoutRepository.save(template); } async overwriteSystemTemplateLayout(templateId, updates) { let template = await this.layoutRepository.findOne({ where: { id: templateId }, }); if (!template) { throw new common_1.NotFoundException(`Template not found: ${templateId}`); } if (template.scope === 'system') { if (!updates.scope) { throw new common_1.BadRequestException('Scope is required when overwriting system template'); } const existingTemplate = await this.layoutRepository.findOne({ where: { name: template.name, locale: template.locale, scope: updates.scope, scopeId: (0, typeorm_2.Equal)(updates.scopeId), }, }); if (existingTemplate) { template = existingTemplate; } else { const newTemplate = this.layoutRepository.create({ ...template, id: undefined, createdAt: undefined, updatedAt: undefined, scope: updates.scope, scopeId: updates.scopeId, }); template = newTemplate; } } template = this.layoutRepository.merge(template, updates); await this.layoutRepository.save(template); return template; } async updateTemplateLayout(id, updates, canUpdateSystemTemplate = false) { let template = await this.layoutRepository.findOne({ where: { id }, }); if (!template) { throw new common_1.NotFoundException(`Template not found: ${id}`); } if (template.scope === 'system' && !canUpdateSystemTemplate) { if (updates.scope) { return this.overwriteSystemTemplateLayout(id, updates); } else { throw new common_1.ForbiddenException('Cannot update system templates'); } } template = this.layoutRepository.merge(template, updates); return this.layoutRepository.save(template); } async deleteTemplateLayout(id, canDeleteSystemTemplate = false) { const template = await this.layoutRepository.findOne({ where: { id }, }); if (!template) { throw new Error(`Template not found: ${id}`); } if (template.scope === 'system' && !canDeleteSystemTemplate) { throw new common_1.ForbiddenException('Cannot delete system templates'); } await this.layoutRepository.remove(template); } }; exports.TemplateLayoutService = TemplateLayoutService; exports.TemplateLayoutService = TemplateLayoutService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__param(0, (0, typeorm_1.InjectRepository)(template_layout_entity_1.NestDynamicTemplateLayout)), tslib_1.__metadata("design:paramtypes", [typeorm_2.Repository, template_engine_registry_1.TemplateEngineRegistryService]) ], TemplateLayoutService); //# sourceMappingURL=template-layout.service.js.map