diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a0e0000..0ad33f0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,4 +1,4 @@ -name: Build PR +name: Run Eslint & Test cases on: pull_request: @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout Project - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Install pnpm run: npm install -g pnpm@7.33.5 - name: Install Project Dependencies @@ -20,3 +20,7 @@ jobs: run: pnpm mesh build - name: Test project run: pnpm test + env: + DATAMODEL_ID: ${{ secrets.DATAMODEL_ID }} + BEARER_TOKEN: ${{ secrets.BEARER_TOKEN }} + API_KEY: ${{ secrets.API_KEY }} diff --git a/.gitignore b/.gitignore index 2549b93..e5264a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules/ -.mesh/ \ No newline at end of file +.mesh/ +.env +.vscode \ No newline at end of file diff --git a/.meshrc.yml b/.meshrc.yml index 91025df..8a729dd 100644 --- a/.meshrc.yml +++ b/.meshrc.yml @@ -5,10 +5,11 @@ sources: endpoint: https://develop.protocol.mygateway.xyz/v1/graphql transforms: - filterSchema: - mode: bare + mode: bare filters: - - Query.!transaction - - Query.!transactions + - Query.!transaction + - Query.!transactions + - Query.!myTransactions customFetch: ./custom-fetch.ts sdk: diff --git a/src/Gateway.ts b/src/Gateway.ts index 3194e73..f0b27e7 100644 --- a/src/Gateway.ts +++ b/src/Gateway.ts @@ -1,7 +1,9 @@ import { getMeshSDK, Sdk } from '../.mesh'; +import { Organization } from './organization/organization'; export class Gateway { private sdk: Sdk; + public organization: Organization; constructor({ apiKey, token }: { apiKey: string; token: string }) { if (!apiKey && !token) throw new Error('No token found'); @@ -9,5 +11,6 @@ export class Gateway { apiKey, token, }); + this.organization = new Organization(this.sdk); } } diff --git a/src/organization/organization.ts b/src/organization/organization.ts index cdc9f28..3daec2e 100644 --- a/src/organization/organization.ts +++ b/src/organization/organization.ts @@ -1,4 +1,13 @@ -import { CreateOrganizationInput, Sdk, TransferMemberInput } from '../../.mesh'; +import { + CreateOrganizationInput, + FilterOrganizationInput, + MemberInput, + Sdk, + TransferMemberInput, + UpdateOrganizationInput, +} from '../../.mesh'; +import { OrganizationIdentifierType } from '../types'; +import { errorHandler } from '../utils/errorHandler'; export class Organization { private sdk: Sdk; @@ -8,16 +17,74 @@ export class Organization { } async createOrganization(organizationInput: CreateOrganizationInput) { - return await this.sdk.createOrganization_mutation({ - input: organizationInput, - }); + try { + return await this.sdk.createOrganization_mutation({ + input: organizationInput, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + async addMemberToOrganization(memberInput: MemberInput) { + try { + return await this.sdk.addMemberToOrganization_mutation({ + input: memberInput, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + async changeMemberRole(memberInput: MemberInput) { + try { + return await this.sdk.changeMemberRole_mutation({ input: memberInput }); + } catch (error) { + throw new Error(errorHandler(error)); + } } async removeMemberFromOrganization(memberInput: TransferMemberInput) { - return await this.sdk.removeMemberFromOrganization_mutation({ - input: memberInput, - }); + try { + return await this.sdk.removeMemberFromOrganization_mutation({ + input: memberInput, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } } - + async updateOrganization(updatedOrganization: UpdateOrganizationInput) { + try { + return await this.sdk.updateOrganization_mutation({ + input: updatedOrganization, + }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + async getOrganization(type: OrganizationIdentifierType, value: string) { + try { + return await this.sdk.organization_query({ input: { type, value } }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } + + async getOrganizations({ + filter, + skip, + take, + }: { + filter?: FilterOrganizationInput; + skip?: number; + take?: number; + }) { + try { + return await this.sdk.organizations_query({ filter, skip, take }); + } catch (error) { + throw new Error(errorHandler(error)); + } + } } diff --git a/src/types.ts b/src/types.ts index 9c5c601..071469c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,3 +5,14 @@ export enum UserIdentifierType { SOLANA = 'SOLANA', USER_ID = 'USER_ID', } + +export enum OrganizationIdentifierType { + GATEWAY_ID = 'GATEWAY_ID', + ORG_ID = 'ORG_ID', +} + +export enum OrganizationRole { + Admin = 'Admin', + Member = 'Member', + Owner = 'Owner', +} diff --git a/src/utils/errorHandler.ts b/src/utils/errorHandler.ts new file mode 100644 index 0000000..66a43dc --- /dev/null +++ b/src/utils/errorHandler.ts @@ -0,0 +1,7 @@ +export const errorHandler = (error: any): string => { + if (typeof error === 'object' && error !== null && 'message' in error) { + return error.message; + } else { + return 'Something went wrong!'; + } +}; diff --git a/test/organization.test.ts b/test/organization.test.ts new file mode 100644 index 0000000..611893b --- /dev/null +++ b/test/organization.test.ts @@ -0,0 +1,86 @@ +import dotenv from 'dotenv'; +import { Gateway } from '../src/Gateway'; +import { + OrganizationIdentifierType, + OrganizationRole, + UserIdentifierType, +} from '../src/types'; +dotenv.config(); +const DEFAULT_TIMEOUT = 10000; + +let api: Gateway; + +beforeAll(() => { + api = new Gateway({ + apiKey: process.env.API_KEY!, + token: process.env.BEARER_TOKEN!, + }); +}); + +describe('ORGANIZATION SERVICE TESTING', () => { + it( + 'organization crud', + async () => { + let obj = { + username: 'test_for_sdk_2', + name: 'test org sdk 2', + description: 'test organization', + }; + const { createOrganization } = + await api.organization.createOrganization(obj); + const { organization } = await api.organization.getOrganization( + OrganizationIdentifierType.ORG_ID, + createOrganization.id, + ); + expect(organization?.id).toEqual(createOrganization.id); + let updatedOrgObj = { + description: 'changed description for organization', + id: createOrganization.id, + }; + const { updateOrganization } = + await api.organization.updateOrganization(updatedOrgObj); + expect(updateOrganization.description).toEqual(updatedOrgObj.description); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'member crud organization', + async () => { + let addMemberObj = { + organization: { + type: OrganizationIdentifierType.ORG_ID, + value: process.env.ORGAINZATION_ID!, + }, + user: { type: UserIdentifierType.GATEWAY_ID, value: 'testing_sdk' }, + }; + let changeMemberRoleObj = { + ...addMemberObj, + role: OrganizationRole.Admin, + }; + const { addMemberToOrganization } = + await api.organization.addMemberToOrganization(addMemberObj); + expect(addMemberToOrganization).toBeDefined(); + const { changeMemberRole } = + await api.organization.changeMemberRole(changeMemberRoleObj); + expect(changeMemberRole).toBeDefined(); + const { removeMemberFromOrganization } = + await api.organization.removeMemberFromOrganization(addMemberObj); + expect(removeMemberFromOrganization).toBeDefined(); + }, + DEFAULT_TIMEOUT, + ); + + it( + 'organizations', + async () => { + const { organizations } = await api.organization.getOrganizations({ + filter: { verified: false }, + skip: 0, + take: 10, + }); + expect(organizations.length).toBeGreaterThanOrEqual(0); + }, + DEFAULT_TIMEOUT, + ); +});