Skip to content
Merged
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
16 changes: 14 additions & 2 deletions src/common/validation.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ export function defaultEndDate(): Date {
return new Date(); // now
}

export function transformArray({ value }: { value: string }) {
return Array.isArray(value) ? value : [value];
export function transformArray({ value }: { value: unknown }) {
if (value === undefined || value === null) return undefined;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ correctness]
Returning undefined when value is undefined or null may lead to unexpected behavior if the caller expects an array. Consider returning an empty array instead to maintain consistent return types.


const splitIfString = (v: unknown) => {
if (typeof v === "string") {
return v
.split(",")
.map((token) => token.trim())
.filter((token) => token !== "");
}
return [v];
};

return Array.isArray(value) ? value.flatMap(splitIfString) : splitIfString(value);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[💡 design]
Using flatMap here is efficient for flattening arrays, but ensure that the caller is aware that nested arrays will be flattened, which might not be the intended behavior if the input is expected to be a single-level array.

}
4 changes: 2 additions & 2 deletions src/reports/sfdc/sfdc-reports.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe("SfdcReportsController", () => {
mockSfdcReportsService.getPaymentsReport.mockResolvedValue([]);

const dto = plainToInstance(PaymentsReportQueryDto, {
billingAccountIds: "80001012",
billingAccountIds: "80001012,80002012",
challengeIds: "e74c3e37-73c9-474e-a838-a38dd4738906",
handles: "user_01",
challengeStatus: "COMPLETED",
Expand All @@ -176,7 +176,7 @@ describe("SfdcReportsController", () => {

expect(mockSfdcReportsService.getPaymentsReport).toHaveBeenCalledWith(
expect.objectContaining({
billingAccountIds: ["80001012"],
billingAccountIds: ["80001012", "80002012"],
challengeIds: ["e74c3e37-73c9-474e-a838-a38dd4738906"],
handles: ["user_01"],
challengeStatus: ["COMPLETED"],
Expand Down
13 changes: 13 additions & 0 deletions src/reports/sfdc/sfdc-reports.dto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,19 @@ describe("TaasResourceBookingsReportQueryDto validation", () => {
expect(dto.billingAccountIds).toEqual(["80001012"]);
});

it("splits comma-delimited billingAccountIds", async () => {
const { dto, errors } = await validateTaasResourceBookingsDto({
// @ts-expect-error intentional single value for transform check
billingAccountIds: "80001012,80002012 , 80003012",
});
expect(errors).toHaveLength(0);
expect(dto.billingAccountIds).toEqual([
"80001012",
"80002012",
"80003012",
]);
});

it("accepts ISO date strings for startDate and endDate", async () => {
const { errors } = await validateTaasResourceBookingsDto({
startDate: "2023-04-01",
Expand Down
Loading