-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add project entity and backoffice resource
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
ActionContext, | ||
ActionRequest, | ||
ActionResponse, | ||
BaseRecord, | ||
ResourceWithOptions, | ||
} from "adminjs"; | ||
import { dataSource } from "../../datasource.js"; | ||
import { COMMON_RESOURCE_LIST_PROPERTIES } from "../common/common.resources.js"; | ||
import { Project } from "@shared/entities/users/projects.entity.js"; | ||
import { Country } from "@shared/entities/country.entity.js"; | ||
|
||
export const ProjectsResource: ResourceWithOptions = { | ||
resource: Project, | ||
options: { | ||
properties: COMMON_RESOURCE_LIST_PROPERTIES, | ||
listProperties: [ | ||
"projectName", | ||
"projectSize", | ||
"abatementPotential", | ||
"totalCostNPV", | ||
"costPerTCO2eNPV", | ||
"initialPriceAssumption", | ||
"activitySubtype", | ||
"projectSizeFilter", | ||
"priceType", | ||
], | ||
sort: { | ||
sortBy: "projectName", | ||
direction: "asc", | ||
}, | ||
navigation: { | ||
name: "Data Management", | ||
icon: "Database", | ||
}, | ||
actions: { | ||
list: { | ||
after: async ( | ||
request: ActionRequest, | ||
response: ActionResponse, | ||
context: ActionContext, | ||
) => { | ||
const { records } = context; | ||
const projectDataRepo = dataSource.getRepository(Project); | ||
const queryBuilder = projectDataRepo | ||
.createQueryBuilder("project") | ||
.leftJoin(Country, "country", "project.countryCode = country.code") | ||
.select("project.id", "id") | ||
.addSelect("project.projectName", "projectName") | ||
.addSelect("project.ecosystem", "ecosystem") | ||
.addSelect("project.activity", "activity") | ||
.addSelect("project.activitySubtype", "activitySubtype") | ||
.addSelect("country.name", "countryName") | ||
.addSelect("project.projectSize", "projectSize") | ||
.addSelect("project.abatementPotential", "abatementPotential") | ||
.addSelect("project.totalCostNPV", "totalCostNPV") | ||
.addSelect("project.costPerTCO2eNPV", "costPerTCO2eNPV") | ||
.addSelect( | ||
"project.initialPriceAssumption", | ||
"initialPriceAssumption", | ||
); | ||
|
||
if (records?.length) { | ||
queryBuilder.andWhere("projectDataImport.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; | ||
}), | ||
}; | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
Column, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
BaseEntity, | ||
ManyToOne, | ||
JoinColumn, | ||
} from "typeorm"; | ||
import { Country } from "@shared/entities/country.entity"; | ||
import { ACTIVITY, ECOSYSTEM } from "@shared/entities/base-data.entity"; | ||
|
||
export enum PROJECT_SIZE_FILTER { | ||
SMALL = "Small", | ||
MEDIUM = "Medium", | ||
LARGE = "Large", | ||
} | ||
|
||
export enum PROJECT_PRICE_TYPE { | ||
OPEN_BREAK_EVEN_PRICE = "Open Break Even Price", | ||
MARKET_PRICE = "Market Price", | ||
} | ||
|
||
@Entity("projects") | ||
export class Project extends BaseEntity { | ||
@PrimaryGeneratedColumn("uuid") | ||
id: string; | ||
|
||
@Column({ name: "project_name", type: "varchar", length: 255 }) | ||
projectName: string; | ||
|
||
@Column({ name: "country_code", length: 3, nullable: true, type: "char" }) | ||
countryCode: string; | ||
|
||
// Unidirectional relation | ||
@ManyToOne(() => Country) | ||
@JoinColumn({ name: "country_code" }) | ||
country: Country; | ||
|
||
@Column({ name: "ecosystem", enum: ECOSYSTEM, type: "enum" }) | ||
ecosystem: ECOSYSTEM; | ||
|
||
@Column({ name: "activity", enum: ACTIVITY, type: "enum" }) | ||
activity: ACTIVITY; | ||
|
||
// TODO: We need to make this a somehow enum, as a subactivity of restoration, that can be null for conservation, and can represent all restoration activities | ||
@Column({ | ||
name: "activity_subtype", | ||
type: "varchar", | ||
length: 255, | ||
nullable: true, | ||
}) | ||
activitySubtype: string; | ||
|
||
@Column({ name: "project_size", type: "decimal" }) | ||
projectSize: number; | ||
|
||
// TODO: We could potentially remove this column from the database and excel, and have a threshold to filter by | ||
@Column({ | ||
name: "project_size_filter", | ||
type: "enum", | ||
enum: PROJECT_SIZE_FILTER, | ||
}) | ||
projectSizeFilter: string; | ||
|
||
@Column({ name: "abatement_potential", type: "decimal" }) | ||
abatementPotential: number; | ||
|
||
@Column({ name: "total_cost_npv", type: "decimal" }) | ||
totalCostNPV: number; | ||
|
||
@Column({ name: "total_cost", type: "decimal" }) | ||
totalCost: number; | ||
|
||
@Column({ name: "cost_per_tco2e_npv", type: "decimal" }) | ||
costPerTCO2eNPV: number; | ||
|
||
@Column({ name: "cost_per_tco2e", type: "decimal" }) | ||
costPerTCO2e: number; | ||
|
||
@Column({ name: "initial_price_assumption", type: "varchar", length: 255 }) | ||
initialPriceAssumption: string; | ||
|
||
@Column({ name: "price_type", enum: PROJECT_PRICE_TYPE, type: "enum" }) | ||
priceType: PROJECT_PRICE_TYPE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters