Skip to content

Commit

Permalink
fix(connection): upsert return full connection, logs (#2219)
Browse files Browse the repository at this point in the history
## Describe your changes

Fixes NAN-1041

- Upserting connection now returns full row 
Some part of the code still query connection again after that, ideally
we would reuse the object but I tried to limit the number of changes

- Add missing context for logs where needed
  • Loading branch information
bodinsamuel authored May 29, 2024
1 parent 9854182 commit a00e30d
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 159 deletions.
2 changes: 1 addition & 1 deletion packages/jobs/lib/crons/autoIdleDemo.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('Auto Idle Demo', async () => {
})
.returning('id');
const conn = await connectionService.upsertConnection(connName, DEMO_GITHUB_CONFIG_KEY, 'github', {} as any, {}, env.id, 0);
const connId = conn[0]!.id;
const connId = conn[0]!.connection.id!;
const sync = (await createSync(connId, DEMO_SYNC_NAME))!;
await createSchedule(sync.id, '86400', 0, ScheduleStatus.RUNNING, nanoid());
const schedBefore = await getSchedule(sync.id);
Expand Down
2 changes: 1 addition & 1 deletion packages/persist/lib/server.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ const initDb = async () => {
);

const connectionRes = await connectionService.upsertConnection(`conn-test`, `provider-test`, 'google', {} as AuthCredentials, {}, env.id, 0);
const connectionId = connectionRes[0]?.id;
const connectionId = connectionRes[0]?.connection.id;
if (!connectionId) throw new Error('Connection not created');

const connection = (await connectionService.getConnectionById(connectionId)) as Connection;
Expand Down
21 changes: 8 additions & 13 deletions packages/server/lib/controllers/apiAuth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,10 @@ class ApiAuthController {
);

if (updatedConnection) {
await logCtx.enrichOperation({ connectionId: updatedConnection.connection.id!, connectionName: updatedConnection.connection.connection_id });
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: AuthModes.ApiKey,
Expand Down Expand Up @@ -240,9 +239,7 @@ class ApiAuthController {
if (logCtx) {
void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId as string,
provider_config_key: providerConfigKey as string,
connection: { connection_id: connectionId!, provider_config_key: providerConfigKey! },
environment,
account,
auth_mode: AuthModes.ApiKey,
Expand Down Expand Up @@ -366,6 +363,8 @@ class ApiAuthController {
return;
}

await logCtx.enrichOperation({ integrationId: config.id!, integrationName: config.unique_key, providerName: config.provider });

const template = configService.getTemplate(config.provider);

if (template.auth_mode !== AuthModes.Basic) {
Expand Down Expand Up @@ -418,7 +417,6 @@ class ApiAuthController {
}

await updateProviderActivityLog(activityLogId as number, String(config.provider));
await logCtx.enrichOperation({ integrationId: config.id!, integrationName: config.unique_key, providerName: config.provider });

await createActivityLogMessage({
level: 'info',
Expand All @@ -443,11 +441,10 @@ class ApiAuthController {
);

if (updatedConnection) {
await logCtx.enrichOperation({ connectionId: updatedConnection.connection.id!, connectionName: updatedConnection.connection.connection_id });
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: AuthModes.Basic,
Expand Down Expand Up @@ -475,9 +472,7 @@ class ApiAuthController {
if (logCtx) {
void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId as string,
provider_config_key: providerConfigKey as string,
connection: { connection_id: connectionId!, provider_config_key: providerConfigKey! },
environment,
account,
auth_mode: AuthModes.ApiKey,
Expand Down
15 changes: 6 additions & 9 deletions packages/server/lib/controllers/appAuth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class AppAuthController {
return;
}

await logCtx.enrichOperation({ integrationId: config.id!, integrationName: config.unique_key, providerName: config.provider });

const template = configService.getTemplate(config.provider);
const tokenUrl = typeof template.token_url === 'string' ? template.token_url : (template.token_url?.[AuthModes.App] as string);

Expand Down Expand Up @@ -202,9 +204,7 @@ class AppAuthController {

void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: AuthModes.App,
Expand Down Expand Up @@ -232,11 +232,10 @@ class AppAuthController {
);

if (updatedConnection) {
await logCtx.enrichOperation({ connectionId: updatedConnection.connection.id!, connectionName: updatedConnection.connection.connection_id });
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: AuthModes.App,
Expand Down Expand Up @@ -295,9 +294,7 @@ class AppAuthController {

void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: AuthModes.App,
Expand Down
13 changes: 4 additions & 9 deletions packages/server/lib/controllers/appStoreAuth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ class AppStoreAuthController {
if (!success || !credentials) {
void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: AuthModes.AppStore,
Expand Down Expand Up @@ -210,11 +208,10 @@ class AppStoreAuthController {
);

if (updatedConnection) {
await logCtx.enrichOperation({ connectionId: updatedConnection.connection.id!, connectionName: updatedConnection.connection.connection_id });
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: AuthModes.AppStore,
Expand Down Expand Up @@ -242,9 +239,7 @@ class AppStoreAuthController {
if (logCtx) {
void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId as string,
provider_config_key: providerConfigKey as string,
connection: { connection_id: connectionId!, provider_config_key: providerConfigKey! },
environment,
account,
auth_mode: AuthModes.AppStore,
Expand Down
22 changes: 6 additions & 16 deletions packages/server/lib/controllers/connection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,7 @@ class ConnectionController {
const connCreatedHook = async (res: ConnectionUpsertResponse) => {
void connectionCreatedHook(
{
id: res.id,
connection_id,
provider_config_key,
connection: res.connection,
environment,
account,
auth_mode: ProviderAuthModes.OAuth2,
Expand Down Expand Up @@ -630,9 +628,7 @@ class ConnectionController {
const connCreatedHook = async (res: ConnectionUpsertResponse) => {
void connectionCreatedHook(
{
id: res.id,
connection_id,
provider_config_key,
connection: res.connection,
environment,
account,
auth_mode: ProviderAuthModes.OAuth2,
Expand Down Expand Up @@ -674,9 +670,7 @@ class ConnectionController {
const connCreatedHook = async (res: ConnectionUpsertResponse) => {
void connectionCreatedHook(
{
id: res.id,
connection_id,
provider_config_key,
connection: res.connection,
environment,
account,
auth_mode: ProviderAuthModes.ApiKey,
Expand Down Expand Up @@ -716,9 +710,7 @@ class ConnectionController {
const connCreatedHook = async (res: ConnectionUpsertResponse) => {
void connectionCreatedHook(
{
id: res.id,
connection_id,
provider_config_key,
connection: res.connection,
environment,
account,
auth_mode: ProviderAuthModes.ApiKey,
Expand Down Expand Up @@ -801,12 +793,10 @@ class ConnectionController {
return;
}

if (updatedConnection && updatedConnection.id && runHook) {
if (updatedConnection && updatedConnection.connection.id && runHook) {
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id,
provider_config_key,
connection: updatedConnection.connection,
environment,
account,
auth_mode: template.auth_mode,
Expand Down
47 changes: 16 additions & 31 deletions packages/server/lib/controllers/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,10 @@ class OAuthController {
);

if (updatedConnection) {
await logCtx.enrichOperation({ connectionId: updatedConnection.connection.id!, connectionName: updatedConnection.connection.connection_id });
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: ProviderAuthModes.None,
Expand Down Expand Up @@ -572,9 +571,7 @@ class OAuthController {
if (logCtx) {
void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId as string,
provider_config_key: providerConfigKey as string,
connection: { connection_id: connectionId!, provider_config_key: providerConfigKey! },
environment,
account,
auth_mode: ProviderAuthModes.OAuth2CC,
Expand Down Expand Up @@ -1202,9 +1199,7 @@ class OAuthController {

void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: template.auth_mode,
Expand Down Expand Up @@ -1367,9 +1362,7 @@ class OAuthController {

void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: template.auth_mode,
Expand Down Expand Up @@ -1457,7 +1450,6 @@ class OAuthController {
);

await updateProviderActivityLog(activityLogId, session.provider);
await logCtx.enrichOperation({ integrationId: config.id!, integrationName: config.unique_key, providerName: config.provider });

await createActivityLogMessageAndEnd({
level: 'debug',
Expand Down Expand Up @@ -1490,14 +1482,13 @@ class OAuthController {
);

if (updatedConnection) {
await logCtx.enrichOperation({ connectionId: updatedConnection.connection.id!, connectionName: updatedConnection.connection.connection_id });
// don't initiate a sync if custom because this is the first step of the oauth flow
const initiateSync = template.auth_mode === ProviderAuthModes.Custom ? false : true;
const runPostConnectionScript = true;
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: template.auth_mode,
Expand All @@ -1516,9 +1507,7 @@ class OAuthController {
const connCreatedHook = async (res: ConnectionUpsertResponse) => {
void connectionCreatedHook(
{
id: res.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: res.connection,
environment,
account,
auth_mode: ProviderAuthModes.App,
Expand Down Expand Up @@ -1588,9 +1577,7 @@ class OAuthController {

void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: template.auth_mode,
Expand Down Expand Up @@ -1637,9 +1624,7 @@ class OAuthController {

void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: template.auth_mode,
Expand Down Expand Up @@ -1694,14 +1679,16 @@ class OAuthController {
});

if (updatedConnection) {
await logCtx.enrichOperation({
connectionId: updatedConnection.connection.id!,
connectionName: updatedConnection.connection.connection_id
});
// syncs not support for oauth1
const initiateSync = false;
const runPostConnectionScript = true;
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: template.auth_mode,
Expand Down Expand Up @@ -1753,9 +1740,7 @@ class OAuthController {

void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: { connection_id: connectionId, provider_config_key: providerConfigKey },
environment,
account,
auth_mode: template.auth_mode,
Expand Down
9 changes: 3 additions & 6 deletions packages/server/lib/controllers/unauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,10 @@ class UnAuthController {
);

if (updatedConnection) {
await logCtx.enrichOperation({ connectionId: updatedConnection.connection.id!, connectionName: updatedConnection.connection.connection_id });
void connectionCreatedHook(
{
id: updatedConnection.id,
connection_id: connectionId,
provider_config_key: providerConfigKey,
connection: updatedConnection.connection,
environment,
account,
auth_mode: AuthModes.None,
Expand Down Expand Up @@ -191,9 +190,7 @@ class UnAuthController {
if (logCtx) {
void connectionCreationFailedHook(
{
id: -1,
connection_id: connectionId as string,
provider_config_key: providerConfigKey as string,
connection: { connection_id: connectionId!, provider_config_key: providerConfigKey! },
environment,
account,
auth_mode: AuthModes.None,
Expand Down
Loading

0 comments on commit a00e30d

Please sign in to comment.