-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #609 from desci-labs/doi-submission-check
Email service and DOI submission
- Loading branch information
Showing
4 changed files
with
103 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import sgMail from '@sendgrid/mail'; | ||
|
||
import { logger as parentLogger } from '../logger.js'; | ||
import { DoiMintedEmailHtml } from '../templates/emails/utils/emailRenderer.js'; | ||
|
||
export enum EmailTypes { | ||
DoiMinted, | ||
DOI_REGISTRATION_REQUESTED, | ||
} | ||
|
||
type DoiRegisteredPayload = { | ||
type: EmailTypes.DoiMinted; | ||
payload: { to: string; name: string; title: string; dpid: string; doi: string }; | ||
}; | ||
|
||
type DoiRequestedPayload = { | ||
type: EmailTypes.DOI_REGISTRATION_REQUESTED; | ||
payload: { to: string; subject: string; name: string }; | ||
}; | ||
|
||
export type EmailProps = DoiRegisteredPayload | DoiRequestedPayload; | ||
|
||
const logger = parentLogger.child({ module: 'EmailService' }); | ||
|
||
export function assertNever(value: never) { | ||
console.error('Unknown value', value); | ||
throw Error('Not Possible'); | ||
} | ||
|
||
async function sendDoiRegisteredEmail({ to, name, title, dpid, doi }: DoiRegisteredPayload['payload']) { | ||
const message = { | ||
to, | ||
from: 'no-reply@desci.com', | ||
subject: 'DOI Registration successful 🎉', | ||
text: `Hello ${name}, You DOI registration for the research object ${title} has been completed. Here is your DOI: ${process.env.CROSSREF_DOI_URL}/${doi}`, | ||
html: DoiMintedEmailHtml({ | ||
dpid, | ||
doi, | ||
nodeTitle: title, | ||
userName: name.split(' ')?.[0] ?? '', | ||
dpidPath: `${process.env.DAPP_URL}/dpid/${dpid}`, | ||
doiLink: `${process.env.CROSSREF_DOI_URL}/${doi}`, | ||
}), | ||
}; | ||
|
||
try { | ||
if (process.env.NODE_ENV === 'production') { | ||
const response = await sgMail.send(message); | ||
logger.trace(response, '[EMAIL]:: Response'); | ||
} else { | ||
logger.info({ nodeEnv: process.env.NODE_ENV }, message.subject); | ||
} | ||
} catch (err) { | ||
logger.error({ err }, '[ERROR]:: DOI MINTED EMAIL'); | ||
} | ||
} | ||
|
||
async function sendDoiRequested(payload: DoiRequestedPayload['payload']) { | ||
// | ||
} | ||
|
||
export const sendEmail = async (props: EmailProps) => { | ||
switch (props.type) { | ||
case EmailTypes.DoiMinted: | ||
return sendDoiRegisteredEmail(props.payload); | ||
case EmailTypes.DOI_REGISTRATION_REQUESTED: | ||
return sendDoiRequested(props.payload); | ||
default: | ||
assertNever(props); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters