@@ -69,7 +69,11 @@ InvitationRef.implement({
69
69
} ) ;
70
70
71
71
const InviteInviterError = builder . enumType ( "InviteInviterError" , {
72
- values : [ "INVITER_NOT_AUTHENTICATED" , "INVITER_NO_INVITATIONS_LEFT" , "INVITER_EMAIL_SEND_FAILED" ] as const ,
72
+ values : [
73
+ "INVITER_NOT_AUTHENTICATED" ,
74
+ "INVITER_NO_INVITATIONS_LEFT" ,
75
+ "INVITER_EMAIL_SEND_FAILED" ,
76
+ ] as const ,
73
77
} ) ;
74
78
75
79
const InviteEmailError = builder . enumType ( "InviteEmailError" , {
@@ -227,7 +231,7 @@ builder.mutationField("invite", (t) =>
227
231
await ctx . db . update ( accountTable ) . set ( {
228
232
leftInvitations : sql `${ accountTable . leftInvitations } + 1` ,
229
233
} ) . where ( eq ( accountTable . id , ctx . account . id ) ) ;
230
-
234
+
231
235
// Return validation error to inform the user
232
236
return {
233
237
inviter : "INVITER_EMAIL_SEND_FAILED" ,
@@ -245,26 +249,34 @@ builder.mutationField("invite", (t) =>
245
249
const LOCALES_DIR = join ( import . meta. dirname ! , "locales" ) ;
246
250
247
251
// Cache for email templates
248
- let cachedTemplates : Map < string , { subject : string ; emailContent : string ; emailContentWithMessage : string } > | null = null ;
252
+ let cachedTemplates :
253
+ | Map <
254
+ string ,
255
+ { subject : string ; emailContent : string ; emailContentWithMessage : string }
256
+ >
257
+ | null = null ;
249
258
let cachedAvailableLocales : Record < string , string > | null = null ;
250
259
251
260
async function loadEmailTemplates ( ) : Promise < void > {
252
261
if ( cachedTemplates && cachedAvailableLocales ) return ;
253
-
262
+
254
263
const availableLocales : Record < string , string > = { } ;
255
- const templates = new Map < string , { subject : string ; emailContent : string ; emailContentWithMessage : string } > ( ) ;
256
-
264
+ const templates = new Map <
265
+ string ,
266
+ { subject : string ; emailContent : string ; emailContentWithMessage : string }
267
+ > ( ) ;
268
+
257
269
const files = expandGlob ( join ( LOCALES_DIR , "*.json" ) , {
258
270
includeDirs : false ,
259
271
} ) ;
260
-
272
+
261
273
for await ( const file of files ) {
262
274
if ( ! file . isFile ) continue ;
263
275
const match = file . name . match ( / ^ ( .+ ) \. j s o n $ / ) ;
264
276
if ( match == null ) continue ;
265
277
const localeName = match [ 1 ] ;
266
278
availableLocales [ localeName ] = file . path ;
267
-
279
+
268
280
try {
269
281
const json = await Deno . readTextFile ( file . path ) ;
270
282
const data = JSON . parse ( json ) ;
@@ -274,10 +286,13 @@ async function loadEmailTemplates(): Promise<void> {
274
286
emailContentWithMessage : data . invite . emailContentWithMessage ,
275
287
} ) ;
276
288
} catch ( error ) {
277
- console . warn ( `Failed to load email template for locale ${ localeName } :` , error ) ;
289
+ console . warn (
290
+ `Failed to load email template for locale ${ localeName } :` ,
291
+ error ,
292
+ ) ;
278
293
}
279
294
}
280
-
295
+
281
296
cachedTemplates = templates ;
282
297
cachedAvailableLocales = availableLocales ;
283
298
}
@@ -287,16 +302,18 @@ async function getEmailTemplate(
287
302
message : boolean ,
288
303
) : Promise < { subject : string ; content : string } > {
289
304
await loadEmailTemplates ( ) ;
290
-
305
+
291
306
const selectedLocale =
292
307
negotiateLocale ( locale , Object . keys ( cachedAvailableLocales ! ) ) ??
293
308
new Intl . Locale ( "en" ) ;
294
-
309
+
295
310
const template = cachedTemplates ! . get ( selectedLocale . baseName ) ;
296
311
if ( ! template ) {
297
- throw new Error ( `No email template found for locale ${ selectedLocale . baseName } ` ) ;
312
+ throw new Error (
313
+ `No email template found for locale ${ selectedLocale . baseName } ` ,
314
+ ) ;
298
315
}
299
-
316
+
300
317
return {
301
318
subject : template . subject ,
302
319
content : message ? template . emailContentWithMessage : template . emailContent ,
0 commit comments