Skip to content

Commit

Permalink
Last fixes according to review
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Oct 21, 2024
1 parent c5b2677 commit 571e067
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useGraphData } from '@/settings/developers/webhook/hooks/useGraphData';
import { webhookGraphDataState } from '@/settings/developers/webhook/states/webhookGraphDataState';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';
import { useSetRecoilState } from 'recoil';

type SettingsDevelopersWebhookUsageGraphEffectProps = {
Expand All @@ -11,14 +11,18 @@ export const SettingsDevelopersWebhookUsageGraphEffect = ({
webhookId,
}: SettingsDevelopersWebhookUsageGraphEffectProps) => {
const setWebhookGraphData = useSetRecoilState(webhookGraphDataState);
const [isLoaded, setIsLoaded] = useState(false);

const { fetchGraphData } = useGraphData(webhookId);

useEffect(() => {
fetchGraphData('7D').then((graphInput) => {
setWebhookGraphData(graphInput);
});
}, [fetchGraphData, setWebhookGraphData, webhookId]);
if (!isLoaded) {
fetchGraphData('7D').then((graphInput) => {
setWebhookGraphData(graphInput);
});
setIsLoaded(true);
}
}, [fetchGraphData, isLoaded, setWebhookGraphData, webhookId]);

return <></>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { renderHook } from '@testing-library/react';

import { CurrentUser, currentUserState } from '@/auth/states/currentUserState';
import { useAnalyticsTinybirdJwt } from '@/settings/developers/webhook/hooks/useAnalyticsTinybirdJwt';
import { act } from 'react';
import { useSetRecoilState } from 'recoil';
import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';

const Wrapper = getJestMetadataAndApolloMocksWrapper({
apolloMocks: [],
});

describe('useAnalyticsTinybirdJwt', () => {
it('should return the analytics jwt token', async () => {
const { result } = renderHook(
() => {
const setCurrentUserState = useSetRecoilState(currentUserState);

return {
useAnalyticsTinybirdJwt: useAnalyticsTinybirdJwt(),
setCurrentUserState,
};
},
{ wrapper: Wrapper },
);

act(() => {
result.current.setCurrentUserState({
analyticsTinybirdJwt: 'jwt',
} as CurrentUser);
});

expect(result.current.useAnalyticsTinybirdJwt).toBe('jwt');

act(() => {
result.current.setCurrentUserState(null);
});

expect(result.current.useAnalyticsTinybirdJwt).toBeUndefined();

act(() => {
result.current.setCurrentUserState({} as CurrentUser);
});

expect(result.current.useAnalyticsTinybirdJwt).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { useRecoilValue } from 'recoil';

import { currentUserState } from '@/auth/states/currentUserState';
import { isNull } from '@sniptt/guards';

export const useAnalyticsTinybirdJwt = (): string | null | undefined => {
export const useAnalyticsTinybirdJwt = (): string | undefined => {
const currentUser = useRecoilValue(currentUserState);
return currentUser?.analyticsTinybirdJwt;

if (!currentUser) {
return undefined;
}

if (isNull(currentUser.analyticsTinybirdJwt)) {
return undefined;
}

return currentUser.analyticsTinybirdJwt;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useAnalyticsTinybirdJwt } from '@/settings/developers/webhook/hooks/use
import { fetchGraphDataOrThrow } from '@/settings/developers/webhook/utils/fetchGraphDataOrThrow';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { isUndefined } from '@sniptt/guards';

export const useGraphData = (webhookId: string) => {
const { enqueueSnackBar } = useSnackBar();
Expand All @@ -10,7 +11,7 @@ export const useGraphData = (webhookId: string) => {
windowLengthGraphOption: '7D' | '1D' | '12H' | '4H',
) => {
try {
if (typeof analyticsTinybirdJwt !== 'string') {
if (isUndefined(analyticsTinybirdJwt)) {
throw new Error('No analyticsTinybirdJwt found');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { JwtModule as NestJwtModule } from '@nestjs/jwt';

import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
import { JwtModule } from 'src/engine/core-modules/jwt/jwt.module';

import { AnalyticsResolver } from './analytics.resolver';
import { AnalyticsService } from './analytics.service';

const TINYBIRD_BASE_URL = 'https://api.eu-central-1.aws.tinybird.co/v0';

const InternalTinybirdJwtModule = NestJwtModule.registerAsync({
useFactory: async (environmentService: EnvironmentService) => {
return {
secret: environmentService.get('TINYBIRD_GENERATE_JWT_TOKEN'),
signOptions: {
expiresIn: environmentService.get('TINYBIRD_TOKEN_EXPIRES_IN'),
},
};
},
inject: [EnvironmentService],
});

@Module({
providers: [AnalyticsResolver, AnalyticsService],
imports: [
JwtModule,
HttpModule.register({
baseURL: TINYBIRD_BASE_URL,
}),
InternalTinybirdJwtModule,
],
exports: [AnalyticsService],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { HttpService } from '@nestjs/axios';
import { Injectable, Logger } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

import { AxiosRequestConfig } from 'axios';

import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service';
import { JwtWrapperService } from 'src/engine/core-modules/jwt/services/jwt-wrapper.service';

type CreateEventInput = {
action: string;
Expand All @@ -17,9 +17,9 @@ export class AnalyticsService {
private readonly defaultDatasource = 'event';

constructor(
private readonly jwtWrapperService: JwtWrapperService,
private readonly environmentService: EnvironmentService,
private readonly httpService: HttpService,
private readonly jwtService: JwtService,
) {}

async create(
Expand Down Expand Up @@ -104,6 +104,9 @@ export class AnalyticsService {
],
};

return this.jwtService.sign(payload);
return this.jwtWrapperService.sign(payload, {
secret: this.environmentService.get('TINYBIRD_GENERATE_JWT_TOKEN'),
expiresIn: '7d',
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ export class EnvironmentVariables {
@ValidateIf((env) => env.ANALYTICS_ENABLED)
TINYBIRD_WORKSPACE_UUID: string;

@IsString()
@ValidateIf((env) => env.ANALYTICS_ENABLED)
TINYBIRD_TOKEN_EXPIRES_IN: string;

@IsString()
@ValidateIf((env) => env.ANALYTICS_ENABLED)
TINYBIRD_GENERATE_JWT_TOKEN: string;
Expand Down

0 comments on commit 571e067

Please sign in to comment.