Skip to content

Commit

Permalink
added a function to cancel a service
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-singhal committed Apr 19, 2024
1 parent 2427f52 commit b0db026
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
22 changes: 21 additions & 1 deletion packages/client/src/services/__tests__/services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ describe('Service', () => {
const serviceDetails = testServiceDetails;
const userId = testUserId;
const existingServiceId = testServiceId;


// Act
const response = await service.update(serviceDetails, userId, parseInt(existingServiceId));
Expand All @@ -84,6 +83,27 @@ describe('Service', () => {
})
})

describe('cancel', () => {
it('should cancel an existing service', async () => {
// Arrange
const userId = testUserId;
const serviceId = parseInt(testServiceId);

// Act
const response = await service.cancel(userId, serviceId);

// Assert
expect(response).toEqual(testAddress);
expect(mockViemClient.writeContract).toHaveBeenCalledWith(
'talentLayerService',
'cancelService',
[userId, serviceId]
);


})
})

describe('search', () => {
it('should reaturn services based on criteria', async () => {
// Arranage
Expand Down
29 changes: 27 additions & 2 deletions packages/client/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Hash } from 'viem';
import GraphQLClient from '../graphql';
import IPFSClient from '../ipfs';
import { Logger } from '../logger';
Expand Down Expand Up @@ -27,6 +28,7 @@ export interface IService {
updloadServiceDataToIpfs(serviceData: ServiceDetails): Promise<string>;
getServices(params: IProps): Promise<any>;
search(params: IProps): Promise<any>;
cancel(userId: string, serviceId: number): Promise<Hash>;
}

/**
Expand Down Expand Up @@ -168,14 +170,14 @@ export class Service {
serviceDetails: ServiceDetails,
userId: string,
existingServiceId: number,
): Promise<ClientTransactionResponse> {
): Promise<ClientTransactionResponse> {

const cid = await this.updloadServiceDataToIpfs(serviceDetails);

const tx = await this.viemClient.writeContract(
'talentLayerService',
'updateServiceData',
[userId, existingServiceId, cid],
[userId, existingServiceId, cid],
);

if (cid && tx) {
Expand All @@ -184,4 +186,27 @@ export class Service {

throw new Error('Unable to update service');
}

/**
* Cancel an existing service.
* @param {string} userId - Id of the user cancelling the service
* @param {number} serviceId - Id of the service being updated
* @returns {Promise<Hash>} - A promise that resolves to the transaction hash for the cancelService function call
*/
public async cancel(
userId: string,
serviceId: number
): Promise<Hash> {
const tx = await this.viemClient.writeContract(
'talentLayerService',
'cancelService',
[userId, serviceId]
)

if (tx) {
return tx;
}

throw new Error('Unable to cancel service');
}
}

0 comments on commit b0db026

Please sign in to comment.