@@ -14,7 +14,7 @@ const getMailCountKey = (projectId, monthKey) =>
1414
1515const loadProjectMailConfig = async ( projectId ) => {
1616 return Project . findById ( projectId )
17- . select ( "+resendApiKey.encrypted +resendApiKey.iv +resendApiKey.tag" )
17+ . select ( "+resendApiKey.encrypted +resendApiKey.iv +resendApiKey.tag resendFromEmail " )
1818 . lean ( ) ;
1919} ;
2020
@@ -62,16 +62,17 @@ module.exports.sendMail = async (req, res) => {
6262 return res . status ( 401 ) . json ( { error : "Project context missing." } ) ;
6363 }
6464
65- const limit = getMonthlyMailLimit ( req . project ) ;
66- const { count, key } = await reserveMonthlyMailSlot ( projectId , limit ) ;
67- consumedQuotaKey = key ;
68-
6965 const project = await loadProjectMailConfig ( projectId ) ;
7066 if ( ! project ) {
7167 return res . status ( 404 ) . json ( { error : "Project not found." } ) ;
7268 }
7369
74- const decryptedByokKey = decrypt ( project . resendApiKey ) ;
70+ const encryptedByokKey =
71+ project . resendApiKey && typeof project . resendApiKey === "object" && Object . keys ( project . resendApiKey ) . length > 0
72+ ? project . resendApiKey
73+ : null ;
74+ const decryptedByokKey = encryptedByokKey ? decrypt ( encryptedByokKey ) : null ;
75+
7576 const usingByok = typeof decryptedByokKey === "string" && decryptedByokKey . trim ( ) . length > 0 ;
7677 const clientKey = usingByok
7778 ? decryptedByokKey . trim ( )
@@ -81,6 +82,10 @@ module.exports.sendMail = async (req, res) => {
8182 return res . status ( 500 ) . json ( { error : "Resend API key is not configured." } ) ;
8283 }
8384
85+ const limit = getMonthlyMailLimit ( req . project ) ;
86+ const { count, key } = await reserveMonthlyMailSlot ( projectId , limit ) ;
87+ consumedQuotaKey = key ;
88+
8489 const resend = new Resend ( clientKey ) ;
8590
8691 let fromAddress = DEFAULT_FROM ;
0 commit comments