Skip to content

Commit b81a6ee

Browse files
committed
Reverted a few from app/api/src/transactions
Signed-off-by: Eric Le Ponner <eric.leponner@icloud.com>
1 parent be46b47 commit b81a6ee

4 files changed

Lines changed: 35 additions & 39 deletions

File tree

back-end/apps/api/src/transactions/approvers/approvers.service.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ export class ApproversService {
6464
) {}
6565

6666
/* Get the approver by id */
67-
async getTransactionApproverById(
67+
getTransactionApproverById(
6868
id: number,
6969
entityManager?: EntityManager,
70-
): Promise<TransactionApprover | null> {
70+
): Promise<TransactionApprover> {
7171
if (!id) return null;
7272

7373
const find: FindOneOptions<TransactionApprover> = {
@@ -90,7 +90,7 @@ export class ApproversService {
9090
userId?: number,
9191
entityManager?: EntityManager,
9292
): Promise<TransactionApprover[]> {
93-
if (typeof transactionId !== 'number' || (userId && typeof userId !== 'number')) return [];
93+
if (typeof transactionId !== 'number' || (userId && typeof userId !== 'number')) return null;
9494

9595
return (entityManager || this.repo).query(
9696
`
@@ -145,8 +145,8 @@ export class ApproversService {
145145
if (
146146
userKeysToSign.length === 0 &&
147147
transaction.creatorKey?.userId !== user.id &&
148-
!(transaction.observers && transaction.observers.some(o => o.userId === user.id)) &&
149-
!(transaction.signers && transaction.signers.some(s => s.userKey?.userId === user.id)) &&
148+
!transaction.observers.some(o => o.userId === user.id) &&
149+
!transaction.signers.some(s => s.userKey?.userId === user.id) &&
150150
!approvers.some(a => a.userId === user.id)
151151
)
152152
throw new UnauthorizedException("You don't have permission to view this transaction");
@@ -206,7 +206,7 @@ export class ApproversService {
206206

207207
/* Soft deletes approvers' tree */
208208
async removeNode(listId: number): Promise<void> {
209-
if (!listId || typeof listId !== 'number') return;
209+
if (!listId || typeof listId !== 'number') return null;
210210

211211
await this.repo.query(
212212
`
@@ -291,7 +291,7 @@ export class ApproversService {
291291
if (
292292
dtoApprover.approvers &&
293293
dtoApprover.approvers.length > 0 &&
294-
(dtoApprover.threshold === undefined || isNaN(dtoApprover.threshold))
294+
(dtoApprover.threshold === null || isNaN(dtoApprover.threshold))
295295
)
296296
throw new Error(this.THRESHOLD_REQUIRED);
297297

@@ -305,17 +305,16 @@ export class ApproversService {
305305
/* Check if the approver threshold is less or equal to the number of approvers */
306306
if (
307307
dtoApprover.approvers &&
308-
dtoApprover.threshold !== undefined &&
309308
(dtoApprover.threshold > dtoApprover.approvers.length || dtoApprover.threshold === 0)
310309
)
311310
throw new Error(this.THRESHOLD_LESS_OR_EQUAL_APPROVERS(dtoApprover.approvers.length));
312311

313312
const data: DeepPartial<TransactionApprover> = {
314313
transactionId:
315-
dtoApprover.listId === undefined || isNaN(dtoApprover.listId) ? transactionId : undefined,
314+
dtoApprover.listId === null || isNaN(dtoApprover.listId) ? transactionId : null,
316315
listId: dtoApprover.listId,
317316
threshold:
318-
dtoApprover.threshold && dtoApprover.approvers ? dtoApprover.threshold : undefined,
317+
dtoApprover.threshold && dtoApprover.approvers ? dtoApprover.threshold : null,
319318
userId: dtoApprover.userId,
320319
};
321320

@@ -346,7 +345,7 @@ export class ApproversService {
346345
const nestedApprover = { ...nestedDtoApprover, listId: approver.id };
347346

348347
if (!nestedDtoApprover.approvers || nestedDtoApprover.approvers.length === 0) {
349-
nestedApprover.threshold = undefined;
348+
nestedApprover.threshold = null;
350349
}
351350

352351
await createApprover({ ...nestedDtoApprover, listId: approver.id });
@@ -429,7 +428,7 @@ export class ApproversService {
429428
/* Soft delete the parent if there are no more children */
430429
if (newParentApproversLength === 0) {
431430
await transactionalEntityManager.softRemove(TransactionApprover, parent);
432-
} else if (parent.threshold != null && newParentApproversLength < parent.threshold) {
431+
} else if (newParentApproversLength < parent.threshold) {
433432
/* Update the parent threshold if the current one is more than the children */
434433
await transactionalEntityManager.update(TransactionApprover, parent.id, {
435434
threshold: newParentApproversLength,
@@ -468,10 +467,10 @@ export class ApproversService {
468467
/* Update the list id and sets the transaction id to null */
469468
await transactionalEntityManager.update(TransactionApprover, approver.id, {
470469
listId: dto.listId,
471-
transactionId: undefined,
470+
transactionId: null,
472471
});
473472
approver.listId = dto.listId;
474-
approver.transactionId = undefined;
473+
approver.transactionId = null;
475474
updated = true;
476475

477476
return approver;
@@ -552,8 +551,7 @@ export class ApproversService {
552551

553552
const result = await this.removeNode(approver.id);
554553

555-
const dtos = approver.transactionId !== undefined ? [{ entityId: approver.transactionId }] : []
556-
await emitTransactionStatusUpdate(this.notificationsPublisher, dtos);
554+
await emitTransactionStatusUpdate(this.notificationsPublisher, [{ entityId: approver.transactionId }]);
557555

558556
return result;
559557
}
@@ -584,7 +582,7 @@ export class ApproversService {
584582
const signatureKey = user.keys.find(key => key.id === dto.userKeyId);
585583

586584
/* Gets the public key that the signature belongs to */
587-
const publicKey = signatureKey ? PublicKey.fromString(signatureKey.publicKey) : undefined;
585+
const publicKey = PublicKey.fromString(signatureKey?.publicKey);
588586

589587
/* Get the transaction body */
590588
const transaction = await this.dataSource.manager.findOne(Transaction, {
@@ -606,7 +604,6 @@ export class ApproversService {
606604

607605
/* Verify the signature matches the transaction */
608606
if (
609-
!publicKey ||
610607
!verifyTransactionBodyWithoutNodeAccountIdSignature(sdkTransaction, dto.signature, publicKey)
611608
)
612609
throw new BadRequestException(ErrorCodes.SNMP);
@@ -671,13 +668,13 @@ export class ApproversService {
671668
) {
672669
const find: FindManyOptions<TransactionApprover> = {
673670
where: {
674-
listId: typeof approver.listId === 'number' ? approver.listId : undefined,
675-
userId: typeof approver.userId === 'number' ? approver.userId : undefined,
671+
listId: typeof approver.listId === 'number' ? approver.listId : null,
672+
userId: typeof approver.userId === 'number' ? approver.userId : null,
676673
threshold:
677674
typeof approver.threshold === 'number' && approver.threshold !== 0
678675
? approver.threshold
679-
: undefined,
680-
transactionId: typeof approver.listId === 'number' ? undefined : transactionId,
676+
: null,
677+
transactionId: typeof approver.listId === 'number' ? null : transactionId,
681678
},
682679
};
683680

@@ -709,9 +706,9 @@ export class ApproversService {
709706
/* Validates the approver DTO */
710707
private validateApprover(approver: CreateTransactionApproverDto): void {
711708
if (
712-
(approver.listId === null || !approver.listId || isNaN(approver.listId)) &&
713-
(approver.threshold === null || !approver.threshold || isNaN(approver.threshold) || approver.threshold === 0) &&
714-
(approver.userId === null || !approver.userId || isNaN(approver.userId)) &&
709+
(approver.listId === null || isNaN(approver.listId)) &&
710+
(approver.threshold === null || isNaN(approver.threshold) || approver.threshold === 0) &&
711+
(approver.userId === null || isNaN(approver.userId)) &&
715712
(!approver.approvers || approver.approvers.length === 0)
716713
)
717714
throw new BadRequestException(this.CANNOT_CREATE_EMPTY_APPROVER);

back-end/apps/api/src/transactions/observers/observers.service.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ export class ObserversService {
4747

4848
const observers: TransactionObserver[] = [];
4949

50-
const allObservers = transaction.observers ?? [];
5150
for (const userId of dto.userIds) {
52-
if (!allObservers.some(o => o.userId === userId)) {
51+
if (!transaction.observers.some(o => o.userId === userId)) {
5352
const observer = this.repo.create({ userId, transactionId, role: Role.FULL });
5453
observers.push(observer);
5554
}
@@ -101,18 +100,18 @@ export class ObserversService {
101100
const approvers = await this.approversService.getApproversByTransactionId(transaction.id);
102101

103102
if ([TransactionStatus.EXECUTED, TransactionStatus.FAILED].includes(transaction.status))
104-
return transaction.observers ?? [];
103+
return transaction.observers;
105104

106105
if (
107106
userKeysToSign.length === 0 &&
108107
transaction.creatorKey?.userId !== user.id &&
109-
!transaction.observers?.some(o => o.userId === user.id) &&
110-
!transaction.signers?.some(s => s.userKey?.userId === user.id) &&
108+
!transaction.observers.some(o => o.userId === user.id) &&
109+
!transaction.signers.some(s => s.userKey?.userId === user.id) &&
111110
!approvers.some(a => a.userId === user.id)
112111
)
113112
throw new UnauthorizedException("You don't have permission to view this transaction");
114113

115-
return transaction.observers ?? [];
114+
return transaction.observers;
116115
}
117116

118117
/* Update a transaction observer with the data provided for the given observer id. */

back-end/apps/api/src/transactions/signers/signers.service.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class SignersService {
3434
) {}
3535

3636
/* Get the signature for the given signature id */
37-
async getSignatureById(id: number): Promise<TransactionSigner | null> {
37+
getSignatureById(id: number): Promise<TransactionSigner | null> {
3838
if (!id) {
3939
return null;
4040
}
@@ -50,6 +50,8 @@ export class SignersService {
5050
{ limit, offset, page, size }: Pagination,
5151
withDeleted: boolean = false,
5252
): Promise<PaginatedResourceDto<TransactionSigner>> {
53+
if (!user) return null;
54+
5355
const [items, totalItems] = await this.repo.findAndCount({
5456
where: {
5557
userId: user.id,
@@ -77,10 +79,13 @@ export class SignersService {
7779
}
7880

7981
/* Get the signatures for the given transaction id */
80-
async getSignaturesByTransactionId(
82+
getSignaturesByTransactionId(
8183
transactionId: number,
8284
withDeleted: boolean = false,
8385
): Promise<TransactionSigner[]> {
86+
if (!transactionId) {
87+
return null;
88+
}
8489
return this.repo.find({
8590
where: {
8691
transaction: {
@@ -149,7 +154,7 @@ export class SignersService {
149154
if (!signersByTransaction.has(signer.transactionId)) {
150155
signersByTransaction.set(signer.transactionId, new Set());
151156
}
152-
signersByTransaction.get(signer.transactionId)!.add(signer.userKeyId);
157+
signersByTransaction.get(signer.transactionId).add(signer.userKeyId);
153158
}
154159

155160
return { transactionMap, signersByTransaction };

back-end/apps/api/src/transactions/transactions.controller.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
BadRequestException,
32
Body,
43
Controller,
54
Get,
@@ -22,7 +21,6 @@ import { TransactionId } from '@hiero-ledger/sdk';
2221
import * as semver from 'semver';
2322

2423
import {
25-
ErrorCodes,
2624
Filtering,
2725
FilteringParams,
2826
OnlyOwnerKey,
@@ -197,9 +195,6 @@ export class TransactionsController {
197195
@Param('transactionId', ParseIntPipe) transactionId: number,
198196
): Promise<number[]> {
199197
const transaction = await this.transactionsService.getTransactionById(transactionId);
200-
if (transaction === null) {
201-
throw new BadRequestException(ErrorCodes.TNF);
202-
}
203198
return this.transactionsService.getUserKeysToSign(transaction, user);
204199
}
205200

0 commit comments

Comments
 (0)