From 9e1199f3f4c46c06189287b9f5c7141f8ceddc3a Mon Sep 17 00:00:00 2001 From: giancarlo Date: Tue, 9 Apr 2024 19:24:15 +0800 Subject: [PATCH] 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 --- .../account-invitations-webhook.service.ts | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/packages/features/team-accounts/src/server/services/webhooks/account-invitations-webhook.service.ts b/packages/features/team-accounts/src/server/services/webhooks/account-invitations-webhook.service.ts index c8e6c9cb9..bd3562694 100644 --- a/packages/features/team-accounts/src/server/services/webhooks/account-invitations-webhook.service.ts +++ b/packages/features/team-accounts/src/server/services/webhooks/account-invitations-webhook.service.ts @@ -29,38 +29,72 @@ const env = z export class AccountInvitationsWebhookService { private namespace = 'accounts.invitations.webhook'; - constructor(private readonly client: SupabaseClient) {} + constructor(private readonly adminClient: SupabaseClient) {} + /** + * @name handleInvitationWebhook + * @description Handles the webhook event for invitations + * @param invitation + */ async handleInvitationWebhook(invitation: Invitation) { return this.dispatchInvitationEmail(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') .select('email, name') .eq('id', invitation.invited_by) .single(); if (inviter.error) { + logger.error( + { + error: inviter.error, + name: this.namespace, + }, + 'Failed to fetch inviter details', + ); + throw inviter.error; } - const team = await this.client + const team = await this.adminClient .from('accounts') .select('name') .eq('id', invitation.account_id) .single(); if (team.error) { + logger.error( + { + error: team.error, + name: this.namespace, + }, + 'Failed to fetch team details', + ); + throw team.error; } - const logger = await getLogger(); + const ctx = { + invitationId: invitation.id, + name: this.namespace, + }; + + logger.info(ctx, 'Invite retrieved. Sending invitation email...'); try { const { renderInviteEmail } = await import('@kit/email-templates'); const { getMailer } = await import('@kit/mailers'); + const mailer = await getMailer(); const html = renderInviteEmail({ @@ -79,27 +113,17 @@ export class AccountInvitationsWebhookService { html, }) .then(() => { - logger.info('Invitation email sent', { - email: invitation.email, - account: invitation.account_id, - name: this.namespace, - }); + logger.info(ctx, 'Invitation email successfully sent!'); }) .catch((error) => { - logger.warn( - { error, name: this.namespace }, - 'Failed to send invitation email', - ); + logger.warn({ error, ...ctx }, 'Failed to send invitation email'); }); return { success: true, }; } catch (error) { - logger.warn( - { error, name: this.namespace }, - 'Failed to invite user to team', - ); + logger.warn({ error, ...ctx }, 'Failed to invite user to team'); return { error,