UNPKG

@wepublish/api

Version:
76 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MailTemplateSyncService = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const client_1 = require("@prisma/client"); const api_1 = require("../../../../mail-api/src"); let MailTemplateSyncService = exports.MailTemplateSyncService = class MailTemplateSyncService { constructor(prismaService, mailContext) { this.prismaService = prismaService; this.mailContext = mailContext; } /** * Synchronizes the local template list with the remote mail provider. * This performs the following actions: * - updates locally existing templates to match their remote counterpart * - creates local templates for any new remote templates * - marks any local templates as outdated if their remote counterpart is missing */ synchronizeTemplates() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const localTemplates = yield this.prismaService.mailTemplate.findMany(); const { mailProvider } = yield this.mailContext; const remoteTemplates = (yield mailProvider.getTemplates()); const updatedRemoteTemplates = this.findUpdatedRemoteTemplates(localTemplates, remoteTemplates); for (const remoteTemplate of updatedRemoteTemplates) { yield this.prismaService.mailTemplate.update({ where: { externalMailTemplateId: remoteTemplate.uniqueIdentifier }, data: { name: remoteTemplate.name, remoteMissing: false } }); } const newRemoteTemplates = this.findNewRemoteTemplates(localTemplates, remoteTemplates); for (const remoteTemplate of newRemoteTemplates) { yield this.prismaService.mailTemplate.create({ data: { name: remoteTemplate.name, externalMailTemplateId: remoteTemplate.uniqueIdentifier, remoteMissing: false } }); } const outdatedLocalTemplates = this.findOutdatedLocalTemplates(localTemplates, remoteTemplates); for (const localTemplate of outdatedLocalTemplates) { yield this.prismaService.mailTemplate.update({ where: { externalMailTemplateId: localTemplate.externalMailTemplateId }, data: { remoteMissing: true } }); } }); } findUpdatedRemoteTemplates(localTemplates, remoteTemplates) { return remoteTemplates.filter(remoteTemplate => { return localTemplates.find(localTemplate => localTemplate.externalMailTemplateId === remoteTemplate.uniqueIdentifier); }); } findNewRemoteTemplates(localTemplates, remoteTemplates) { return remoteTemplates.filter(remoteTemplate => { return !localTemplates.find(localTemplate => localTemplate.externalMailTemplateId === remoteTemplate.uniqueIdentifier); }); } findOutdatedLocalTemplates(localTemplates, remoteTemplates) { return localTemplates.filter(localTemplate => { return !remoteTemplates.find(remoteTemplate => remoteTemplate.uniqueIdentifier === localTemplate.externalMailTemplateId); }); } }; exports.MailTemplateSyncService = MailTemplateSyncService = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [client_1.PrismaClient, api_1.MailContext]) ], MailTemplateSyncService); //# sourceMappingURL=mail-template-sync.service.js.map