Skip to content

Commit

Permalink
fix: remove to getAccountUUIDFromEnvironment (#1944)
Browse files Browse the repository at this point in the history
## Describe your changes

While implementing Logs V2:

- Fix wrong uuid used in webhook
- Query full account object (with a single query) because I'll need it
to log properly
I don't have a way to test this feature, I'm not sure what it is
actually :D
  • Loading branch information
bodinsamuel authored Apr 3, 2024
1 parent 9a16171 commit 339a802
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
6 changes: 3 additions & 3 deletions packages/server/lib/controllers/config.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ class ConfigController {
}

const config = await configService.getProviderConfig(providerConfigKey, environmentId);
const environmentUuid = await environmentService.getAccountUUIDFromEnvironment(environmentId);
const environment = await environmentService.getById(environmentId);

if (config == null) {
if (!config || !environment) {
errorManager.errRes(res, 'unknown_provider_config');
return;
}
Expand Down Expand Up @@ -325,7 +325,7 @@ class ConfigController {
const connection_count = connections.length;
let webhookUrl: string | null = null;
if (hasWebhook) {
webhookUrl = `${getGlobalWebhookReceiveUrl()}/${environmentUuid}/${config.provider}`;
webhookUrl = `${getGlobalWebhookReceiveUrl()}/${environment.uuid}/${config.provider}`;
}

const configRes: ProviderIntegration | IntegrationWithCreds = includeCreds
Expand Down
21 changes: 9 additions & 12 deletions packages/shared/lib/services/environment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,15 @@ class EnvironmentService {
return result[0].account_id;
}

async getAccountUUIDFromEnvironment(environment_id: number): Promise<string | null> {
const result = await db.knex.select('account_id').from<Environment>(TABLE).where({ id: environment_id });

if (result == null || result.length == 0 || result[0] == null) {
return null;
}

const accountId = result[0].account_id;

const uuid = await accountService.getUUIDFromAccountId(accountId);

return uuid;
async getAccountFromEnvironment(environment_id: number): Promise<Account | null> {
const result = await db.knex
.select<Account>('_nango_accounts.*')
.from(TABLE)
.join('_nango_accounts', '_nango_accounts.id', '_nango_environments.account_id')
.where({ id: environment_id })
.first();

return result || null;
}

async getAccountUUIDFromEnvironmentUUID(environment_uuid: string): Promise<string | null> {
Expand Down
32 changes: 22 additions & 10 deletions packages/shared/lib/services/notification/slack.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ class SlackService {
return;
}

const accountUUID = await environmentService.getAccountUUIDFromEnvironment(environment_id);
payload.content = `${payload.content} [Account ${accountUUID} Environment ${environment_id}]`;
const account = await environmentService.getAccountFromEnvironment(environment_id);
if (!account) {
throw new Error('failed_to_get_account');
}

payload.content = `${payload.content} [Account ${account.uuid} Environment ${environment_id}]`;

if (ts) {
payload.ts = ts;
Expand Down Expand Up @@ -211,8 +215,12 @@ class SlackService {

const { success, error, response: slackNotificationStatus } = await this.addFailingConnection(nangoConnection, syncName, syncType);

const accountUUID = await environmentService.getAccountUUIDFromEnvironment(environment_id);
const slackConnectionId = `account-${accountUUID}`;
const account = await environmentService.getAccountFromEnvironment(environment_id);
if (!account) {
throw new Error('failed_to_get_account');
}

const slackConnectionId = `account-${account.uuid}`;
const nangoEnvironmentId = await this.getAdminEnvironmentId();

// we get the connection on the nango admin account to be able to send the notification
Expand Down Expand Up @@ -299,8 +307,8 @@ class SlackService {
);

const content = isOk(actionResponse)
? `The action ${this.actionName} was successfully triggered for the ${flowType} ${syncName} for environment ${slackConnection?.environment_id} for account ${accountUUID}.`
: `The action ${this.actionName} failed to trigger for the ${flowType} ${syncName} with the error: ${actionResponse.err.message} for environment ${slackConnection?.environment_id} for account ${accountUUID}.`;
? `The action ${this.actionName} was successfully triggered for the ${flowType} ${syncName} for environment ${slackConnection?.environment_id} for account ${account.uuid}.`
: `The action ${this.actionName} failed to trigger for the ${flowType} ${syncName} with the error: ${actionResponse.err.message} for environment ${slackConnection?.environment_id} for account ${account.uuid}.`;

await createActivityLogMessage({
level: isOk(actionResponse) ? 'info' : 'error',
Expand Down Expand Up @@ -365,9 +373,13 @@ class SlackService {
return;
}

const accountUUID = await environmentService.getAccountUUIDFromEnvironment(environment_id);
const account = await environmentService.getAccountFromEnvironment(environment_id);
if (!account) {
throw new Error('failed_to_get_account');
}

const nangoEnvironmentId = await this.getAdminEnvironmentId();
const slackConnectionId = `account-${accountUUID}`;
const slackConnectionId = `account-${account.uuid}`;
const { success: connectionSuccess, response: slackConnection } = await connectionService.getConnection(
slackConnectionId,
this.integrationKey,
Expand Down Expand Up @@ -405,8 +417,8 @@ class SlackService {
await this.sendDuplicateNotificationToNangoAdmins(payload, activityLogId as number, environment_id, undefined, admin_slack_timestamp);

const content = isOk(actionResponse)
? `The action ${this.actionName} was successfully triggered for the ${syncType} ${syncName} for environment ${slackConnection?.environment_id} for account ${accountUUID}.`
: `The action ${this.actionName} failed to trigger for the ${syncType} ${syncName} with the error: ${actionResponse.err.message} for environment ${slackConnection?.environment_id} for account ${accountUUID}.`;
? `The action ${this.actionName} was successfully triggered for the ${syncType} ${syncName} for environment ${slackConnection?.environment_id} for account ${account.uuid}.`
: `The action ${this.actionName} failed to trigger for the ${syncType} ${syncName} with the error: ${actionResponse.err.message} for environment ${slackConnection?.environment_id} for account ${account.uuid}.`;

await createActivityLogMessage({
level: isOk(actionResponse) ? 'info' : 'error',
Expand Down

0 comments on commit 339a802

Please sign in to comment.