Skip to content

Commit

Permalink
get filtered assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Nov 21, 2024
1 parent 33e0eed commit 47b99e6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 42 deletions.
80 changes: 46 additions & 34 deletions api/src/modules/calculations/assumptions.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,37 @@ import { GetOverridableAssumptionsDTO } from '@shared/dtos/custom-projects/get-o
import { ACTIVITY } from '@shared/entities/activity.enum';
import { ECOSYSTEM } from '@shared/entities/ecosystem.enum';

export enum ECOSYSTEM_RESTORATION_RATE_NAMES {
MANGROVE = 'Mangrove restoration rate',
SEAGRASS = 'Seagrass restoration rate',
SALT_MARSH = 'Salt marsh restoration rate',
}

export enum ACTIVITY_PROJECT_LENGTH_NAMES {
CONSERVATION = 'Conservation project length',
RESTORATION = 'Restoration project length',
}

export const COMMON_OVERRIDABLE_ASSUMPTION_NAMES = [
'Baseline reassessment frequency',
'Buffer',
'Carbon price increase',
'Discount rate',
'Verification frequency',
] as const;

@Injectable()
export class AssumptionsRepository extends Repository<ModelAssumptions> {
map: Record<
ACTIVITY & ECOSYSTEM,
ECOSYSTEM_RESTORATION_RATE_NAMES & ACTIVITY_PROJECT_LENGTH_NAMES
> = {
[ACTIVITY.CONSERVATION]: ACTIVITY_PROJECT_LENGTH_NAMES.CONSERVATION,
[ACTIVITY.RESTORATION]: ACTIVITY_PROJECT_LENGTH_NAMES.RESTORATION,
[ECOSYSTEM.MANGROVE]: ECOSYSTEM_RESTORATION_RATE_NAMES.MANGROVE,
[ECOSYSTEM.SEAGRASS]: ECOSYSTEM_RESTORATION_RATE_NAMES.SEAGRASS,
[ECOSYSTEM.SALT_MARSH]: ECOSYSTEM_RESTORATION_RATE_NAMES.SALT_MARSH,
};
constructor(
@InjectRepository(ModelAssumptions)
private repo: Repository<ModelAssumptions>,
Expand All @@ -16,44 +45,27 @@ export class AssumptionsRepository extends Repository<ModelAssumptions> {
}

async getOverridableModelAssumptions(dto: GetOverridableAssumptionsDTO) {
const queryBuilder = this.createQueryBuilder('assumptions')
const assumptions = await this.createQueryBuilder('model_assumptions')
.select(['name', 'unit', 'value'])
.where({
name: In([
'Verification frequency',
'Baseline reassessment frequency',
'Carbon price increase',
'Buffer',
'Discount rate',
]),
});

if (dto.activity === ACTIVITY.CONSERVATION) {
queryBuilder.andWhere('name = :activity', {
activity: 'Conservation project length',
});
}
if (dto.activity === ACTIVITY.RESTORATION) {
queryBuilder.andWhere('name = :activity', {
activity: 'Restoration project length',
});
name: In(this.getAssumptionNamesByCountryAndEcosystem(dto)),
})
.orderBy('name', 'ASC')
.getRawMany();
if (assumptions.length !== 7) {
throw new Error('Not all required overridable assumptions were found');
}
return assumptions;
}

if (dto.ecosystem === ECOSYSTEM.MANGROVE) {
queryBuilder.andWhere('name = :ecosystem', {
ecosystem: 'Mangrove restoration rate',
});
}
if (dto.ecosystem === ECOSYSTEM.SEAGRASS) {
queryBuilder.andWhere('name = :ecosystem', {
ecosystem: 'Seagrass restoration rate',
});
}
if (dto.ecosystem === ECOSYSTEM.SALT_MARSH) {
queryBuilder.andWhere('name = :ecosystem', {
ecosystem: 'Salt marsh restoration rate',
});
}
private getAssumptionNamesByCountryAndEcosystem(
dto: GetOverridableAssumptionsDTO,
): string[] {
const { ecosystem, activity } = dto;
const assumptions = [...COMMON_OVERRIDABLE_ASSUMPTION_NAMES] as string[];
assumptions.push(this.map[ecosystem]);
assumptions.push(this.map[activity]);
return assumptions;
}

async getAllModelAssumptions() {
Expand Down
1 change: 0 additions & 1 deletion api/src/modules/calculations/data.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export type CarbonInputs = {
export class DataRepository extends Repository<BaseDataView> {
constructor(
@InjectRepository(BaseDataView) private repo: Repository<BaseDataView>,
private readonly assumptionsRepository: AssumptionsRepository,
) {
super(repo.target, repo.manager, repo.queryRunner);
}
Expand Down
36 changes: 29 additions & 7 deletions api/test/integration/custom-projects/custom-projects-setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { TestManager } from '../../utils/test-manager';
import { customProjectContract } from '@shared/contracts/custom-projects.contract';
import { ECOSYSTEM } from '@shared/entities/ecosystem.enum';
import { ACTIVITY } from '@shared/entities/activity.enum';
import {
ACTIVITY_PROJECT_LENGTH_NAMES,
ECOSYSTEM_RESTORATION_RATE_NAMES,
} from '@api/modules/calculations/assumptions.repository';

describe('Create Custom Projects - Setup', () => {
let testManager: TestManager;
Expand All @@ -20,20 +24,38 @@ describe('Create Custom Projects - Setup', () => {
await testManager.close();
});

describe('Get Overridable Model Assumptions', () => {
test('Should return overridable model assumptions based on ecosystem and activity', async () => {
const response = await testManager
.request()
.get(customProjectContract.getDefaultAssumptions.path)
.query({
ecosystem: ECOSYSTEM.MANGROVE,
activity: ACTIVITY.CONSERVATION,
});

expect(response.body.data).toHaveLength(7);
expect(response.body.data.map((assumptions) => assumptions.name)).toEqual(
[
'Baseline reassessment frequency',
'Buffer',
'Carbon price increase',
'Conservation project length',
'Discount rate',
'Mangrove restoration rate',
'Verification frequency',
],
);
});
});

test('Should return the list of countries that are available to create a custom project', async () => {
const response = await testManager
.request()
.get(customProjectContract.getAvailableCountries.path);

expect(response.body.data).toHaveLength(9);
});
test('Should return default model assumptions', async () => {
const response = await testManager
.request()
.get(customProjectContract.getDefaultAssumptions.path);

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

test('Should return default cost inputs given required filters', async () => {
const response = await testManager
Expand Down

0 comments on commit 47b99e6

Please sign in to comment.