Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion runtime/backend/src/common/Schedulers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { UserAggregationCommand } from "../statistics/schedulers/UserAggregation
import { UserTopActivitiesCommand } from "../statistics/schedulers/UserTopActivities/UserTopActivitiesCommand";

// notifier scope
import { AlertsModule } from "../notifier/modules/AlertsModule";
import { ReportNotifierCommand } from "../notifier/schedulers/ReportNotifier/ReportNotifierCommand";

/**
Expand Down Expand Up @@ -102,7 +103,7 @@ export const Schedulers: { [key: string]: any[] } = {
UserAggregationCommand,
UserTopActivitiesCommand,
],
notifier: [ReportNotifierCommand.register()],
notifier: [AlertsModule, ReportNotifierCommand.register()],
oauth: [],
users: [],
};
1 change: 0 additions & 1 deletion runtime/backend/src/common/models/AccountDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { BaseDTO } from "./BaseDTO";
* This class shall be used in **HTTP responses** to avoid any additional
* data about accounts to be revealed.
*
* @todo The transaction timestamp in `firstTransactionAt` should probably be a **number** to avoid timezone issues.
* @since v0.1.0
*/
export class AccountDTO extends BaseDTO {
Expand Down
1 change: 0 additions & 1 deletion runtime/backend/src/common/models/BaseDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* This class shall be used in **HTTP responses** to avoid any additional
* data about accounts to be revealed.
*
* @todo Currently this is using a "to any"-cast trick that should be avoided.
* @since v0.3.0
*/
export class BaseDTO {
Expand Down
1 change: 0 additions & 1 deletion runtime/backend/src/common/models/DappConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export type AppConnectionPayload = {
* This interface is mainly used **internally** to restrict the configuration
* values provided to some modules or services and methods.
*
* @todo Allow for updated discovery sources configuration (must be backwards compatible).
* @link DappConfig
* @since v0.1.0
*/
Expand Down
17 changes: 17 additions & 0 deletions runtime/backend/src/common/models/PaginatedResultDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,21 @@ export class PaginatedResultDTO<TData> {
(this.pagination.total ?? 0)
);
}

/**
* Method to dynamically create and return an instance of this class
* with the provided data type, data content and pagination content.
*
* @access public
* @static
* @param {TData[]} data The results returned by the query in an array of template type `TData`.
* @param {Pageable & Countable} pagination The pagination object, contains properties such as `pageSize`, `pageNumber` and `total`.
* @returns {PaginatedResultDTO<TData>}
*/
public static create<TData>(
data: TData[],
pagination: Pageable & Countable,
): PaginatedResultDTO<TData> {
return new PaginatedResultDTO<TData>(data, pagination);
}
}
18 changes: 7 additions & 11 deletions runtime/backend/src/common/models/StateDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import { ApiProperty } from "@nestjs/swagger";

// internal dependencies
import { StateData } from "./StateData";
import { BaseDTO } from "./BaseDTO";

/**
Expand Down Expand Up @@ -47,22 +46,19 @@ export class StateDTO extends BaseDTO {
public name: string;

/**
* Contains the actual state cache data. This field is usually
* populated or updated within a service class.
* Contains the actual state cache data's hash.
* <br /><br />
* This field can hold **any** type of information as it extends
* the `Record<string, any>` type to permit greater flexibility
* around state cache entries.
* The content is hashed with SHA3-256 to prevent revealing actual
* state data to the client.
*
* @todo We probably don't want this data to be as public, instead should be the "cache hash".
* @access public
* @var {string}
*/
@ApiProperty({
type: Object,
example: { stateKey1: 1, stateKey2: "value2", stateKey3: true },
type: "string",
example: "",
description:
"Contains the actual state cache data. This field is usually populated or updated within a service class.",
"Contains the actual state cache data's hash. The content is hashed with SHA3-256 to prevent revealing actual state data to the client.",
})
public data: StateData;
public data: string;
}
4 changes: 3 additions & 1 deletion runtime/backend/src/common/models/StateSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// external dependencies
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { Model } from "mongoose";
import { sha3_256 } from "js-sha3";

