Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/user methods #8

Merged
merged 18 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
.mesh/
.vscode/
.env
.env
build/
1 change: 1 addition & 0 deletions .meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sources:
- Query.!transaction
- Query.!transactions
- Query.!myTransactions

customFetch: ./custom-fetch.ts

sdk:
Expand Down
3 changes: 3 additions & 0 deletions src/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Organization } from './organization/organization';
import { Auth } from './auth/auth';
import { PDA } from './pda/pda';
import { DataRequestTemplate } from './dataRequestsTemplate/dataRequestsTemplate';
import { User } from './user/user';

export class Gateway {
private sdk: Sdk;
public user: User;
public pda: PDA;
public dataRequestTemplate: DataRequestTemplate;
public organization: Organization;
Expand All @@ -18,6 +20,7 @@ export class Gateway {
token,
});
this.pda = new PDA(this.sdk);
this.user = new User(this.sdk);
this.dataRequestTemplate = new DataRequestTemplate(this.sdk);
this.organization = new Organization(this.sdk);
this.auth = new Auth(this.sdk);
Expand Down
2 changes: 1 addition & 1 deletion src/dataRequestsTemplate/dataRequestsTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class DataRequestTemplate {
order?: JSON;
skip?: number;
take?: number;
}) {
} = {}) {
try {
return await this.sdk.dataRequestTemplates_query({
filter,
Expand Down
2 changes: 1 addition & 1 deletion src/organization/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class Organization {
filter?: FilterOrganizationInput;
skip?: number;
take?: number;
}) {
} = {}) {
try {
return await this.sdk.organizations_query({ filter, skip, take });
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/pda/pda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class PDA {
* @param {PDAFilter} - - `filter`: An object that contains filter criteria for the query.
* @returns a Promise that resolves to a value of type PDAs_queryQuery.
*/
async getPDAs({ filter, order, skip, take }: PDAFilter) {
async getPDAs({ filter, order, skip, take }: PDAFilter = {}) {
try {
return await this.sdk.PDAs_query({ filter, order, skip, take });
} catch (error) {
Expand All @@ -66,7 +66,7 @@ export class PDA {
* used to specify conditions that the returned PDAs must meet.
* @returns a Promise that resolves to an object of type `issuedPDAs_queryQuery`.
*/
async getIssuedPDAs({ filter, order, skip, take }: PDAFilter) {
async getIssuedPDAs({ filter, order, skip, take }: PDAFilter = {}) {
try {
return await this.sdk.issuedPDAs_query({ filter, order, skip, take });
} catch (error) {
Expand Down
196 changes: 196 additions & 0 deletions src/user/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import {
FilterDataModelInput,
FilterDataRequestTemplateInput,
FilterPDAInput,
Sdk,
UpdateUserInput,
} from '../../.mesh';
import { PDAFilter, UserIdentifierType } from '../types';
import { errorHandler } from '../utils/errorHandler';

export class User {
private sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
}

/**
* The function `me` makes an asynchronous call to `me_query` and returns the result, or throws an
* error if something goes wrong.
* @returns a Promise that resolves to me.
*/
async me() {
try {
return await this.sdk.me_query();
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function takes a user identifier type and value as input, queries the user using the SDK, and
* returns the result.
* @param - - `type`: The type of user identifier. It can be one of the following values:
* @returns The `user` function is returning the result of the `user_query` method call from the `sdk`
* object.
*/
async getSingleUser({
type,
value,
}: {
type: UserIdentifierType;
value: string;
}) {
try {
return await this.sdk.user_query({ input: { type, value } });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `myPDACount` is an asynchronous function that returns the count of a user's PDA
* based on an optional filter.
* @param {FilterPDAInput} [filter] - The `filter` parameter is an optional input that allows you to
* specify criteria for filtering the PDAs before counting them. It is
* of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async myPDACount(filter?: FilterPDAInput) {
try {
return (await this.sdk.myPDACount_query({ filter })).myPDACount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `myPDAs` is an asynchronous function that takes in a `PDAFilter` object and returns a
* promise that resolves to a `myPDAs_queryQuery` object.
* @param {PDAFilter} - - `filter`: An object that contains filter criteria for the query.
* @returns a Promise that resolves to a value of type `myPDAs_queryQuery`.
*/
async myPDAs({ filter, order, skip, take }: PDAFilter = {}) {
try {
return await this.sdk.myPDAs_query({ filter, order, skip, take });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `myDataModelsCount` is an asynchronous function that retrieves the count of data
* models based on an optional filter and returns the count.
* @param {FilterDataModelInput} [filter] - The `filter` parameter is an optional input that allows
* you to specify conditions to filter the data models. It is of type `FilterDataModelInput`. You can
* use this parameter to define criteria such as filtering by a specific field value or applying
* logical operators like AND and OR to combine multiple conditions.
* @returns the count of data models that match the provided filter.
*/
async myDataModelsCount(filter?: FilterDataModelInput) {
try {
return (await this.sdk.dataModelsCount_query({ filter })).dataModelsCount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `myDataRequestTemplatesCount` is an asynchronous function that retrieves the count of
* data request templates based on an optional filter and returns the count.
* @param {FilterDataRequestTemplateInput} [filter] - The `filter` parameter is an optional input
* that allows you to specify criteria for filtering the data request templates. It is of type
* `FilterDataRequestTemplateInput`. You can use this parameter to narrow down the results based on
* specific conditions such as template name, creator, or any other relevant attributes.
* @returns the count of myDataRequestTemplates that match the provided filter.
*/
async myDataRequestTemplatesCount(filter?: FilterDataRequestTemplateInput) {
try {
return (await this.sdk.myDataRequestTemplatesCount_query({ filter }))
.myDataRequestTemplatesCount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `updateUser` updates a user's information and returns the updated user.
* @param {UpdateUserInput} updatedUser - The `updatedUser` parameter is an object of type
* `UpdateUserInput`. It contains the data that will be used to update a user. The specific
* properties and their types within the `UpdateUserInput` object will depend on the requirements of
* your application.
* @returns The updateUser function is returning the result of the updateUser_mutation API call.
*/
async updateUser(updatedUser: UpdateUserInput) {
try {
return await this.sdk.updateUser_mutation({ input: updatedUser });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function updates the display name of the user using a mutation and returns the result, or
* throws an error if something goes wrong.
* @param {string} displayName - The `displayName` parameter is a string that represents the new
* display name that you want to update.
* @returns the result of the `updateMyDisplayName_mutation` method call, which is likely a Promise
* that resolves to the updated display name.
*/
async updateMyDisplayName(displayName: string) {
try {
return await this.sdk.updateMyDisplayName_mutation({ displayName });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `updateMyGatewayId` updates the gateway ID using a mutation and returns the result,
* or throws an error if something goes wrong.
* @param {string} gatewayId - The `gatewayId` parameter is a string that represents the ID of a
* gateway.
* @returns the result of the `updateMyGatewayId_mutation` method call, which is awaited using the
* `await` keyword.
*/
async updateMyGatewayId(gatewayId: string) {
try {
return await this.sdk.updateMyGatewayId_mutation({ gatewayId });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function updates the user's profile picture by making a mutation request to the SDK.
* @param {string} profilePictureUrl - The `profilePictureUrl` parameter is a string that represents
* the URL of the new profile picture that you want to update.
* @returns the result of the `updateMyProfilePicture_mutation` mutation.
*/
async updateMyProfilePicture(profilePictureUrl: string) {
try {
return await this.sdk.updateMyProfilePicture_mutation({
profilePictureUrl,
});
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `updateNotificationEmail` updates the notification email by making a mutation request
* to the SDK and returns the result, or throws an error if something goes wrong.
* @param {string} email - The email parameter is a string that represents the new notification email
* that needs to be updated.
* @returns the result of the `updateNotificationEmail_mutation` method call.
*/
async updateNotificationEmail(email: string) {
try {
return (await this.sdk.updateNotificationEmail_mutation({ email }))
.updateNotificationEmail;
} catch (error) {
throw new Error(errorHandler(error));
}
}
}
93 changes: 93 additions & 0 deletions test/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import dotenv from 'dotenv';
import { Gateway } from '../src/Gateway';
import { 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('USER Testing', () => {
it(
'me',
async () => {
const { me } = await api.user.me();
expect(me.gatewayId).toEqual('sid');
},
DEFAULT_TIMEOUT,
);

it(
'single user',
async () => {
const { user } = await api.user.getSingleUser({
type: UserIdentifierType.GATEWAY_ID,
value: 'sid',
});
expect(user?.gatewayId).toEqual('sid');
},
DEFAULT_TIMEOUT,
);

it(
'my pdas count',
async () => {
const count = await api.user.myPDACount({});
expect(count).toBeGreaterThanOrEqual(0);
},
DEFAULT_TIMEOUT,
);

it(
'my pdas',
async () => {
const { myPDAs } = await api.user.myPDAs({
skip: 0,
take: 10,
});
expect(myPDAs.length).toBeGreaterThanOrEqual(0);
},
DEFAULT_TIMEOUT,
);

it(
'my data models count',
async () => {
const count = await api.user.myDataModelsCount();
expect(count).toBeGreaterThanOrEqual(0);
},
DEFAULT_TIMEOUT,
);

it(
'my data requests template count',
async () => {
const count = await api.user.myDataRequestTemplatesCount();
expect(count).toBeGreaterThanOrEqual(0);
},
DEFAULT_TIMEOUT,
);

it('update user', async () => {
const { updateUser } = await api.user.updateUser({
displayName: 'siddharth9890',
});
expect(updateUser.displayName).toEqual('siddharth9890');
});

it('update profile picture', async () => {
const { updateMyProfilePicture } = await api.user.updateMyProfilePicture(
'https://www.tryodyssey.xyz/images/campaigns/lifi/odyssey_lifi.png',
);
expect(updateMyProfilePicture).toEqual(
'https://www.tryodyssey.xyz/images/campaigns/lifi/odyssey_lifi.png',
);
});
});