Skip to content

Commit

Permalink
refactor: leverage generated enums + prefer inlining variables within…
Browse files Browse the repository at this point in the history
… function definitions
  • Loading branch information
jamesgeorge007 committed May 1, 2024
1 parent 5b0b43a commit f34109d
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 104 deletions.
179 changes: 93 additions & 86 deletions packages/hoppscotch-sh-admin/src/composables/useConfigHandler.ts
@@ -1,15 +1,18 @@
import { AnyVariables, UseMutationResponse } from '@urql/vue';
import { cloneDeep } from 'lodash-es';
import { onMounted, ref } from 'vue';

import { useI18n } from '~/composables/i18n';
import {
AllowedAuthProvidersDocument,
AuthProvider,
EnableAndDisableSsoArgs,
EnableAndDisableSsoMutation,
InfraConfigArgs,
InfraConfigEnum,
InfraConfigsDocument,
ResetInfraConfigsMutation,
ServiceStatus,
ToggleAnalyticsCollectionMutation,
UpdateInfraConfigsMutation,
} from '~/helpers/backend/graphql';
Expand Down Expand Up @@ -70,50 +73,50 @@ export function useConfigHandler(updatedConfigs?: ServerConfigs) {
await fetchInfraConfigs();
await fetchAllowedAuthProviders();

const getFieldValue = (name: string) =>
const getFieldValue = (name: InfraConfigEnum) =>
infraConfigs.value.find((x) => x.name === name)?.value ?? '';

// Transforming the fetched data into a Configs object
currentConfigs.value = {
providers: {
google: {
name: 'google',
enabled: allowedAuthProviders.value.includes('GOOGLE'),
enabled: allowedAuthProviders.value.includes(AuthProvider.Google),
fields: {
client_id: getFieldValue('GOOGLE_CLIENT_ID'),
client_secret: getFieldValue('GOOGLE_CLIENT_SECRET'),
callback_url: getFieldValue('GOOGLE_CALLBACK_URL'),
scope: getFieldValue('GOOGLE_SCOPE'),
client_id: getFieldValue(InfraConfigEnum.GoogleClientId),
client_secret: getFieldValue(InfraConfigEnum.GoogleClientSecret),
callback_url: getFieldValue(InfraConfigEnum.GoogleCallbackUrl),
scope: getFieldValue(InfraConfigEnum.GoogleScope),
},
},
github: {
name: 'github',
enabled: allowedAuthProviders.value.includes('GITHUB'),
enabled: allowedAuthProviders.value.includes(AuthProvider.Github),
fields: {
client_id: getFieldValue('GITHUB_CLIENT_ID'),
client_secret: getFieldValue('GITHUB_CLIENT_SECRET'),
callback_url: getFieldValue('GITHUB_CALLBACK_URL'),
scope: getFieldValue('GITHUB_SCOPE'),
client_id: getFieldValue(InfraConfigEnum.GithubClientId),
client_secret: getFieldValue(InfraConfigEnum.GithubClientSecret),
callback_url: getFieldValue(InfraConfigEnum.GoogleCallbackUrl),
scope: getFieldValue(InfraConfigEnum.GithubScope),
},
},
microsoft: {
name: 'microsoft',
enabled: allowedAuthProviders.value.includes('MICROSOFT'),
enabled: allowedAuthProviders.value.includes(AuthProvider.Microsoft),
fields: {
client_id: getFieldValue('MICROSOFT_CLIENT_ID'),
client_secret: getFieldValue('MICROSOFT_CLIENT_SECRET'),
callback_url: getFieldValue('MICROSOFT_CALLBACK_URL'),
scope: getFieldValue('MICROSOFT_SCOPE'),
tenant: getFieldValue('MICROSOFT_TENANT'),
client_id: getFieldValue(InfraConfigEnum.MicrosoftClientId),
client_secret: getFieldValue(InfraConfigEnum.MicrosoftClientSecret),
callback_url: getFieldValue(InfraConfigEnum.MicrosoftCallbackUrl),
scope: getFieldValue(InfraConfigEnum.MicrosoftScope),
tenant: getFieldValue(InfraConfigEnum.MicrosoftTenant),
},
},
},
mailConfigs: {
name: 'email',
enabled: allowedAuthProviders.value.includes('EMAIL'),
enabled: allowedAuthProviders.value.includes(AuthProvider.Email),
fields: {
mailer_smtp_url: getFieldValue('MAILER_SMTP_URL'),
mailer_from_address: getFieldValue('MAILER_ADDRESS_FROM'),
mailer_smtp_url: getFieldValue(InfraConfigEnum.MailerSmtpUrl),
mailer_from_address: getFieldValue(InfraConfigEnum.MailerAddressFrom),
},
},
dataSharingConfigs: {
Expand Down Expand Up @@ -149,74 +152,45 @@ export function useConfigHandler(updatedConfigs?: ServerConfigs) {
);
};

/**
* The updated configs are transformed into a format that can be used by the mutations
*/

const workingUpdatedConfigs: ConfigTransform[] = [
{
config: GOOGLE_CONFIGS,
enabled: updatedConfigs?.providers.google.enabled,
fields: updatedConfigs?.providers.google.fields,
},
{
config: GITHUB_CONFIGS,
enabled: updatedConfigs?.providers.github.enabled,
fields: updatedConfigs?.providers.github.fields,
},
{
config: MICROSOFT_CONFIGS,
enabled: updatedConfigs?.providers.microsoft.enabled,
fields: updatedConfigs?.providers.microsoft.fields,
},
{
config: MAIL_CONFIGS,
enabled: updatedConfigs?.mailConfigs.enabled,
fields: updatedConfigs?.mailConfigs.fields,
},
];
// Transforming the working configs back into the format required by the mutations
const transformInfraConfigs = () => {
const updatedWorkingConfigs: ConfigTransform[] = [
{
config: GOOGLE_CONFIGS,
enabled: updatedConfigs?.providers.google.enabled,
fields: updatedConfigs?.providers.google.fields,
},
{
config: GITHUB_CONFIGS,
enabled: updatedConfigs?.providers.github.enabled,
fields: updatedConfigs?.providers.github.fields,
},
{
config: MICROSOFT_CONFIGS,
enabled: updatedConfigs?.providers.microsoft.enabled,
fields: updatedConfigs?.providers.microsoft.fields,
},
{
config: MAIL_CONFIGS,
enabled: updatedConfigs?.mailConfigs.enabled,
fields: updatedConfigs?.mailConfigs.fields,
},
];

// Push or filter the configs based on the enabled condition
const transformInfraConfigs = (workingConfigs: ConfigTransform[]) => {
let newConfigs: UpdatedConfigs[] = [];
const transformedConfigs: UpdatedConfigs[] = [];

workingConfigs.forEach(({ config, enabled, fields }) => {
updatedWorkingConfigs.forEach(({ config, enabled, fields }) => {
config.forEach(({ name, key }) => {
if (enabled && fields) {
const value =
typeof fields === 'string' ? fields : String(fields[key]);
newConfigs.push({ name, value });
transformedConfigs.push({ name, value });
}
});
});
return newConfigs;
};

// Transforming the working configs back into the format required by the mutations
const updatedInfraConfigs = () =>
updatedConfigs ? transformInfraConfigs(workingUpdatedConfigs) : [];

// Updated allowed auth providers
const updatedAllowedAuthProviders = [
{
provider: 'GOOGLE',
status: updatedConfigs?.providers.google.enabled ? 'ENABLE' : 'DISABLE',
},
{
provider: 'MICROSOFT',
status: updatedConfigs?.providers.microsoft.enabled
? 'ENABLE'
: 'DISABLE',
},
{
provider: 'GITHUB',
status: updatedConfigs?.providers.github.enabled ? 'ENABLE' : 'DISABLE',
},
{
provider: 'EMAIL',
status: updatedConfigs?.mailConfigs.enabled ? 'ENABLE' : 'DISABLE',
},
];
return transformedConfigs;
};

// Generic function to handle mutation execution and error handling
const executeMutation = async <T, V>(
Expand All @@ -237,26 +211,59 @@ export function useConfigHandler(updatedConfigs?: ServerConfigs) {
// Updating the auth provider configurations
const updateAuthProvider = (
updateProviderStatus: UseMutationResponse<EnableAndDisableSsoMutation>
) =>
executeMutation(
) => {
const updatedAllowedAuthProviders: EnableAndDisableSsoArgs[] = [
{
provider: AuthProvider.Google,
status: updatedConfigs?.providers.google.enabled
? ServiceStatus.Enable
: ServiceStatus.Disable,
},
{
provider: AuthProvider.Microsoft,
status: updatedConfigs?.providers.microsoft.enabled
? ServiceStatus.Enable
: ServiceStatus.Disable,
},
{
provider: AuthProvider.Github,
status: updatedConfigs?.providers.github.enabled
? ServiceStatus.Enable
: ServiceStatus.Disable,
},
{
provider: AuthProvider.Email,
status: updatedConfigs?.mailConfigs.enabled
? ServiceStatus.Enable
: ServiceStatus.Disable,
},
];

return executeMutation(
updateProviderStatus,
{
providerInfo: updatedAllowedAuthProviders as EnableAndDisableSsoArgs[],
providerInfo: updatedAllowedAuthProviders,
},
'configs.auth_providers.update_failure'
);
};

// Updating the infra configurations
const updateInfraConfigs = (
updateInfraConfigsMutation: UseMutationResponse<UpdateInfraConfigsMutation>
) =>
executeMutation(
) => {
const infraConfigs: InfraConfigArgs[] = updatedConfigs
? transformInfraConfigs()
: [];

return executeMutation(
updateInfraConfigsMutation,
{
infraConfigs: updatedInfraConfigs() as InfraConfigArgs[],
infraConfigs,
},
'configs.update_failure'
);
};

// Resetting the infra configurations
const resetInfraConfigs = (
Expand All @@ -275,8 +282,8 @@ export function useConfigHandler(updatedConfigs?: ServerConfigs) {
toggleDataSharingMutation,
{
status: updatedConfigs?.dataSharingConfigs.enabled
? 'ENABLE'
: 'DISABLE',
? ServiceStatus.Enable
: ServiceStatus.Disable,
},
'configs.data_sharing.update_failure'
);
Expand Down
38 changes: 20 additions & 18 deletions packages/hoppscotch-sh-admin/src/helpers/configs.ts
@@ -1,3 +1,5 @@
import { InfraConfigEnum } from './backend/graphql';

export type SsoAuthProviders = 'google' | 'microsoft' | 'github';

export type ServerConfigs = {
Expand Down Expand Up @@ -51,7 +53,7 @@ export type ServerConfigs = {
};

export type UpdatedConfigs = {
name: string;
name: InfraConfigEnum;
value: string;
};

Expand All @@ -67,85 +69,85 @@ export type ConfigSection = {
};

export type Config = {
name: string;
name: InfraConfigEnum;
key: string;
};

export const GOOGLE_CONFIGS: Config[] = [
{
name: 'GOOGLE_CLIENT_ID',
name: InfraConfigEnum.GoogleClientId,
key: 'client_id',
},
{
name: 'GOOGLE_CLIENT_SECRET',
name: InfraConfigEnum.GoogleClientSecret,
key: 'client_secret',
},
{
name: 'GOOGLE_CALLBACK_URL',
name: InfraConfigEnum.GoogleCallbackUrl,
key: 'callback_url',
},
{
name: 'GOOGLE_SCOPE',
name: InfraConfigEnum.GoogleScope,
key: 'scope',
},
];

export const MICROSOFT_CONFIGS: Config[] = [
{
name: 'MICROSOFT_CLIENT_ID',
name: InfraConfigEnum.MicrosoftClientId,
key: 'client_id',
},
{
name: 'MICROSOFT_CLIENT_SECRET',
name: InfraConfigEnum.MicrosoftClientSecret,
key: 'client_secret',
},
{
name: 'MICROSOFT_CALLBACK_URL',
name: InfraConfigEnum.MicrosoftCallbackUrl,
key: 'callback_url',
},
{
name: 'MICROSOFT_SCOPE',
name: InfraConfigEnum.MicrosoftScope,
key: 'scope',
},
{
name: 'MICROSOFT_TENANT',
name: InfraConfigEnum.MicrosoftTenant,
key: 'tenant',
},
];

export const GITHUB_CONFIGS: Config[] = [
{
name: 'GITHUB_CLIENT_ID',
name: InfraConfigEnum.GithubClientId,
key: 'client_id',
},
{
name: 'GITHUB_CLIENT_SECRET',
name: InfraConfigEnum.GithubClientSecret,
key: 'client_secret',
},
{
name: 'GITHUB_CALLBACK_URL',
name: InfraConfigEnum.GithubCallbackUrl,
key: 'callback_url',
},
{
name: 'GITHUB_SCOPE',
name: InfraConfigEnum.GithubScope,
key: 'scope',
},
];

export const MAIL_CONFIGS: Config[] = [
{
name: 'MAILER_SMTP_URL',
name: InfraConfigEnum.MailerSmtpUrl,
key: 'mailer_smtp_url',
},
{
name: 'MAILER_ADDRESS_FROM',
name: InfraConfigEnum.MailerAddressFrom,
key: 'mailer_from_address',
},
];

const DATA_SHARING_CONFIGS: Omit<Config, 'key'>[] = [
{
name: 'ALLOW_ANALYTICS_COLLECTION',
name: InfraConfigEnum.AllowAnalyticsCollection,
},
];

Expand Down

0 comments on commit f34109d

Please sign in to comment.