Skip to content

Commit

Permalink
(re)cast numbers-as-strings to actual JS numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
hotzevzl committed May 9, 2024
1 parent 0992f57 commit 2b67dd2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ScenarioAccessControl } from '@marxan-api/modules/access-control/scenar
import { assertDefined } from '@marxan/utils';
import { forbiddenError } from '@marxan-api/modules/access-control';
import { Either, left, right } from 'fp-ts/lib/Either';
import { plainToClass } from 'class-transformer';

@Injectable()
export class ScenarioFeaturesGapDataService extends AppBaseService<
Expand Down Expand Up @@ -59,7 +60,19 @@ export class ScenarioFeaturesGapDataService extends AppBaseService<
) {
return left(forbiddenError);
}
return right(await super.findAllPaginated(fetchSpecification, info));
/**
* @debt Explicitly applying transforms (via `plainToClass()`) here: it
* would be best to do this at AppBaseService level, but that would
* currently open a rabbit hole due to the use of generics.
*/
const { data, metadata } = await this.findAllPaginated(
fetchSpecification,
info,
);
return right({
data: plainToClass(ScenarioFeaturesGapData, data),
metadata,
});
}

async setFilters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { forbiddenError } from '@marxan-api/modules/access-control';
import { assertDefined } from '@marxan/utils';
import { Either, left, right } from 'fp-ts/lib/Either';
import { ScenarioAccessControl } from '@marxan-api/modules/access-control/scenarios-acl/scenario-access-control';
import { plainToClass } from 'class-transformer';

const scenarioFeaturesOutputGapDataFilterKeyNames = ['runId'] as const;
type ScenarioFeaturesOutputGapDataFilterKeys = keyof Pick<
Expand Down Expand Up @@ -73,7 +74,19 @@ export class ScenarioFeaturesOutputGapDataService extends AppBaseService<
) {
return left(forbiddenError);
}
return right(await super.findAllPaginated(fetchSpecification, info));
/**
* @debt Explicitly applying transforms (via `plainToClass()`) here: it
* would be best to do this at AppBaseService level, but that would
* currently open a rabbit hole due to the use of generics.
*/
const { data, metadata } = await this.findAllPaginated(
fetchSpecification,
info,
);
return right({
data: plainToClass(ScenarioFeaturesOutputGapData, data),
metadata,
});
}

async setFilters(
Expand Down
3 changes: 0 additions & 3 deletions api/apps/api/src/modules/scenarios/scenarios.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ import {
GeometryKind,
} from '@marxan-api/decorators/file-interceptors.decorator';
import { ProtectedAreaDto } from '@marxan-api/modules/scenarios/dto/protected-area.dto';
import { UploadShapefileDto } from '@marxan-api/modules/scenarios/dto/upload.shapefile.dto';
import { ProtectedAreasChangeDto } from '@marxan-api/modules/scenarios/dto/protected-area-change.dto';
import { StartScenarioBlmCalibrationDto } from '@marxan-api/modules/scenarios/dto/start-scenario-blm-calibration.dto';
import { BlmCalibrationRunResultDto } from './dto/scenario-blm-calibration-results.dto';
Expand All @@ -108,8 +107,6 @@ import { RequestScenarioCloneResponseDto } from './dto/scenario-clone.dto';
import { ensureShapefileHasRequiredFiles } from '@marxan-api/utils/file-uploads.utils';
import { WebshotPdfReportConfig } from '@marxan/webshot/webshot.dto';
import { ClearLockStatusParams } from '@marxan-api/modules/scenarios/dto/clear-lock-status-param.dto';
import { CostRangeDto } from '@marxan-api/modules/scenarios/dto/cost-range.dto';
import { plainToClass } from 'class-transformer';
import { ProjectsService } from '@marxan-api/modules/projects/projects.service';
import { CostSurfaceService } from '@marxan-api/modules/cost-surface/cost-surface.service';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { Column, ViewEntity } from 'typeorm';

@ViewEntity('scenario_features_gap_data')
Expand All @@ -23,6 +24,7 @@ export class ScenarioFeaturesGapData {
// explicitly set type, otherwise TypeORM (v10, at least) will cast to integer
// TypeORM will still represent the value as string though (https://github.com/typeorm/typeorm/issues/873#issuecomment-328912050)
@Column({ name: 'met', type: 'double precision' })
@Transform(parseFloat)
met!: number;

@ApiProperty()
Expand All @@ -33,6 +35,7 @@ export class ScenarioFeaturesGapData {
// explicitly set type, otherwise TypeORM (v10, at least) will cast to integer
// TypeORM will still represent the value as string though (https://github.com/typeorm/typeorm/issues/873#issuecomment-328912050)
@Column({ name: 'coverage_target', type: 'double precision' })
@Transform(parseFloat)
coverageTarget!: number;

@ApiProperty()
Expand Down

0 comments on commit 2b67dd2

Please sign in to comment.