@punks/backend-entity-manager 0.0.41 → 0.0.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import { HtmlEmailInput, IEmailProvider, IEmailTemplate, TemplatedEmailInput } f
2
2
  import { SendgridEmailTemplateData } from "../../abstractions";
3
3
  export declare class SendgridEmailProvider implements IEmailProvider<SendgridEmailTemplateData> {
4
4
  private readonly client;
5
+ private readonly logger;
5
6
  constructor();
6
7
  sendTemplatedEmail<TPayload, TAugmentedPayload>(input: TemplatedEmailInput<TPayload>, template: IEmailTemplate<SendgridEmailTemplateData, TPayload, TAugmentedPayload>): Promise<void>;
7
8
  sendHtmlEmail<TPayload>(input: HtmlEmailInput<TPayload>): Promise<void>;
@@ -5,5 +5,6 @@ export type SendgridSettings = {
5
5
  defaultReplyTo?: string;
6
6
  defaultCc?: string[];
7
7
  defaultBcc?: string[];
8
+ loggingEnabled?: boolean;
8
9
  };
9
10
  export declare const sendgridSettings: AppInMemorySettings<SendgridSettings>;
package/dist/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { applyDecorators, Injectable, SetMetadata, Global, Module, Scope, Logger, HttpStatus, HttpException } from '@nestjs/common';
2
2
  import { AsyncLocalStorage } from 'async_hooks';
3
3
  import { hash } from 'bcrypt';
4
- import { Log, toDict, newUuid as newUuid$1 } from '@punks/backend-core';
4
+ import { Log, toDict, newUuid as newUuid$1, removeUndefinedProps } from '@punks/backend-core';
5
5
  import { PATH_METADATA } from '@nestjs/common/constants';
6
6
  import { STATIC_CONTEXT } from '@nestjs/core/injector/constants';
7
7
  import { MetadataScanner } from '@nestjs/core/metadata-scanner';
@@ -25007,8 +25007,11 @@ const WpSendgridEmailTemplate = (templateId, sendgridTemplateData) => applyDecor
25007
25007
 
25008
25008
  const sendgridSettings = new AppInMemorySettings();
25009
25009
 
25010
+ const sanitizeValue = (value) => value?.trim() ? value?.trim() : undefined;
25011
+ const sanitizeArray = (value) => (value?.length ?? 0) > 0 ? value : undefined;
25010
25012
  let SendgridEmailProvider = class SendgridEmailProvider {
25011
25013
  constructor() {
25014
+ this.logger = Log.getLogger("Sendgrid");
25012
25015
  this.client = new MailService();
25013
25016
  this.client.setApiKey(sendgridSettings.value.apiKey);
25014
25017
  }
@@ -25020,7 +25023,7 @@ let SendgridEmailProvider = class SendgridEmailProvider {
25020
25023
  if (!body) {
25021
25024
  throw new Error(`Missing body template for language ${input.languageId}`);
25022
25025
  }
25023
- const subject = getLocalizedText(templateData.bodyTemplate, input.languageId);
25026
+ const subject = getLocalizedText(templateData.subjectTemplate, input.languageId);
25024
25027
  if (!subject) {
25025
25028
  throw new Error(`Missing subject template for language ${input.languageId}`);
25026
25029
  }
@@ -25035,34 +25038,44 @@ let SendgridEmailProvider = class SendgridEmailProvider {
25035
25038
  });
25036
25039
  return;
25037
25040
  }
25038
- await this.client.send({
25041
+ const request = removeUndefinedProps({
25039
25042
  from: input.from ?? templateData.from ?? sendgridSettings.value.defaultSender,
25040
- cc: [
25043
+ cc: sanitizeArray([
25041
25044
  ...(input.cc ?? templateData.cc ?? []),
25042
25045
  ...(sendgridSettings.value.defaultCc ?? []),
25043
- ],
25044
- bcc: [
25046
+ ]),
25047
+ bcc: sanitizeArray([
25045
25048
  ...(input.bcc ?? templateData.bcc ?? []),
25046
25049
  ...(sendgridSettings.value.defaultBcc ?? []),
25047
- ],
25050
+ ]),
25048
25051
  subject: input.subjectTemplate
25049
25052
  ? renderHandlebarsTemplate({
25050
25053
  template: input.subjectTemplate,
25051
25054
  context: processedPayload,
25052
25055
  })
25053
25056
  : undefined,
25054
- replyTo: templateData.replyTo ?? sendgridSettings.value.defaultReplyTo,
25057
+ replyTo: sanitizeValue(templateData.replyTo ?? sendgridSettings.value.defaultReplyTo),
25055
25058
  dynamicTemplateData: {
25056
25059
  ...processedPayload,
25057
25060
  },
25058
25061
  templateId: templateData.sendgridTemplateId,
25059
25062
  });
25063
+ if (sendgridSettings.value.loggingEnabled) {
25064
+ this.logger.info("Sending templated email", request);
25065
+ }
25066
+ await this.client.send(request);
25067
+ if (sendgridSettings.value.loggingEnabled) {
25068
+ this.logger.info("Templated email sent", request);
25069
+ }
25060
25070
  }
25061
25071
  async sendHtmlEmail(input) {
25062
- await this.client.send({
25072
+ const request = removeUndefinedProps({
25063
25073
  from: input.from ?? sendgridSettings.value.defaultSender,
25064
- cc: input.cc,
25065
- bcc: [...(input.bcc ?? []), ...(sendgridSettings.value.defaultBcc ?? [])],
25074
+ cc: sanitizeArray(input.cc),
25075
+ bcc: sanitizeArray([
25076
+ ...(input.bcc ?? []),
25077
+ ...(sendgridSettings.value.defaultBcc ?? []),
25078
+ ]),
25066
25079
  subject: renderHandlebarsTemplate({
25067
25080
  template: input.subjectTemplate,
25068
25081
  context: input.payload,
@@ -25071,8 +25084,15 @@ let SendgridEmailProvider = class SendgridEmailProvider {
25071
25084
  template: input.bodyTemplate,
25072
25085
  context: input.payload,
25073
25086
  }),
25074
- replyTo: input.replyTo ?? sendgridSettings.value.defaultReplyTo,
25087
+ replyTo: sanitizeValue(input.replyTo ?? sendgridSettings.value.defaultReplyTo),
25075
25088
  });
25089
+ if (sendgridSettings.value.loggingEnabled) {
25090
+ this.logger.info("Sending html email", request);
25091
+ }
25092
+ await this.client.send(request);
25093
+ if (sendgridSettings.value.loggingEnabled) {
25094
+ this.logger.info("Html email sent", request);
25095
+ }
25076
25096
  }
25077
25097
  };
25078
25098
  SendgridEmailProvider = __decorate([