Optimize logging in invitation webhook service

The code changes introduce more detailed logging in the invitation webhook service. These logs provide comprehensive information about invitation webhooks events being handled, potential errors during fetching inviter or team details, as well as the
This commit is contained in:
giancarlo
2024-04-09 19:24:15 +08:00
parent 6753c2a7db
commit 9e1199f3f4

View File

@@ -29,38 +29,72 @@ const env = z
export class AccountInvitationsWebhookService { export class AccountInvitationsWebhookService {
private namespace = 'accounts.invitations.webhook'; private namespace = 'accounts.invitations.webhook';
constructor(private readonly client: SupabaseClient<Database>) {} constructor(private readonly adminClient: SupabaseClient<Database>) {}
/**
* @name handleInvitationWebhook
* @description Handles the webhook event for invitations
* @param invitation
*/
async handleInvitationWebhook(invitation: Invitation) { async handleInvitationWebhook(invitation: Invitation) {
return this.dispatchInvitationEmail(invitation); return this.dispatchInvitationEmail(invitation);
} }
private async dispatchInvitationEmail(invitation: Invitation) { private async dispatchInvitationEmail(invitation: Invitation) {
const inviter = await this.client const logger = await getLogger();
logger.info(
{ invitation, name: this.namespace },
'Handling invitation webhook event...',
);
const inviter = await this.adminClient
.from('accounts') .from('accounts')
.select('email, name') .select('email, name')
.eq('id', invitation.invited_by) .eq('id', invitation.invited_by)
.single(); .single();
if (inviter.error) { if (inviter.error) {
logger.error(
{
error: inviter.error,
name: this.namespace,
},
'Failed to fetch inviter details',
);
throw inviter.error; throw inviter.error;
} }
const team = await this.client const team = await this.adminClient
.from('accounts') .from('accounts')
.select('name') .select('name')
.eq('id', invitation.account_id) .eq('id', invitation.account_id)
.single(); .single();
if (team.error) { if (team.error) {
logger.error(
{
error: team.error,
name: this.namespace,
},
'Failed to fetch team details',
);
throw team.error; throw team.error;
} }
const logger = await getLogger(); const ctx = {
invitationId: invitation.id,
name: this.namespace,
};
logger.info(ctx, 'Invite retrieved. Sending invitation email...');
try { try {
const { renderInviteEmail } = await import('@kit/email-templates'); const { renderInviteEmail } = await import('@kit/email-templates');
const { getMailer } = await import('@kit/mailers'); const { getMailer } = await import('@kit/mailers');
const mailer = await getMailer(); const mailer = await getMailer();
const html = renderInviteEmail({ const html = renderInviteEmail({
@@ -79,27 +113,17 @@ export class AccountInvitationsWebhookService {
html, html,
}) })
.then(() => { .then(() => {
logger.info('Invitation email sent', { logger.info(ctx, 'Invitation email successfully sent!');
email: invitation.email,
account: invitation.account_id,
name: this.namespace,
});
}) })
.catch((error) => { .catch((error) => {
logger.warn( logger.warn({ error, ...ctx }, 'Failed to send invitation email');
{ error, name: this.namespace },
'Failed to send invitation email',
);
}); });
return { return {
success: true, success: true,
}; };
} catch (error) { } catch (error) {
logger.warn( logger.warn({ error, ...ctx }, 'Failed to invite user to team');
{ error, name: this.namespace },
'Failed to invite user to team',
);
return { return {
error, error,