Browse Source

fix(core): Correctly assign StockLocations to channels

Michael Bromley 2 years ago
parent
commit
4b8fe04d4c
1 changed files with 22 additions and 6 deletions
  1. 22 6
      packages/core/src/service/services/stock-location.service.ts

+ 22 - 6
packages/core/src/service/services/stock-location.service.ts

@@ -35,8 +35,7 @@ export class StockLocationService {
 
 
     findOne(ctx: RequestContext, stockLocationId: ID): Promise<StockLocation | undefined> {
     findOne(ctx: RequestContext, stockLocationId: ID): Promise<StockLocation | undefined> {
         return this.connection
         return this.connection
-            .getRepository(ctx, StockLocation)
-            .findOne({ where: { id: stockLocationId } })
+            .findOneInChannel(ctx, StockLocation, stockLocationId, ctx.channelId)
             .then(result => result ?? undefined);
             .then(result => result ?? undefined);
     }
     }
 
 
@@ -47,6 +46,7 @@ export class StockLocationService {
     ): Promise<PaginatedList<StockLocation>> {
     ): Promise<PaginatedList<StockLocation>> {
         return this.listQueryBuilder
         return this.listQueryBuilder
             .build(StockLocation, options, {
             .build(StockLocation, options, {
+                channelId: ctx.channelId,
                 relations,
                 relations,
                 ctx,
                 ctx,
             })
             })
@@ -57,13 +57,16 @@ export class StockLocationService {
             }));
             }));
     }
     }
 
 
-    create(ctx: RequestContext, input: CreateStockLocationInput): Promise<StockLocation> {
-        return this.connection.getRepository(ctx, StockLocation).save(
+    async create(ctx: RequestContext, input: CreateStockLocationInput): Promise<StockLocation> {
+        const stockLocation = await this.connection.getRepository(ctx, StockLocation).save(
             new StockLocation({
             new StockLocation({
                 name: input.name,
                 name: input.name,
                 description: input.description,
                 description: input.description,
             }),
             }),
         );
         );
+        await this.channelService.assignToCurrentChannel(stockLocation, ctx);
+        await this.connection.getRepository(ctx, StockLocation).save(stockLocation);
+        return stockLocation;
     }
     }
 
 
     async update(ctx: RequestContext, input: UpdateStockLocationInput): Promise<StockLocation> {
     async update(ctx: RequestContext, input: UpdateStockLocationInput): Promise<StockLocation> {
@@ -143,7 +146,11 @@ export class StockLocationService {
         const ctx = await this.requestContextService.create({
         const ctx = await this.requestContextService.create({
             apiType: 'admin',
             apiType: 'admin',
         });
         });
-        const stockLocations = await this.connection.getRepository(ctx, StockLocation).find();
+        const stockLocations = await this.connection.getRepository(ctx, StockLocation).find({
+            relations: {
+                channels: true,
+            },
+        });
         if (stockLocations.length === 0) {
         if (stockLocations.length === 0) {
             const defaultStockLocation = await this.connection.getRepository(ctx, StockLocation).save(
             const defaultStockLocation = await this.connection.getRepository(ctx, StockLocation).save(
                 new StockLocation({
                 new StockLocation({
@@ -151,7 +158,16 @@ export class StockLocationService {
                     description: 'The default stock location',
                     description: 'The default stock location',
                 }),
                 }),
             );
             );
-            await this.channelService.assignToCurrentChannel(defaultStockLocation, ctx);
+            stockLocations.push(defaultStockLocation);
+            await this.connection.getRepository(ctx, StockLocation).save(defaultStockLocation);
+        }
+        const defaultChannel = await this.channelService.getDefaultChannel();
+        for (const stockLocation of stockLocations) {
+            if (!stockLocation.channels.find(c => c.id === defaultChannel.id)) {
+                await this.channelService.assignToChannels(ctx, StockLocation, stockLocation.id, [
+                    defaultChannel.id,
+                ]);
+            }
         }
         }
     }
     }
 }
 }