|
| 1 | +import { AccountController } from "@nmshd/transport"; |
| 2 | +import { CommandModule } from "yargs"; |
| 3 | +import { BaseCommand, ConfigFileOptions, configOptionBuilder } from "../../BaseCommand"; |
| 4 | + |
| 5 | +export const identityInitHandler = async ({ config }: ConfigFileOptions): Promise<void> => { |
| 6 | + await new IdentityInit().run(config); |
| 7 | +}; |
| 8 | +export const yargsIdentityInitCommand: CommandModule<{}, ConfigFileOptions> = { |
| 9 | + command: "init", |
| 10 | + describe: "initialize the identity", |
| 11 | + handler: identityInitHandler, |
| 12 | + builder: configOptionBuilder |
| 13 | +}; |
| 14 | + |
| 15 | +export default class IdentityInit extends BaseCommand { |
| 16 | + public async runInternal(): Promise<{ message: string }> { |
| 17 | + if (!this.transport || !this.connectorConfig) { |
| 18 | + throw new Error("Transport or connectorConfig not initialized"); |
| 19 | + } |
| 20 | + const db = await this.transport.createDatabase(`${this.connectorConfig.database.dbNamePrefix}${this.connectorConfig.database.dbName}`); |
| 21 | + const identityCollection = await db.getMap("AccountInfo"); |
| 22 | + const identity = await identityCollection.get("identity"); |
| 23 | + if (identity) { |
| 24 | + this.log.log("Identity already created!"); |
| 25 | + return { message: "Identity already created!" }; |
| 26 | + } |
| 27 | + const accountController = new AccountController(this.transport, db, this.transport.config); |
| 28 | + await accountController.init(); |
| 29 | + |
| 30 | + this.log.log("Identity created successfully!"); |
| 31 | + return { message: "Identity created successfully!" }; |
| 32 | + } |
| 33 | +} |
0 commit comments