-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathhandler.ts
More file actions
63 lines (51 loc) · 1.81 KB
/
handler.ts
File metadata and controls
63 lines (51 loc) · 1.81 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
/**
* Batch Create Command Handler
*/
import type { CommandHandlerArgs, CommandResult } from '@/core';
import type { Command } from '@/core/commands/command.interface';
import type { KeyManagerName } from '@/core/services/kms/kms-types.interface';
import type { CreateBatchOutput } from './output';
import { ValidationError } from '@/core/errors';
import { composeKey } from '@/core/utils/key-composer';
import { ZustandBatchStateHelper } from '@/plugins/batch/zustand-state-helper';
import { CreateBatchInputSchema } from './input';
export class BatchCreateCommand implements Command {
async execute(args: CommandHandlerArgs): Promise<CommandResult> {
const { api, logger } = args;
const batchState = new ZustandBatchStateHelper(api.state, logger);
const validArgs = CreateBatchInputSchema.parse(args.args);
const name = validArgs.name;
const batchKey = validArgs.key;
const network = api.network.getCurrentNetwork();
const key = composeKey(network, name);
if (batchState.hasBatch(key)) {
throw new ValidationError(`Batch with name '${key}' already exists`);
}
const keyManager =
validArgs.keyManager ||
api.config.getOption<KeyManagerName>('default_key_manager');
const resolved = await api.keyResolver.resolveSigningKey(
batchKey,
keyManager,
['batch:signer'],
);
const batchData = {
name,
keyRefId: resolved.keyRefId,
executed: false,
success: false,
transactions: [],
};
batchState.saveBatch(key, batchData);
const outputData: CreateBatchOutput = {
name: batchData.name,
keyRefId: batchData.keyRefId,
};
return { result: outputData };
}
}
export async function batchCreate(
args: CommandHandlerArgs,
): Promise<CommandResult> {
return new BatchCreateCommand().execute(args);
}