@vrplatform/email 1.0.2-434 → 1.0.2-43579466270

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 (144) hide show
  1. package/build/main/components/ActionEmailTemplate.d.ts +3 -1
  2. package/build/main/components/ActionEmailTemplate.js +9 -13
  3. package/build/main/components/ActionEmailTemplate.js.map +1 -1
  4. package/build/main/components/Button.js.map +1 -1
  5. package/build/main/components/EmailContainer.d.ts +4 -1
  6. package/build/main/components/EmailContainer.js +4 -3
  7. package/build/main/components/EmailContainer.js.map +1 -1
  8. package/build/main/components/EmailHeading.d.ts +8 -0
  9. package/build/main/components/EmailHeading.js +21 -0
  10. package/build/main/components/EmailHeading.js.map +1 -0
  11. package/build/main/components/index.d.ts +1 -0
  12. package/build/main/components/index.js +1 -0
  13. package/build/main/components/index.js.map +1 -1
  14. package/build/main/components/utils/parse-email-html.js +13 -4
  15. package/build/main/components/utils/parse-email-html.js.map +1 -1
  16. package/build/main/const.d.ts +4 -4
  17. package/build/main/index.d.ts +1 -1
  18. package/build/main/index.js +2 -3
  19. package/build/main/index.js.map +1 -1
  20. package/build/main/render.d.ts +2 -0
  21. package/build/main/render.js +24 -0
  22. package/build/main/render.js.map +1 -0
  23. package/build/main/templates/BankAccountChangedEmailTemplate.d.ts +12 -0
  24. package/build/main/templates/BankAccountChangedEmailTemplate.js +12 -0
  25. package/build/main/templates/BankAccountChangedEmailTemplate.js.map +1 -0
  26. package/build/main/templates/BankAccountChangedTeamAdminEmailTemplate.d.ts +17 -0
  27. package/build/main/templates/BankAccountChangedTeamAdminEmailTemplate.js +12 -0
  28. package/build/main/templates/BankAccountChangedTeamAdminEmailTemplate.js.map +1 -0
  29. package/build/main/templates/BankDetailsRequestEmailTemplate.d.ts +17 -0
  30. package/build/main/templates/BankDetailsRequestEmailTemplate.js +15 -0
  31. package/build/main/templates/BankDetailsRequestEmailTemplate.js.map +1 -0
  32. package/build/main/templates/InviteOwnerEmailTemplate.d.ts +2 -2
  33. package/build/main/templates/InviteOwnerEmailTemplate.js +1 -1
  34. package/build/main/templates/InviteOwnerEmailTemplate.js.map +1 -1
  35. package/build/main/templates/InvitePmEmailTemplate.d.ts +2 -1
  36. package/build/main/templates/InvitePmEmailTemplate.js +4 -7
  37. package/build/main/templates/InvitePmEmailTemplate.js.map +1 -1
  38. package/build/main/templates/OwnerStatementEmailTemplate.d.ts +6 -3
  39. package/build/main/templates/OwnerStatementEmailTemplate.js +70 -9
  40. package/build/main/templates/OwnerStatementEmailTemplate.js.map +1 -1
  41. package/build/main/templates/TeamRoleGrantedEmailTemplate.d.ts +12 -0
  42. package/build/main/templates/TeamRoleGrantedEmailTemplate.js +12 -0
  43. package/build/main/templates/TeamRoleGrantedEmailTemplate.js.map +1 -0
  44. package/build/main/templates/TenantDeletionEmailTemplate.js +2 -6
  45. package/build/main/templates/TenantDeletionEmailTemplate.js.map +1 -1
  46. package/build/main/templates/TenantSuspensionEmailTemplate.js +2 -6
  47. package/build/main/templates/TenantSuspensionEmailTemplate.js.map +1 -1
  48. package/build/main/templates/index.d.ts +4 -0
  49. package/build/main/templates/index.js +5 -0
  50. package/build/main/templates/index.js.map +1 -1
  51. package/build/main/theme.js.map +1 -1
  52. package/build/main/types.d.ts +3 -0
  53. package/build/main/types.js.map +1 -1
  54. package/build/main/utils/find-postmark-email.d.ts +8 -0
  55. package/build/main/utils/find-postmark-email.js +88 -0
  56. package/build/main/utils/find-postmark-email.js.map +1 -0
  57. package/build/main/utils/index.d.ts +1 -0
  58. package/build/main/utils/index.js +1 -0
  59. package/build/main/utils/index.js.map +1 -1
  60. package/build/main/utils/send-email.d.ts +28 -3
  61. package/build/main/utils/send-email.js +142 -22
  62. package/build/main/utils/send-email.js.map +1 -1
  63. package/build/module/components/ActionEmailTemplate.d.ts +3 -1
  64. package/build/module/components/ActionEmailTemplate.js +9 -13
  65. package/build/module/components/ActionEmailTemplate.js.map +1 -1
  66. package/build/module/components/EmailContainer.d.ts +4 -1
  67. package/build/module/components/EmailContainer.js +5 -4
  68. package/build/module/components/EmailContainer.js.map +1 -1
  69. package/build/module/components/EmailHeading.d.ts +8 -0
  70. package/build/module/components/EmailHeading.js +17 -0
  71. package/build/module/components/EmailHeading.js.map +1 -0
  72. package/build/module/components/index.d.ts +1 -0
  73. package/build/module/components/index.js +1 -0
  74. package/build/module/components/index.js.map +1 -1
  75. package/build/module/components/utils/parse-email-html.js +13 -4
  76. package/build/module/components/utils/parse-email-html.js.map +1 -1
  77. package/build/module/const.d.ts +4 -4
  78. package/build/module/index.d.ts +1 -1
  79. package/build/module/index.js +1 -1
  80. package/build/module/index.js.map +1 -1
  81. package/build/module/render.d.ts +2 -0
  82. package/build/module/render.js +20 -0
  83. package/build/module/render.js.map +1 -0
  84. package/build/module/templates/BankAccountChangedEmailTemplate.d.ts +12 -0
  85. package/build/module/templates/BankAccountChangedEmailTemplate.js +8 -0
  86. package/build/module/templates/BankAccountChangedEmailTemplate.js.map +1 -0
  87. package/build/module/templates/BankAccountChangedTeamAdminEmailTemplate.d.ts +17 -0
  88. package/build/module/templates/BankAccountChangedTeamAdminEmailTemplate.js +8 -0
  89. package/build/module/templates/BankAccountChangedTeamAdminEmailTemplate.js.map +1 -0
  90. package/build/module/templates/BankDetailsRequestEmailTemplate.d.ts +17 -0
  91. package/build/module/templates/BankDetailsRequestEmailTemplate.js +11 -0
  92. package/build/module/templates/BankDetailsRequestEmailTemplate.js.map +1 -0
  93. package/build/module/templates/InviteOwnerEmailTemplate.d.ts +2 -2
  94. package/build/module/templates/InviteOwnerEmailTemplate.js +1 -1
  95. package/build/module/templates/InviteOwnerEmailTemplate.js.map +1 -1
  96. package/build/module/templates/InvitePmEmailTemplate.d.ts +2 -1
  97. package/build/module/templates/InvitePmEmailTemplate.js +4 -7
  98. package/build/module/templates/InvitePmEmailTemplate.js.map +1 -1
  99. package/build/module/templates/OwnerStatementEmailTemplate.d.ts +6 -3
  100. package/build/module/templates/OwnerStatementEmailTemplate.js +71 -10
  101. package/build/module/templates/OwnerStatementEmailTemplate.js.map +1 -1
  102. package/build/module/templates/TeamRoleGrantedEmailTemplate.d.ts +12 -0
  103. package/build/module/templates/TeamRoleGrantedEmailTemplate.js +8 -0
  104. package/build/module/templates/TeamRoleGrantedEmailTemplate.js.map +1 -0
  105. package/build/module/templates/TenantDeletionEmailTemplate.js +3 -7
  106. package/build/module/templates/TenantDeletionEmailTemplate.js.map +1 -1
  107. package/build/module/templates/TenantSuspensionEmailTemplate.js +3 -7
  108. package/build/module/templates/TenantSuspensionEmailTemplate.js.map +1 -1
  109. package/build/module/templates/index.d.ts +4 -0
  110. package/build/module/templates/index.js +5 -0
  111. package/build/module/templates/index.js.map +1 -1
  112. package/build/module/types.d.ts +3 -0
  113. package/build/module/types.js.map +1 -1
  114. package/build/module/utils/find-postmark-email.d.ts +8 -0
  115. package/build/module/utils/find-postmark-email.js +52 -0
  116. package/build/module/utils/find-postmark-email.js.map +1 -0
  117. package/build/module/utils/index.d.ts +1 -0
  118. package/build/module/utils/index.js +1 -0
  119. package/build/module/utils/index.js.map +1 -1
  120. package/build/module/utils/send-email.d.ts +28 -3
  121. package/build/module/utils/send-email.js +108 -22
  122. package/build/module/utils/send-email.js.map +1 -1
  123. package/package.json +8 -7
  124. package/src/components/ActionEmailTemplate.tsx +14 -16
  125. package/src/components/EmailContainer.tsx +20 -4
  126. package/src/components/EmailHeading.tsx +36 -0
  127. package/src/components/index.ts +1 -0
  128. package/src/components/utils/parse-email-html.ts +20 -6
  129. package/src/index.ts +1 -1
  130. package/src/render.ts +24 -0
  131. package/src/templates/BankAccountChangedEmailTemplate.tsx +53 -0
  132. package/src/templates/BankAccountChangedTeamAdminEmailTemplate.tsx +55 -0
  133. package/src/templates/BankDetailsRequestEmailTemplate.tsx +63 -0
  134. package/src/templates/InviteOwnerEmailTemplate.tsx +1 -0
  135. package/src/templates/InvitePmEmailTemplate.tsx +5 -7
  136. package/src/templates/OwnerStatementEmailTemplate.tsx +178 -14
  137. package/src/templates/TeamRoleGrantedEmailTemplate.tsx +54 -0
  138. package/src/templates/TenantDeletionEmailTemplate.tsx +9 -10
  139. package/src/templates/TenantSuspensionEmailTemplate.tsx +9 -10
  140. package/src/templates/index.ts +6 -0
  141. package/src/types.ts +3 -0
  142. package/src/utils/find-postmark-email.ts +80 -0
  143. package/src/utils/index.ts +1 -0
  144. package/src/utils/send-email.ts +184 -28
