Browse Source

fix(server): Fix adding/removing Country from Zone

Michael Bromley 7 years ago
parent
commit
33c6005d37

+ 3 - 2
server/src/service/helpers/utils/get-entity-or-throw.ts

@@ -1,5 +1,5 @@
 import { ID, Type } from 'shared/shared-types';
-import { Connection } from 'typeorm';
+import { Connection, FindOneOptions } from 'typeorm';
 
 import { VendureEntity } from '../../../entity/base/base.entity';
 import { I18nError } from '../../../i18n/i18n-error';
@@ -11,8 +11,9 @@ export async function getEntityOrThrow<T extends VendureEntity>(
     connection: Connection,
     entityType: Type<T>,
     id: ID,
+    findOptions?: FindOneOptions<T>,
 ): Promise<T> {
-    const entity = await connection.getRepository(entityType).findOne(id);
+    const entity = await connection.getRepository(entityType).findOne(id, findOptions);
     if (!entity) {
         throw new I18nError(`error.entity-with-id-not-found`, {
             entityName: entityType.name,

+ 2 - 2
server/src/service/services/zone.service.ts

@@ -71,7 +71,7 @@ export class ZoneService {
 
     async addMembersToZone(ctx: RequestContext, input: AddMembersToZoneMutationArgs): Promise<Zone> {
         const countries = await this.getCountriesFromIds(input.memberIds);
-        const zone = await getEntityOrThrow(this.connection, Zone, input.zoneId);
+        const zone = await getEntityOrThrow(this.connection, Zone, input.zoneId, { relations: ['members'] });
         const members = unique(zone.members.concat(countries), 'id');
         zone.members = members;
         await this.connection.getRepository(Zone).save(zone);
@@ -82,7 +82,7 @@ export class ZoneService {
         ctx: RequestContext,
         input: RemoveMembersFromZoneMutationArgs,
     ): Promise<Zone> {
-        const zone = await getEntityOrThrow(this.connection, Zone, input.zoneId);
+        const zone = await getEntityOrThrow(this.connection, Zone, input.zoneId, { relations: ['members'] });
         zone.members = zone.members.filter(country => !input.memberIds.includes(country.id as string));
         await this.connection.getRepository(Zone).save(zone);
         return assertFound(this.findOne(ctx, zone.id));