Skip to content

Commit

Permalink
use buffer type instead of multer, move Country entity
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 6, 2024
1 parent 82fb9ca commit f44647c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 47 deletions.
9 changes: 8 additions & 1 deletion api/src/modules/config/app-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { COMMON_DATABASE_ENTITIES } from '@shared/entities/database.entities';
import { TOKEN_TYPE_ENUM } from '@shared/schemas/auth/token-type.schema';
import { JwtConfigHandler } from '@api/modules/config/auth-config.handler';
import { ApiEventsEntity } from '@api/modules/api-events/api-events.entity';
import { BaseData } from '@api/modules/model/base-data.entity';
import { Country } from '@api/modules/model/entities/country.entity';

export type JWTConfig = {
secret: string;
Expand All @@ -30,7 +32,12 @@ export class ApiConfigService {
username: this.configService.get('DB_USERNAME'),
password: this.configService.get('DB_PASSWORD'),
database: this.configService.get('DB_NAME'),
entities: [...COMMON_DATABASE_ENTITIES, ApiEventsEntity],
entities: [
...COMMON_DATABASE_ENTITIES,
ApiEventsEntity,
Country,
BaseData,
],
synchronize: true,
ssl: this.isProduction()
? { require: true, rejectUnauthorized: false }
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/import/import.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export class ImportController {
@RequiredRoles(ROLES.ADMIN)
@UseInterceptors(FileInterceptor('file'))
async uploadFile(@UploadXlsm() file: Express.Multer.File): Promise<any> {
return this.service.import(file);
return this.service.import(file.buffer);
}
}
5 changes: 2 additions & 3 deletions api/src/modules/import/import.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common';
//import { Multer } from 'multer';
import { XlsxParser } from '@api/modules/import/services/xlsx.parser';
import { EntityPreprocessor } from '@api/modules/import/services/entity.preprocessor';
import { BaseDataRepository } from '@api/modules/model/base-data.repository';
Expand All @@ -12,10 +11,10 @@ export class ImportService {
private readonly preprocessor: EntityPreprocessor,
) {}

async import(file: Express.Multer.File) {
async import(fileBuffer: Buffer) {
let data;
try {
data = await this.xlsxParser.parseExcel<any>(file.buffer);
data = await this.xlsxParser.parseExcel<any>(fileBuffer);
const dbEntities = this.preprocessor.toDbEntities(data);

const dbResult = await this.repo.insertData(dbEntities);
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/import/services/entity.preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { Country } from '@shared/entities/countries/country.entity';
import { BaseData } from '@api/modules/model/base-data.entity';
import { Country } from '@api/modules/model/entities/country.entity';

export type ParsedDBEntities = {
countries: Country[];
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/model/base-data.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ManyToOne,
JoinColumn,
} from 'typeorm';
import { Country } from '@shared/entities/countries/country.entity';
import { Country } from '@api/modules/model/entities/country.entity';

export enum ECOSYSTEM {
MANGROVE = 'Mangrove',
Expand Down
2 changes: 1 addition & 1 deletion api/src/modules/model/base-data.repository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DataSource, Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { Country } from '@shared/entities/countries/country.entity';

import { ParsedDBEntities } from '@api/modules/import/services/entity.preprocessor';
import { BaseData } from '@api/modules/model/base-data.entity';
import { Country } from '@api/modules/model/entities/country.entity';

@Injectable()
export class BaseDataRepository extends Repository<BaseData> {
Expand Down
37 changes: 37 additions & 0 deletions api/src/modules/model/entities/country.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';

export enum CONTINENTS {
AFRICA = 'Africa',
ASIA = 'Asia',
EUROPE = 'Europe',
EUROPE_AND_ASIA = 'Europe/Asia',
NORTH_AMERICA = 'North America',
OCEANIA = 'Oceania',
SOUTH_AMERICA = 'South America',
ANTARCTICA = 'Antarctica',
}

@Entity('countries')
export class Country {
@PrimaryColumn({ name: 'country_code', length: 3 })
countryCode: string;

@Column({ length: 100 })
country: string;

// TODO: Right now the dataset contains null values for continents, but not sure if this is correct
@Column({ type: 'enum', enum: CONTINENTS, nullable: true })
continent: CONTINENTS;

@Column({ name: 'region_1', length: 50, nullable: true })
region1?: string;

@Column({ name: 'region_2', length: 50, nullable: true })
region2?: string;

@Column({ name: 'numeric_code', length: 3, nullable: true })
numericCode?: string;

@Column({ name: 'hdi', type: 'int', nullable: true })
hdi?: number;
}
37 changes: 0 additions & 37 deletions shared/entities/countries/country.entity.ts

This file was deleted.

3 changes: 1 addition & 2 deletions shared/entities/database.entities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { User } from "@shared/entities/users/user.entity";
import { Country } from "@shared/entities/countries/country.entity";

export const COMMON_DATABASE_ENTITIES = [User, Country];
export const COMMON_DATABASE_ENTITIES = [User];

0 comments on commit f44647c

Please sign in to comment.