@vrplatform/email 1.0.2-5 → 1.0.2-556

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.
Files changed (39) hide show
  1. package/build/main/components/EmailContainer.js +1 -1
  2. package/build/main/components/EmailContainer.js.map +1 -1
  3. package/build/main/templates/InviteOwnerEmailTemplate.d.ts +3 -1
  4. package/build/main/templates/InviteOwnerEmailTemplate.js +10 -6
  5. package/build/main/templates/InviteOwnerEmailTemplate.js.map +1 -1
  6. package/build/main/templates/InvitePmEmailTemplate.js +7 -3
  7. package/build/main/templates/InvitePmEmailTemplate.js.map +1 -1
  8. package/build/main/utils/find-postmark-email.d.ts +8 -0
  9. package/build/main/utils/find-postmark-email.js +66 -0
  10. package/build/main/utils/find-postmark-email.js.map +1 -0
  11. package/build/main/utils/index.d.ts +2 -0
  12. package/build/main/utils/index.js +2 -0
  13. package/build/main/utils/index.js.map +1 -1
  14. package/build/main/utils/send-email.d.ts +20 -3
  15. package/build/main/utils/send-email.js +97 -19
  16. package/build/main/utils/send-email.js.map +1 -1
  17. package/build/module/components/EmailContainer.js +1 -1
  18. package/build/module/components/EmailContainer.js.map +1 -1
  19. package/build/module/templates/InviteOwnerEmailTemplate.d.ts +3 -1
  20. package/build/module/templates/InviteOwnerEmailTemplate.js +10 -6
  21. package/build/module/templates/InviteOwnerEmailTemplate.js.map +1 -1
  22. package/build/module/templates/InvitePmEmailTemplate.js +7 -3
  23. package/build/module/templates/InvitePmEmailTemplate.js.map +1 -1
  24. package/build/module/utils/find-postmark-email.d.ts +8 -0
  25. package/build/module/utils/find-postmark-email.js +63 -0
  26. package/build/module/utils/find-postmark-email.js.map +1 -0
  27. package/build/module/utils/index.d.ts +2 -0
  28. package/build/module/utils/index.js +2 -0
  29. package/build/module/utils/index.js.map +1 -1
  30. package/build/module/utils/send-email.d.ts +20 -3
  31. package/build/module/utils/send-email.js +97 -19
  32. package/build/module/utils/send-email.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/components/EmailContainer.tsx +1 -1
  35. package/src/templates/InviteOwnerEmailTemplate.tsx +20 -7
  36. package/src/templates/InvitePmEmailTemplate.tsx +7 -3
  37. package/src/utils/find-postmark-email.ts +93 -0
  38. package/src/utils/index.ts +2 -0
  39. package/src/utils/send-email.ts +165 -23
@@ -6,17 +6,21 @@ export const getDefaultInvitePropertyManagerEmailData = (mode, args) => {
6
6
  ? EMAIL_ACTION_LINK_COMPONENT
7
7
  : `<a href='${args.signInLink}' target='_blank'>${args.signInLink}</a>`;
8
8
  return {
9
- bodyOne: `<p>You are invited to join us on ${args.appName}.</p><p>To start using ${args.appName}, accept the invitation and verify your email address.</p>`,
9
+ bodyOne: `<p>You are invited to join us on ${args.appName}.</p><p>Accept the invitation below to set up your team access.</p>`,
10
10
  bodyTwo: `<p>If you have any trouble with the button, you can use the following link below: ${signInLink}</p>`,
11
11
  buttonText: 'Accept Invitation',
12
12
  header: 'Welcome to the team!',
13
- shortDescription: 'You have been invited to join the team. To start using VRPlatform, verify your email address at link below.',
13
+ shortDescription: `Accept your invitation to join your team on ${args.appName}.`,
14
14
  href: args.signInLink,
15
15
  subject: `You are invited to join your team on ${args.appName}`,
16
16
  };
17
17
  };
