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

Improve Logging #743

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
88 changes: 70 additions & 18 deletions packages/api/src/ticketing/@webhook/handler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,48 @@ export class TicketingWebhookHandlerService {
data: { [key: string]: any },
mw_ids: string[],
) {
const conn = await this.prisma.connections.findFirst({
where: {
id_connection: id_connection,
},
});
switch (conn.provider_slug) {
case 'zendesk':
return await this.zendesk.createWebhook(data, mw_ids);
default:
this.logger.log(
`Attempting to create external webhook for connection ID: ${id_connection}`,
);

try {
const conn = await this.prisma.connections.findFirst({
where: {
id_connection: id_connection,
},
});

if (!conn) {
this.logger.warn(
`No connection found for ID: ${id_connection}. Aborting webhook creation.`,
);
return;
}

this.logger.log(
`Connection found: ${conn.provider_slug}. Proceeding with webhook creation.`,
);

switch (conn.provider_slug) {
case 'zendesk':
this.logger.log(
`Creating webhook for provider: zendesk with data: ${JSON.stringify(
data,
)}`,
);
return await this.zendesk.createWebhook(data, mw_ids);
default:
this.logger.warn(
`Unhandled provider slug: ${conn.provider_slug}. Webhook creation skipped.`,
);
return;
}
} catch (error) {
this.logger.error(
`Error occurred while creating external webhook: ${error.message}`,
error.stack,
);
throw error;
}
}

Expand All @@ -37,15 +69,35 @@ export class TicketingWebhookHandlerService {
payload: any;
headers: any;
}) {
switch (metadata.connector_name) {
case 'zendesk':
return await this.zendesk.handler(
metadata.payload,
metadata.headers,
metadata.id_managed_webhook,
);
default:
return;
this.logger.log(
`Handling incoming webhook for connector: ${metadata.connector_name} with managed webhook ID: ${metadata.id_managed_webhook}`,
);

try {
switch (metadata.connector_name) {
case 'zendesk':
this.logger.log(
`Processing webhook for zendesk with payload: ${JSON.stringify(
metadata.payload,
)}`,
);
return await this.zendesk.handler(
metadata.payload,
metadata.headers,
metadata.id_managed_webhook,
);
default:
this.logger.warn(
`Unhandled connector name: ${metadata.connector_name}. Webhook handling skipped.`,
);
return;
}
} catch (error) {
this.logger.error(
`Error occurred while handling incoming webhook: ${error.message}`,
error.stack,
);
throw error;
}
}
}
37 changes: 27 additions & 10 deletions packages/api/src/ticketing/account/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ export class AccountService {
project_id: string,
remote_data?: boolean,
): Promise<UnifiedTicketingAccountOutput> {
this.logger.log(`Fetching account with id: ${id_ticketing_account}`);
try {
const account = await this.prisma.tcg_accounts.findUnique({
where: {
id_tcg_account: id_ticketing_account,
},
});

// Fetch field mappings for the ticket
if (!account) {
this.logger.warn(`Account not found with id: ${id_ticketing_account}`);
} else {
this.logger.log(`Account retrieved: ${account.name}`);
}

const values = await this.prisma.value.findMany({
where: {
entity: {
Expand All @@ -37,17 +43,14 @@ export class AccountService {
},
});

// Create a map to store unique field mappings
const fieldMappingsMap = new Map();

values.forEach((value) => {
fieldMappingsMap.set(value.attribute.slug, value.data);
});

// Convert the map to an array of objects
const field_mappings = Object.fromEntries(fieldMappingsMap);

// Transform to UnifiedTicketingAccountOutput format
const unifiedAccount: UnifiedTicketingAccountOutput = {
id: account.id_tcg_account,
name: account.name,
Expand All @@ -61,6 +64,7 @@ export class AccountService {
let res: UnifiedTicketingAccountOutput = unifiedAccount;

if (remote_data) {
this.logger.log(`Fetching remote data for account: ${id_ticketing_account}`);
const resp = await this.prisma.remote_data.findFirst({
where: {
ressource_owner_id: account.id_tcg_account,
Expand All @@ -73,6 +77,8 @@ export class AccountService {
remote_data: remote_data,
};
}

this.logger.log(`Creating event log for account pull: ${id_ticketing_account}`);
await this.prisma.events.create({
data: {
id_connection: connection_id,
Expand All @@ -88,8 +94,10 @@ export class AccountService {
id_linked_user: linkedUserId,
},
});

return res;
} catch (error) {
this.logger.error(`Error fetching account: ${error.message}`, error.stack);
throw error;
}
}
Expand All @@ -107,20 +115,21 @@ export class AccountService {
prev_cursor: null | string;
next_cursor: null | string;
}> {
this.logger.log(`Fetching accounts for connection: ${connection_id}`);
try {
//TODO: handle case where data is not there (not synced) or old synced

let prev_cursor = null;
let next_cursor = null;

if (cursor) {
this.logger.log(`Validating cursor: ${cursor}`);
const isCursorPresent = await this.prisma.tcg_accounts.findFirst({
where: {
id_connection: connection_id,
id_tcg_account: cursor,
},
});
if (!isCursorPresent) {
this.logger.warn(`Cursor not found: ${cursor}`);
throw new ReferenceError(`The provided cursor does not exist!`);
}
}
Expand All @@ -140,6 +149,12 @@ export class AccountService {
},
});

if (accounts.length > 0) {
this.logger.log(`Fetched ${accounts.length} accounts`);
} else {
this.logger.warn(`No accounts found for connection: ${connection_id}`);
}

if (accounts.length === limit + 1) {
next_cursor = Buffer.from(
accounts[accounts.length - 1].id_tcg_account,
Expand All @@ -154,7 +169,6 @@ export class AccountService {
const unifiedAccounts: UnifiedTicketingAccountOutput[] =
await Promise.all(
accounts.map(async (account) => {
// Fetch field mappings for the account
const values = await this.prisma.value.findMany({
where: {
entity: {
Expand All @@ -165,20 +179,18 @@ export class AccountService {
attribute: true,
},
});
// Create a map to store unique field mappings

const fieldMappingsMap = new Map();

values.forEach((value) => {
fieldMappingsMap.set(value.attribute.slug, value.data);
});

// Convert the map to an array of objects
const field_mappings = Array.from(
fieldMappingsMap,
([key, value]) => ({ [key]: value }),
);

// Transform to UnifiedTicketingAccountOutput format
return {
id: account.id_tcg_account,
name: account.name,
Expand All @@ -194,6 +206,7 @@ export class AccountService {
let res: UnifiedTicketingAccountOutput[] = unifiedAccounts;

if (remote_data) {
this.logger.log(`Fetching remote data for accounts`);
const remote_array_data: UnifiedTicketingAccountOutput[] =
await Promise.all(
res.map(async (account) => {
Expand All @@ -209,6 +222,8 @@ export class AccountService {

res = remote_array_data;
}

this.logger.log(`Creating event log for accounts pull`);
await this.prisma.events.create({
data: {
id_connection: connection_id,
Expand All @@ -224,12 +239,14 @@ export class AccountService {
id_linked_user: linkedUserId,
},
});

return {
data: res,
prev_cursor,
next_cursor,
};
} catch (error) {
this.logger.error(`Error fetching accounts: ${error.message}`, error.stack);
throw error;
}
}
Expand Down
Loading