@@ -1,14 +1,65 @@
1
+ import { day } from '@vrplatform/utils';
2
+ import * as z from 'zod';
3
+
4
+ const postmarkErrorCodeSchema = z.number().int().nonnegative();
5
+ const postmarkResponseErrorCodeSchema = z
6
+ .object({ ErrorCode: postmarkErrorCodeSchema })
7
+ .transform(({ ErrorCode }) => ErrorCode);
8
+ const postmarkHeaderErrorCodeSchema = z
9
+ .string()
10
+ .regex(/^\d+$/)
11
+ .transform(Number)
12
+ .pipe(postmarkErrorCodeSchema);
13
+ const postmarkSuccessResponseSchema = z.object({
14
+ ErrorCode: z.literal(0),
15
+ MessageID: z.string().min(1),
16
+ SubmittedAt: z.string().refine((value) => day(value).isValid()),
17
+ });
18
+
19
+ function parseOptional<T>(schema: z.ZodType<T>, value: unknown) {
20
+ const result = schema.safeParse(value);
21
+ if (result.success) return result.data;
22
+ }
23
+
24
+ type EmailDeliveryFailureDetails = {
25
+ httpStatus: number;
26
+ postmarkErrorCode?: number;
27
+ provider: 'postmark';
28
+ providerCategory: 'inactiveRecipient' | 'rejected';
29
+ };
30
+
31
+ type EmailDeliveryErrorDetails =
32
+ | EmailDeliveryFailureDetails
33
+ | {
34
+ failureCount: number;
35
+ failures: EmailDeliveryFailureDetails[];
36
+ };
37
+
1
38
  export class EmailDeliveryError extends Error {
2
39
  constructor(
3
40
  message: string,
4
- public readonly details?: Record<string, unknown>,
5
- public readonly expected = false
41
+ public readonly details?: EmailDeliveryErrorDetails,
42
+ public readonly expected = false,
43
+ public readonly retryable = true
6
44
  ) {
7
45
  super(message);
8
46
  this.name = 'EmailDeliveryError';
9
47
  }
10
48
  }
