From b0db0269cb33382218734b5c7352a2313ff0d740 Mon Sep 17 00:00:00 2001 From: pranavsinghal Date: Fri, 19 Apr 2024 16:14:39 +0530 Subject: [PATCH] added a function to cancel a service --- .../src/services/__tests__/services.spec.ts | 22 +++++++++++++- packages/client/src/services/index.ts | 29 +++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/packages/client/src/services/__tests__/services.spec.ts b/packages/client/src/services/__tests__/services.spec.ts index 7d2b353..791697a 100644 --- a/packages/client/src/services/__tests__/services.spec.ts +++ b/packages/client/src/services/__tests__/services.spec.ts @@ -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)); @@ -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 diff --git a/packages/client/src/services/index.ts b/packages/client/src/services/index.ts index 5327fa9..1144c10 100644 --- a/packages/client/src/services/index.ts +++ b/packages/client/src/services/index.ts @@ -1,3 +1,4 @@ +import { Hash } from 'viem'; import GraphQLClient from '../graphql'; import IPFSClient from '../ipfs'; import { Logger } from '../logger'; @@ -27,6 +28,7 @@ export interface IService { updloadServiceDataToIpfs(serviceData: ServiceDetails): Promise; getServices(params: IProps): Promise; search(params: IProps): Promise; + cancel(userId: string, serviceId: number): Promise; } /** @@ -168,14 +170,14 @@ export class Service { serviceDetails: ServiceDetails, userId: string, existingServiceId: number, - ): Promise { + ): Promise { const cid = await this.updloadServiceDataToIpfs(serviceDetails); const tx = await this.viemClient.writeContract( 'talentLayerService', 'updateServiceData', - [userId, existingServiceId, cid], + [userId, existingServiceId, cid], ); if (cid && tx) { @@ -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} - A promise that resolves to the transaction hash for the cancelService function call + */ + public async cancel( + userId: string, + serviceId: number + ): Promise { + const tx = await this.viemClient.writeContract( + 'talentLayerService', + 'cancelService', + [userId, serviceId] + ) + + if (tx) { + return tx; + } + + throw new Error('Unable to cancel service'); + } }