Skip to content

Commit

Permalink
decouple country service from project service
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 28, 2024
1 parent a9e5dc6 commit 9e3f411
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
22 changes: 19 additions & 3 deletions api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { tsRestHandler, TsRestHandler } from '@ts-rest/nest';
import { ControllerResponse } from '@api/types/controller-response.type';
import { projectsContract } from '@shared/contracts/projects.contract';
import { ProjectsService } from '@api/modules/projects/projects.service';
import { CountriesService } from '@api/modules/countries/countries.service';
import { CountryWithNoGeometry } from '@shared/entities/country.entity';

@Controller()
export class ProjectsController {
constructor(private readonly projectsService: ProjectsService) {}
constructor(
private readonly projectsService: ProjectsService,
private readonly countryService: CountriesService,
) {}

@TsRestHandler(projectsContract.getProjects)
async getProjects(): ControllerResponse {
Expand All @@ -19,8 +24,19 @@ export class ProjectsController {
@TsRestHandler(projectsContract.getProjectCountries)
async getProjectCountries(): ControllerResponse {
return tsRestHandler(projectsContract.getProjectCountries, async () => {
const data = await this.projectsService.getProjectCountries();
return { body: { data }, status: HttpStatus.OK };
const projectCountryCodes = await this.projectsService.projectRepository
.find()
.then((projects) => projects.map((p) => p.countryCode));
const [countries] = await this.countryService.findAll({
filter: { code: projectCountryCodes },
omitFields: ['geometry'],
disablePagination: true,
});

return {
body: { data: countries as CountryWithNoGeometry[] },
status: HttpStatus.OK,
};
});
}

Expand Down
18 changes: 1 addition & 17 deletions api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { AppBaseService } from '@api/utils/app-base.service';
import { Project } from '@shared/entities/projects.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CountryWithNoGeometry } from '@shared/entities/country.entity';
import { CountriesService } from '@api/modules/countries/countries.service';

@Injectable()
export class ProjectsService extends AppBaseService<
Expand All @@ -15,22 +13,8 @@ export class ProjectsService extends AppBaseService<
> {
constructor(
@InjectRepository(Project)
private readonly projectRepository: Repository<Project>,
private readonly countryService: CountriesService,
public readonly projectRepository: Repository<Project>,
) {
super(projectRepository, 'project', 'projects');
}

async getProjectCountries(): Promise<CountryWithNoGeometry[]> {
const projects = await this.projectRepository.find();

const countryCodes = projects.map((p) => p.countryCode);
const [countries] = await this.countryService.findAll({
filter: { code: countryCodes },
omitFields: ['geometry'],
disablePagination: true,
});

return countries as CountryWithNoGeometry[];
}
}

0 comments on commit 9e3f411

Please sign in to comment.