11
49
 
50
+ /**
51
+ * Formats a verified sender address with a display name for the From header.
52
+ * Registrations store bare addresses; recipients should still see the company
53
+ * name the way they see `VRPlatform <notifications@vrplatform.app>`.
54
+ */
55
+ export function formatSenderIdentity(
56
+ name: string | null | undefined,
57
+ address: string
58
+ ) {
59
+ const display = (name ?? '').replace(/[\r\n"<>]/g, '').trim();
60
+ return display ? `"${display}" <${address}>` : address;
61
+ }
62
+
12
63
  export async function sendEmail(
13
64
  data: {
14
65
  to: string;
@@ -16,6 +67,10 @@ export async function sendEmail(
16
67
  text: string;
17
68
  html: string;
18
69
  replyTo?: string;
70
+ tag?: string;
71
+ metadata?: Record<string, string>;
72
+ from?: string;
73
+ headers?: Record<string, string>;
19
74
  },
20
75
  options: {
21
76
  fetch?: (
@@ -26,7 +81,12 @@ export async function sendEmail(
26
81
  }
27
82
  ) {
28
83
  if (!options.postmarkToken) {
29
- throw new EmailDeliveryError('Postmark is not configured');
84
+ throw new EmailDeliveryError(
85
+ 'Postmark is not configured',
86
+ undefined,
87
+ false,
88
+ false
89
+ );
30
90
  }
31
91
 
32
92
  const response = await (options.fetch ?? fetch)(
@@ -39,48 +99,144 @@ export async function sendEmail(
39
99
  'X-Postmark-Server-Token': options.postmarkToken,
40
100
  },
41
101
  body: JSON.stringify({
42
- From: 'VRPlatform <notifications@vrplatform.app>',
102
+ From: data.from ?? 'VRPlatform <notifications@vrplatform.app>',
43
103
  To: data.to,
44
104
  Subject: data.subject,
45
105
  ReplyTo: data.replyTo,
46
106
  TextBody: data.text,
47
107
  HtmlBody: data.html,
48
108
  MessageStream: 'outbound',
109
+ Tag: data.tag,
110
+ Metadata: data.metadata,
111
+ Headers: data.headers
112
+ ? Object.entries(data.headers).map(([Name, Value]) => ({
113
+ Name,
114
+ Value,
115
+ }))
116
+ : undefined,
49
117
  }),
50
118
  }
51
119
  );
52
120
 
53
121
  const providerResponse: unknown = await response.json().catch(() => ({}));
54
- const providerMessage =
55
- providerResponse !== null &&
56
- typeof providerResponse === 'object' &&
57
- 'Message' in providerResponse &&
58
- typeof providerResponse.Message === 'string'
59
- ? providerResponse.Message
60
- : undefined;
61
-
62
- if (!response.ok) {
63
- if (
64
- response.status === 422 &&
65
- providerMessage?.includes('marked as inactive')
66
- ) {
122
+ const postmarkErrorCode =
123
+ parseOptional(postmarkResponseErrorCodeSchema, providerResponse) ??
124
+ parseOptional(
125
+ postmarkHeaderErrorCodeSchema,
126
+ response.headers.get('X-PM-ApiErrorCode')
127
+ );
128
+
129
+ if (
130
+ !response.ok ||
131
+ (postmarkErrorCode !== undefined && postmarkErrorCode !== 0)
132
+ ) {
133
+ const inactiveRecipient =
134
+ response.status === 422 && postmarkErrorCode === 406;
135
+ const details: EmailDeliveryFailureDetails = {
136
+ provider: 'postmark',
137
+ providerCategory: inactiveRecipient ? 'inactiveRecipient' : 'rejected',
138
+ httpStatus: response.status,
139
+ ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),
140
+ };
141
+
142
+ if (inactiveRecipient) {
67
143
  throw new EmailDeliveryError(
68
144
  'Postmark recipient is inactive',
69
- {
70
- providerMessage,
71
- status: response.status,
72
- statusText: response.statusText,
73
- },
74
- true
145
+ details,
146
+ true,
147
+ false
75
148
  );
76
149
  }
77
150
 
78
- throw new EmailDeliveryError('Postmark rejected the email', {
79
- providerResponse,
80
- status: response.status,
81
- statusText: response.statusText,
151
+ throw new EmailDeliveryError(
152
+ 'Postmark rejected the email',
153
+ details,
154
+ false,
155
+ response.status === 429 || response.status >= 500
156
+ );
157
+ }
158
+
159
+ const result = postmarkSuccessResponseSchema.safeParse(providerResponse);
160
+ if (!result.success) {
161
+ throw new EmailDeliveryError('Postmark returned an invalid response', {
162
+ provider: 'postmark',
163
+ providerCategory: 'rejected',
164
+ httpStatus: response.status,
165
+ ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),
82
166
  });
83
167
  }
84
168
 
85
- return { status: response.status };
169
+ return {
170
+ httpStatus: response.status,
171
+ messageId: result.data.MessageID,
172
+ submittedAt: result.data.SubmittedAt,
173
+ };
174
+ }
175
+
176
+ export async function settleEmailDeliveries(deliveries: Promise<unknown>[]) {
177
+ const failures = (await Promise.allSettled(deliveries)).filter(
178
+ (result) => result.status === 'rejected'
179
+ );
180
+ if (failures.length === 0) return;
181
+
182
+ const unexpectedDeliveryError = failures.find(
183
+ (failure) =>
184
+ failure.reason instanceof EmailDeliveryError && !failure.reason.expected
185
+ )?.reason;
186
+ const allExpected = failures.every(
187
+ (failure) =>
188
+ failure.reason instanceof EmailDeliveryError && failure.reason.expected
189
+ );
190
+ const firstDeliveryError = failures.find(
191
+ (failure) => failure.reason instanceof EmailDeliveryError
192
+ )?.reason;
193
+ const failureDetails = failures.flatMap((failure) => {
194
+ if (
195
+ !(failure.reason instanceof EmailDeliveryError) ||
196
+ !failure.reason.details ||
197
+ !('provider' in failure.reason.details)
198
+ ) {
199
+ return [];
200
+ }
201
+ const { httpStatus, postmarkErrorCode, provider, providerCategory } =
202
+ failure.reason.details;
203
+ return [
204
+ {
205
+ httpStatus,
206
+ provider,
207
+ providerCategory,
208
+ ...(postmarkErrorCode !== undefined ? { postmarkErrorCode } : {}),
209
+ },
210
+ ];
211
+ });
212
+
213
+ const details: EmailDeliveryErrorDetails = {
214
+ failureCount: failures.length,
215
+ failures: failureDetails,
216
+ };
217
+ if (unexpectedDeliveryError instanceof EmailDeliveryError) {
218
+ if (unexpectedDeliveryError.message === 'Postmark is not configured') {
219
+ throw new EmailDeliveryError(
220
+ 'Postmark is not configured',
221
+ details,
222
+ false,
223
+ false
224
+ );
225
+ }
226
+ throw new EmailDeliveryError(
227
+ 'Postmark rejected the email',
228
+ details,
229
+ false,
230
+ unexpectedDeliveryError.retryable
231
+ );
232
+ }
233
+ if (allExpected && firstDeliveryError instanceof EmailDeliveryError) {
234
+ throw new EmailDeliveryError(
235
+ 'Postmark recipient is inactive',
236
+ details,
237
+ true,
238
+ false
239
+ );
240
+ }
241
+ throw new EmailDeliveryError('Email delivery failed', details);
86
242
  }