From 13ded8245d1c2b3a9830993eb2ccea98a9910739 Mon Sep 17 00:00:00 2001
From: alexeh <alexurola@gmail.com>
Date: Mon, 7 Aug 2023 02:27:07 +0300
Subject: [PATCH] Create material-indicator to h3 mapping table

---
 .../modules/indicators/indicator.entity.ts    |  8 ++++
 .../material-indicator-to-h3.entity.ts        | 40 +++++++++++++++++++
 .../material-indicator-to-h3.service.ts       | 11 +++++
 api/src/modules/materials/material.entity.ts  |  7 ++++
 api/src/modules/materials/materials.module.ts | 14 ++++++-
 5 files changed, 78 insertions(+), 2 deletions(-)
 create mode 100644 api/src/modules/materials/material-indicator-to-h3.entity.ts
 create mode 100644 api/src/modules/materials/material-indicator-to-h3.service.ts

diff --git a/api/src/modules/indicators/indicator.entity.ts b/api/src/modules/indicators/indicator.entity.ts
index 537ab51b0b..6327789ccb 100644
--- a/api/src/modules/indicators/indicator.entity.ts
+++ b/api/src/modules/indicators/indicator.entity.ts
@@ -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',
@@ -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
diff --git a/api/src/modules/materials/material-indicator-to-h3.entity.ts b/api/src/modules/materials/material-indicator-to-h3.entity.ts
new file mode 100644
index 0000000000..65334b9b91
--- /dev/null
+++ b/api/src/modules/materials/material-indicator-to-h3.entity.ts
@@ -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;
+}
diff --git a/api/src/modules/materials/material-indicator-to-h3.service.ts b/api/src/modules/materials/material-indicator-to-h3.service.ts
new file mode 100644
index 0000000000..f3dea34f34
--- /dev/null
+++ b/api/src/modules/materials/material-indicator-to-h3.service.ts
@@ -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());
+  }
+}
diff --git a/api/src/modules/materials/material.entity.ts b/api/src/modules/materials/material.entity.ts
index 5267ff390b..b3fa5d4dde 100644
--- a/api/src/modules/materials/material.entity.ts
+++ b/api/src/modules/materials/material.entity.ts
@@ -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',
@@ -108,4 +109,10 @@ export class Material extends TimestampedBaseEntity {
     (materialToH3: MaterialToH3) => materialToH3.material,
   )
   materialToH3s: MaterialToH3[];
+
+  @OneToMany(
+    () => MaterialToH3,
+    (materialToH3: MaterialToH3) => materialToH3.material,
+  )
+  materialIndicatorToH3: MaterialIndicatorToH3[];
 }
diff --git a/api/src/modules/materials/materials.module.ts b/api/src/modules/materials/materials.module.ts
index bf4a1bf21b..0604c5ef0c 100644
--- a/api/src/modules/materials/materials.module.ts
+++ b/api/src/modules/materials/materials.module.ts
@@ -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 {}