-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathhandler.ts
More file actions
72 lines (60 loc) · 2.13 KB
/
handler.ts
File metadata and controls
72 lines (60 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Batch Delete Command Handler
* Deletes a whole batch by name or a single transaction by name and order
*/
import type { CommandHandlerArgs, CommandResult } from '@/core';
import type { Command } from '@/core/commands/command.interface';
import type { DeleteBatchOutput } from './output';
import { NotFoundError } from '@/core/errors';
import { composeKey } from '@/core/utils/key-composer';
import { ZustandBatchStateHelper } from '@/plugins/batch/zustand-state-helper';
import { DeleteBatchInputSchema } from './input';
export class BatchDeleteCommand implements Command {
async execute(args: CommandHandlerArgs): Promise<CommandResult> {
const { api, logger } = args;
const batchState = new ZustandBatchStateHelper(api.state, logger);
const validArgs = DeleteBatchInputSchema.parse(args.args);
const name = validArgs.name;
const order = validArgs.order;
const network = api.network.getCurrentNetwork();
const key = composeKey(network, name);
const batch = batchState.getBatch(key);
if (!batch) {
throw new NotFoundError(`Batch not found: ${name}`, {
context: { name },
});
}
if (order) {
// Delete single transaction by order
const transactionIndex = batch.transactions.findIndex(
(tx) => tx.order === order,
);
if (transactionIndex === -1) {
throw new NotFoundError(
`Transaction with order ${order} not found in batch '${name}'`,
{ context: { name, order } },
);
}
batch.transactions.splice(transactionIndex, 1);
batchState.saveBatch(key, batch);
logger.info(`Removed transaction at order ${order} from batch '${name}'`);
const outputData: DeleteBatchOutput = {
name,
order,
};
return { result: outputData };
}
// Delete whole batch
batchState.deleteBatch(key);
logger.info(`Deleted batch '${name}'`);
const outputData: DeleteBatchOutput = {
name,
};
return { result: outputData };
}
}
export async function batchDelete(
args: CommandHandlerArgs,
): Promise<CommandResult> {
return new BatchDeleteCommand().execute(args);
}