@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 2.76 kB
Source Map (JSON)
{"version":3,"file":"resend.mjs","names":["constants"],"sources":["../../../../src/libs/email/storage/resend.ts"],"sourcesContent":["import constants from \"../../../constants/constants.js\";\nimport type { ServiceResponse } from \"../../../utils/services/types.js\";\nimport { parseEmailStorageRules } from \"./config.js\";\nimport type { EmailResendState, EmailStorageConfig } from \"./types.js\";\n\n/**\n * Checks if an email strategy contains data that is removed after send.\n */\nexport const hasNeverStoreEmailStorageRules = (\n\tstorage?: EmailStorageConfig | null,\n): Awaited<ServiceResponse<boolean>> => {\n\tconst rulesRes = parseEmailStorageRules(storage);\n\tif (rulesRes.error) return rulesRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: rulesRes.data.some(({ rule }) => rule.neverStore === true),\n\t};\n};\n\n/**\n * Calculates whether the UI and API can offer resend for a stored email.\n */\nexport const getEmailResendState = (props: {\n\tcreatedAt: Date | string | null;\n\tstorage?: EmailStorageConfig | null;\n\tresendWindowDays: number;\n\tnow?: Date;\n}): Awaited<ServiceResponse<EmailResendState>> => {\n\tconst hasNeverStoreRes = hasNeverStoreEmailStorageRules(props.storage);\n\tif (hasNeverStoreRes.error) return hasNeverStoreRes;\n\n\tconst createdAt =\n\t\tprops.createdAt instanceof Date\n\t\t\t? props.createdAt\n\t\t\t: props.createdAt\n\t\t\t\t? new Date(props.createdAt)\n\t\t\t\t: null;\n\n\tconst validCreatedAt =\n\t\tcreatedAt !== null && !Number.isNaN(createdAt.getTime());\n\tconst resendWindowMs =\n\t\tMath.max(0, props.resendWindowDays) *\n\t\tconstants.email.storage.millisecondsInDay;\n\tconst now = props.now ?? new Date();\n\tconst active =\n\t\tvalidCreatedAt && now.getTime() - createdAt.getTime() <= resendWindowMs;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: {\n\t\t\tenabled: active && !hasNeverStoreRes.data,\n\t\t\t...(active && hasNeverStoreRes.data\n\t\t\t\t? { reason: \"unstoredData\" as const }\n\t\t\t\t: {}),\n\t\t\t...(!active ? { reason: \"outsideResendWindow\" as const } : {}),\n\t\t},\n\t};\n};\n"],"mappings":"sGAQA,MAAa,EACZ,GACuC,CACvC,IAAM,EAAW,EAAuB,CAAO,EAG/C,OAFI,EAAS,MAAc,EAEpB,CACN,MAAO,IAAA,GACP,KAAM,EAAS,KAAK,MAAM,CAAE,UAAW,EAAK,aAAe,EAAI,CAChE,CACD,EAKa,EAAuB,GAKc,CACjD,IAAM,EAAmB,EAA+B,EAAM,OAAO,EACrE,GAAI,EAAiB,MAAO,OAAO,EAEnC,IAAM,EACL,EAAM,qBAAqB,KACxB,EAAM,UACN,EAAM,UACL,IAAI,KAAK,EAAM,SAAS,EACxB,KAEC,EACL,IAAc,MAAQ,CAAC,OAAO,MAAM,EAAU,QAAQ,CAAC,EAClD,EACL,KAAK,IAAI,EAAG,EAAM,gBAAgB,EAClCA,EAAU,MAAM,QAAQ,kBACnB,EAAM,EAAM,KAAO,IAAI,KACvB,EACL,GAAkB,EAAI,QAAQ,EAAI,EAAU,QAAQ,GAAK,EAE1D,MAAO,CACN,MAAO,IAAA,GACP,KAAM,CACL,QAAS,GAAU,CAAC,EAAiB,KACrC,GAAI,GAAU,EAAiB,KAC5B,CAAE,OAAQ,cAAwB,EAClC,CAAC,EACJ,GAAK,EAAsD,CAAC,EAA9C,CAAE,OAAQ,qBAA+B,CACxD,CACD,CACD"}