diff --git a/apps/api/src/shares/shares.controller.ts b/apps/api/src/shares/shares.controller.ts index 63f95b5..cbf2dc2 100644 --- a/apps/api/src/shares/shares.controller.ts +++ b/apps/api/src/shares/shares.controller.ts @@ -74,4 +74,19 @@ export class SharesController { userId, }); } + + @Get('transactions/find/:sharesId') + @ApiOperation({ + summary: 'Find Bitsacco shares transaction with given ID', + }) + @ApiParam({ + name: 'sharesId', + type: 'string', + description: 'Share Transaction ID', + }) + findSharesTransaction(@Param('sharesId') sharesId: string) { + return this.sharesService.findSharesTransaction({ + sharesId, + }); + } } diff --git a/apps/api/src/shares/shares.service.ts b/apps/api/src/shares/shares.service.ts index 46262a9..d5431af 100644 --- a/apps/api/src/shares/shares.service.ts +++ b/apps/api/src/shares/shares.service.ts @@ -7,6 +7,7 @@ import { TransferSharesDto, UserSharesDto, UpdateSharesDto, + FindSharesTxDto, } from '@bitsacco/common'; import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; import { type ClientGrpc } from '@nestjs/microservices'; @@ -49,4 +50,8 @@ export class SharesService implements OnModuleInit { allSharesTransactions(req: Empty) { return this.client.allSharesTransactions(req); } + + findSharesTransaction(req: FindSharesTxDto) { + return this.client.findSharesTransaction(req); + } } diff --git a/apps/shares/src/shares.controller.ts b/apps/shares/src/shares.controller.ts index 3ef0374..686c173 100644 --- a/apps/shares/src/shares.controller.ts +++ b/apps/shares/src/shares.controller.ts @@ -2,6 +2,7 @@ import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; import { type Empty, + FindSharesTxDto, OfferSharesDto, SharesServiceControllerMethods, SubscribeSharesDto, @@ -50,4 +51,9 @@ export class SharesController { allSharesTransactions(_: Empty) { return this.sharesService.allSharesTransactions(); } + + @GrpcMethod() + findSharesTransaction(request: FindSharesTxDto) { + return this.sharesService.findSharesTransaction(request); + } } diff --git a/apps/shares/src/shares.service.ts b/apps/shares/src/shares.service.ts index 0dca9c6..6378bc2 100644 --- a/apps/shares/src/shares.service.ts +++ b/apps/shares/src/shares.service.ts @@ -2,8 +2,10 @@ import { Injectable, Logger } from '@nestjs/common'; import { AllSharesOffers, AllSharesTxsResponse, + FindShareTxDto, OfferSharesDto, SharesOffer, + SharesTx, SharesTxStatus, SubscribeSharesDto, TransferSharesDto, @@ -129,11 +131,13 @@ export class SharesService { const originShares = await this.shares.findOne({ _id: sharesId }); const { quantity, status, transfer, offerId } = updates; + this.logger.log(`Updates : ${JSON.stringify(updates)}`); + let { userId } = await this.shares.findOneAndUpdate( { _id: sharesId }, { - quantity: quantity ?? originShares.quantity, - status: status ?? originShares.status, + quantity: quantity !== undefined ? quantity : originShares.quantity, + status: status !== undefined ? status : originShares.status, transfer: transfer ?? originShares.transfer, offerId: offerId ?? originShares.offerId, updatedAt: Date.now(), @@ -177,4 +181,14 @@ export class SharesService { offers, }; } + + async findSharesTransaction({ sharesId }: FindShareTxDto): Promise { + const shares = toSharesTx(await this.shares.findOne({ _id: sharesId })); + + if (!shares) { + throw new Error('Shares transaction not found'); + } + + return shares; + } } diff --git a/libs/common/src/dto/shares.dto.ts b/libs/common/src/dto/shares.dto.ts index 088d967..40e1bec 100644 --- a/libs/common/src/dto/shares.dto.ts +++ b/libs/common/src/dto/shares.dto.ts @@ -11,6 +11,7 @@ import { IsNotEmptyObject, } from 'class-validator'; import { + FindShareTxRequest, OfferSharesRequest, SharesTxStatus, type SharesTxTransferMeta, @@ -161,3 +162,11 @@ export class UserSharesDto implements UserSharesTxsRequest { @ApiProperty({ example: '7b158dfd-cb98-40b1-9ed2-a13006a9f670' }) userId: string; } + +export class FindSharesTxDto implements FindShareTxRequest { + @IsNotEmpty() + @IsString() + @Type(() => String) + @ApiProperty({ example: '3e158dfd-cb98-40b1-9ed2-a13006a9f430' }) + sharesId: string; +} diff --git a/libs/common/src/types/proto/shares.ts b/libs/common/src/types/proto/shares.ts index 5594942..a702b4e 100644 --- a/libs/common/src/types/proto/shares.ts +++ b/libs/common/src/types/proto/shares.ts @@ -111,6 +111,10 @@ export interface AllSharesTxsResponse { offers: AllSharesOffers | undefined; } +export interface FindShareTxRequest { + sharesId: string; +} + export interface SharesServiceClient { offerShares(request: OfferSharesRequest): Observable; @@ -131,6 +135,8 @@ export interface SharesServiceClient { ): Observable; allSharesTransactions(request: Empty): Observable; + + findSharesTransaction(request: FindShareTxRequest): Observable; } export interface SharesServiceController { @@ -176,6 +182,10 @@ export interface SharesServiceController { | Promise | Observable | AllSharesTxsResponse; + + findSharesTransaction( + request: FindShareTxRequest, + ): Promise | Observable | SharesTx; } export function SharesServiceControllerMethods() { @@ -188,6 +198,7 @@ export function SharesServiceControllerMethods() { 'updateShares', 'userSharesTransactions', 'allSharesTransactions', + 'findSharesTransaction', ]; for (const method of grpcMethods) { const descriptor: any = Reflect.getOwnPropertyDescriptor( diff --git a/proto/shares.proto b/proto/shares.proto index 5454f51..7ab6e60 100644 --- a/proto/shares.proto +++ b/proto/shares.proto @@ -12,6 +12,7 @@ service SharesService { rpc UpdateShares(UpdateSharesRequest) returns (UserShareTxsResponse); rpc UserSharesTransactions(UserSharesTxsRequest) returns (UserShareTxsResponse); rpc AllSharesTransactions(lib.Empty) returns (AllSharesTxsResponse); + rpc FindSharesTransaction(FindShareTxRequest) returns (SharesTx); } message OfferSharesRequest { @@ -132,3 +133,7 @@ message AllSharesTxsResponse { repeated SharesTx shares = 1; AllSharesOffers offers = 2; } + +message FindShareTxRequest { + string shares_id = 1; +}