(options: OptionValues)
| 30 | ) {} |
| 31 | |
| 32 | async run(options: OptionValues): Promise<Response> { |
| 33 | const policyApplies$ = this.accountService.activeAccount$.pipe( |
| 34 | getUserId, |
| 35 | switchMap((userId) => |
| 36 | this.policyService.policyAppliesToUser$(PolicyType.DisablePersonalVaultExport, userId), |
| 37 | ), |
| 38 | ); |
| 39 | |
| 40 | if (options.organizationid == null && (await firstValueFrom(policyApplies$))) { |
| 41 | return Response.badRequest( |
| 42 | "One or more organization policies prevents you from exporting your personal vault.", |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | let password = options.password; |
| 47 | |
| 48 | // has password and format is 'json' => should have the same behaviour as 'encrypted_json' |
| 49 | // format is 'undefined' => Defaults to 'csv' |
| 50 | // Any other case => returns the options.format |
| 51 | const format = |
| 52 | password && options.format == "json" ? "encrypted_json" : (options.format ?? "csv"); |
| 53 | |
| 54 | if (!this.isSupportedExportFormat(format)) { |
| 55 | return Response.badRequest( |
| 56 | `'${format}' is not a supported export format. Supported formats: ${EXPORT_FORMATS.join( |
| 57 | ", ", |
| 58 | )}.`, |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | if (options.organizationid != null && !Utils.isGuid(options.organizationid)) { |
| 63 | return Response.error("`" + options.organizationid + "` is not a GUID."); |
| 64 | } |
| 65 | |
| 66 | let exportContent: ExportedVault = null; |
| 67 | try { |
| 68 | if (format === "encrypted_json") { |
| 69 | password = await this.promptPassword(password); |
| 70 | } |
| 71 | |
| 72 | const userId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId)); |
| 73 | |
| 74 | exportContent = |
| 75 | options.organizationid == null |
| 76 | ? await this.exportService.getExport(userId, format, password) |
| 77 | : await this.exportService.getOrganizationExport( |
| 78 | userId, |
| 79 | options.organizationid, |
| 80 | format, |
| 81 | password, |
| 82 | ); |
| 83 | |
| 84 | const eventType = options.organizationid |
| 85 | ? EventType.Organization_ClientExportedVault |
| 86 | : EventType.User_ClientExportedVault; |
| 87 | // FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling. |
| 88 | // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 89 | this.eventCollectionService.collect(eventType, null, true, options.organizationid); |
no test coverage detected