diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6cfc491d..9119c9e5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 7bf484d2..a6cf3fb3 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -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 diff --git a/package.json b/package.json index 99955d82..2740c5ed 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.8.6", + "version": "0.8.7", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/src/modules/settings/index.tsx b/src/modules/settings/index.tsx index db70f234..71d4495f 100644 --- a/src/modules/settings/index.tsx +++ b/src/modules/settings/index.tsx @@ -1,4 +1,5 @@ import { + APP_SETTING_QUERY, APP_SETTINGS_QUERY, AppSettingsQuery, COMPANY_SETTING_QUERY, @@ -6,7 +7,7 @@ import { USERS_SETTINGS_QUERY, } from '../../queries'; import { - AppSettingsQueryResponse, + AdminAppSettingsQueryResponse, AppSettingsResponse, ClientType, CompanySettingsResponse, @@ -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 { + async appSettings(): Promise { try { const { data } = await this.client.query({ query: APP_SETTINGS_QUERY, @@ -38,6 +39,31 @@ export default class Settings { return undefined; } } + + async appSetting(name: string): Promise { + 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 { + return this.appSettings(); + } + + /** + * @deprecated + */ async getAppSettings(): Promise { try { const { @@ -167,7 +193,7 @@ export default class Settings { }, }); return response.data.deleteUserSetting; - } catch (err){ + } catch (err) { return undefined; } } diff --git a/src/queries/settings.query.ts b/src/queries/settings.query.ts index e03679b3..696bc220 100644 --- a/src/queries/settings.query.ts +++ b/src/queries/settings.query.ts @@ -46,3 +46,9 @@ export const APP_SETTINGS_QUERY = gql` } `; +export const APP_SETTING_QUERY = gql` + query adminAppSetting($key: String!) { + adminAppSetting(key: $key) + } +`; + diff --git a/src/types/settings.ts b/src/types/settings.ts index 23912f6b..a263cfbc 100644 --- a/src/types/settings.ts +++ b/src/types/settings.ts @@ -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[]; +} diff --git a/test/customFields.test.ts b/test/customFields.test.ts new file mode 100644 index 00000000..dcf7da21 --- /dev/null +++ b/test/customFields.test.ts @@ -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(); + }); +}); \ No newline at end of file diff --git a/test/setupClient.ts b/test/setupClient.ts index 6ba561a8..44e96013 100644 --- a/test/setupClient.ts +++ b/test/setupClient.ts @@ -5,7 +5,7 @@ dotenv.config(); let client: KanvasCore; -export const initializeClient = async () => { +export const initializeClient = async (adminKey?: string) => { const getKey = async (): Promise => { return localStorage.getItem("token") || null; // wherever you have saved the user token }; @@ -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)], });