Skip to content

Commit

Permalink
feat: find bitsacco shares transaction using id
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Jan 2, 2025
1 parent be5390e commit 8d7074a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
15 changes: 15 additions & 0 deletions apps/api/src/shares/shares.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
5 changes: 5 additions & 0 deletions apps/api/src/shares/shares.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions apps/shares/src/shares.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
import {
type Empty,
FindSharesTxDto,
OfferSharesDto,
SharesServiceControllerMethods,
SubscribeSharesDto,
Expand Down Expand Up @@ -50,4 +51,9 @@ export class SharesController {
allSharesTransactions(_: Empty) {
return this.sharesService.allSharesTransactions();
}

@GrpcMethod()
findSharesTransaction(request: FindSharesTxDto) {
return this.sharesService.findSharesTransaction(request);
}
}
20 changes: 18 additions & 2 deletions apps/shares/src/shares.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Injectable, Logger } from '@nestjs/common';
import {
AllSharesOffers,
AllSharesTxsResponse,
FindSharesTxDto,
OfferSharesDto,
SharesOffer,
SharesTx,
SharesTxStatus,
SubscribeSharesDto,
TransferSharesDto,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -177,4 +181,16 @@ export class SharesService {
offers,
};
}

async findSharesTransaction({
sharesId,
}: FindSharesTxDto): Promise<SharesTx> {
const shares = toSharesTx(await this.shares.findOne({ _id: sharesId }));

if (!shares) {
throw new Error('Shares transaction not found');
}

return shares;
}
}
9 changes: 9 additions & 0 deletions libs/common/src/dto/shares.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
IsNotEmptyObject,
} from 'class-validator';
import {
FindShareTxRequest,
OfferSharesRequest,
SharesTxStatus,
type SharesTxTransferMeta,
Expand Down Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions libs/common/src/types/proto/shares.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions proto/shares.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -132,3 +133,7 @@ message AllSharesTxsResponse {
repeated SharesTx shares = 1;
AllSharesOffers offers = 2;
}

message FindShareTxRequest {
string shares_id = 1;
}

0 comments on commit 8d7074a

Please sign in to comment.