Skip to content

Commit

Permalink
Merge pull request #295 from bakaphp/feat-admin-app-setting
Browse files Browse the repository at this point in the history
feat: add get app setting
  • Loading branch information
kaioken authored Sep 9, 2024
2 parents c346c60 + ba4a283 commit 18c44fd
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
echo "KANVAS_APP_KEY=${{ secrets.KANVAS_APP_KEY }}" >> $GITHUB_ENV
echo "KANVAS_TEST_USER=${{ secrets.KANVAS_TEST_USER }}" >> $GITHUB_ENV
echo "KANVAS_TEST_PASSWORD=${{ secrets.KANVAS_TEST_PASSWORD }}" >> $GITHUB_ENV
echo "KANVAS_APP_SECRET=${{ secrets.KANVAS_APP_SECRET }}" >> $GITHUB_ENV
- name: Run install
uses: borales/actions-yarn@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
echo "KANVAS_APP_KEY=${{ secrets.KANVAS_APP_KEY }}" >> $GITHUB_ENV
echo "KANVAS_TEST_USER=${{ secrets.KANVAS_TEST_USER }}" >> $GITHUB_ENV
echo "KANVAS_TEST_PASSWORD=${{ secrets.KANVAS_TEST_PASSWORD }}" >> $GITHUB_ENV
echo "KANVAS_APP_SECRET=${{ secrets.KANVAS_APP_SECRET }}" >> $GITHUB_ENV
- run: yarn install --frozen-lockfile

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.8.6",
"version": "0.8.7",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
34 changes: 30 additions & 4 deletions src/modules/settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
APP_SETTING_QUERY,
APP_SETTINGS_QUERY,
AppSettingsQuery,
COMPANY_SETTING_QUERY,
ConfigInput,
USERS_SETTINGS_QUERY,
} from '../../queries';
import {
AppSettingsQueryResponse,
AdminAppSettingsQueryResponse,
AppSettingsResponse,
ClientType,
CompanySettingsResponse,
Expand All @@ -24,9 +25,9 @@ import {
} from '../../mutations';

export default class Settings {
constructor(protected client: ClientType, protected key: string) {}
constructor(protected client: ClientType, protected key: string) { }

async fetchAppSettings(): Promise<AppSettingsQueryResponse | undefined> {
async appSettings(): Promise<AdminAppSettingsQueryResponse | undefined> {
try {
const { data } = await this.client.query({
query: APP_SETTINGS_QUERY,
Expand All @@ -38,6 +39,31 @@ export default class Settings {
return undefined;
}
}

async appSetting(name: string): Promise<any | undefined> {
try {
const { data } = await this.client.query({
query: APP_SETTING_QUERY,
variables: {
key: name,
},
fetchPolicy: 'network-only',
partialRefetch: true,
});

return data.adminAppSetting;
} catch {
return undefined;
}
}

async fetchAppSettings(): Promise<AdminAppSettingsQueryResponse | undefined> {
return this.appSettings();
}

/**
* @deprecated
*/
async getAppSettings(): Promise<SettingsResponse | undefined> {
try {
const {
Expand Down Expand Up @@ -167,7 +193,7 @@ export default class Settings {
},
});
return response.data.deleteUserSetting;
} catch (err){
} catch (err) {
return undefined;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/queries/settings.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ export const APP_SETTINGS_QUERY = gql`
}
`;

export const APP_SETTING_QUERY = gql`
query adminAppSetting($key: String!) {
adminAppSetting(key: $key)
}
`;

9 changes: 9 additions & 0 deletions src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ export interface UserSettingsResponse {
export interface AppSettingsQueryResponse {
appSettings: Array<{ key: string; value: any; public?: boolean }>;
}
export interface AdminAppSetting {
key: string;
value: any;
public?: boolean;
}

export interface AdminAppSettingsQueryResponse {
adminAppSettings: AdminAppSetting[];
}
51 changes: 51 additions & 0 deletions test/customFields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { initializeClient, getClient } from './setupClient';
import dotenv from 'dotenv';

dotenv.config();

beforeAll(async () => {
await initializeClient(process.env.KANVAS_APP_SECRET!);
});

/**
* @todo add profile photo upload test
*/
describe('Test the Kanvas Custom Fields', () => {

it('gets app settings - deprecated', async () => {
const client = getClient();

const appSettings = await client.settings.getAppSettings();
expect(appSettings).toBeDefined();
expect(appSettings?.name).toBeDefined();
expect(appSettings?.settings).toBeDefined();
});

it('gets admin app settings', async () => {
const client = getClient();

const appSettings = await client.settings.fetchAppSettings();
expect(appSettings?.adminAppSettings).toBeDefined();
expect(appSettings?.adminAppSettings[0].key).toBeDefined();
expect(appSettings?.adminAppSettings[0].value).toBeDefined();
expect(appSettings?.adminAppSettings[0].public).toBeDefined();
});

it('gets admin app settings v2', async () => {
const client = getClient();

const appSettings = await client.settings.appSettings();
expect(appSettings?.adminAppSettings).toBeDefined();
expect(appSettings?.adminAppSettings[0].key).toBeDefined();
expect(appSettings?.adminAppSettings[0].value).toBeDefined();
expect(appSettings?.adminAppSettings[0].public).toBeDefined();
});

it('gets admin one app setting', async () => {
const client = getClient();

const appSettings = await client.settings.appSettings();
const appSetting = await client.settings.appSetting(appSettings?.adminAppSettings[0].key ?? '');
expect(appSetting).toBeDefined();
});
});
3 changes: 2 additions & 1 deletion test/setupClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dotenv.config();

let client: KanvasCore;

export const initializeClient = async () => {
export const initializeClient = async (adminKey?: string) => {
const getKey = async (): Promise<string | null> => {
return localStorage.getItem("token") || null; // wherever you have saved the user token
};
Expand Down Expand Up @@ -35,6 +35,7 @@ export const initializeClient = async () => {
client = new KanvasCore({
url: process.env.KANVAS_URL!,
key: process.env.KANVAS_APP_KEY!,
...(adminKey && { adminKey }), // Add adminKey only if it is provided
middlewares: [genericAuthMiddleware(getKey)],
});

Expand Down

0 comments on commit 18c44fd

Please sign in to comment.