-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): allow to get scenarios solutions results
- Loading branch information
Showing
8 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
api/apps/api/src/modules/scenarios/dto/scenario-solution-result.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
ApiExtraModels, | ||
ApiProperty, | ||
getSchemaPath, | ||
refs, | ||
} from '@nestjs/swagger'; | ||
import { ScenariosOutputResultsGeoEntity } from '@marxan/scenarios-planning-unit'; | ||
import { oneOf } from 'purify-ts'; | ||
|
||
class ScenarioSolutionsDataDto { | ||
@ApiProperty() | ||
type: 'solutions' = 'solutions'; | ||
|
||
@ApiProperty() | ||
id!: string; | ||
|
||
@ApiProperty({ | ||
isArray: true, | ||
type: () => ScenariosOutputResultsGeoEntity, | ||
}) | ||
attributes!: ScenariosOutputResultsGeoEntity[]; | ||
} | ||
|
||
class ScenarioSolutionDataDto { | ||
@ApiProperty() | ||
type: 'solution' = 'solution'; | ||
|
||
@ApiProperty() | ||
id!: string; | ||
|
||
@ApiProperty({ | ||
type: () => ScenariosOutputResultsGeoEntity, | ||
}) | ||
attributes!: ScenariosOutputResultsGeoEntity; | ||
} | ||
|
||
@ApiExtraModels(ScenarioSolutionsDataDto, ScenarioSolutionDataDto) | ||
export class ScenarioSolutionResultDto { | ||
@ApiProperty({ | ||
oneOf: [ | ||
{ $ref: getSchemaPath(ScenarioSolutionsDataDto) }, | ||
{ $ref: getSchemaPath(ScenarioSolutionDataDto) }, | ||
], | ||
}) | ||
data!: ScenarioSolutionsDataDto | ScenarioSolutionDataDto; | ||
} |
23 changes: 23 additions & 0 deletions
23
api/apps/api/src/modules/scenarios/dto/scenario-solution.serializer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PaginationMeta } from '@marxan-api/utils/app-base.service'; | ||
import { ScenariosOutputResultsGeoEntity } from '@marxan/scenarios-planning-unit'; | ||
import { SolutionResultCrudService } from '../solutions-result/solution-result-crud.service'; | ||
|
||
@Injectable() | ||
export class ScenarioSolutionSerializer { | ||
constructor( | ||
private readonly scenariosSolutionsCrudService: SolutionResultCrudService, | ||
) {} | ||
|
||
async serialize( | ||
entities: | ||
| Partial<ScenariosOutputResultsGeoEntity> | ||
| (Partial<ScenariosOutputResultsGeoEntity> | undefined)[], | ||
paginationMeta?: PaginationMeta, | ||
): Promise<any> { | ||
return this.scenariosSolutionsCrudService.serialize( | ||
entities, | ||
paginationMeta, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
api/apps/api/src/modules/scenarios/solutions-result/solution-result-crud.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Repository } from 'typeorm'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { | ||
AppBaseService, | ||
JSONAPISerializerConfig, | ||
} from '@marxan-api/utils/app-base.service'; | ||
|
||
import { AppInfoDTO } from '@marxan-api/dto/info.dto'; | ||
import { AppConfig } from '@marxan-api/utils/config.utils'; | ||
import { FetchSpecification } from 'nestjs-base-service'; | ||
import { ScenariosOutputResultsGeoEntity } from '@marxan/scenarios-planning-unit'; | ||
import { DbConnections } from '@marxan-api/ormconfig.connections'; | ||
|
||
@Injectable() | ||
export class SolutionResultCrudService extends AppBaseService< | ||
ScenariosOutputResultsGeoEntity, | ||
never, | ||
never, | ||
AppInfoDTO | ||
> { | ||
constructor( | ||
@InjectRepository( | ||
ScenariosOutputResultsGeoEntity, | ||
DbConnections.geoprocessingDB, | ||
) | ||
protected readonly repository: Repository<ScenariosOutputResultsGeoEntity>, | ||
) { | ||
super(repository, 'solution', 'solutions', { | ||
logging: { muteAll: AppConfig.get<boolean>('logging.muteAll', false) }, | ||
}); | ||
} | ||
|
||
get serializerConfig(): JSONAPISerializerConfig<ScenariosOutputResultsGeoEntity> { | ||
return { | ||
attributes: [ | ||
'id', | ||
'planningUnits', | ||
'missingValues', | ||
'cost', | ||
'score', | ||
'run', | ||
], | ||
keyForAttribute: 'camelCase', | ||
}; | ||
} | ||
|
||
async extendFindAllResults( | ||
entitiesAndCount: [ScenariosOutputResultsGeoEntity[], number], | ||
_fetchSpecification?: FetchSpecification, | ||
_info?: AppInfoDTO, | ||
): Promise<[ScenariosOutputResultsGeoEntity[], number]> { | ||
const extendedEntities: Promise<ScenariosOutputResultsGeoEntity>[] = entitiesAndCount[0].map( | ||
(entity) => this.extendGetByIdResult(entity), | ||
); | ||
return [await Promise.all(extendedEntities), entitiesAndCount[1]]; | ||
} | ||
|
||
async extendGetByIdResult( | ||
entity: ScenariosOutputResultsGeoEntity, | ||
_fetchSpecification?: FetchSpecification, | ||
_info?: AppInfoDTO, | ||
): Promise<ScenariosOutputResultsGeoEntity> { | ||
entity.planningUnits = 17; | ||
entity.missingValues = 13; | ||
entity.cost = 400; | ||
entity.score = 999; | ||
entity.run = 1; | ||
return entity; | ||
} | ||
|
||
// TODO DTO (x2) | ||
// TODO params switcher | ||
// TODO serializer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { LockStatus } from './lock-status.enum'; | ||
export { ScenariosPlanningUnitGeoEntity } from './scenarios-planning-unit.geo.entity'; | ||
export { ScenariosOutputResultsGeoEntity } from './scenarios-output-results.geo.entity'; | ||
export * from './domain'; |
Oops, something went wrong.