{"version":3,"file":"email.mjs","sources":["../../../server/src/services/email.ts"],"sourcesContent":["import * as _ from 'lodash';\nimport { objects, template } from '@strapi/utils';\n\nimport type {\n EmailConfig,\n EmailOptions,\n EmailTemplate,\n EmailTemplateData,\n SendOptions,\n} from '../types';\n\nconst { createStrictInterpolationRegExp } = template;\n\nconst getProviderSettings = (): EmailConfig => strapi.config.get('plugin::email');\n\nconst send = async (options: SendOptions) => strapi.plugin('email').provider.send(options);\n\n/**\n * fill subject, text and html using lodash template\n * @param {object} emailOptions - to, from and replyto...\n * @param {object} emailTemplate - object containing attributes to fill\n * @param {object} data - data used to fill the template\n * @returns {{ subject, text, subject }}\n */\nconst sendTemplatedEmail = (\n emailOptions: EmailOptions,\n emailTemplate: EmailTemplate,\n data: EmailTemplateData\n) => {\n const attributes = ['subject', 'text', 'html'];\n const missingAttributes = _.difference(attributes, Object.keys(emailTemplate));\n\n if (missingAttributes.length > 0) {\n throw new Error(\n `Following attributes are missing from your email template : ${missingAttributes.join(', ')}`\n );\n }\n\n const allowedInterpolationVariables = objects.keysDeep(data);\n const interpolate = createStrictInterpolationRegExp(allowedInterpolationVariables, 'g');\n\n const templatedAttributes = attributes.reduce(\n (compiled, attribute) =>\n emailTemplate[attribute]\n ? Object.assign(compiled, {\n [attribute]: _.template(emailTemplate[attribute], {\n interpolate,\n })(data),\n })\n : compiled,\n {}\n );\n\n return strapi.plugin('email').provider.send({ ...emailOptions, ...templatedAttributes });\n};\n\nconst emailService = () => ({\n getProviderSettings,\n send,\n sendTemplatedEmail,\n});\n\nexport default emailService;\n"],"names":["createStrictInterpolationRegExp","template","getProviderSettings","strapi","config","get","send","options","plugin","provider","sendTemplatedEmail","emailOptions","emailTemplate","data","attributes","missingAttributes","_","difference","Object","keys","length","Error","join","allowedInterpolationVariables","objects","keysDeep","interpolate","templatedAttributes","reduce","compiled","attribute","assign","emailService"],"mappings":";;;AAWA,MAAM,EAAEA,+BAA+B,EAAE,GAAGC,QAAAA;AAE5C,MAAMC,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