Skip to content

Commit

Permalink
New table structure (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-oancea authored Nov 1, 2024
1 parent bf3b589 commit 7af35a6
Show file tree
Hide file tree
Showing 110 changed files with 2,286 additions and 2,358 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# adminjs
.adminjs/

# Logs
logs
*.log
Expand Down
50 changes: 25 additions & 25 deletions admin/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@ import { DataSource } from "typeorm";
import { User } from "@shared/entities/users/user.entity.js";
import { ApiEventsEntity } from "@api/modules/api-events/api-events.entity.js";
import { Country } from "@shared/entities/country.entity.js";
import { BaseData } from "@shared/entities/base-data.entity.js";
import { ProjectSize } from "@shared/entities/project-size.entity.js";
import { FeasibilityAnalysis } from "@shared/entities/feasability-analysis.entity.js";
import { ConservationPlanningAndAdmin } from "@shared/entities/conservation-and-planning-admin.entity.js";
import { DataCollectionAndFieldCosts } from "@shared/entities/data-collection-and-field-costs.entity.js";
import { CarbonStandardFees } from "@shared/entities/carbon-standard-fees.entity.js";
import { CommunityBenefitSharingFund } from "@shared/entities/community-benefit-sharing-fund.entity.js";
import { CommunityCashFlow } from "@shared/entities/community-cash-flow.entity.js";
import { CommunityRepresentation } from "@shared/entities/community-representation.entity.js";
import { EcosystemLoss } from "@shared/entities/ecosystem-loss.entity.js";
import { CarbonRights } from "@shared/entities/establishing-carbon-rights.entity.js";
import { FinancingCost } from "@shared/entities/financing-cost.entity.js";
import { ImplementationLaborCost } from "@shared/entities/implementation-labor.entity.js";
import { Maintenance } from "@shared/entities/maintenance.entity.js";
import { MonitoringCost } from "@shared/entities/monitoring.entity.js";
import { RestorableLand } from "@shared/entities/restorable-land.entity.js";
import { ValidationCost } from "@shared/entities/validation.entity.js";
import { BaselineReassessment } from "@shared/entities/baseline-reassessment.entity.js";
import { BlueCarbonProjectPlanning } from "@shared/entities/blue-carbon-project-planning.entity.js";
import { EmissionFactors } from "@shared/entities/emission-factors.entity.js";
import { LongTermProjectOperating } from "@shared/entities/long-term-project-operating.entity.js";
import { MRV } from "@shared/entities/mrv.entity.js";
import { SequestrationRate } from "@shared/entities/sequestration-rate.entity.js";
import { Project } from "@shared/entities/projects.entity.js";
import { EcosystemExtent } from "@shared/entities/carbon-inputs/ecosystem-extent.entity.js";
import { EcosystemLoss } from "@shared/entities/carbon-inputs/ecosystem-loss.entity.js";
import { EmissionFactors } from "@shared/entities/carbon-inputs/emission-factors.entity.js";
import { RestorableLand } from "@shared/entities/carbon-inputs/restorable-land.entity.js";
import { SequestrationRate } from "@shared/entities/carbon-inputs/sequestration-rate.entity.js";
import { BaselineReassessment } from "@shared/entities/cost-inputs/baseline-reassessment.entity.js";
import { BlueCarbonProjectPlanning } from "@shared/entities/cost-inputs/blue-carbon-project-planning.entity.js";
import { CarbonStandardFees } from "@shared/entities/cost-inputs/carbon-standard-fees.entity.js";
import { CommunityBenefitSharingFund } from "@shared/entities/cost-inputs/community-benefit-sharing-fund.entity.js";
import { CommunityCashFlow } from "@shared/entities/cost-inputs/community-cash-flow.entity.js";
import { CommunityRepresentation } from "@shared/entities/cost-inputs/community-representation.entity.js";
import { ConservationPlanningAndAdmin } from "@shared/entities/cost-inputs/conservation-and-planning-admin.entity.js";
import { DataCollectionAndFieldCosts } from "@shared/entities/cost-inputs/data-collection-and-field-costs.entity.js";
import { CarbonRights } from "@shared/entities/cost-inputs/establishing-carbon-rights.entity.js";
import { FeasibilityAnalysis } from "@shared/entities/cost-inputs/feasability-analysis.entity.js";
import { FinancingCost } from "@shared/entities/cost-inputs/financing-cost.entity.js";
import { LongTermProjectOperating } from "@shared/entities/cost-inputs/long-term-project-operating.entity.js";
import { Maintenance } from "@shared/entities/cost-inputs/maintenance.entity.js";
import { MonitoringCost } from "@shared/entities/cost-inputs/monitoring.entity.js";
import { MRV } from "@shared/entities/cost-inputs/mrv.entity.js";
import { ProjectSize } from "@shared/entities/cost-inputs/project-size.entity.js";
import { ValidationCost } from "@shared/entities/cost-inputs/validation.entity.js";
import { ImplementationLaborCost } from "@shared/entities/cost-inputs/implementation-labor-cost.entity.js";

// TODO: If we import the COMMON_DATABASE_ENTITIES from shared, we get an error where DataSouce is not set for a given entity
export const ADMINJS_ENTITIES = [
User,
ApiEventsEntity,
Country,
BaseData,
ProjectSize,
FeasibilityAnalysis,
ConservationPlanningAndAdmin,
Expand All @@ -44,7 +43,6 @@ export const ADMINJS_ENTITIES = [
EcosystemLoss,
CarbonRights,
FinancingCost,
ImplementationLaborCost,
Maintenance,
MonitoringCost,
RestorableLand,
Expand All @@ -55,7 +53,9 @@ export const ADMINJS_ENTITIES = [
LongTermProjectOperating,
MRV,
SequestrationRate,
EcosystemExtent,
Project,
ImplementationLaborCost,
];

export const dataSource = new DataSource({
Expand Down
8 changes: 4 additions & 4 deletions admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import * as AdminJSTypeorm from "@adminjs/typeorm";
import { dataSource } from "./datasource.js";
import { AuthProvider } from "./providers/auth.provider.js";
import { UserResource } from "./resources/users/user.resource.js";
import { ProjectSizeResource } from "./resources/project-size/project-size.resource.js";
import { Country } from "@shared/entities/country.entity.js";
import { FeasibilityAnalysisResource } from "./resources/feasability-analysis/feasability-analysis.resource.js";
import { ConservationAndPlanningAdminResource } from "./resources/conservation-and-planning-admin/conservation-and-planning-admin.resource.js";
import { CommunityRepresentationResource } from "./resources/community-representation/community-representation.resource.js";
import { CarbonRightsResource } from "./resources/carbon-righs/carbon-rights.resource.js";
import { FinancingCostResource } from "./resources/financing-cost/financing-cost.resource.js";
import { ValidationCostResource } from "./resources/validation-cost/validation-cost.resource.js";
import { ImplementationLaborCostResource } from "./resources/implementation-labor/implementation-labor-cost.resource.js";
import { MonitoringCostResource } from "./resources/monitoring-cost/monitoring-cost.resource.js";
import { MaintenanceResource } from "./resources/maintenance/maintenance.resource.js";
import { DataCollectionAndFieldCostResource } from "./resources/data-collection-and-field-cost/data-collection-and-field-cost.resource.js";
Expand All @@ -30,6 +28,8 @@ import { BlueCarbonProjectPlanningResource } from "./resources/blue-carbon-proje
import { LongTermProjectOperatingResource } from "./resources/long-term-project-operating/long-term-project-operating.resource.js";
import { SequestrationRateResource } from "./resources/sequestration-rate/sequestration-rate.resource.js";
import { ProjectsResource } from "./resources/projects/projects.resource.js";
import { ProjectSizeResource } from "./resources/project-size/project-size.resource.js";
import { ImplementationLaborCostResource } from "./resources/implementation-labor-cost/implementation-labor-cost.resource.js";

AdminJS.registerAdapter({
Database: AdminJSTypeorm.Database,
Expand Down Expand Up @@ -63,7 +63,6 @@ const start = async () => {
CarbonRightsResource,
FinancingCostResource,
ValidationCostResource,
ImplementationLaborCostResource,
MonitoringCostResource,
MaintenanceResource,
DataCollectionAndFieldCostResource,
Expand All @@ -79,6 +78,7 @@ const start = async () => {
LongTermProjectOperatingResource,
SequestrationRateResource,
ProjectsResource,
ImplementationLaborCostResource,
{
resource: Country,
name: "Country",
Expand Down Expand Up @@ -113,7 +113,7 @@ const start = async () => {

app.listen(PORT, () => {
console.log(
`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`,
`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`
);
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import {
ActionContext,
ActionRequest,
ActionResponse,
BaseRecord,
ResourceWithOptions,
} from "adminjs";
import { dataSource } from "../../datasource.js";
import { BaseData } from "@shared/entities/base-data.entity.js";
import { Country } from "@shared/entities/country.entity.js";
import { BaselineReassessment } from "@shared/entities/baseline-reassessment.entity.js";
import { COMMON_RESOURCE_LIST_PROPERTIES } from "../common/common.resources.js";
import { ResourceWithOptions } from "adminjs";
import { BaselineReassessment } from "@shared/entities/cost-inputs/baseline-reassessment.entity.js";

export const BaselineReassessmentResource: ResourceWithOptions = {
resource: BaselineReassessment,
options: {
properties: COMMON_RESOURCE_LIST_PROPERTIES,
listProperties: [
"baselineReassessmentCost",
"countryName",
"ecosystem",
"activity",
],
sort: {
sortBy: "baselineReassessmentCost",
direction: "desc",
Expand All @@ -29,46 +12,5 @@ export const BaselineReassessmentResource: ResourceWithOptions = {
name: "Data Management",
icon: "Database",
},
actions: {
list: {
after: async (
request: ActionRequest,
response: ActionResponse,
context: ActionContext,
) => {
const { records } = context;
const baseDataRepo = dataSource.getRepository(BaseData);
const queryBuilder = baseDataRepo
.createQueryBuilder("baseData")
.leftJoin(
BaselineReassessment,
"r",
"baseData.baselineReassessment = r.id",
)
.leftJoin(Country, "country", "country.code = baseData.countryCode")
.select("r.id", "id")
.addSelect("r.baselineReassessmentCost", "baselineReassessmentCost")
.addSelect("country.name", "countryName")
.addSelect("baseData.ecosystem", "ecosystem")
.addSelect("baseData.activity", "activity");

if (records?.length) {
queryBuilder.andWhere("r.id IN (:...ids)", {
ids: records.map((r) => r.params.id),
});
}

const result = await queryBuilder.getRawMany();

return {
...request,
records: records!.map((record: BaseRecord) => {
record.params = result.find((q) => q.id === record.params.id);
return record;
}),
};
},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -1,80 +1,39 @@
import {
ActionContext,
ActionRequest,
ActionResponse,
BaseRecord,
ResourceWithOptions,
} from "adminjs";
import { dataSource } from "../../datasource.js";
import { BaseData } from "@shared/entities/base-data.entity.js";
import { Country } from "@shared/entities/country.entity.js";
import { BlueCarbonProjectPlanning } from "@shared/entities/blue-carbon-project-planning.entity.js";
import { COMMON_RESOURCE_LIST_PROPERTIES } from "../common/common.resources.js";
import { BlueCarbonProjectPlanning } from "@shared/entities/cost-inputs/blue-carbon-project-planning.entity.js";
import { ResourceWithOptions } from "adminjs";

export const BlueCarbonProjectPlanningResource: ResourceWithOptions = {
resource: BlueCarbonProjectPlanning,
options: {
properties: COMMON_RESOURCE_LIST_PROPERTIES,
listProperties: [
"inputSelection",
"input1",
"input2",
"input3",
"countryName",
"ecosystem",
"activity",
],
properties: {
id: {
isVisible: { list: false, show: false, edit: false, filter: false },
},
country: {
isVisible: { list: true, show: true, edit: true, filter: true },
},
inputSelection: {
isVisible: { list: true, show: true, edit: true, filter: true },
},
input1: {
isVisible: { list: true, show: true, edit: true, filter: true },
},
input2: {
isVisible: { list: true, show: true, edit: true, filter: true },
},
input3: {
isVisible: { list: true, show: true, edit: true, filter: true },
},
blueCarbon: {
isVisible: { list: true, show: true, edit: false, filter: true },
},
},
sort: {
sortBy: "inputSelection",
direction: "asc",
sortBy: "blueCarbon",
direction: "desc",
},
navigation: {
name: "Data Management",
icon: "Database",
},
actions: {
list: {
after: async (
request: ActionRequest,
response: ActionResponse,
context: ActionContext,
) => {
const { records } = context;
const baseDataRepo = dataSource.getRepository(BaseData);
const queryBuilder = baseDataRepo
.createQueryBuilder("baseData")
.leftJoin(
BlueCarbonProjectPlanning,
"r",
"baseData.blueCarbonProjectPlanning = r.id",
)
.leftJoin(Country, "country", "country.code = baseData.countryCode")
.select("r.id", "id")
.addSelect("r.inputSelection", "inputSelection")
.addSelect("r.input1", "input1")
.addSelect("r.input2", "input2")
.addSelect("r.input3", "input3")
.addSelect("country.name", "countryName")
.addSelect("baseData.ecosystem", "ecosystem")
.addSelect("baseData.activity", "activity");

if (records?.length) {
queryBuilder.andWhere("r.id IN (:...ids)", {
ids: records.map((r) => r.params.id),
});
}

const result = await queryBuilder.getRawMany();

return {
...request,
records: records!.map((record: BaseRecord) => {
record.params = result.find((q) => q.id === record.params.id);
return record;
}),
};
},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import {
ActionContext,
ActionRequest,
ActionResponse,
BaseRecord,
ResourceWithOptions,
} from "adminjs";
import { dataSource } from "../../datasource.js";
import { BaseData } from "@shared/entities/base-data.entity.js";
import { Country } from "@shared/entities/country.entity.js";
import { CarbonStandardFees } from "@shared/entities/carbon-standard-fees.entity.js";
import { COMMON_RESOURCE_LIST_PROPERTIES } from "../common/common.resources.js";
import { ResourceWithOptions } from "adminjs";
import { CarbonStandardFees } from "@shared/entities/cost-inputs/carbon-standard-fees.entity.js";

export const CarbonStandardFeesResource: ResourceWithOptions = {
resource: CarbonStandardFees,
options: {
properties: COMMON_RESOURCE_LIST_PROPERTIES,
listProperties: [
"carbonStandardFee",
"countryName",
"ecosystem",
"activity",
],
sort: {
sortBy: "carbonStandardFee",
direction: "desc",
Expand All @@ -29,46 +12,5 @@ export const CarbonStandardFeesResource: ResourceWithOptions = {
name: "Data Management",
icon: "Database",
},
actions: {
list: {
after: async (
request: ActionRequest,
response: ActionResponse,
context: ActionContext,
) => {
const { records } = context;
const baseDataRepo = dataSource.getRepository(BaseData);
const queryBuilder = baseDataRepo
.createQueryBuilder("baseData")
.leftJoin(
CarbonStandardFees,
"r",
"baseData.carbonStandardFees = r.id",
)
.leftJoin(Country, "country", "country.code = baseData.countryCode")
.select("r.id", "id")
.addSelect("r.carbonStandardFee", "carbonStandardFee")
.addSelect("country.name", "countryName")
.addSelect("baseData.ecosystem", "ecosystem")
.addSelect("baseData.activity", "activity");

if (records?.length) {
queryBuilder.andWhere("r.id IN (:...ids)", {
ids: records.map((r) => r.params.id),
});
}

const result = await queryBuilder.getRawMany();

return {
...request,
records: records!.map((record: BaseRecord) => {
record.params = result.find((q) => q.id === record.params.id);
return record;
}),
};
},
},
},
},
};
Loading

0 comments on commit 7af35a6

Please sign in to comment.