Skip to content

Commit

Permalink
get default model assumptions and base data for project calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Nov 2, 2024
1 parent 657afc2 commit 652c587
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
40 changes: 39 additions & 1 deletion api/src/modules/calculations/calculation.engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { ModelAssumptions } from '@shared/entities/model-assumptions.entity';
import { Country } from '@shared/entities/country.entity';
import { ECOSYSTEM } from '@shared/entities/ecosystem.enum';
import { ACTIVITY } from '@shared/entities/activity.enum';
import { BaseDataView } from '@shared/entities/base-data.view';

export type GetBaseData = {
countryCode: Country['code'];
ecosystem: ECOSYSTEM;
activity: ACTIVITY;
};

export type BaseDataForCalculation = {
defaultAssumptions: ModelAssumptions[];
baseData: BaseDataView;
};

@Injectable()
export class CalculationEngine {}
export class CalculationEngine {
constructor(private readonly dataSource: DataSource) {}

async getBaseData(filter: GetBaseData): Promise<BaseDataForCalculation> {
return this.dataSource.transaction(async (manager) => {
const defaultAssumptions = await manager
.getRepository(ModelAssumptions)
.find();
const baseData = await manager.getRepository(BaseDataView).findOne({
where: {
country_code: filter.countryCode,
ecosystem: filter.ecosystem,
activity: filter.activity,
},
});
return {
defaultAssumptions,
baseData,
};
});
}
}
1 change: 1 addition & 0 deletions api/src/modules/calculations/calculations.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { CalculationEngine } from '@api/modules/calculations/calculation.engine'

@Module({
providers: [CalculationEngine],
exports: [CalculationEngine],
})
export class CalculationsModule {}
27 changes: 15 additions & 12 deletions api/src/modules/custom-projects/custom-projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { ModelAssumptions } from '@shared/entities/model-assumptions.entity';
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
import { customProjectContract } from '@shared/contracts/custom-projects.contract';
import { CustomProjectsService } from '@api/modules/custom-projects/custom-projects.service';

@Controller()
export class CustomProjectsController {
constructor(
private readonly countries: CountriesService,
private readonly dataSource: DataSource,
private readonly customProjects: CustomProjectsService,
) {}

@TsRestHandler(customProjectContract.getAvailableCountries)
Expand Down Expand Up @@ -39,16 +41,17 @@ export class CustomProjectsController {
);
}

// @TsRestHandler(customProjectContract.createCustomProject)
// async create(): Promise<ControllerResponse> {
// return tsRestHandler(
// customProjectContract.createCustomProject,
// async ({ body }) => {
// // return {
// // status: 201,
// // body: null,
// // };
// },
// );
// }
@TsRestHandler(customProjectContract.createCustomProject)
async create(): Promise<ControllerResponse> {
return tsRestHandler(
customProjectContract.createCustomProject,
async ({ body }) => {
const customProject = await this.customProjects.create(body);
return {
status: 201,
body: { data: customProject },
};
},
);
}
}
7 changes: 6 additions & 1 deletion api/src/modules/custom-projects/custom-projects.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { CountriesModule } from '@api/modules/countries/countries.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CustomProject } from '@shared/entities/custom-project.entity';
import { CustomProjectsController } from './custom-projects.controller';
import { CalculationsModule } from '@api/modules/calculations/calculations.module';

@Module({
imports: [TypeOrmModule.forFeature([CustomProject]), CountriesModule],
imports: [
TypeOrmModule.forFeature([CustomProject]),
CountriesModule,
CalculationsModule,
],
providers: [CustomProjectsService],
controllers: [CustomProjectsController],
})
Expand Down
31 changes: 30 additions & 1 deletion api/src/modules/custom-projects/custom-projects.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import { Injectable } from '@nestjs/common';
import { AppBaseService } from '@api/utils/app-base.service';
import { CreateCustomProjectDto } from '@shared/dtos/custom-projects/create-custom-project.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Project } from '@shared/entities/projects.entity';
import { Repository } from 'typeorm';
import { CustomProject } from '@shared/entities/custom-project.entity';
import { CalculationEngine } from '@api/modules/calculations/calculation.engine';

@Injectable()
export class CustomProjectsService {}
export class CustomProjectsService extends AppBaseService<
CustomProject,
CreateCustomProjectDto,
unknown,
unknown
> {
constructor(
@InjectRepository(CustomProject)
public readonly repo: Repository<CustomProject>,
public readonly calculationEngine: CalculationEngine,
) {
super(repo, 'customProject', 'customProjects');
}

async create(dto: CreateCustomProjectDto): Promise<any> {
const { countryCode, ecosystem, activity } = dto;
return this.calculationEngine.getBaseData({
countryCode,
ecosystem,
activity,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('Create Custom Projects - Setup', () => {
const response = await testManager
.request()
.get(customProjectContract.getDefaultAssumptions.path);
console.log(response.body);

expect(response.body.data).toHaveLength(18);
});
Expand Down

0 comments on commit 652c587

Please sign in to comment.