Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 33 additions & 42 deletions functions/send-verification-link/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,20 @@ const GetUser = gql`
}
`;

const GetDatabaseInfo = gql`
query GetDatabaseInfo($databaseId: UUID!) {
databases(where: { id: { equalTo: $databaseId } }, first: 1) {
const GetSiteInfo = gql`
query GetSiteInfo {
sites(first: 1) {
nodes {
sites {
config
title
siteThemes(first: 1) {
nodes {
domains {
nodes {
subdomain
domain
}
}
logo
title
siteThemes {
nodes {
theme
}
}
siteModules(where: { name: { equalTo: "legal_terms_module" } }) {
nodes {
data
}
}
theme
}
}
siteModules(where: { name: { equalTo: "legal_terms_module" } }) {
nodes {
data
}
}
}
Expand Down Expand Up @@ -82,7 +72,7 @@ const sendEmailLink = async (
params: SendEmailParams,
context: SendEmailContext
) => {
const { client, meta, databaseId, env, log } = context;
const { client, env, log } = context;
const isDryRun =
parseEnvBoolean(env.SEND_VERIFICATION_LINK_DRY_RUN) ??
parseEnvBoolean(env.SEND_EMAIL_LINK_DRY_RUN) ??
Expand Down Expand Up @@ -123,41 +113,35 @@ const sendEmailLink = async (
return typeValidation;
}

const databaseInfo = await meta.request<any>(GetDatabaseInfo, {
databaseId
});
const siteInfo = await client.request<any>(GetSiteInfo);

const site = databaseInfo?.databases?.nodes?.[0]?.sites?.nodes?.[0];
const site = siteInfo?.sites?.nodes?.[0];
if (!site) {
throw new Error('Site not found for database');
}

const legalTermsModule = site.siteModules?.nodes?.[0];
const domainNode = site.domains?.nodes?.[0];
const theme = site.siteThemes?.nodes?.[0]?.theme;

if (!legalTermsModule || !domainNode || !theme) {
if (!legalTermsModule || !theme) {
throw new Error('Missing site configuration for email');
}

const subdomain = domainNode.subdomain;
const domain = domainNode.domain;
const supportEmail = legalTermsModule.data.emails.support;
const logo = site.logo?.url;
const logo = site.config?.logo?.url;
const company = legalTermsModule.data.company;
const website = company.website;
const nick = company.nick;
const name = company.name;
const primary = theme.primary;

const isLocalDomain =
domain === 'localhost' ||
domain.startsWith('localhost') ||
domain === '0.0.0.0';

const hostname = subdomain && !isLocalDomain
? [subdomain, domain].join('.')
: domain;
const siteUrl = legalTermsModule.data.site?.siteUrl;
const siteHost = legalTermsModule.data.site?.host || legalTermsModule.data.site?.www;
const hostnameSource = siteUrl || siteHost;
const configuredUrl =
hostnameSource?.startsWith('http://') || hostnameSource?.startsWith('https://')
? new URL(hostnameSource)
: null;
const hostname = configuredUrl?.hostname || hostnameSource || 'localhost';

const isLocalHost =
hostname.startsWith('localhost') ||
Expand All @@ -170,7 +154,14 @@ const sendEmailLink = async (
: '';

const protocol = isLocalHost && isDryRun ? 'http' : 'https';
const url = new URL(`${protocol}://${hostname}${localPort}`);
const url = new URL(
configuredUrl
? configuredUrl.href
: `${protocol}://${hostname}${localPort}`
);
if (configuredUrl && isLocalHost && isDryRun && env.LOCAL_APP_PORT && !url.port) {
url.port = env.LOCAL_APP_PORT;
}

let subject: string;
let subMessage: string;
Expand Down
1 change: 1 addition & 0 deletions packages/fn-runtime/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const createClients = (
// names like identityProviders. Use X-Meta-Schema instead.
const meta = createGraphQLClient(metaGraphqlUrl, env, {
hostHeaderEnvVar: 'META_GRAPHQL_HOST_HEADER',
databaseId,
useMetaSchema: true,
});

Expand Down
Loading