Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send notification when certificate completion succeeds #510

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/models/certificate.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function getCertificateByUsername(username: Certificate['username']
}

export function getCertificateById(id: Certificate['id']) {
return prisma.certificate.findUniqueOrThrow({ where: { id } });
return prisma.certificate.findUniqueOrThrow({ where: { id }, include: { user: true } });
}

export function createCertificate(data: Pick<Certificate, 'username' | 'domain'>) {
Expand Down
20 changes: 19 additions & 1 deletion app/queues/certificate/order-completer-worker.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { redis } from '~/lib/redis.server';
import logger from '~/lib/logger.server';
import LetsEncrypt from '~/lib/lets-encrypt.server';
import * as certificateModel from '~/models/certificate.server';
import { addNotification } from '../notifications/notifications.server';

import type { CertificateJobData } from './certificateJobTypes.server';
import { isUserDeactivated } from '~/models/user.server';
Expand Down Expand Up @@ -74,7 +75,15 @@ export const orderCompleterWorker = new Worker<CertificateJobData>(
({ privateKey, certificate, validFrom, validTo } = await letsEncrypt.completeOrder());
} catch (e) {
logger.error('failed to finalize certificate', e);

// send error email notification:
logger.debug(
SerpentBytes marked this conversation as resolved.
Show resolved Hide resolved
`Sending failed notification email for certificate with id=${certificateEntry.id}, username=${certificateEntry.username}, domain=${certificateEntry.domain}`
);
await addNotification({
emailAddress: certificateEntry.user.email,
subject: 'Attention: Certificate creation failed',
message: `${certificateEntry.username}, your certificate with domain: ${certificateEntry.domain} encountered an error. Please try again.`,
});
// rethrow
throw e;
}
Expand All @@ -88,6 +97,15 @@ export const orderCompleterWorker = new Worker<CertificateJobData>(
});

logger.info(`Certificate ${certificateId} successfully issued and stored`);
// send success email notification
logger.debug(
`Sending success notification email for certificate with id=${certificateEntry.id}, username=${certificateEntry.username}, domain=${certificateEntry.domain}`
);
await addNotification({
emailAddress: certificateEntry.user.email,
subject: 'Attention: Certificate creation completed',
SerpentBytes marked this conversation as resolved.
Show resolved Hide resolved
message: `${certificateEntry.username}, your certificate with domain: ${certificateEntry.domain} is ready. Log in to view/manage certificates.`,
SerpentBytes marked this conversation as resolved.
Show resolved Hide resolved
});
},
{ connection: redis }
);
Expand Down