zone.service.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Injectable } from '@nestjs/common';
  2. import { InjectConnection } from '@nestjs/typeorm';
  3. import {
  4. AddMembersToZoneMutationArgs,
  5. CreateZoneInput,
  6. RemoveMembersFromZoneMutationArgs,
  7. UpdateZoneInput,
  8. } from 'shared/generated-types';
  9. import { ID } from 'shared/shared-types';
  10. import { unique } from 'shared/unique';
  11. import { Connection } from 'typeorm';
  12. import { assertFound } from '../../common/utils';
  13. import { Country } from '../../entity/country/country.entity';
  14. import { Zone } from '../../entity/zone/zone.entity';
  15. import { I18nError } from '../../i18n/i18n-error';
  16. import { patchEntity } from '../helpers/patch-entity';
  17. @Injectable()
  18. export class ZoneService {
  19. constructor(@InjectConnection() private connection: Connection) {}
  20. findAll(): Promise<Zone[]> {
  21. return this.connection.getRepository(Zone).find({
  22. relations: ['members'],
  23. });
  24. }
  25. findOne(zoneId: ID): Promise<Zone | undefined> {
  26. return this.connection.getRepository(Zone).findOne(zoneId, {
  27. relations: ['members'],
  28. });
  29. }
  30. async create(input: CreateZoneInput): Promise<Zone> {
  31. const zone = new Zone(input);
  32. if (input.memberIds) {
  33. zone.members = await this.getCountriesFromIds(input.memberIds);
  34. }
  35. const newZone = await this.connection.getRepository(Zone).save(zone);
  36. return assertFound(this.findOne(newZone.id));
  37. }
  38. async update(input: UpdateZoneInput): Promise<Zone> {
  39. const zone = await this.getZoneOrThrow(input.id);
  40. const updatedZone = patchEntity(zone, input);
  41. await this.connection.getRepository(Zone).save(updatedZone);
  42. return assertFound(this.findOne(zone.id));
  43. }
  44. async addMembersToZone(input: AddMembersToZoneMutationArgs): Promise<Zone> {
  45. const countries = await this.getCountriesFromIds(input.memberIds);
  46. const zone = await this.getZoneOrThrow(input.zoneId);
  47. const members = unique(zone.members.concat(countries), 'id');
  48. zone.members = members;
  49. await this.connection.getRepository(Zone).save(zone);
  50. return zone;
  51. }
  52. async removeMembersFromZone(input: RemoveMembersFromZoneMutationArgs): Promise<Zone> {
  53. const zone = await this.getZoneOrThrow(input.zoneId);
  54. zone.members = zone.members.filter(country => !input.memberIds.includes(country.id as string));
  55. await this.connection.getRepository(Zone).save(zone);
  56. return zone;
  57. }
  58. private async getZoneOrThrow(id: ID): Promise<Zone> {
  59. const zone = await this.findOne(id);
  60. if (!zone) {
  61. throw new I18nError(`error.entity-with-id-not-found`, { entityName: 'Zone', id });
  62. }
  63. return zone;
  64. }
  65. private getCountriesFromIds(ids: ID[]): Promise<Country[]> {
  66. return this.connection.getRepository(Country).findByIds(ids);
  67. }
  68. }