Skip to content

Commit

Permalink
Create material-indicator to h3 mapping table
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Aug 6, 2023
1 parent 26c8794 commit 13ded82
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api/src/modules/indicators/indicator.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { BaseServiceResource } from 'types/resource.interface';
import { Unit } from 'modules/units/unit.entity';
import { H3Data } from 'modules/h3-data/h3-data.entity';
import { MaterialIndicatorToH3 } from 'modules/materials/material-indicator-to-h3.entity';

export enum INDICATOR_STATUS {
ACTIVE = 'active',
Expand Down Expand Up @@ -97,6 +98,13 @@ export class Indicator extends BaseEntity {
@JoinColumn()
h3Grid: H3Data;

@OneToMany(
() => MaterialIndicatorToH3,
(miToH3: MaterialIndicatorToH3) => miToH3.indicator,
{},
)
materialIndicatorToH3: MaterialIndicatorToH3[];

/**
* Defines the calculation dependencies for each indicator. These will be static, and very unlikely to change
* includeItself is a convenience parameter, that makes the passed indicatorType as part of the dependency list; it's
Expand Down
40 changes: 40 additions & 0 deletions api/src/modules/materials/material-indicator-to-h3.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Material } from 'modules/materials/material.entity';
import { Indicator } from 'modules/indicators/indicator.entity';
import { H3Data } from 'modules/h3-data/h3-data.entity';

@Entity('material_indicator_to_h3')
export class MaterialIndicatorToH3 {
@PrimaryGeneratedColumn('uuid')
id!: string;

// TODO: double check how relations should be

@ManyToOne(() => Material, (material: Material) => material.materialToH3s)
@JoinColumn({ name: 'materialId' })
material!: Material;
@Column()
materialId!: string;

@ManyToOne(
() => Indicator,
(indicator: Indicator) => indicator.materialIndicatorToH3,
)
@JoinColumn({ name: 'indicatorId' })
indicator!: Indicator;
@Column()
indicatorId!: string;

@OneToOne(() => H3Data)
@JoinColumn({ name: 'h3DataId' })
h3Data!: H3Data;
@Column()
h3DataId!: string;
}
11 changes: 11 additions & 0 deletions api/src/modules/materials/material-indicator-to-h3.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm/repository/Repository';
import { DataSource } from 'typeorm';
import { MaterialIndicatorToH3 } from 'modules/materials/material-indicator-to-h3.entity';

@Injectable()
export class MaterialIndicatorToH3Service extends Repository<MaterialIndicatorToH3> {
constructor(private dataSource: DataSource) {
super(MaterialIndicatorToH3, dataSource.createEntityManager());
}
}
7 changes: 7 additions & 0 deletions api/src/modules/materials/material.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { IndicatorCoefficient } from 'modules/indicator-coefficients/indicator-c
import { SourcingLocation } from 'modules/sourcing-locations/sourcing-location.entity';
import { TimestampedBaseEntity } from 'baseEntities/timestamped-base-entity';
import { MaterialToH3 } from 'modules/materials/material-to-h3.entity';
import { MaterialIndicatorToH3 } from 'modules/materials/material-indicator-to-h3.entity';

export enum MATERIALS_STATUS {
ACTIVE = 'active',
Expand Down Expand Up @@ -108,4 +109,10 @@ export class Material extends TimestampedBaseEntity {
(materialToH3: MaterialToH3) => materialToH3.material,
)
materialToH3s: MaterialToH3[];

@OneToMany(
() => MaterialToH3,
(materialToH3: MaterialToH3) => materialToH3.material,
)
materialIndicatorToH3: MaterialIndicatorToH3[];
}
14 changes: 12 additions & 2 deletions api/src/modules/materials/materials.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ import { BusinessUnitsModule } from 'modules/business-units/business-units.modul
import { SuppliersModule } from 'modules/suppliers/suppliers.module';
import { MaterialsToH3sService } from 'modules/materials/materials-to-h3s.service';
import { MaterialRepository } from 'modules/materials/material.repository';
import { MaterialIndicatorToH3Service } from 'modules/materials/material-indicator-to-h3.service';

@Module({
imports: [
TypeOrmModule.forFeature([Material, MaterialToH3]),
TypeOrmModule.forFeature([
Material,
MaterialToH3,
MaterialIndicatorToH3Service,
]),
forwardRef(() => AdminRegionsModule),
forwardRef(() => BusinessUnitsModule),
forwardRef(() => SuppliersModule),
forwardRef(() => SourcingLocationsModule),
],
controllers: [MaterialsController],
providers: [MaterialsService, MaterialsToH3sService, MaterialRepository],
providers: [
MaterialsService,
MaterialsToH3sService,
MaterialIndicatorToH3Service,
MaterialRepository,
],
exports: [MaterialsService, MaterialsToH3sService, MaterialRepository],
})
export class MaterialsModule {}

0 comments on commit 13ded82

Please sign in to comment.