18
18
  export const InvitePmEmailTemplate = (props) => {
19
- return (_jsx(ActionEmailTemplate, { ...props, tenant: {
19
+ return (_jsx(ActionEmailTemplate, { ...props, data: {
20
+ ...props.data,
21
+ // Add guidance at render time so saved custom templates receive it too.
22
+ bodyTwo: `${props.data.bodyTwo}<hr style="width:100%;border:none;border-top:1px solid #eaeaea;margin:24px 0" /><p>This invitation link is for one-time setup of your team access and expires after 7 days. Sign up using the exact email address this invitation was sent to.</p><p>If your invitation expires before setup is complete, contact your team administrator for a new invitation.</p><p>For questions about team access, contact your team administrator.</p>`,
23
+ }, tenant: {
20
24
  logo: undefined,
21
25
  name: props.tenant.name,
22
26
  } }));
@@ -1 +1 @@
1
- {"version":3,"file":"InvitePmEmailTemplate.js","sourceRoot":"src/","sources":["templates/InvitePmEmailTemplate.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAEL,mBAAmB,GAEpB,MAAM,eAAe,CAAC;AAEvB,MAAM,CAAC,MAAM,wCAAwC,GAAG,CACtD,IAAoB,EACpB,IAGC,EACgB,EAAE;IACnB,MAAM,UAAU,GACd,IAAI,KAAK,KAAK;QACZ,CAAC,CAAC,2BAA2B;QAC7B,CAAC,CAAC,YAAY,IAAI,CAAC,UAAU,qBAAqB,IAAI,CAAC,UAAU,MAAM,CAAC;IAE5E,OAAO;QACL,OAAO,EAAE,oCAAoC,IAAI,CAAC,OAAO,0BAA0B,IAAI,CAAC,OAAO,4DAA4D;QAC3J,OAAO,EAAE,qFAAqF,UAAU,MAAM;QAC9G,UAAU,EAAE,mBAAmB;QAC/B,MAAM,EAAE,sBAAsB;QAC9B,gBAAgB,EACd,6GAA6G;QAC/G,IAAI,EAAE,IAAI,CAAC,UAAU;QACrB,OAAO,EAAE,wCAAwC,IAAI,CAAC,OAAO,EAAE;KAChE,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,KAA+B,EAAE,EAAE;IACvE,OAAO,CACL,KAAC,mBAAmB,OACd,KAAK,EACT,MAAM,EAAE;YACN,IAAI,EAAE,SAAS;YACf,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI;SACxB,GACD,CACH,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { EMAIL_ACTION_LINK_COMPONENT } from '../extensions';\nimport {\n type ActionEmailData,\n ActionEmailTemplate,\n type ActionEmailTemplateProps,\n} from '../components';\n\nexport const getDefaultInvitePropertyManagerEmailData = (\n mode: 'tag' | 'text',\n args: {\n appName: string;\n signInLink: string;\n }\n): ActionEmailData => {\n const signInLink =\n mode === 'tag'\n ? EMAIL_ACTION_LINK_COMPONENT\n : `<a href='${args.signInLink}' target='_blank'>${args.signInLink}</a>`;\n\n return {\n bodyOne: `<p>You are invited to join us on ${args.appName}.</p><p>To start using ${args.appName}, accept the invitation and verify your email address.</p>`,\n bodyTwo: `<p>If you have any trouble with the button, you can use the following link below: ${signInLink}</p>`,\n buttonText: 'Accept Invitation',\n header: 'Welcome to the team!',\n shortDescription:\n 'You have been invited to join the team. To start using VRPlatform, verify your email address at link below.',\n href: args.signInLink,\n subject: `You are invited to join your team on ${args.appName}`,\n };\n};\n\nexport const InvitePmEmailTemplate = (props: ActionEmailTemplateProps) => {\n return (\n <ActionEmailTemplate\n {...props}\n tenant={{\n logo: undefined,\n name: props.tenant.name,\n }}\n />\n );\n};\n"]}
1
+ {"version":3,"file":"InvitePmEmailTemplate.js","sourceRoot":"src/","sources":["templates/InvitePmEmailTemplate.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAEL,mBAAmB,GAEpB,MAAM,eAAe,CAAC;AAEvB,MAAM,CAAC,MAAM,wCAAwC,GAAG,CACtD,IAAoB,EACpB,IAGC,EACgB,EAAE;IACnB,MAAM,UAAU,GACd,IAAI,KAAK,KAAK;QACZ,CAAC,CAAC,2BAA2B;QAC7B,CAAC,CAAC,YAAY,IAAI,CAAC,UAAU,qBAAqB,IAAI,CAAC,UAAU,MAAM,CAAC;IAE5E,OAAO;QACL,OAAO,EAAE,oCAAoC,IAAI,CAAC,OAAO,qEAAqE;QAC9H,OAAO,EAAE,qFAAqF,UAAU,MAAM;QAC9G,UAAU,EAAE,mBAAmB;QAC/B,MAAM,EAAE,sBAAsB;QAC9B,gBAAgB,EAAE,+CAA+C,IAAI,CAAC,OAAO,GAAG;QAChF,IAAI,EAAE,IAAI,CAAC,UAAU;QACrB,OAAO,EAAE,wCAAwC,IAAI,CAAC,OAAO,EAAE;KAChE,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,KAA+B,EAAE,EAAE;IACvE,OAAO,CACL,KAAC,mBAAmB,OACd,KAAK,EACT,IAAI,EAAE;YACJ,GAAG,KAAK,CAAC,IAAI;YACb,wEAAwE;YACxE,OAAO,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,6aAA6a;SAC5c,EACD,MAAM,EAAE;YACN,IAAI,EAAE,SAAS;YACf,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI;SACxB,GACD,CACH,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { EMAIL_ACTION_LINK_COMPONENT } from '../extensions';\nimport {\n type ActionEmailData,\n ActionEmailTemplate,\n type ActionEmailTemplateProps,\n} from '../components';\n\nexport const getDefaultInvitePropertyManagerEmailData = (\n mode: 'tag' | 'text',\n args: {\n appName: string;\n signInLink: string;\n }\n): ActionEmailData => {\n const signInLink =\n mode === 'tag'\n ? EMAIL_ACTION_LINK_COMPONENT\n : `<a href='${args.signInLink}' target='_blank'>${args.signInLink}</a>`;\n\n return {\n bodyOne: `<p>You are invited to join us on ${args.appName}.</p><p>Accept the invitation below to set up your team access.</p>`,\n bodyTwo: `<p>If you have any trouble with the button, you can use the following link below: ${signInLink}</p>`,\n buttonText: 'Accept Invitation',\n header: 'Welcome to the team!',\n shortDescription: `Accept your invitation to join your team on ${args.appName}.`,\n href: args.signInLink,\n subject: `You are invited to join your team on ${args.appName}`,\n };\n};\n\nexport const InvitePmEmailTemplate = (props: ActionEmailTemplateProps) => {\n return (\n <ActionEmailTemplate\n {...props}\n data={{\n ...props.data,\n // Add guidance at render time so saved custom templates receive it too.\n bodyTwo: `${props.data.bodyTwo}<hr style=\"width:100%;border:none;border-top:1px solid #eaeaea;margin:24px 0\" /><p>This invitation link is for one-time setup of your team access and expires after 7 days. Sign up using the exact email address this invitation was sent to.</p><p>If your invitation expires before setup is complete, contact your team administrator for a new invitation.</p><p>For questions about team access, contact your team administrator.</p>`,\n }}\n tenant={{\n logo: undefined,\n name: props.tenant.name,\n }}\n />\n );\n};\n"]}
@@ -0,0 +1,8 @@
1
+ export declare function findPostmarkEmail(deliveryId: string, options: {
2
+ fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
3
+ postmarkToken?: string;
4
+ }): Promise<{
5
+ httpStatus: number;
6
+ messageId: any;
7
+ submittedAt: any;
8
+ } | undefined>;
@@ -0,0 +1,63 @@
1
+ import { day } from '@vrplatform/utils';
2
+ import { EmailDeliveryError } from './send-email';
3
+ export async function findPostmarkEmail(deliveryId, options) {
4
+ if (!options.postmarkToken) {
5
+ throw new EmailDeliveryError('Postmark is not configured', undefined, false, false);
6
+ }
7
+ const url = new URL('https://api.postmarkapp.com/messages/outbound');
8
+ url.searchParams.set('count', '1');
9
+ url.searchParams.set('offset', '0');
10
+ url.searchParams.set('messagestream', 'outbound');
11
+ url.searchParams.set('metadata_deliveryId', deliveryId);
12
+ const response = await (options.fetch ?? fetch)(url, {
13
+ method: 'GET',
14
+ headers: {
15
+ Accept: 'application/json',
16
+ 'X-Postmark-Server-Token': options.postmarkToken,
17
+ },
18
+ });
19
+ const providerResponse = await response.json().catch(() => ({}));
20
+ if (!response.ok) {
21
+ throw new EmailDeliveryError('Postmark message lookup failed', {
22
+ provider: 'postmark',
23
+ providerCategory: 'rejected',
24
+ httpStatus: response.status,
25
+ }, false, response.status === 429 || response.status >= 500);
26
+ }
27
+ if (!providerResponse ||
28
+ typeof providerResponse !== 'object' ||
29
+ !('TotalCount' in providerResponse) ||
30
+ typeof providerResponse.TotalCount !== 'number' ||
31
+ !('Messages' in providerResponse) ||
32
+ !Array.isArray(providerResponse.Messages)) {
33
+ throw new EmailDeliveryError('Postmark returned an invalid response');
34
+ }
35
+ if (providerResponse.TotalCount === 0)
36
+ return undefined;
37
+ const message = providerResponse.Messages.find((value) => value &&
38
+ typeof value === 'object' &&
39
+ 'Metadata' in value &&
40
+ value.Metadata &&
41
+ typeof value.Metadata === 'object' &&
42
+ 'deliveryId' in value.Metadata &&
43
+ value.Metadata.deliveryId === deliveryId);
44
+ const messageId = message &&
45
+ typeof message === 'object' &&
46
+ 'MessageID' in message &&
47
+ typeof message.MessageID === 'string' &&
48
+ message.MessageID
49
+ ? message.MessageID
50
+ : undefined;
51
+ const submittedAt = message &&
52
+ typeof message === 'object' &&
53
+ 'ReceivedAt' in message &&
54
+ typeof message.ReceivedAt === 'string' &&
55
+ day(message.ReceivedAt).isValid()
56
+ ? message.ReceivedAt
57
+ : undefined;
58
+ if (!messageId || !submittedAt) {
59
+ throw new EmailDeliveryError('Postmark returned an invalid response');
60
+ }
61
+ return { httpStatus: response.status, messageId, submittedAt };
62
+ }
63
+ //# sourceMappingURL=find-postmark-email.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find-postmark-email.js","sourceRoot":"src/","sources":["utils/find-postmark-email.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,UAAkB,EAClB,OAMC;IAED,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;QAC3B,MAAM,IAAI,kBAAkB,CAC1B,4BAA4B,EAC5B,SAAS,EACT,KAAK,EACL,KAAK,CACN,CAAC;IACJ,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,+CAA+C,CAAC,CAAC;IACrE,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;IACnC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;IACpC,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;IAClD,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC;IACxD,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,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B,KAAK,CAAC,QAAQ,CAAC,UAAU,KAAK,UAAU,CAC3C,CAAC;IACF,MAAM,SAAS,GACb,OAAO;QACP,OAAO,OAAO,KAAK,QAAQ;QAC3B,WAAW,IAAI,OAAO;QACtB,OAAO,OAAO,CAAC,SAAS,KAAK,QAAQ;QACrC,OAAO,CAAC,SAAS;QACf,CAAC,CAAC,OAAO,CAAC,SAAS;QACnB,CAAC,CAAC,SAAS,CAAC;IAChB,MAAM,WAAW,GACf,OAAO;QACP,OAAO,OAAO,KAAK,QAAQ;QAC3B,YAAY,IAAI,OAAO;QACvB,OAAO,OAAO,CAAC,UAAU,KAAK,QAAQ;QACtC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE;QAC/B,CAAC,CAAC,OAAO,CAAC,UAAU;QACpB,CAAC,CAAC,SAAS,CAAC;IAChB,IAAI,CAAC,SAAS,IAAI,CAAC,WAAW,EAAE,CAAC;QAC/B,MAAM,IAAI,kBAAkB,CAAC,uCAAuC,CAAC,CAAC;IACxE,CAAC;IAED,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC;AACjE,CAAC","sourcesContent":["import { day } from '@vrplatform/utils';\nimport { EmailDeliveryError } from './send-email';\n\nexport async function findPostmarkEmail(\n deliveryId: string,\n options: {\n fetch?: (\n input: string | URL | Request,\n init?: RequestInit\n ) => Promise<Response>;\n postmarkToken?: string;\n }\n) {\n if (!options.postmarkToken) {\n throw new EmailDeliveryError(\n 'Postmark is not configured',\n undefined,\n false,\n false\n );\n }\n\n const url = new URL('https://api.postmarkapp.com/messages/outbound');\n url.searchParams.set('count', '1');\n url.searchParams.set('offset', '0');\n url.searchParams.set('messagestream', 'outbound');\n url.searchParams.set('metadata_deliveryId', deliveryId);\n const response = await (options.fetch ?? fetch)(url, {\n method: 'GET',\n headers: {\n Accept: 'application/json',\n 'X-Postmark-Server-Token': options.postmarkToken,\n },\n });\n const providerResponse: unknown = await response.json().catch(() => ({}));\n\n if (!response.ok) {\n throw new EmailDeliveryError(\n 'Postmark message lookup failed',\n {\n provider: 'postmark',\n providerCategory: 'rejected',\n httpStatus: response.status,\n },\n false,\n response.status === 429 || response.status >= 500\n );\n }\n\n if (\n !providerResponse ||\n typeof providerResponse !== 'object' ||\n !('TotalCount' in providerResponse) ||\n typeof providerResponse.TotalCount !== 'number' ||\n !('Messages' in providerResponse) ||\n !Array.isArray(providerResponse.Messages)\n ) {\n throw new EmailDeliveryError('Postmark returned an invalid response');\n }\n if (providerResponse.TotalCount === 0) return undefined;\n\n const message = providerResponse.Messages.find(\n (value) =>\n value &&\n typeof value === 'object' &&\n 'Metadata' in value &&\n value.Metadata &&\n typeof value.Metadata === 'object' &&\n 'deliveryId' in value.Metadata &&\n value.Metadata.deliveryId === deliveryId\n );\n const messageId =\n message &&\n typeof message === 'object' &&\n 'MessageID' in message &&\n typeof message.MessageID === 'string' &&\n message.MessageID\n ? message.MessageID\n : undefined;\n const submittedAt =\n message &&\n typeof message === 'object' &&\n 'ReceivedAt' in message &&\n typeof message.ReceivedAt === 'string' &&\n day(message.ReceivedAt).isValid()\n ? message.ReceivedAt\n : undefined;\n if (!messageId || !submittedAt) {\n throw new EmailDeliveryError('Postmark returned an invalid response');\n }\n\n return { httpStatus: response.status, messageId, submittedAt };\n}\n"]}
@@ -1 +1,3 @@
1
+ export * from './find-postmark-email';
2
+ export * from './find-postmark-email';
1
3
  export * from './send-email';
@@ -1,2 +1,4 @@
1
+ export * from './find-postmark-email';
2
+ export * from './find-postmark-email';
1
3
  export * from './send-email';
2
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"src/","sources":["utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC","sourcesContent":["export * from './send-email';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC","sourcesContent":["export * from './find-postmark-email';\nexport * from './find-postmark-email';\nexport * from './send-email';\n"]}
@@ -1,7 +1,18 @@
1
+ type EmailDeliveryFailureDetails = {
2
+ httpStatus: number;
3
+ postmarkErrorCode?: number;
4
+ provider: 'postmark';
5
+ providerCategory: 'inactiveRecipient' | 'rejected';
6
+ };
7
+ type EmailDeliveryErrorDetails = EmailDeliveryFailureDetails | {
8
+ failureCount: number;
9
+ failures: EmailDeliveryFailureDetails[];
10
+ };
1
11
  export declare class EmailDeliveryError extends Error {
2
- readonly details?: Record<string, unknown> | undefined;
12
+ readonly details?: EmailDeliveryErrorDetails | undefined;
3
13
  readonly expected: boolean;
4
- constructor(message: string, details?: Record<string, unknown> | undefined, expected?: boolean);
14
+ readonly retryable: boolean;
15
+ constructor(message: string, details?: EmailDeliveryErrorDetails | undefined, expected?: boolean, retryable?: boolean);
5
16
  }
6
17
  export declare function sendEmail(data: {
7
18
  to: string;
@@ -9,9 +20,15 @@ export declare function sendEmail(data: {
9
20
  text: string;
10
21
  html: string;
11
22
  replyTo?: string;
23
+ tag?: string;
24
+ metadata?: Record<string, string>;
12
25
  }, options: {
13
26
  fetch?: (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
14
27
  postmarkToken?: string;
15
28
  }): Promise<{
16
- status: number;
29
+ httpStatus: number;
30
+ messageId: string;
31
+ submittedAt: string;
17
32
  }>;
33
+ export declare function settleEmailDeliveries(deliveries: Promise<unknown>[]): Promise<void>;
34
+ export {};
@@ -1,16 +1,19 @@
1
+ import { day } from '@vrplatform/utils';
1
2
  export class EmailDeliveryError extends Error {
2
3
  details;
3
4
  expected;
4
- constructor(message, details, expected = false) {
5
+ retryable;
6
+ constructor(message, details, expected = false, retryable = true) {
5
7
  super(message);
6
8
  this.details = details;
7
9
  this.expected = expected;
10
+ this.retryable = retryable;
8
11
  this.name = 'EmailDeliveryError';
9
12
  }
10
13
  }
11
14
  export async function sendEmail(data, options) {
12
15
  if (!options.postmarkToken) {
13
- throw new EmailDeliveryError('Postmark is not configured');
16
+ throw new EmailDeliveryError('Postmark is not configured', undefined, false, false);
14
17
  }
15
18
  const response = await (options.fetch ?? fetch)('https://api.postmarkapp.com/email', {
16
19
  method: 'POST',
@@ -27,30 +30,105 @@ export async function sendEmail(data, options) {
27
30
  TextBody: data.text,
28
31
  HtmlBody: data.html,
29
32
  MessageStream: 'outbound',
33
+ Tag: data.tag,
34
+ Metadata: data.metadata,
30
35
  }),
31
36
  });
32
37
  const providerResponse = await response.json().catch(() => ({}));
33
- const providerMessage = providerResponse !== null &&
38
+ const responseErrorCode = providerResponse !== null &&
34
39
  typeof providerResponse === 'object' &&
35
- 'Message' in providerResponse &&
36
- typeof providerResponse.Message === 'string'
37
- ? providerResponse.Message
40
+ 'ErrorCode' in providerResponse &&
41
+ typeof providerResponse.ErrorCode === 'number'
42
+ ? providerResponse.ErrorCode
38
43
  : undefined;
39
- if (!response.ok) {
40
- if (response.status === 422 &&
41
- providerMessage?.includes('marked as inactive')) {
42
- throw new EmailDeliveryError('Postmark recipient is inactive', {
43
- providerMessage,
44
- status: response.status,
45
- statusText: response.statusText,
46
- }, true);
44
+ const headerErrorCode = response.headers.get('X-PM-ApiErrorCode');
45
+ const errorCode = responseErrorCode ??
46
+ (headerErrorCode && /^\d+$/.test(headerErrorCode)
47
+ ? Number(headerErrorCode)
48
+ : undefined);
49
+ const postmarkErrorCode = typeof errorCode === 'number' &&
50
+ Number.isSafeInteger(errorCode) &&
51
+ errorCode >= 0
52
+ ? errorCode
53
+ : undefined;
54
+ if (!response.ok ||
55
+ (postmarkErrorCode !== undefined && postmarkErrorCode !== 0)) {
56
+ const inactiveRecipient = response.status === 422 && postmarkErrorCode === 406;
57
+ const details = {
58
+ provider: 'postmark',
59
+ providerCategory: inactiveRecipient ? 'inactiveRecipient' : 'rejected',
60
+ httpStatus: response.status,
61
+ ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),
62
+ };
63
+ if (inactiveRecipient) {
64
+ throw new EmailDeliveryError('Postmark recipient is inactive', details, true, false);
47
65
  }
48
- throw new EmailDeliveryError('Postmark rejected the email', {
49
- providerResponse,
50
- status: response.status,
51
- statusText: response.statusText,
66
+ throw new EmailDeliveryError('Postmark rejected the email', details, false, response.status === 429 || response.status >= 500);
67
+ }
68
+ const messageId = providerResponse !== null &&
69
+ typeof providerResponse === 'object' &&
70
+ 'MessageID' in providerResponse &&
71
+ typeof providerResponse.MessageID === 'string' &&
72
+ providerResponse.MessageID
73
+ ? providerResponse.MessageID
74
+ : undefined;
75
+ const submittedAt = providerResponse !== null &&
76
+ typeof providerResponse === 'object' &&
77
+ 'SubmittedAt' in providerResponse &&
78
+ typeof providerResponse.SubmittedAt === 'string' &&
79
+ day(providerResponse.SubmittedAt).isValid()
80
+ ? providerResponse.SubmittedAt
81
+ : undefined;
82
+ if (!messageId || !submittedAt || postmarkErrorCode !== 0) {
83
+ throw new EmailDeliveryError('Postmark returned an invalid response', {
84
+ provider: 'postmark',
85
+ providerCategory: 'rejected',
86
+ httpStatus: response.status,
87
+ ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),
52
88
  });
53
89
  }
54
- return { status: response.status };
90
+ return {
91
+ httpStatus: response.status,
92
+ messageId,
93
+ submittedAt,
94
+ };
95
+ }
96
+ export async function settleEmailDeliveries(deliveries) {
97
+ const failures = (await Promise.allSettled(deliveries)).filter((result) => result.status === 'rejected');
98
+ if (failures.length === 0)
99
+ return;
100
+ const unexpectedDeliveryError = failures.find((failure) => failure.reason instanceof EmailDeliveryError && !failure.reason.expected)?.reason;
101
+ const allExpected = failures.every((failure) => failure.reason instanceof EmailDeliveryError && failure.reason.expected);
102
+ const firstDeliveryError = failures.find((failure) => failure.reason instanceof EmailDeliveryError)?.reason;
103
+ const failureDetails = failures.flatMap((failure) => {
104
+ if (!(failure.reason instanceof EmailDeliveryError) ||
105
+ !failure.reason.details ||
106
+ !('provider' in failure.reason.details)) {
107
+ return [];
108
+ }
109
+ const { httpStatus, postmarkErrorCode, provider, providerCategory } = failure.reason.details;
110
+ return [
111
+ {
112
+ httpStatus,
113
+ provider,
114
+ providerCategory,
115
+ ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),
116
+ },
117
+ ];
118
+ });
119
+ const details = {
120
+ failureCount: failures.length,
121
+ failures: failureDetails,
122
+ };
123
+ if (unexpectedDeliveryError instanceof EmailDeliveryError) {
124
+ if (unexpectedDeliveryError.message === 'Postmark is not configured') {
125
+ throw new EmailDeliveryError('Postmark is not configured', details, false, false);
126
+ }
127
+ throw new EmailDeliveryError('Postmark rejected the email', details, false, unexpectedDeliveryError.retryable);
128
+ }
129
+ if (allExpected && firstDeliveryError instanceof EmailDeliveryError) {
130
+ throw new EmailDeliveryError('Postmark recipient is inactive', details, true, false);
131
+ }
132
+ throw new EmailDeliveryError('Email delivery failed', details);
55
133
  }
56
134
  //# sourceMappingURL=send-email.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"send-email.js","sourceRoot":"src/","sources":["utils/send-email.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAGzB;IACA;IAHlB,YACE,OAAe,EACC,OAAiC,EACjC,WAAW,KAAK;QAEhC,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,YAAO,GAAP,OAAO,CAA0B;QACjC,aAAQ,GAAR,QAAQ,CAAQ;QAGhC,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,IAMC,EACD,OAMC;IAED,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;QAC3B,MAAM,IAAI,kBAAkB,CAAC,4BAA4B,CAAC,CAAC;IAC7D,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,CAC7C,mCAAmC,EACnC;QACE,MAAM,E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AAU;SAChC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;AACrC,CAAC","sourcesContent":["export class EmailDeliveryError extends Error {\n constructor(\n message: string,\n public readonly details?: Record<string, unknown>,\n public readonly expected = false\n ) {\n super(message);\n this.name = 'EmailDeliveryError';\n }\n}\n\nexport async function sendEmail(\n data: {\n to: string;\n subject: string;\n text: string;\n html: string;\n replyTo?: string;\n },\n options: {\n fetch?: (\n input: string | URL | Request,\n init?: RequestInit\n ) => Promise<Response>;\n postmarkToken?: string;\n }\n) {\n if (!options.postmarkToken) {\n throw new EmailDeliveryError('Postmark is not configured');\n }\n\n const response = await (options.fetch ?? fetch)(\n 'https://api.postmarkapp.com/email',\n {\n method: 'POST',\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n 'X-Postmark-Server-Token': options.postmarkToken,\n },\n body: JSON.stringify({\n From: 'VRPlatform <notifications@vrplatform.app>',\n To: data.to,\n Subject: data.subject,\n ReplyTo: data.replyTo,\n TextBody: data.text,\n HtmlBody: data.html,\n MessageStream: 'outbound',\n }),\n }\n );\n\n const providerResponse: unknown = await response.json().catch(() => ({}));\n const providerMessage =\n providerResponse !== null &&\n typeof providerResponse === 'object' &&\n 'Message' in providerResponse &&\n typeof providerResponse.Message === 'string'\n ? providerResponse.Message\n : undefined;\n\n if (!response.ok) {\n if (\n response.status === 422 &&\n providerMessage?.includes('marked as inactive')\n ) {\n throw new EmailDeliveryError(\n 'Postmark recipient is inactive',\n {\n providerMessage,\n status: response.status,\n statusText: response.statusText,\n },\n true\n );\n }\n\n throw new EmailDeliveryError('Postmark rejected the email', {\n providerResponse,\n status: response.status,\n statusText: response.statusText,\n });\n }\n\n return { status: response.status };\n}\n"]}
1
+ {"version":3,"file":"send-email.js","sourceRoot":"src/","sources":["utils/send-email.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAgBxC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C,CAAC,CAAC,EAAE,iBAAiB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAClE;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,GAA8B;QACzC,YAAY,EAAE,QAAQ,CAAC,MAAM;QAC7B,QAAQ,EAAE,cAAc;KACzB,CAAC;IACF,IAAI,uBAAuB,YAAY,kBAAkB,EAAE,CAAC;QAC1D,IAAI,uBAAuB,CAAC,OAAO,KAAK,4BAA4B,EAAE,CAAC;YACrE,MAAM,IAAI,kBAAkB,CAC1B,4BAA4B,EAC5B,OAAO,EACP,KAAK,EACL,KAAK,CACN,CAAC;QACJ,CAAC;QACD,MAAM,IAAI,kBAAkB,CAC1B,6BAA6B,EAC7B,OAAO,EACP,KAAK,EACL,uBAAuB,CAAC,SAAS,CAClC,CAAC;IACJ,CAAC;IACD,IAAI,WAAW,IAAI,kBAAkB,YAAY,kBAAkB,EAAE,CAAC;QACpE,MAAM,IAAI,kBAAkB,CAC1B,gCAAgC,EAChC,OAAO,EACP,IAAI,EACJ,KAAK,CACN,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,kBAAkB,CAAC,uBAAuB,EAAE,OAAO,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { day } from '@vrplatform/utils';\n\ntype EmailDeliveryFailureDetails = {\n httpStatus: number;\n postmarkErrorCode?: number;\n provider: 'postmark';\n providerCategory: 'inactiveRecipient' | 'rejected';\n};\n\ntype EmailDeliveryErrorDetails =\n | EmailDeliveryFailureDetails\n | {\n failureCount: number;\n failures: EmailDeliveryFailureDetails[];\n };\n\nexport class EmailDeliveryError extends Error {\n constructor(\n message: string,\n public readonly details?: EmailDeliveryErrorDetails,\n public readonly expected = false,\n public readonly retryable = true\n ) {\n super(message);\n this.name = 'EmailDeliveryError';\n }\n}\n\nexport async function sendEmail(\n data: {\n to: string;\n subject: string;\n text: string;\n html: string;\n replyTo?: string;\n tag?: string;\n metadata?: Record<string, string>;\n },\n options: {\n fetch?: (\n input: string | URL | Request,\n init?: RequestInit\n ) => Promise<Response>;\n postmarkToken?: string;\n }\n) {\n if (!options.postmarkToken) {\n throw new EmailDeliveryError(\n 'Postmark is not configured',\n undefined,\n false,\n false\n );\n }\n\n const response = await (options.fetch ?? fetch)(\n 'https://api.postmarkapp.com/email',\n {\n method: 'POST',\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n 'X-Postmark-Server-Token': options.postmarkToken,\n },\n body: JSON.stringify({\n From: 'VRPlatform <notifications@vrplatform.app>',\n To: data.to,\n Subject: data.subject,\n ReplyTo: data.replyTo,\n TextBody: data.text,\n HtmlBody: data.html,\n MessageStream: 'outbound',\n Tag: data.tag,\n Metadata: data.metadata,\n }),\n }\n );\n\n const providerResponse: unknown = await response.json().catch(() => ({}));\n const responseErrorCode =\n providerResponse !== null &&\n typeof providerResponse === 'object' &&\n 'ErrorCode' in providerResponse &&\n typeof providerResponse.ErrorCode === 'number'\n ? providerResponse.ErrorCode\n : undefined;\n const headerErrorCode = response.headers.get('X-PM-ApiErrorCode');\n const errorCode =\n responseErrorCode ??\n (headerErrorCode && /^\\d+$/.test(headerErrorCode)\n ? Number(headerErrorCode)\n : undefined);\n const postmarkErrorCode =\n typeof errorCode === 'number' &&\n Number.isSafeInteger(errorCode) &&\n errorCode >= 0\n ? errorCode\n : undefined;\n\n if (\n !response.ok ||\n (postmarkErrorCode !== undefined && postmarkErrorCode !== 0)\n ) {\n const inactiveRecipient =\n response.status === 422 && postmarkErrorCode === 406;\n const details: EmailDeliveryFailureDetails = {\n provider: 'postmark',\n providerCategory: inactiveRecipient ? 'inactiveRecipient' : 'rejected',\n httpStatus: response.status,\n ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),\n };\n\n if (inactiveRecipient) {\n throw new EmailDeliveryError(\n 'Postmark recipient is inactive',\n details,\n true,\n false\n );\n }\n\n throw new EmailDeliveryError(\n 'Postmark rejected the email',\n details,\n false,\n response.status === 429 || response.status >= 500\n );\n }\n\n const messageId =\n providerResponse !== null &&\n typeof providerResponse === 'object' &&\n 'MessageID' in providerResponse &&\n typeof providerResponse.MessageID === 'string' &&\n providerResponse.MessageID\n ? providerResponse.MessageID\n : undefined;\n const submittedAt =\n providerResponse !== null &&\n typeof providerResponse === 'object' &&\n 'SubmittedAt' in providerResponse &&\n typeof providerResponse.SubmittedAt === 'string' &&\n day(providerResponse.SubmittedAt).isValid()\n ? providerResponse.SubmittedAt\n : undefined;\n if (!messageId || !submittedAt || postmarkErrorCode !== 0) {\n throw new EmailDeliveryError('Postmark returned an invalid response', {\n provider: 'postmark',\n providerCategory: 'rejected',\n httpStatus: response.status,\n ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),\n });\n }\n\n return {\n httpStatus: response.status,\n messageId,\n submittedAt,\n };\n}\n\nexport async function settleEmailDeliveries(deliveries: Promise<unknown>[]) {\n const failures = (await Promise.allSettled(deliveries)).filter(\n (result) => result.status === 'rejected'\n );\n if (failures.length === 0) return;\n\n const unexpectedDeliveryError = failures.find(\n (failure) =>\n failure.reason instanceof EmailDeliveryError && !failure.reason.expected\n )?.reason;\n const allExpected = failures.every(\n (failure) =>\n failure.reason instanceof EmailDeliveryError && failure.reason.expected\n );\n const firstDeliveryError = failures.find(\n (failure) => failure.reason instanceof EmailDeliveryError\n )?.reason;\n const failureDetails = failures.flatMap((failure) => {\n if (\n !(failure.reason instanceof EmailDeliveryError) ||\n !failure.reason.details ||\n !('provider' in failure.reason.details)\n ) {\n return [];\n }\n const { httpStatus, postmarkErrorCode, provider, providerCategory } =\n failure.reason.details;\n return [\n {\n httpStatus,\n provider,\n providerCategory,\n ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),\n },\n ];\n });\n\n const details: EmailDeliveryErrorDetails = {\n failureCount: failures.length,\n failures: failureDetails,\n };\n if (unexpectedDeliveryError instanceof EmailDeliveryError) {\n if (unexpectedDeliveryError.message === 'Postmark is not configured') {\n throw new EmailDeliveryError(\n 'Postmark is not configured',\n details,\n false,\n false\n );\n }\n throw new EmailDeliveryError(\n 'Postmark rejected the email',\n details,\n false,\n unexpectedDeliveryError.retryable\n );\n }\n if (allExpected && firstDeliveryError instanceof EmailDeliveryError) {\n throw new EmailDeliveryError(\n 'Postmark recipient is inactive',\n details,\n true,\n false\n );\n }\n throw new EmailDeliveryError('Email delivery failed', details);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vrplatform/email",
3
- "version": "1.0.2-5",
3
+ "version": "1.0.2-556",
4
4
  "description": "VRTrust transactional email templates and Postmark transport",
5
5
  "license": "ISC",
6
6
  "main": "build/main/index.js",
@@ -124,7 +124,7 @@ const Footer = () => (
124
124
  color: '#737373',
125
125
  }}
126
126
  >
127
- © {day().year()} VRPlatform Ventures Ltd. All rights reserved.
127
+ © {day().year()} VR Technologies. All rights reserved.
128
128
  </Text>
129
129
  <Text
130
130
  style={{
@@ -16,17 +16,30 @@ export const getDefaultInviteOwnerEmailData = (
16
16
  mode === 'tag' ? EMAIL_ACTION_LINK_COMPONENT : args.signInLink;
17
17
 
18
18
  return {
19
- bodyOne: `<p>You are invited to join us on ${args.appName}.</p><p>To start using ${args.appName}, accept the invitation and verify your email address.</p>`,
19
+ bodyOne: `<p>You are invited to access your Owner Portal on ${args.appName}.</p><p>Accept the invitation below to set up your access.</p>`,
20
20
  bodyTwo: `<p>If you have any trouble with the button, you can copy and paste the link below into your browser: ${signInLink}</p>`,
21
21
  buttonText: 'Accept Invitation',
22
- header: 'Welcome to the team!',
23
- shortDescription:
24
- 'You have been invited to join the team. To start using VRPlatform, verify your email address at link below.',
22
+ header: 'Welcome to your Owner Portal!',
23
+ shortDescription: 'Set up your Owner Portal access using your invitation.',
25
24
  href: args.signInLink,
26
- subject: `You are invited to join your team on ${args.appName}`,
25
+ subject: `Your invitation to the ${args.appName} Owner Portal`,
27
26
  };
28
27
  };
29
28
 
30
- export const InviteOwnerEmailTemplate = (props: ActionEmailTemplateProps) => {
31
- return <ActionEmailTemplate {...props} />;
29
+ export const InviteOwnerEmailTemplate = ({
30
+ ownerAccessRole = 'admin',
31
+ ...props
32
+ }: ActionEmailTemplateProps & {
33
+ ownerAccessRole?: 'admin' | 'viewer';
34
+ }) => {
35
+ return (
36
+ <ActionEmailTemplate
37
+ {...props}
38
+ data={{
39
+ ...props.data,
40
+ // Add guidance at render time so saved custom templates receive it too.
41
+ bodyTwo: `${props.data.bodyTwo}<hr style="width:100%;border:none;border-top:1px solid #eaeaea;margin:24px 0" /><p>This invitation link is for one-time setup of your Owner Portal access and expires after 7 days. Sign up using the exact email address this invitation was sent to.</p><p>If your invitation expires before setup is complete, contact your property manager for a new invitation.</p><p>For questions about ${ownerAccessRole === 'viewer' ? 'portal access' : 'your statements or portal access'}, contact your property manager.</p>`,
42
+ }}
43
+ />
44
+ );
32
45
  };
@@ -18,12 +18,11 @@ export const getDefaultInvitePropertyManagerEmailData = (
18
18
  : `<a href='${args.signInLink}' target='_blank'>${args.signInLink}</a>`;
19
19
 
20
20
  return {
21
- bodyOne: `<p>You are invited to join us on ${args.appName}.</p><p>To start using ${args.appName}, accept the invitation and verify your email address.</p>`,
21
+ bodyOne: `<p>You are invited to join us on ${args.appName}.</p><p>Accept the invitation below to set up your team access.</p>`,
22
22
  bodyTwo: `<p>If you have any trouble with the button, you can use the following link below: ${signInLink}</p>`,
23
23
  buttonText: 'Accept Invitation',
24
24
  header: 'Welcome to the team!',
25
- shortDescription:
26
- 'You have been invited to join the team. To start using VRPlatform, verify your email address at link below.',
25
+ shortDescription: `Accept your invitation to join your team on ${args.appName}.`,
27
26
  href: args.signInLink,
28
27
  subject: `You are invited to join your team on ${args.appName}`,
29
28
  };
@@ -33,6 +32,11 @@ export const InvitePmEmailTemplate = (props: ActionEmailTemplateProps) => {
33
32
  return (
34
33
  <ActionEmailTemplate
35
34
  {...props}
35
+ data={{
36
+ ...props.data,
37
+ // Add guidance at render time so saved custom templates receive it too.
38
+ bodyTwo: `${props.data.bodyTwo}<hr style="width:100%;border:none;border-top:1px solid #eaeaea;margin:24px 0" /><p>This invitation link is for one-time setup of your team access and expires after 7 days. Sign up using the exact email address this invitation was sent to.</p><p>If your invitation expires before setup is complete, contact your team administrator for a new invitation.</p><p>For questions about team access, contact your team administrator.</p>`,
39
+ }}
36
40
  tenant={{
37
41
  logo: undefined,
38
42
  name: props.tenant.name,
@@ -0,0 +1,93 @@
1
+ import { day } from '@vrplatform/utils';
2
+ import { EmailDeliveryError } from './send-email';
3
+
4
+ export async function findPostmarkEmail(
5
+ deliveryId: string,
6
+ options: {
7
+ fetch?: (
8
+ input: string | URL | Request,
9
+ init?: RequestInit
10
+ ) => Promise<Response>;
11
+ postmarkToken?: string;
12
+ }
13
+ ) {
14
+ if (!options.postmarkToken) {
15
+ throw new EmailDeliveryError(
16
+ 'Postmark is not configured',
17
+ undefined,
18
+ false,
19
+ false
20
+ );
21
+ }
22
+
23
+ const url = new URL('https://api.postmarkapp.com/messages/outbound');
24
+ url.searchParams.set('count', '1');
25
+ url.searchParams.set('offset', '0');
26
+ url.searchParams.set('messagestream', 'outbound');
27
+ url.searchParams.set('metadata_deliveryId', deliveryId);
28
+ const response = await (options.fetch ?? fetch)(url, {
29
+ method: 'GET',
30
+ headers: {
31
+ Accept: 'application/json',
32
+ 'X-Postmark-Server-Token': options.postmarkToken,
33
+ },
34
+ });
35
+ const providerResponse: unknown = await response.json().catch(() => ({}));
36
+
37
+ if (!response.ok) {
38
+ throw new EmailDeliveryError(
39
+ 'Postmark message lookup failed',
40
+ {
41
+ provider: 'postmark',
42
+ providerCategory: 'rejected',
43
+ httpStatus: response.status,
44
+ },
45
+ false,
46
+ response.status === 429 || response.status >= 500
47
+ );
48
+ }
49
+
50
+ if (
51
+ !providerResponse ||
52
+ typeof providerResponse !== 'object' ||
53
+ !('TotalCount' in providerResponse) ||
54
+ typeof providerResponse.TotalCount !== 'number' ||
55
+ !('Messages' in providerResponse) ||
56
+ !Array.isArray(providerResponse.Messages)
57
+ ) {
58
+ throw new EmailDeliveryError('Postmark returned an invalid response');
59
+ }
60
+ if (providerResponse.TotalCount === 0) return undefined;
61
+
62
+ const message = providerResponse.Messages.find(
63
+ (value) =>
64
+ value &&
65
+ typeof value === 'object' &&
66
+ 'Metadata' in value &&
67
+ value.Metadata &&
68
+ typeof value.Metadata === 'object' &&
69
+ 'deliveryId' in value.Metadata &&
70
+ value.Metadata.deliveryId === deliveryId
71
+ );
72
+ const messageId =
73
+ message &&
74
+ typeof message === 'object' &&
75
+ 'MessageID' in message &&
76
+ typeof message.MessageID === 'string' &&
77
+ message.MessageID
78
+ ? message.MessageID
79
+ : undefined;
80
+ const submittedAt =
81
+ message &&
82
+ typeof message === 'object' &&
83
+ 'ReceivedAt' in message &&
84
+ typeof message.ReceivedAt === 'string' &&
85
+ day(message.ReceivedAt).isValid()
86
+ ? message.ReceivedAt
87
+ : undefined;
88
+ if (!messageId || !submittedAt) {
89
+ throw new EmailDeliveryError('Postmark returned an invalid response');
90
+ }
91
+
92
+ return { httpStatus: response.status, messageId, submittedAt };
93
+ }
@@ -1 +1,3 @@
1
+ export * from './find-postmark-email';
2
+ export * from './find-postmark-email';
1
3
  export * from './send-email';