diff --git a/bitwarden_license/bit-common/src/dirt/reports/risk-insights/models/password-health.ts b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/models/password-health.ts index 62eb0122dca..6be24d60b89 100644 --- a/bitwarden_license/bit-common/src/dirt/reports/risk-insights/models/password-health.ts +++ b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/models/password-health.ts @@ -158,6 +158,13 @@ export interface PasswordHealthReportApplicationsRequest { url: string; } +export interface EncryptedDataModel { + organizationId: OrganizationId; + encryptedData: string; + encryptionKey: string; + date: Date; +} + // FIXME: update to use a const object instead of a typescript enum // eslint-disable-next-line @bitwarden/platform/no-enums export enum DrawerType { diff --git a/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.spec.ts b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.spec.ts new file mode 100644 index 00000000000..ef9fc768944 --- /dev/null +++ b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.spec.ts @@ -0,0 +1,83 @@ +import { mock } from "jest-mock-extended"; + +import { ApiService } from "@bitwarden/common/abstractions/api.service"; + +import { EncryptedDataModel } from "../models/password-health"; + +import { RiskInsightsApiService } from "./risk-insights-api.service"; + +describe("RiskInsightsApiService", () => { + let service: RiskInsightsApiService; + const mockApiService = mock(); + + beforeEach(() => { + service = new RiskInsightsApiService(mockApiService); + }); + + it("should be created", () => { + expect(service).toBeTruthy(); + }); + + describe("getRiskInsightsSummary", () => { + it("should call apiService.send with correct parameters and return an Observable", (done) => { + const orgId = "org123"; + const minDate = new Date("2024-01-01"); + const maxDate = new Date("2024-01-31"); + const mockResponse: EncryptedDataModel[] = [{ encryptedData: "abc" } as EncryptedDataModel]; + + mockApiService.send.mockResolvedValueOnce(mockResponse); + + service.getRiskInsightsSummary(orgId, minDate, maxDate).subscribe((result) => { + expect(mockApiService.send).toHaveBeenCalledWith( + "GET", + `organization-report-summary/org123?from=2024-01-01&to=2024-01-31`, + null, + true, + true, + ); + expect(result).toEqual(mockResponse); + done(); + }); + }); + }); + + describe("saveRiskInsightsSummary", () => { + it("should call apiService.send with correct parameters and return an Observable", (done) => { + const data: EncryptedDataModel = { encryptedData: "xyz" } as EncryptedDataModel; + + mockApiService.send.mockResolvedValueOnce(undefined); + + service.saveRiskInsightsSummary(data).subscribe((result) => { + expect(mockApiService.send).toHaveBeenCalledWith( + "POST", + "organization-report-summary", + data, + true, + true, + ); + expect(result).toBeUndefined(); + done(); + }); + }); + }); + + describe("updateRiskInsightsSummary", () => { + it("should call apiService.send with correct parameters and return an Observable", (done) => { + const data: EncryptedDataModel = { encryptedData: "xyz" } as EncryptedDataModel; + + mockApiService.send.mockResolvedValueOnce(undefined); + + service.updateRiskInsightsSummary(data).subscribe((result) => { + expect(mockApiService.send).toHaveBeenCalledWith( + "PUT", + "organization-report-summary", + data, + true, + true, + ); + expect(result).toBeUndefined(); + done(); + }); + }); + }); +}); diff --git a/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.ts b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.ts new file mode 100644 index 00000000000..0d69947b826 --- /dev/null +++ b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/risk-insights-api.service.ts @@ -0,0 +1,45 @@ +import { from, Observable } from "rxjs"; + +import { ApiService } from "@bitwarden/common/abstractions/api.service"; + +import { EncryptedDataModel } from "../models/password-health"; + +export class RiskInsightsApiService { + constructor(private apiService: ApiService) {} + + getRiskInsightsSummary( + orgId: string, + minDate: Date, + maxDate: Date, + ): Observable { + const minDateStr = minDate.toISOString().split("T")[0]; + const maxDateStr = maxDate.toISOString().split("T")[0]; + const dbResponse = this.apiService.send( + "GET", + `organization-report-summary/${orgId.toString()}?from=${minDateStr}&to=${maxDateStr}`, + null, + true, + true, + ); + + return from(dbResponse as Promise); + } + + saveRiskInsightsSummary(data: EncryptedDataModel): Observable { + const dbResponse = this.apiService.send( + "POST", + "organization-report-summary", + data, + true, + true, + ); + + return from(dbResponse as Promise); + } + + updateRiskInsightsSummary(data: EncryptedDataModel): Observable { + const dbResponse = this.apiService.send("PUT", "organization-report-summary", data, true, true); + + return from(dbResponse as Promise); + } +}