// internal dependencies
import { Documentable } from "../concerns/Documentable";
Expand Down Expand Up @@ -134,7 +135,8 @@ export class State extends Transferable<StateDTO> {
*/
public static fillDTO(doc: StateDocument, dto: StateDTO): StateDTO {
dto.name = doc.name;
dto.data = doc.data;
// hash the actual state data in the dto to prevent its exposure to the client.
dto.data = sha3_256(JSON.stringify(doc.data));
return dto;
}
}
Expand Down
1 change: 0 additions & 1 deletion runtime/backend/src/common/models/TransactionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { TransactionDTO } from "../../discovery/models/TransactionDTO";
* Note that this class uses the generic {@link Transferable} trait to
* enable a `toDTO()` method on the model.
*
* @todo Timestamp fields should be **numbers** to avoid timezone issues.
* @since v0.2.0
*/
@Schema({
Expand Down
4 changes: 0 additions & 4 deletions runtime/backend/src/common/modules/LogModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import { QueryModule } from "../modules/QueryModule";
import { Log, LogSchema } from "../models/LogSchema";
import { LogService } from "../services/LogService";

// @todo decouple from notifier scope
import { AlertsModule } from "../../notifier/modules/AlertsModule";

/**
* @class LogModule
* @description The main definition for the Log module.
Expand All @@ -33,7 +30,6 @@ import { AlertsModule } from "../../notifier/modules/AlertsModule";
schema: LogSchema,
}, // requirement from LogModule
]),
AlertsModule,
QueryModule, // requirement from LogService
],
providers: [LogService],
Expand Down
14 changes: 2 additions & 12 deletions runtime/backend/src/common/services/QueryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,7 @@ export class QueryService<
};

// returns wrapped entity page
// @todo move to static factory
const result: PaginatedResultDTO<TDocument> =
new PaginatedResultDTO<TDocument>();
result.data = data;
result.pagination = pagination;
return result;
return PaginatedResultDTO.create<TDocument>(data, pagination);
}

/**
Expand Down Expand Up @@ -410,12 +405,7 @@ export class QueryService<
T4thDocument;

// returns wrapped entity page
// @todo move to static factory
const result: PaginatedResultDTO<TResultDocument> =
new PaginatedResultDTO<TResultDocument>();
result.data = data;
result.pagination = pagination;
return result;
return PaginatedResultDTO.create<TResultDocument>(data, pagination);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion runtime/backend/src/discovery/routes/AccountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class AccountsController {
);

// wraps for transport
return new PaginatedResultDTO<AccountDTO>(
return PaginatedResultDTO.create<AccountDTO>(
data.data.map((d: AccountDocument) =>
Account.fillDTO(d, new AccountDTO()),
),
Expand Down
4 changes: 2 additions & 2 deletions runtime/backend/src/discovery/routes/AssetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class AssetsController {
);

// wraps for transport
return new PaginatedResultDTO<AssetDTO>(
return PaginatedResultDTO.create<AssetDTO>(
data.data.map((d: AssetDocument) => Asset.fillDTO(d, new AssetDTO())),
data.pagination,
);
Expand Down Expand Up @@ -198,7 +198,7 @@ export class AssetsController {
const data = await this.assetsService.find(safeQuery);

// wraps for transport using AssetDTO
return new PaginatedResultDTO<AssetDTO>(
return PaginatedResultDTO.create<AssetDTO>(
data.data.map((d: AssetDocument) => Asset.fillDTO(d, new AssetDTO())),
data.pagination,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class TransactionsController {
);

// wraps for transport
return new PaginatedResultDTO<TransactionDTO>(
return PaginatedResultDTO.create<TransactionDTO>(
data.data.map((d: TransactionDocument) =>
Transaction.fillDTO(d, new TransactionDTO()),
),
Expand Down
4 changes: 2 additions & 2 deletions runtime/backend/src/payout/routes/PayoutsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class PayoutsController {
);

// wraps for transport
return new PaginatedResultDTO<PayoutDTO>(
return PaginatedResultDTO.create<PayoutDTO>(
data.data.map((d: PayoutDocument) => Payout.fillDTO(d, new PayoutDTO())),
data.pagination,
);
Expand Down Expand Up @@ -198,7 +198,7 @@ export class PayoutsController {
const data = await this.payoutsService.find(safeQuery);

// wraps for transport using PayoutDTO
return new PaginatedResultDTO<PayoutDTO>(
return PaginatedResultDTO.create<PayoutDTO>(
data.data.map((d: PayoutDocument) => Payout.fillDTO(d, new PayoutDTO())),
data.pagination,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class OperationsController {
);

// wraps for transport
return new PaginatedResultDTO<OperationDTO>(
return PaginatedResultDTO.create<OperationDTO>(
data.data.map((d: OperationDocument) =>
Operation.fillDTO(d, new OperationDTO()),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class LeaderboardsController {
);

// wraps for transport
return new PaginatedResultDTO<StatisticsDTO>(
return PaginatedResultDTO.create<StatisticsDTO>(
data.data.map((d: StatisticsDocument) => {
return Statistics.fillDTO(d, new StatisticsDTO());
}),
Expand Down
2 changes: 1 addition & 1 deletion runtime/backend/src/statistics/routes/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class UsersController {
const data = await this.statisticsService.find(safeQuery);

// wraps for transport using StatisticsDTO
return new PaginatedResultDTO<StatisticsDTO>(
return PaginatedResultDTO.create<StatisticsDTO>(
data.data.map((d: StatisticsDocument) =>
Statistics.fillDTO(d, new StatisticsDTO()),
),
Expand Down
4 changes: 2 additions & 2 deletions runtime/backend/src/users/routes/ActivitiesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class ActivitiesController {
);

// wraps for transport
return new PaginatedResultDTO<ActivityDTO>(
return PaginatedResultDTO.create<ActivityDTO>(
data.data.map((d: ActivityDocument) =>
Activity.fillDTO(d, new ActivityDTO()),
),
Expand Down Expand Up @@ -204,7 +204,7 @@ export class ActivitiesController {
const data = await this.activitiesService.find(safeQuery);

// wraps for transport using ActivityDTO
return new PaginatedResultDTO<ActivityDTO>(
return PaginatedResultDTO.create<ActivityDTO>(
data.data.map((d: ActivityDocument) =>
Activity.fillDTO(d, new ActivityDTO()),
),
Expand Down
5 changes: 5 additions & 0 deletions runtime/backend/tests/unit/common/ScopeFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ jest.mock("../../../src/notifier/NotifierModule", () => {
return { NotifierModule: NotifierModuleMock };
});

const AlertsModuleMock: any = jest.fn();
jest.mock("../../../src/notifier/modules/AlertsModule", () => {
return { AlertsModule: AlertsModuleMock };
});

// oauth scope
const OAuthModuleMock: any = jest.fn();
jest.mock("../../../src/oauth/OAuthModule", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ describe("common/PaginatedResultDTO", () => {
expect(paginatedResultDto.pagination.pageNumber).toEqual(1);
});

it("should create with default data value", () => {
// prepare
const pagination = {
pageNumber: -1,
pageSize: -1,
total: 1,
};

// act
const paginatedResultDto = new PaginatedResultDTO(undefined, pagination);

// assert
expect(paginatedResultDto.data).toEqual([]);
expect(paginatedResultDto.pagination.pageNumber).toEqual(1);
});

it("should create with default pagination values", () => {
// prepare
const data = ["data1", "data2"];
Expand Down
8 changes: 7 additions & 1 deletion runtime/backend/tests/unit/common/models/StateSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* @author dHealth Network <[email protected]>
* @license LGPL-3.0
*/
const sha3_256Call = jest.fn().mockReturnValue("hashed-content");
jest.mock("js-sha3", () => ({
sha3_256: sha3_256Call
}));

// internal dependencies
import { StateDTO } from "../../../../src/common/models/StateDTO";
import { State, StateDocument } from "../../../../src/common/models/StateSchema";
Expand All @@ -31,7 +36,7 @@ describe("common/StateSchema", () => {
it("should return correct instance", () => {
// prepare
const name = "test-name";
const data = { key: "value" };
const data: any = "hashed-content";
const state = new State();
(state as any).name = name;
(state as any).data = data;
Expand All @@ -41,6 +46,7 @@ describe("common/StateSchema", () => {
const result = State.fillDTO(state as StateDocument, new StateDTO());

// assert
expect(sha3_256Call).toHaveBeenCalledTimes(1);
expect(result).toEqual(expectedResult);
});
});
Expand Down
Loading